Ask Your Question
0

Result of inRange ?

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

hvn gravatar image

Hi,

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 ?

Thanks

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
2

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();
 Vec3b rgb=image.at<Vec3b>(y,x);
 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);

    Vec3b hsv=HSV.at<Vec3b>(0,0);
    int H=hsv.val[0];
    int S=hsv.val[1];
    int V=hsv.val[2];

    char name[30];
    sprintf(name,"B=%d",B);
    putText(image,name, Point(150,40) , FONT_HERSHEY_SIMPLEX, .7, Scalar(0,255,0), 2,8,false );

    sprintf(name,"G=%d",G);
    putText(image,name, Point(150,80) , FONT_HERSHEY_SIMPLEX, .7, Scalar(0,255,0), 2,8,false );

    sprintf(name,"R=%d",R);
    putText(image,name, Point(150,120) , FONT_HERSHEY_SIMPLEX, .7, Scalar(0,255,0), 2,8,false );

    sprintf(name,"H=%d",H);
    putText(image,name, Point(25,40) , FONT_HERSHEY_SIMPLEX, .7, Scalar(0,255,0), 2,8,false );

    sprintf(name,"S=%d",S);
    putText(image,name, Point(25,80) , FONT_HERSHEY_SIMPLEX, .7, Scalar(0,255,0), 2,8,false );

    sprintf(name,"V=%d",V);
    putText(image,name, Point(25,120) , FONT_HERSHEY_SIMPLEX, .7, Scalar(0,255,0), 2,8,false );

    sprintf(name,"X=%d",x);
    putText(image,name, Point(25,300) , FONT_HERSHEY_SIMPLEX, .7, Scalar(0,0,255), 2,8,false );

    sprintf(name,"Y=%d",y);
    putText(image,name, Point(25,340) , FONT_HERSHEY_SIMPLEX, .7, Scalar(0,0,255), 2,8,false );

 //imwrite("hsv.jpg",image);
 imshow( window_name, image );
}



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

image description

edit flag offensive delete link more

Question Tools

Stats

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

Seen: 3,075 times

Last updated: Jun 24 '14