Why doesn't the inRange() function detect this little red colored blob in this 50px Mat? [closed]
I created the following 50px by 50px
image in Adobe PS, and the red circle has an HSV
value of H=5, S=90, V=80
, where the HSV
scale in PS is H=0-360, S=0-100, V=0-100
. The HSV
scale in OpenCV is H=0-180, S=0-100, V=0-100
, so in OpenCV terms, the HSV
value of the red circle in the image is H= 5/2 =2.5
, S= (90/100)*255 =229.5
, V= (80/100)*255 =204
.
The range I have given to the inRange()
function in the following code snippet is H=0-10, S=50-255, V=40-255
.
I stored the in my sdcard by the name of rgbaFrame.jpg
.
Please refer to the code below. rgbaFrame
Mat
converted to HSV
Mat
resulted in the following image named hsvImage.jpg
.
But then when the inRange
method was executed, the resulting Mat
was maskedImage.jpg
. (So obvicously I got an Exception here):
public void doProcessing(View view) {
Mat rgbaFrame = Highgui.imread("/mnt/sdcard/DCIM/rgbaFrame.jpg");
Mat hsvImage = new Mat();
Imgproc.cvtColor(rgbaFrame, hsvImage, Imgproc.COLOR_RGB2HSV);
Highgui.imwrite("/mnt/sdcard/DCIM/hsvImage.jpg", hsvImage);// check
Mat maskedImage = new Mat();
/**Color Tested: In PS: H=5, S=90, V=80 | In OpenCV: H=5/2=2.5, S=90/100*255=229.5, V=80/100*255=204 (OPENCV SCALE: h=0-180, s=0-255, v=0-255) */
Core.inRange(hsvImage, new Scalar(0, 50, 40), new Scalar(10, 255, 255), maskedImage);
Highgui.imwrite("/mnt/sdcard/DCIM/maskedImage.jpg", maskedImage);// check
}
Note: The code given is a part of an SSCCE I wrote. If you think I should post the complete SSCCE, please let me know.
What is the hsv value of the color according to opencv? Is it what you expect? Also, avoid posting/using compressed image formats (jpg) for accurate color detection/conversion stuff.
@boaz001 The red color is
H=2.5
,S=229.5
,V=204
, according to the scale of OpenCV. What image format should I use?Isn't it because of Scalar, that has the forth element as 0? There is no variation in the forth element...
@thdrksdfthmn Which Scalar? Are you talking about
new Scalar(0, 50, 40)
andnew Scalar(10, 255, 255)
?If so, there is no 4th element there. I am sorry I am not getting what you are saying? Could you elaborate?OpenCV reads images in BGR format. Therefore, you should use
COLOR_BGR2HSV
in yourcvtColor
functionYes Scalar has 4 elements, and I am not really sure how are they used in functions applied on Mat, because it may have less than 4 channels, so which value is for what channel? Always first for first and so on or how, because RGB and BGR are different and there are also other color spaces, like HSV or BGRA, etc
@boaz001 As I mentioned in the last comment, my HSV value of
(2.5, 229.5, 204)
which does lie in the range given toinRange()
. Do you think otherwise?Have you printed the values in the Mat (HSV one)?
You are mentioning that you are getting an exception there, what is it saying?
@thdrksdfthmn No, lemme try that.