0

i want to detect an image is green in python using the ycbcr color space on open cv, I've tried to define the lower and upper bounds of the color. but still having trouble finding the right format. here my code :

img4 = cv.cvtColor(img3, cv.COLOR_BGR2YCrCb)
lower_green = np.array([93, 147, 72])
upper_green = np.array([75, 113, 87])
mask_green = cv.inRange(img4,lower_green, upper_green)
green = cv.bitwise_and(img4,img4, mask=mask_green)
cv.imshow('Original2 Image',img4 )
cv.imshow('mask green color',green )

enter image description here

and how do I detect color if I have a lot of images, for example if I have 100 images of apples and there are red and green colors, do I have to do it bitwise or between the lower and upper limits of red and green?, and whether the masking is done during the import iteration of the image or when the image has been inserted into the array like :

data = []
data.append(im)
X = np.array(data)
pratama
  • 45
  • 8
  • 2
    Check out the answer provided by nathancy and modify it to find the color range in YCrCb space https://stackoverflow.com/questions/22588146/tracking-white-color-using-python-opencv – Jeru Luke Jun 11 '22 at 12:34
  • the average answer uses hsv instead of ycrcb – pratama Jun 11 '22 at 12:36
  • 1
    If you read the comment again, modify that answer by converting to YCrCb instead of HSV – Jeru Luke Jun 11 '22 at 12:38
  • ok I will try it, but what is the answer to my second question? – pratama Jun 11 '22 at 12:39
  • This should help too... https://stackoverflow.com/a/72425243/2836621 – Mark Setchell Jun 11 '22 at 13:30
  • 1
    You might find it easier to use LAB A channel to determine red vs green. From https://en.wikipedia.org/wiki/CIELAB_color_space "The a* axis is relative to the green–red opponent colors, with negative values toward green and positive values toward red." Compare that to https://en.wikipedia.org/wiki/YCbCr where it says "CB and CR are the blue-difference and red-difference chroma components". So in the LAB A channel, red is positive and green is negative. So red will be light and green dark in the A channel. So you can threshold easily on one or the other. – fmw42 Jun 11 '22 at 16:27
  • how to apply it to a lot of pictures? @fmw42 – pratama Jun 12 '22 at 12:09
  • @pratama Have a look at my recent post where we can detect green color in images: https://stackoverflow.com/questions/47483951/how-to-define-a-threshold-value-to-detect-only-green-colour-objects-in-an-image/72264323#72264323 – Jeru Luke Jun 12 '22 at 17:52

0 Answers0