问题解决:
报错信息显示在文件路径的字符串中,出现了转义字符的问题。为了解决这个问题,可以使用原始字符串(raw string)来表示文件路径。
将以下代码修改为原始字符串即可:image_path = r"C:\Users\WINDOWS\Desktop\taiyuan\python\dune\\dune.tif"
修改后的完整代码如下:import cv2import numpy as np# 1. 分割遥感影像为多块图幅def split_image(image, block_size): height, width = image.shape[:2] block_images = [] for i in range(0, height, block_size): for j in range(0, width, block_size): block = image[i:i+block_size, j:j+block_size] block_images.append(block) return block_images# 2. 分别对每块图幅转为灰度图像def convert_to_gray(image): gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) return gray_image# 3. 对各个图像进行边缘检测def edge_detection(image): edges = cv2.Canny(image, 100, 200) return edges# 4. 消除噪声干扰def remove_noise(image): denoised_image = cv2.medianBlur(image, 5) return denoised_image# 5. 将各个图幅合并为一个图幅def merge_images(images, block_size): rows = int(np.sqrt(len(images))) cols = rows merged_image = np.zeros((rows * block_size, cols * block_size), dtype=np.uint8) for i, image in enumerate(images): x = (i % cols) * block_size y = (i // cols) * block_size merged_image[y:y+block_size, x:x+block_size] = image return merged_image# 6. 导出结果def export_result(image, file_path): cv2.imwrite(file_path, image)# 示例代码image_path = r"C:\Users\WINDOWS\Desktop\taiyuan\python\dune\\dune.tif"block_size = 100# 读取遥感影像image = cv2.imread(image_path)# 1. 分割遥感影像为多块图幅block_images = split_image(image, block_size)# 2. 分别对每块图幅转为灰度图像gray_images = [convert_to_gray(block) for block in block_images]# 3. 对各个图像进行边缘检测edges_images = [edge_detection(gray) for gray in gray_images]# 4. 消除噪声干扰denoised_images = [remove_noise(edges) for edges in edges_images]# 5. 将各个图幅合并为一个图幅merged_image = merge_images(denoised_images, block_size)# 6. 导出结果export_result(merged_image, "output.jpg")
请注意,在运行代码之前,确保dune.tif文件存在于指定的路径中,并且安装了cv2库。
如果回答对你有帮助,请给我一个最佳答案!
|