简体   繁体   English

AttributeError:'numpy.ndarray'对象没有属性'append':图像处理示例

[英]AttributeError: 'numpy.ndarray' object has no attribute 'append':Image processing example

I will first explain what I want to do. 我将首先解释我想做什么。 I have an image and I want to store the pixel values for a specific ROI. 我有一幅图像,我想存储特定ROI的像素值。 For that reason I implement the following loop(found in another topic of the site): 因此,我实现了以下循环(在该站点的另一个主题中找到):

pixels = im.load() 

all_pixels = []
for x in range(SpecificWidth):
  for y in range(SpecificHeight):
    cpixel = pixels[x, y]
    all_pixels.append(cpixel)

However, it does not return a SpecificwidthXSpecificHeight matrix but one of length as much as the values. 但是,它不返回SpecificwidthXSpecificHeight矩阵,而是长度与值一样长的矩阵之一。 Because I want to keep the size of the matrix of the ROI I implement the following loop(much the same with the previous): 因为我想保持ROI矩阵的大小,所以我实现了以下循环(与前面的循环非常相似):

array=np.array(all_pixels)
roi_pixels = np.zeros((SpecificWidth,SpecificHeight))

for i in range(0,array.shape[0],width):
    c_roi_pixels=all_pixels[i]
    roi_pixels.append(c_roi_pixels)

And I have the error as it mentioned in the title. 我有标题中提到的错误。

In numpy, append is a function , not a method . 在numpy中, append函数 ,而不是方法

So you should use eg: 因此,您应该使用例如:

roi_pixels = np.append(roi_pixels, c_roi_pixels)

Note that the append function creates and returns a copy! 注意, append函数会创建并返回一个副本! It does not modify the original. 它不会修改原始文件。

@RolandSmith is absolutely right about the cause of the error message you're seeing. @RolandSmith对于您所看到的错误消息的原因是绝对正确的。 A much more efficient way to achieve what you're trying to do is to convert the whole image to a numpy array, then use slice indexing to get the pixels corresponding to your ROI: 一种更有效的方法来实现您要执行的操作,是将整个图像转换为numpy数组,然后使用切片索引来获取与您的ROI相对应的像素:

# convert the image to a numpy array
allpix = np.array(im)

# array of zeros to hold the ROI pixels
roipix = np.zeros_like(allpix)

# copy the ROI region using slice indexing
roipix[:SpecificHeight, :SpecificWidth] = allpix[:SpecificHeight, :SpecificWidth]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 AttributeError: 'numpy.ndarray' 对象没有属性 'append' - AttributeError: 'numpy.ndarray' object has no attribute 'append' Python - AttributeError:'numpy.ndarray'对象没有属性'append' - Python - AttributeError: 'numpy.ndarray' object has no attribute 'append' AttributeError: 'numpy.ndarray' object 没有属性 'append' 错误 - AttributeError: 'numpy.ndarray' object has no attribute 'append' error Python AttributeError: 'numpy.ndarray' object 没有属性 'append' - Python AttributeError: 'numpy.ndarray' object has no attribute 'append' AttributeError:“ numpy.ndarray”对象没有属性“ A” - AttributeError: 'numpy.ndarray' object has no attribute 'A' 'numpy.ndarray' 对象没有属性 'append' - 'numpy.ndarray' object has no attribute 'append' numpy.ndarray' object 没有属性 'append - numpy.ndarray' object has no attribute 'append Numpy和Matplotlib-AttributeError:“ numpy.ndarray”对象没有属性“ replace” - Numpy and Matplotlib - AttributeError: 'numpy.ndarray' object has no attribute 'replace' 为什么 python 抛出错误:AttributeError: 'numpy.ndarray' object has no attribute 'append'? - Why is python throwing error : AttributeError: 'numpy.ndarray' object has no attribute 'append'? AttributeError: 'numpy.ndarray' 对象没有属性 'insert' - AttributeError: 'numpy.ndarray' object has no attribute 'insert'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM