Justheartyoung 发表于 2023-8-7 20:13:24

帮忙改正代码错误

代码:
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
            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, 0.5, 2)
#   return edges

def edge_detection(image):
    sobel_x = cv2.Sobel(image, cv2.CV_64F, 1, 0, ksize=3)
    sobel_y = cv2.Sobel(image, cv2.CV_64F, 0, 1, ksize=3)
    edges = cv2.magnitude(sobel_x, sobel_y)
    edges = cv2.convertScaleAbs(edges)
    return edges



# 4. 消除噪声干扰
def remove_noise(image):
    denoised_image = cv2.medianBlur(image, 5)
    return denoised_image

# 5. 将各个图幅合并为一个图幅
#


def merge_images(images, original_image_shape):
    merged_image = np.zeros(original_image_shape, dtype=np.uint8)
    block_size = images.shape# assume all blocks have the same size
    rows = original_image_shape // block_size
    cols = original_image_shape // block_size
    for i, image in enumerate(images):
      x = (i % cols) * block_size
      y = (i // cols) * block_size
      merged_image, x:x+image.shape, :] = cv2.cvtColor(image, cv2.COLOR_GRAY2BGR)# 转换为彩色图像
    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 = 800

# 读取遥感影像
image = cv2.imread(image_path)

# 1. 分割遥感影像为多块图幅
block_images = split_image(image, block_size)

# 2. 分别对每块图幅转为灰度图像
gray_images =

# 3. 对各个图像进行边缘检测
edges_images =

# 4. 消除噪声干扰
denoised_images =

# 5. 将各个图幅合并为一个图幅
merged_image = merge_images(denoised_images, block_size)


# 6. 导出结果
export_result(merged_image, "output.jpg")

错误:
Traceback (most recent call last):
File "C:\Users\WINDOWS\Desktop\taiyuan\python\segement.py", line 85, in <module>
    merged_image = merge_images(denoised_images, block_size)
File "C:\Users\WINDOWS\Desktop\taiyuan\python\segement.py", line 54, in merge_images
    rows = original_image_shape // block_size
TypeError: 'int' object is not subscriptable

isdkz 发表于 2023-8-7 20:13:45

错误的原因是在merge_images函数中,original_image_shape被当作了一个整数进行处理,而不是一个元组。这是由于在调用merge_images函数时,block_size被传递为了第二个参数,而不是original_image_shape。

要解决这个问题,我们需要修改merge_images函数的定义和调用。下面是修改后的代码:

def merge_images(images, original_image_shape):
    merged_image = np.zeros(original_image_shape, dtype=np.uint8)
    block_size = images.shape# assume all blocks have the same size
    rows = original_image_shape // block_size
    cols = original_image_shape // block_size
    for i, image in enumerate(images):
      x = (i % cols) * block_size
      y = (i // cols) * block_size
      merged_image, x:x+image.shape, :] = cv2.cvtColor(image, cv2.COLOR_GRAY2BGR)# 转换为彩色图像
    return merged_image

# 示例代码
image_path = r"C:\Users\WINDOWS\Desktop\taiyuan\python\dune\dune.tif"
block_size = 800

# 读取遥感影像
image = cv2.imread(image_path)

# 1. 分割遥感影像为多块图幅
block_images = split_image(image, block_size)

# 2. 分别对每块图幅转为灰度图像
gray_images =

# 3. 对各个图像进行边缘检测
edges_images =

# 4. 消除噪声干扰
denoised_images =

# 5. 将各个图幅合并为一个图幅
merged_image = merge_images(denoised_images, image.shape)

# 6. 导出结果
export_result(merged_image, "output.jpg")


通过将image.shape作为merge_images函数的第二个参数传递,我们可以解决这个错误。这样,original_image_shape将被正确地解析为一个元组,从而避免了TypeError异常。

陶远航 发表于 2023-8-7 20:13:56

错误的原因是在函数merge_images中,original_image_shape被错误地用作一个整数进行了索引操作。修改代码如下:

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
            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):
    sobel_x = cv2.Sobel(image, cv2.CV_64F, 1, 0, ksize=3)
    sobel_y = cv2.Sobel(image, cv2.CV_64F, 0, 1, ksize=3)
    edges = cv2.magnitude(sobel_x, sobel_y)
    edges = cv2.convertScaleAbs(edges)
    return edges

# 4. 消除噪声干扰
def remove_noise(image):
    denoised_image = cv2.medianBlur(image, 5)
    return denoised_image

# 5. 将各个图幅合并为一个图幅
def merge_images(images, block_size):
    rows = len(images) * block_size
    cols = block_size
    merged_image = np.zeros((rows, cols), dtype=np.uint8)
    for i, image in enumerate(images):
      x = 0
      y = i * block_size
      merged_image = 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 = 800

# 读取遥感影像
image = cv2.imread(image_path)

# 1. 分割遥感影像为多块图幅
block_images = split_image(image, block_size)

# 2. 分别对每块图幅转为灰度图像
gray_images =

# 3. 对各个图像进行边缘检测
edges_images =

# 4. 消除噪声干扰
denoised_images =

# 5. 将各个图幅合并为一个图幅
merged_image = merge_images(denoised_images, block_size)

# 6. 导出结果
export_result(merged_image, "output.jpg")


这样修改后,你应该能够正确地合并图像并导出结果。
如果问题已经解决,请设置最佳答案

学习编程中的Ben 发表于 2023-8-7 20:14:02

你好,根据错误信息和代码分析,问题出在获取原始图像形状时,使用了错误的索引方式。为了修复此问题,请修改 merge_images 函数中的以下行:

merged_image = np.zeros(original_image_shape, dtype=np.uint8)


改为:

merged_image = np.zeros((original_image_shape, original_image_shape, 3), dtype=np.uint8)


同时,请将 export_result 函数中的以下行:

cv2.imwrite(file_path, image)


改为:

cv2.imwrite(file_path, cv2.cvtColor(image, cv2.COLOR_BGR2RGB))


这样可以确保导出的图像的颜色通道顺序正确。修改后的完整代码如下:

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
            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):
    sobel_x = cv2.Sobel(image, cv2.CV_64F, 1, 0, ksize=3)
    sobel_y = cv2.Sobel(image, cv2.CV_64F, 0, 1, ksize=3)
    edges = cv2.magnitude(sobel_x, sobel_y)
    edges = cv2.convertScaleAbs(edges)
    return edges

# 4. 消除噪声干扰
def remove_noise(image):
    denoised_image = cv2.medianBlur(image, 5)
    return denoised_image

# 5. 将各个图幅合并为一个图幅
def merge_images(images, original_image_shape):
    merged_image = np.zeros((original_image_shape, original_image_shape, 3), dtype=np.uint8)
    block_size = images.shape# assume all blocks have the same size
    rows = original_image_shape // block_size
    cols = original_image_shape // block_size
    for i, image in enumerate(images):
      x = (i % cols) * block_size
      y = (i // cols) * block_size
      merged_image, x:x+image.shape, :] = cv2.cvtColor(image, cv2.COLOR_GRAY2BGR)# 转换为彩色图像
    return merged_image

# 6. 导出结果
def export_result(image, file_path):
    cv2.imwrite(file_path, cv2.cvtColor(image, cv2.COLOR_BGR2RGB))

# 示例代码
image_path = r"C:\Users\WINDOWS\Desktop\taiyuan\python\dune\dune.tif"
block_size = 800

# 读取遥感影像
image = cv2.imread(image_path)

# 1. 分割遥感影像为多块图幅
block_images = split_image(image, block_size)

# 2. 分别对每块图幅转为灰度图像
gray_images =

# 3. 对各个图像进行边缘检测
edges_images =

# 4. 消除噪声干扰
denoised_images =

# 5. 将各个图幅合并为一个图幅
merged_image = merge_images(denoised_images, image.shape)

# 6. 导出结果
export_result(merged_image, "output.jpg")


希望对你有帮助!如果还有其他问题,请随时提问。

如果回答对你有帮助,请给我一个最佳答案!{:10_254:}{:10_254:}{:10_254:}
页: [1]
查看完整版本: 帮忙改正代码错误