如何获取opencv中所有对象的所有像素?

2024-09-23 14:27:52 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个单独的对象,有一种颜色的图像。你知道吗

示例图像:

example

我想得到每个物体的所有像素。我使用Python和CV2。但我不知道怎么做。你知道吗

我想得到的例子:

  • 对象1:[(x1,y1),(x2,y2)。。。(xn1,yn1)],其中n1-计算object1中的所有像素
  • 对象2:[(x1,y1),(x2,y2)。。。(xn2,yn2)],其中n2-计算object2中的所有像素
  • 。。。你知道吗
  • 对象M:[(x1,y1),(x2,y2)。。。(xnm,ynm)]其中nm-计算objectm中的所有像素

升级:这可以通过cv2.connectedComponents()完成。看这个connected component labeling in python。谢谢beaker


Tags: 对象图像示例颜色像素cv2例子物体
1条回答
网友
1楼 · 发布于 2024-09-23 14:27:52

考虑到img在“img”中是开放的 可以使用immg[i,j]返回蓝-红-绿eg的值

>>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 

相关问题 更多 >