Why is this simple mask not working?
I'm in the process of learning how to use masking and the bitwise_and function. I have some tutorial code that works but mine is not and I can't see why. The source image I'm using is here
import numpy as n
import cv2
# Import the image
im = cv2.imread('orange.jpg')
hsv = cv2.cvtColor(im, cv2.COLOR_BGR2HSV)
upper = n.array([-20,100,100])
lower = n.array([25,100,255])
mask = cv2.inRange(hsv,lower,upper)
result = cv2.bitwise_and(im,im, mask= mask)
# Display the image, esc to kill the window
cv2.imshow('result',result)
cv2.imshow('mask',mask)
cv2.waitKey(0)
cv2.destroyAllWindows()
inRange works like a bandpass filter - you get , what's left between the bounds. you are confusing upper and lower bounds here, and having the same value for both leaves no space inbetween (no matter, if you put 100 for both or 15).
also, hue is in the [0..180] range (full circle, but divided by 2, to fit into a byte) and can't be negative
I'm not sure I follow you on the lower/upper arrays. The example code I have acts like I would expect, placing lower and upper limits on HSV values.
I tried changing the lower/upper arrays in my original code to this:
And it works, but not well. The resulting mask is extremely rough, and the resulting image is still black, like the mask wasn't applied at all.
mask = cv2.inRange(hsv,(0,50,50),(30,255,255))