2

So basically, i want to iterate over all my pixels, and if they are withing range, change their RGB values to white, else to black.

I've seen a couple of examples where a mask is used, but i'm a ittle confused as to how i would use a mask to compare an RGB value. My range is as such

min_YCrCb = np.array([0,133,77],np.uint8) 
max_YCrCb = np.array([255,173,127],np.uint8)

So first i have my image,img, in YCrCb. How do i create a mask such that it'll see if the RGB is in range, and once that's done, how do i set them to black and white?

prelibiton
  • 29
  • 8
lambda
  • 85
  • 1
  • 13

1 Answers1

2

I think the inRange method is what you need.

So, in your example you could use:

# Keep in mind that OpenCV stores things in BGR order, not RGB
lowerBound = cv.Scalar(0, 133, 770)
upperBound = cv.Scalar(255, 173, 127)

# this gives you the mask for those in the ranges you specified
cv.InRange(cv_input, lowerBound, upperBound, cv_output);

For each pixel in your cv_input, if its value is in the given range, it will be set to 255 (all 1s), otherwise 0. If you want the inverse, you can use Not method.

# This will set all bits in cv_input 
cv.Not(cv_output, cv_inverse)
Chong Tang
  • 2,066
  • 15
  • 12