简体   繁体   中英

How i can get all pixels for all objects in opencv?

I have image with separate objects, which have one color.

example image:

例

I want to get all the pixels from each object. I use Python and CV2. But I do not know how to do it.

example that i want to get:

  • object1 : [(x1,y1), (x2,y2) ... (xn1,yn1)] where n1 - count all pixels in object1
  • object2 : [(x1,y1), (x2,y2) ... (xn2,yn2)] where n2 - count all pixels in object2
  • ...
  • objectm : [(x1,y1), (x2,y2) ... (xnm,ynm)] where nm - count all pixels in objectm

UPD: This can be done with cv2.connectedComponents(). See this connected component labeling in python . Thanks beaker

considering that the img is open in 'img' you can use immg[i,j] to return values of blue red green eg

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

so you can use something like

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 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM