鱼C论坛

 找回密码
 立即注册
查看: 1582|回复: 2

请帮忙改正错误。并给出改正后的代码

[复制链接]
发表于 2023-8-7 21:26:37 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
代码:
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[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)
        grayscale_images.append(gray_image)
    return grayscale_images
# 3. 使用其他边缘检测方法代替Canny算法
# canny算子
# def edge_detection(images):
#     edges = []
#     for image in images:
#         edges_image = cv2.Canny(image, 10, 50)  # 调整阈值根据实际情况
#         edges.append(edges_image)
#     return edges

# sobel算子
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

# # 拉普拉斯算子
# def edge_detection(images):
#         edges = []
#         for image in images:
#             edges_image = cv2.Laplacian(image, cv2.CV_8U)
#             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 = cv2.imread(r"C:\\Users\\WINDOWS\\Desktop\\taiyuan\\python\\1=quick_dom.tif")
# 1. 分割遥感影像为多块图幅
num_rows = 3
num_cols = 7
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_output1.jpg')
报错:
Traceback (most recent call last):
  File "C:\Users\WINDOWS\Desktop\taiyuan\python\segement.py", line 153, in <module>
    image = cv2.imread(r"C:\\Users\\WINDOWS\\Desktop\\taiyuan\\python\\1=quick_dom.tif")
cv2.error: OpenCV(4.8.0) D:\a\opencv-python\opencv-python\opencv\modules\imgcodecs\src\loadsave.cpp:79: error: (-215:Assertion failed) pixels <= CV_IO_MAX_IMAGE_PIXELS in function 'cv::validateInputImageSize'
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2023-8-7 21:26:52 | 显示全部楼层
这个错误是由于图像文件的尺寸太大导致的。OpenCV默认限制图像的像素数不超过 CV_IO_MAX_IMAGE_PIXELS ,如果图像的像素数超过了这个限制,就会报错。

为了解决这个问题,你可以尝试以下几种方法:

1. 检查图像文件的路径是否正确。确保文件存在,并且路径中的斜杠使用了正确的转义字符。
2. 尝试使用其他图像文件进行测试,看是否仍然报错。如果其他图像文件可以正常加载,那么可能是当前图像文件的问题。
3. 如果图像文件的尺寸确实很大,你可以尝试缩小图像的尺寸,或者使用其他方法来处理大尺寸图像。例如,你可以将图像切割成更小的块进行处理,然后再将处理后的块合并起来。

另外,你还可以尝试打印出图像的尺寸信息,以便更好地理解问题所在。你可以在加载图像之前添加以下代码:

  1. print("Image shape:", image.shape)
复制代码


这样可以在报错之前打印出图像的尺寸信息,以便更好地了解图像的大小和是否超过了限制。

希望这些方法能够帮助你解决问题!
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2023-8-7 21:27:20 | 显示全部楼层
错误是由于图像的尺寸超过了OpenCV所允许的最大像素数。可以尝试使用cv2.IMREAD_UNCHANGED标志来解决这个问题,这样可以读取任意大小的图像。

下面是修改后的代码:

  1. import cv2
  2. import numpy as np

  3. # 1. 分割遥感影像为多块图幅
  4. def split_image(image, num_rows, num_cols):
  5.     height, width = image.shape[:2]
  6.     row_height = height // num_rows
  7.     col_width = width // num_cols
  8.     images = []
  9.     for r in range(num_rows):
  10.         for c in range(num_cols):
  11.             start_row = r * row_height
  12.             end_row = start_row + row_height
  13.             start_col = c * col_width
  14.             end_col = start_col + col_width
  15.             sub_image = image[start_row:end_row, start_col:end_col]
  16.             images.append(sub_image)
  17.     return images

  18. # 2. 分别对每块图幅转为灰度图像
  19. def convert_to_grayscale(images):
  20.     grayscale_images = []
  21.     for image in images:
  22.         gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
  23.         grayscale_images.append(gray_image)
  24.     return grayscale_images

  25. # 3. 使用其他边缘检测方法代替Canny算法
  26. def edge_detection(images):
  27.     edges = []
  28.     for image in images:
  29.         # 使用其他边缘检测方法,这里以Sobel算子为例
  30.         gradient_x = cv2.Sobel(image, cv2.CV_64F, 1, 0, ksize=3)
  31.         gradient_y = cv2.Sobel(image, cv2.CV_64F, 0, 1, ksize=3)
  32.         edges_image = cv2.magnitude(gradient_x, gradient_y)
  33.         edges.append(edges_image)
  34.     return edges

  35. # 4. 消除噪声干扰
  36. def denoise(images):
  37.     denoised_images = []
  38.     for image in images:
  39.         # 使用其他滤波方法,这里以高斯滤波为例
  40.         denoised_image = cv2.GaussianBlur(image, (5, 5), 0)
  41.         denoised_images.append(denoised_image)
  42.     return denoised_images

  43. # 5. 将各个图幅合并为一个图幅
  44. def merge_images(images, num_rows, num_cols):
  45.     row_height, col_width = images[0].shape[:2]
  46.     merged_image = np.zeros((row_height * num_rows, col_width * num_cols), dtype=np.uint8)
  47.     i = 0
  48.     for r in range(num_rows):
  49.         for c in range(num_cols):
  50.             start_row = r * row_height
  51.             end_row = start_row + row_height
  52.             start_col = c * col_width
  53.             end_col = start_col + col_width
  54.             merged_image[start_row:end_row, start_col:end_col] = images[ i]
  55.             i += 1
  56.     return merged_image

  57. # 6. 导出结果
  58. def export_result(image, filename):
  59.     cv2.imwrite(filename, image)

  60. # 加载遥感影像
  61. image = cv2.imread(r"C:\\Users\\WINDOWS\\Desktop\\taiyuan\\python\\1=quick_dom.tif", cv2.IMREAD_UNCHANGED)

  62. # 1. 分割遥感影像为多块图幅
  63. num_rows = 3
  64. num_cols = 7
  65. sub_images = split_image(image, num_rows, num_cols)

  66. # 2. 分别对每块图幅转为灰度图像
  67. gray_images = convert_to_grayscale(sub_images)

  68. # 3. 使用其他边缘检测方法代替Canny算法
  69. edges = edge_detection(gray_images)

  70. # 4. 消除噪声干扰
  71. denoised_edges = denoise(edges)

  72. # 5. 将各个图幅合并为一个图幅
  73. merged_image = merge_images(denoised_edges, num_rows, num_cols)

  74. # 6. 导出结果
  75. export_result(merged_image, 'path_to_output1.jpg')
复制代码


注意,import cv2import numpy as np 之间需要添加换行符。另外,请确保输入的图像路径是正确的。
如果问题已经解决,请设置最佳答案
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-4-23 05:43

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表