首页 > 解决方案 > 我如何获得opencv中所有对象的所有像素?

问题描述

我有带有单独对象的图像,这些对象具有一种颜色。

示例图像:

例子

我想从每个对象中获取所有像素。我使用 Python 和 CV2。但我不知道该怎么做。

我想得到的例子:

UPD:这可以通过 cv2.connectedComponents() 来完成。在 python 中查看这个连接的组件标签。谢谢烧杯

标签: pythonopencv

解决方案


考虑到 img 在 'img' 中打开,您可以使用 immg[i,j] 返回蓝色红色绿色的值,例如

>>img[12,12]
[143,144,255] // this is what is returned [blue green red]

所以你可以使用类似的东西

rows = img.shape[0]
cols = img.shape[1]
for (i in range(0,rows)):
    for (j in range(0,cols)):
        bgr=img[i,j]
    #now use if condition and match brg values with color you wnana detect then append the pair i,j in the a list if the condition matcches 

推荐阅读