Justheartyoung 发表于 2023-8-8 14:41:09

请帮忙根据报错信息,修改代码。谢谢

代码:
from tifffile import imread, imwrite
from skimage import filters, feature, color, morphology
import matplotlib.pyplot as plt
import numpy as np
from scipy import ndimage
import cv2
# 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
            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

#3.检测目标地物
def edge_detection(images):
    edges = []
    for image in images:
      # 使用其他边缘检测方法,这里以Sobel算子为例
      # 边缘检测,提取沙丘脊线
      edges = filters.sobel(image)

      # 计算局部自适应阈值
      threshold = filters.threshold_local(edges, block_size=11, offset=0.008)

      # 将阈值应用于图像,得到二值化图像
      edges_image = edges < threshold

      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.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 = images
            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 = 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 "F:/duneline/sobel.py", line 142, in <module>
    edges = edge_detection(gray_images)
File "F:/duneline/sobel.py", line 105, in edge_detection
    edges.append(edges_image)
AttributeError: 'numpy.ndarray' object has no attribute 'append'

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

根据报错信息,问题出现在第105行的edges.append(edges_image)。错误提示是'numpy.ndarray' object has no attribute 'append',意思是numpy.ndarray对象没有append属性。

要解决这个问题,可以将edges列表改为edges_image数组。修改后的代码如下:

# 3.检测目标地物
def edge_detection(images):
    edges = []
    for image in images:
      # 使用其他边缘检测方法,这里以Sobel算子为例
      # 边缘检测,提取沙丘脊线
      edge = filters.sobel(image)

      # 计算局部自适应阈值
      threshold = filters.threshold_local(edge, block_size=11, offset=0.008)

      # 将阈值应用于图像,得到二值化图像
      edges_image = edge < threshold

      edges.append(edges_image)
    return edges


请注意,edges改为edges_image。这样修改后,应该能够解决报错问题。

如果回答对你有帮助,请给我一个最佳答案!{:10_254:}{:10_254:}{:10_254:}

陶远航 发表于 2023-8-8 14:41:23

根据报错信息,问题出现在代码的第105行。错误提示是numpy.ndarray对象没有append属性。

这是因为在函数edge_detection中,你定义了edges作为一个空列表,并在循环中尝试使用append方法添加元素。然而,你在之前的代码中将edges赋值为一个NumPy数组,它没有append方法。

要解决这个问题,你可以将edges重新定义为一个空列表,在循环中使用不同的变量名避免冲突。以下是修改后的代码:

def edge_detection(images):
    result = []
    for image in images:
      # 使用其他边缘检测方法,这里以Sobel算子为例
      # 边缘检测,提取沙丘脊线
      edges = filters.sobel(image)
      # 计算局部自适应阈值
      threshold = filters.threshold_local(edges, block_size=11, offset=0.008)
      # 将阈值应用于图像,得到二值化图像
      edges_image = edges < threshold
      result.append(edges_image)
    return result


请注意,我已将列表命名为result,并相应地修改了代码中的变量名。这样就能解决该报错,并保证函数edge_detection返回一个包含所有边缘图像的列表。
如果问题已经解决,请设置最佳答案

isdkz 发表于 2023-8-8 14:41:25

报错信息显示,在函数edge_detection的最后一行代码中,出现了AttributeError错误。错误提示是'numpy.ndarray'对象没有'append'属性。

问题的原因是在函数edge_detection中,edges被定义为一个空列表,然后在循环中尝试使用append方法向edges中添加元素。然而,edges在循环之前已经被赋值为一个numpy数组,而numpy数组没有append方法,因此导致了错误。

为了解决这个问题,我们可以将edges定义为一个空列表,并将其命名为edges_list。然后,在循环中使用append方法将edges_image添加到edges_list中。最后,将edges_list作为函数的返回值。

修改后的代码如下所示:

def edge_detection(images):
    edges_list = []
    for image in images:
      # 使用其他边缘检测方法,这里以Sobel算子为例
      # 边缘检测,提取沙丘脊线
      edges = filters.sobel(image)

      # 计算局部自适应阈值
      threshold = filters.threshold_local(edges, block_size=11, offset=0.008)

      # 将阈值应用于图像,得到二值化图像
      edges_image = edges < threshold

      edges_list.append(edges_image)
    return edges_list


希望这个解答对您有帮助!如果您有任何其他问题,请随时向我提问。
页: [1]
查看完整版本: 请帮忙根据报错信息,修改代码。谢谢