python - 如何分离存储在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
如何解决这个问题?
解决方案
尽管我不知道您如何存储图像,但我建议您将图像存储为 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 数组的每个单元格都是一个像素,那么上述方法将不起作用。我无法复制这个问题,因为信息对于一个明确的答案来说有点不完整。
推荐阅读
- javascript - jQuery DataTables - 调用多个插件?(日期范围/范围搜索)
- python - 马修特征值
- prisma - Prisma - 如何使用多个模型中的字段定义复合唯一约束?
- java - springdoc-openapi、swagger UI 中的 XML 示例
- sql - SQL 查找多个条件的最后一个值
- r - 添加时间间隔的无暴露时段(间隙)的行
- c++ - 调用 movetothread() 后如何停止 Qthread?
- javascript - 一个组件的导出功能并在另一个组件中使用
- javascript - Angular:由于不允许的 MIME 类型(“text/html”),浏览器有时无法加载 es2015 文件
- powershell - 如何使用带有通配符的 Powershell 运行 EXE 来查找文件?