|  | 
 
 发表于 2023-8-8 21:07:12
|
显示全部楼层 
| 修改边缘检测部分函数代码,使其实现自适应边缘检测。 
 复制代码import cv2import numpy as npdef 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, imwriteimport cv2import 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[start_row:end_row, start_col:end_col]            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[0].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[start_row:end_row, start_col:end_col] = images[i]            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 = 7num_cols = 11sub_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”替换为实际的输出路径。
 
 如果回答对你有帮助,请给我一个最佳答案!
    
 | 
 |