首页 > 解决方案 > 如何分离存储在python列表中的不同尺寸的图像

问题描述

我有一个长度列表(Cyst_intensity)l ,其中包含维度中图像轮廓区域的值(n x 3)。即列表列表。

其中3表示三个通道,n表示具有图像值的行。

在此处输入图像描述

我想将红色、蓝色、绿色通道分别存储在一个列表中。

# Cyst pixel generator
cyst_intensity= []

# For each list of contour points...
for i in range(len(cystcontours)):
    # Create a mask image that contains the contour filled in
    cimg = np.zeros_like(image)
    cv.drawContours(cimg, cystcontours, i, color=255, thickness=-1)

    # Access the image pixels and create a 1D numpy array then add to list
    pts = np.where(cimg == 255)
    #Cyst_intensity will contain the original image contour pixel value  
    cyst_intensity.append(image[pts[0], pts[1]])

#separating into channels and averaging out 
B=[]
G=[]
R=[]
cystcolours=[]
i=0
for m in iter(cyst_intensity):
    for j in m[i][0]:
        B.append(j)
        i+=1
    i=0
    for k in m[i][1]:
        G.append(k)
        i+=1
    i=0
    for q in m[i][2]:
        R.append(q)
        i+=1
    cystcolours.append([avg(B),avg(G),avg(R)])

运行上述代码时出现以下错误。

Traceback (most recent call last):
  File "<input>", line 7, in <module>    
TypeError: 'numpy.uint8' object is not iterable

如何解决这个问题?

标签: pythonlist

解决方案


尽管我不知道您如何存储图像,但我建议您将图像存储为 kx3,其中 k 是一个像素。您的错误是 m[i][j] 是单个值而不是数组,我认为这是您的错误。所以如果你想遍历图像的所有行,你应该做这样的事情。

for image in cyst_identity:
 for pixel in image:
  R.append(pixel[0])
  G.append(pixel[1])
  B.append(pixel[2])
 #get average here. Outside the inner loop

如果您存储的每个图像都是 nxn 类型,其中这些 2D 数组的每个单元格都是一个像素,那么上述方法将不起作用。我无法复制这个问题,因为信息对于一个明确的答案来说有点不完整。


推荐阅读