Ask Your Question
0

Separate red and red reflection from an object

asked 2019-09-12 03:38:21 -0500

updated 2019-09-18 04:06:58 -0500

Hello,

Please i need your help, i want to detect a red color in an image, i change the channel to HSV to detect "lower red" and "upper red" by this code and it's working :

            cvtColor(im, imHSV, COLOR_BGR2HSV);
    Mat mask1 = Mat();
    Mat mask2 = Mat();
    inRange(imHSV, Scalar(0, 70, 50), Scalar(10, 255, 255), mask1);
    inRange(imHSV, Scalar(170, 70, 50), Scalar(180, 255, 255), mask2);
    Mat mask_combined = Mat();
    bitwise_or(mask1, mask2, mask_combined);

But i have a problem with this image (in the top right of the image), it's detected as red, i think it's the reflexion of light on a red object :

image description

I used this link ( https://pinetools.com/image-color-picker) to find HSV values of this part of the image, and that's what i found : it's aroud : H : 18 S : 29 V : 27

image description

I tried to use only the "Upper red", it's not detecting the reflected light but the result are not good detecting the real red color.

I I don't understand why i am detecting this as red, i need your help and thank you.

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
2

answered 2019-09-12 04:47:46 -0500

kbarni gravatar image

You can check the color values using the OpenCV GUI: imshow("HSV image",imhsv);. The colors will be off, but you can verify the values using the mouse cursor.

Otherwise it's strange, normally your segmentation should take the V values over 50 and S values over 70.

Anyway, I wrote a small interactive OpenCV utility to help getting the correct limits for segmentation with inRange. Hope it helps:

Mat imHSV,colorimg;

void on_trackbar(int, void *data)
{
    Mat sgm,res;
    int *ch=(int*)data;
    Scalar smin,smax;
    smin[0]=ch[0];smin[1]=ch[2];smin[2]=ch[4];
    smax[0]=ch[1];smax[1]=ch[3];smax[2]=ch[5];
    inRange(imHSV,smin,smax,sgm);
    colorimg.copyTo(res,sgm);
    imshow("Image",res);
}

void main()
{
    colorimg=imread("my_image.jpg");
    cvtColor(colorimg, imHSV, COLOR_BGR2HSV);
    int ch[6];
    ch[0]=ch[2]=ch[4]=0;
    ch[1]=ch[3]=ch[5]=255;
    createTrackbar("H_Min","Image",&ch[0],255,on_trackbar,ch);
    createTrackbar("H_Max","Image",&ch[1],255,on_trackbar,ch);
    createTrackbar("S_Min","Image",&ch[2],255,on_trackbar,ch);
    createTrackbar("S_Max","Image",&ch[3],255,on_trackbar,ch);
    createTrackbar("V_Min","Image",&ch[4],255,on_trackbar,ch);
    createTrackbar("V_Max","Image",&ch[5],255,on_trackbar,ch);
    waitKey();
}
edit flag offensive delete link more

Comments

Thank you, I will test it now

Kitnos gravatar imageKitnos ( 2019-09-12 04:49:50 -0500 )edit

I chose [0,7] instead of [0,10] in "H", and it's working, Thank you.

Kitnos gravatar imageKitnos ( 2019-09-12 07:18:20 -0500 )edit
Login/Signup to Answer

Question Tools

1 follower

Stats

Asked: 2019-09-12 03:38:21 -0500

Seen: 64 times

Last updated: Sep 18