|
发表于 2023-8-7 17:05:37
|
显示全部楼层
根据你提供的代码,我注意到在图像合并的部分可能存在错误。在第13行的代码中,你使用了merged_image[y:y+image.shape[0], x:x+image.shape[1]] = image来拼接图像,但是这可能会导致拼接错误。
为了正确地将图像合并成一个完整的图幅,你需要确保进行图像拼接时的位置索引是正确的。以下是修改后的代码:
- import cv2
- import 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):
- num_blocks = len(images)
- rows = int(np.ceil(np.sqrt(num_blocks))) # 向上取整
- 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+image.shape[0], x:x+image.shape[1]] = image
- return merged_image
- # 6. 导出结果
- def export_result(image, file_path):
- cv2.imwrite(file_path, image)
- # 示例代码
- image_path = r"your_image_path_here"
- block_size = 800
- # 读取遥感影像
- 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")
复制代码
请将image_path替换为你要处理的遥感影像的路径。这段代码将在拼接图像时使用正确的索引,应该能够解决合并结果拼接错误的问题。如有其他问题,请随时提问。
如果问题已经解决,请设置最佳答案 |
|