Ask Your Question

Result of inRange ?

asked 2014-06-24 06:22:22 -0600

hvn gravatar image


Since a couple of days, I've been testing image processing using the inRange function. First I found out that HSV values are different for Gimp and OpenCV. So after converting, I found values I thought I should use and notice that the returned image doesn't show the object(s) itself but merely the outlines of the object(s) within the specified colour range. The values I use to filter on yellow-ish are:

inRange(imageHSVMat, Scalar(20, 20, 20), Scalar(30, 200, 200), imageThreshMat);

While what I want is to find the object and then use findContours. In the version of my code with the C-API (using cvInRangeS) I did get want I want but the new compiler doesn't accept it. So is my new way using inRange correct or should I use different functions in the C++ API ?


edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted

answered 2014-06-24 09:41:38 -0600

Haris gravatar image

The hue value for yellow is something like 25 to 32 so you should use inRange() like

inRange(imageHSVMat, Scalar(25, 20, 20), Scalar(32, 255, 255), imageThreshMat);

And see the HSV-Color space chart here, using which you can chose correct HSV value.

And if you want know the HSV value of specific pixel use below code

char window_name[30] = "HSV Segmentation";
Mat src;

static void onMouse( int event, int x, int y, int f, void* ){
 Mat image=src.clone();
 int B=rgb.val[0];
 int G=rgb.val[1];
 int R=rgb.val[2];

  Mat HSV;
  Mat RGB=image(Rect(x,y,1,1));
  cvtColor(RGB, HSV,CV_BGR2HSV);

    int H=hsv.val[0];
    int S=hsv.val[1];
    int V=hsv.val[2];

    char name[30];
    putText(image,name, Point(150,40) , FONT_HERSHEY_SIMPLEX, .7, Scalar(0,255,0), 2,8,false );

    putText(image,name, Point(150,80) , FONT_HERSHEY_SIMPLEX, .7, Scalar(0,255,0), 2,8,false );

    putText(image,name, Point(150,120) , FONT_HERSHEY_SIMPLEX, .7, Scalar(0,255,0), 2,8,false );

    putText(image,name, Point(25,40) , FONT_HERSHEY_SIMPLEX, .7, Scalar(0,255,0), 2,8,false );

    putText(image,name, Point(25,80) , FONT_HERSHEY_SIMPLEX, .7, Scalar(0,255,0), 2,8,false );

    putText(image,name, Point(25,120) , FONT_HERSHEY_SIMPLEX, .7, Scalar(0,255,0), 2,8,false );

    putText(image,name, Point(25,300) , FONT_HERSHEY_SIMPLEX, .7, Scalar(0,0,255), 2,8,false );

    putText(image,name, Point(25,340) , FONT_HERSHEY_SIMPLEX, .7, Scalar(0,0,255), 2,8,false );

 imshow( window_name, image );

int main(){
 src = imread("bgr.png",1);
 setMouseCallback( window_name, onMouse, 0 );

image description

edit flag offensive delete link more

Question Tools


Asked: 2014-06-24 06:22:22 -0600

Seen: 2,710 times

Last updated: Jun 24 '14