Riandra Putra
Riandra Putra

Reputation: 39

how to detect color white in ycrcb?

i want to detect white object using open cv in python, but i have problem to define lower white and upper white in ycbcr. i try to make program but the program doesn't get right result to detect an object. this my code:

 ycrcb = cv.cvtColor(rgb, cv.COLOR_BGR2YCrCb)
 lower_white = np.array([205, 128, 128], dtype=np.uint8)
 upper_white = np.array([235, 128, 128], dtype=np.uint8)
 img = cv.inRange(ycrcb, lower_white, upper_white)

and i try to detect using structuring element and send to morphology :

se_3 = cv.getStructuringElement(cv.MORPH_RECT,(3,3))
dst_dilate = cv.dilate(img, se_3, iterations = 1)

and put it together using bitwise and:

res = cv.bitwise_and(rgb,rgb, mask= dst_dilate)

i try my best but the result is incorrect, i need your opinion which part to change and get better result.

the object is all birds

Upvotes: 0

Views: 338

Answers (1)

Mark Setchell
Mark Setchell

Reputation: 208077

The easiest way to do this is to load your image, convert it to your desired colourspace and split the channels, laying them out side-by-side. Then use your system's "colour-dropper tool" ("Digital Color Meter" on macOS) to look at the values of the individual channels in the areas that interest you:

import cv2

# Load image
im = cv2.imread('qAK68.jpg')

# Convert to YCrCb colourspace
YCrCb = cv2.cvtColor(im, cv2.COLOR_BGR2YCrCb)

# Split channels and lay out side-by-sise, Y on the left, Cr then Cb on the right
Y, Cr, Cb = cv2.split(YCrCb)
hstack = np.hstack((Y,Cr,Cb))

enter image description here

You should see you need roughly the following ranges:

  • Y 60..255
  • Cr 120..136
  • Cb 120..136

If you don't have a "Color Dropper" tool, just go to ImageJ online tool here and upload my output image below and mouse over it to see the values like this:

enter image description here


If you are on Linux, you can get a colour dropper called gpick with:

sudo apt install gpick

Upvotes: 5

Related Questions