我将首先解释我想做什么。我有一幅图像,我想存储特定ROI的像素值。因此,我实现了以下循环(在该站点的另一个主题中找到):
pixels = im.load()
all_pixels = []
for x in range(SpecificWidth):
for y in range(SpecificHeight):
cpixel = pixels[x, y]
all_pixels.append(cpixel)
但是,它不返回SpecificwidthXSpecificHeight矩阵,而是长度与值一样长的矩阵之一。因为我想保持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)
我有标题中提到的错误。
@RolandSmith对于您所看到的错误消息的原因是绝对正确的。一种更有效的方法来实现您要执行的操作,是将整个图像转换为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]
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句