Why doesn't the inRange() function detect this little red colored blob in this 50px Mat? [closed]

asked 2016-01-11 03:02:39 -0500

Solace gravatar image

updated 2016-01-11 03:05:22 -0500

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.

enter image description here

Please refer to the code below. rgbaFrame Mat converted to HSV Mat resulted in the following image named hsvImage.jpg.

enter image description here

But then when the inRange method was executed, the resulting Mat was maskedImage.jpg. (So obvicously I got an Exception here):

enter image description 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.

edit retag flag offensive reopen merge delete

Closed for the following reason question is not relevant or outdated by sturkmen
close date 2020-10-06 23:11:37.387460



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 gravatar imageboaz001 ( 2016-01-11 03:36:20 -0500 )edit

@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?

Solace gravatar imageSolace ( 2016-01-11 04:07:04 -0500 )edit

Isn't it because of Scalar, that has the forth element as 0? There is no variation in the forth element...

thdrksdfthmn gravatar imagethdrksdfthmn ( 2016-01-11 04:34:14 -0500 )edit

@thdrksdfthmn Which Scalar? Are you talking about new Scalar(0, 50, 40) and new 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?

Solace gravatar imageSolace ( 2016-01-11 04:43:57 -0500 )edit

OpenCV reads images in BGR format. Therefore, you should use COLOR_BGR2HSV in your cvtColor function

LorenaGdL gravatar imageLorenaGdL ( 2016-01-11 04:48:17 -0500 )edit

Yes 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

thdrksdfthmn gravatar imagethdrksdfthmn ( 2016-01-11 04:53:46 -0500 )edit

@boaz001 As I mentioned in the last comment, my HSV value of (2.5, 229.5, 204) which does lie in the range given to inRange(). Do you think otherwise?

Solace gravatar imageSolace ( 2016-01-11 04:59:39 -0500 )edit

Have you printed the values in the Mat (HSV one)?

thdrksdfthmn gravatar imagethdrksdfthmn ( 2016-01-11 05:01:31 -0500 )edit

You are mentioning that you are getting an exception there, what is it saying?

thdrksdfthmn gravatar imagethdrksdfthmn ( 2016-01-11 05:02:52 -0500 )edit

@thdrksdfthmn No, lemme try that.

Solace gravatar imageSolace ( 2016-01-11 05:03:42 -0500 )edit