更改边缘检测部分函数代码,使检测函数为自适应检测。
代码:from tifffile import imread, imwrite
import cv2
import numpy as np
# 1. 分割遥感影像为多块图幅
def split_image(image, num_rows, num_cols):
height, width = image.shape[:2]
row_height = height // num_rows
col_width = width // num_cols
images = []
for r in range(num_rows):
for c in range(num_cols):
start_row = r * row_height
end_row = start_row + row_height
start_col = c * col_width
end_col = start_col + col_width
sub_image = image
images.append(sub_image)
return images
#2. 分别对每块图幅转为灰度图像,并应用直方图均衡化
def convert_to_grayscale(images):
grayscale_images = []
for image in images:
gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
enhanced_image = cv2.equalizeHist(gray_image)# 直方图均衡化增强对比度
grayscale_images.append(enhanced_image)
return grayscale_images
#3.边缘检测
def edge_detection(images):
edges = []
for image in images:
# 使用其他边缘检测方法,这里以Sobel算子为例
gradient_x = cv2.Sobel(image, cv2.CV_64F, 1, 0, ksize=3)
gradient_y = cv2.Sobel(image, cv2.CV_64F, 0, 1, ksize=3)
edges_image = cv2.magnitude(gradient_x, gradient_y)
edges.append(edges_image)
return edges
# 4. 消除噪声干扰
def denoise(images):
denoised_images = []
for image in images:
# 使用其他滤波方法,这里以高斯滤波为例
denoised_image = cv2.GaussianBlur(image, (5, 5), 0)
denoised_images.append(denoised_image)
return denoised_images
# 5. 将各个图幅合并为一个图幅
def merge_images(images, num_rows, num_cols):
row_height, col_width = images.shape[:2]
merged_image = np.zeros((row_height * num_rows, col_width * num_cols), dtype=np.uint8)
i = 0
for r in range(num_rows):
for c in range(num_cols):
start_row = r * row_height
end_row = start_row + row_height
start_col = c * col_width
end_col = start_col + col_width
merged_image = images
i += 1
return merged_image
# 6. 导出结果
def export_result(image, filename):
cv2.imwrite(filename, image)
# 加载遥感影像
image = imread(r"F:\duneline\dune\dune.tif")
# 1. 分割遥感影像为多块图幅
num_rows = 7
num_cols = 11
sub_images = split_image(image, num_rows, num_cols)
# 2. 分别对每块图幅转为灰度图像
gray_images = convert_to_grayscale(sub_images)
# 3. 使用其他边缘检测方法代替Canny算法
edges = edge_detection(gray_images)
# 4. 消除噪声干扰
denoised_edges = denoise(edges)
# 5. 将各个图幅合并为一个图幅
merged_image = merge_images(denoised_edges, num_rows, num_cols)
# 6. 导出结果
export_result(merged_image, 'path_to_output2.jpg') 要使边缘检测函数为自适应检测,您可以使用自适应阈值方法,如自适应高斯阈值法。
以下是修改后的代码:
# 3. 边缘检测(自适应)
def edge_detection(images):
edges = []
for image in images:
# 将图像转换为灰度图像
gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
# 自适应高斯阈值法
edges_image = cv2.adaptiveThreshold(gray_image, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY, 11, 2)
edges.append(edges_image)
return edges
在这里,我们使用cv2.adaptiveThreshold()函数来应用自适应高斯阈值法,生成二值化的边缘图像。
希望这个修改能满足您的需求。如果还有其他问题,请随时提问。
如果问题已经解决,请设置最佳答案 要使边缘检测函数为自适应检测,可以使用自适应阈值方法替代固定阈值方法。以下是修改后的代码:
def edge_detection(images):
edges = []
for image in images:
# 将图像转为灰度图像
gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
# 使用自适应阈值方法进行边缘检测
edges_image = cv2.adaptiveThreshold(gray_image, 255, cv2.ADAPTIVE_THRESH_MEAN_C, cv2.THRESH_BINARY, 11, 2)
edges.append(edges_image)
return edges
在这个修改后的代码中,我们首先将图像转为灰度图像。然后,使用 cv2.adaptiveThreshold 函数进行自适应阈值处理。这个函数根据图像的局部区域计算阈值,从而实现自适应的边缘检测。 cv2.ADAPTIVE_THRESH_MEAN_C 参数表示使用局部区域的均值作为阈值, cv2.THRESH_BINARY 参数表示将大于阈值的像素设置为白色,小于阈值的像素设置为黑色。最后,将处理后的图像添加到结果列表中。
请注意,这个修改后的代码仅仅修改了边缘检测部分,其他部分保持不变。 修改边缘检测部分函数代码,使其实现自适应边缘检测。
import cv2
import numpy as np
def adaptive_edge_detection(images):
edges = []
for image in images:
# 自适应阈值边缘检测方法,这里使用自适应高斯阈值方法
gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
blurred_image = cv2.GaussianBlur(gray_image, (5, 5), 0)
edges_image = cv2.adaptiveThreshold(blurred_image, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY, 11, 2)
edges.append(edges_image)
return edges
# 调用自适应边缘检测函数进行检测
adaptive_edges = adaptive_edge_detection(gray_images)
# 其他代码保持不变
完整修改后的代码如下:
from tifffile import imread, imwrite
import cv2
import numpy as np
# 1. 分割遥感影像为多块图幅
def split_image(image, num_rows, num_cols):
height, width = image.shape[:2]
row_height = height // num_rows
col_width = width // num_cols
images = []
for r in range(num_rows):
for c in range(num_cols):
start_row = r * row_height
end_row = start_row + row_height
start_col = c * col_width
end_col = start_col + col_width
sub_image = image
images.append(sub_image)
return images
# 2. 分别对每块图幅转为灰度图像,并应用直方图均衡化
def convert_to_grayscale(images):
grayscale_images = []
for image in images:
gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
enhanced_image = cv2.equalizeHist(gray_image)# 直方图均衡化增强对比度
grayscale_images.append(enhanced_image)
return grayscale_images
# 修改的部分:自适应边缘检测函数
def adaptive_edge_detection(images):
edges = []
for image in images:
# 自适应阈值边缘检测方法,这里使用自适应高斯阈值方法
gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
blurred_image = cv2.GaussianBlur(gray_image, (5, 5), 0)
edges_image = cv2.adaptiveThreshold(blurred_image, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY, 11, 2)
edges.append(edges_image)
return edges
# 4. 消除噪声干扰
def denoise(images):
denoised_images = []
for image in images:
# 使用其他滤波方法,这里以高斯滤波为例
denoised_image = cv2.GaussianBlur(image, (5, 5), 0)
denoised_images.append(denoised_image)
return denoised_images
# 5. 将各个图幅合并为一个图幅
def merge_images(images, num_rows, num_cols):
row_height, col_width = images.shape[:2]
merged_image = np.zeros((row_height * num_rows, col_width * num_cols), dtype=np.uint8)
i = 0
for r in range(num_rows):
for c in range(num_cols):
start_row = r * row_height
end_row = start_row + row_height
start_col = c * col_width
end_col = start_col + col_width
merged_image = images
i += 1
return merged_image
# 6. 导出结果
def export_result(image, filename):
cv2.imwrite(filename, image)
# 加载遥感影像
image = imread(r"F:\duneline\dune\dune.tif")
# 1. 分割遥感影像为多块图幅
num_rows = 7
num_cols = 11
sub_images = split_image(image, num_rows, num_cols)
# 2. 分别对每块图幅转为灰度图像
gray_images = convert_to_grayscale(sub_images)
# 3. 使用自适应边缘检测方法代替Canny算法
adaptive_edges = adaptive_edge_detection(gray_images)
# 4. 消除噪声干扰
denoised_edges = denoise(adaptive_edges)
# 5. 将各个图幅合并为一个图幅
merged_image = merge_images(denoised_edges, num_rows, num_cols)
# 6. 导出结果
export_result(merged_image, 'path_to_output2.jpg')
请根据你的需求将“path_to_output2.jpg”替换为实际的输出路径。
如果回答对你有帮助,请给我一个最佳答案!{:10_254:}{:10_254:}{:10_254:}
给个最佳答案呗!求你了!!!{:10_254:}
页:
[1]