0

I have a marker array defining different parts of an image, and i need to calculate de mean color of each region defined by the markers. I've got some results, but they don't seem right to me... so I coded this test to see what is happening:

img = cv2.imread(ImgPath)

markers = cv2.connectedComponents(foreground)[1]
markers += 1  # Add one to all labels so that background is 1, not 0
markers[unknown==255] = 0  # mark the region of unknown with zero
markers = cv2.watershed(img, markers)

MARKER = 16

copia=img.copy()
copia[markers==MARKER]=(0,0,255)
average=cv2.mean(copia[markers==MARKER])
copia2=img.copy()
copia2[markers==MARKER]=(0,0,average[0])
print(average)

cv2.imshow('watershed result', labeled_img2)
cv2.imshow('original', img)
cv2.imshow('copia', copia)
cv2.imshow('copia2', copia2)

Note that the definition of "foreground" and "labeled_img2" are not important for the question, so i didn't included them for readability.

The output is as follows:

(85.0, 0.0, 0.0, 0.0)

Image Output

For some reason the screen capture modifies the color of the image, i don't know why at all, but that's not the issue. From the code above I would expect the two bottom images to be exactly equal. Also, I would expect that the output of "print(average)" was something similar to "(0,0,255)".

The main question is: why cv2.mean(copia[markers==MARKER]) gives a non-red color if I defined copia[markers==MARKER] as red?

Some other related questions are: is there any reason for the format of colors being BGR and not RGB? Which format is the mean output in? RGBA?

Pepv
  • 76
  • 15
  • 1
    "Which format is the mean output in?" Same as input, but it's an OpenCV scalar, so it always has 4 elements (the extra ones are set to 0). – Dan Mašek Dec 23 '19 at 17:53
  • 1
    As for why BGR vs RGB -- historical reasons. Search around, there are multiple questions here on the topic, e.g. https://retrocomputing.stackexchange.com/questions/3023/why-bgr-color-order https://stackoverflow.com/questions/14556545/why-opencv-using-bgr-colour-space-instead-of-rgb https://stackoverflow.com/questions/367449/what-exactly-is-bgr-color-space ... – Dan Mašek Dec 23 '19 at 17:59

0 Answers0