1

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

example image:

example

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

Andriy Rizhiy
  • 233
  • 1
  • 2
  • 7

1 Answers1

0

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 
sheikh hamza
  • 101
  • 3