Ask Your Question

A problem met in using cvInRange

asked 2013-06-01 16:00:02 -0500

Torlek gravatar image

updated 2013-06-01 20:59:11 -0500

Hi: I'm new to get my hand on opencv. In order to seprate specified object with specified color in image, I used cvInRange, but it fails with some exception. Since I compiled it using vc+clr and did not use the debug static libs, I cannot see anything on the information should be contained in the exception thrown. I ask for help in determine the problem which caused the program failing. Let me describe the program flow short:

 //get image from cam
 IplImage* frame = cvQueryFrame( capture );
 //convert to hsv space
 cvCvtColor( frame , frame , CV_BGR2HSV );
 //conversion completed, set some Scalar
  CvScalar color1;
  CvScalar color2;

color1.val[0] = 113/2;
color1.val[1] = 100;
color1.val[2] = 100;
color1.val[3] = 0;

color2.val[0] = 205/2;
color2.val[1] = 255;
color2.val[2] = 255;
color2.val[3] = 0;

 //create destine mask
 IplImage* imageMask = cvCreateImage(cvGetSize(frame),
 //get the points
 cvInRange( frame, 
 //cvInRangeS fails as well...
 /*cvInRangeS( frame, 
 The program fails the last statement. I tried several days on this, and cannot resolve this, can any one help find out what the problem is?
edit retag flag offensive close merge delete



first, please do not use opencv's arcane c-api, but the c++ one

please use cv::Mat and cv::inRange , then come back, if you run into problems.

please also have a look at the tutorials

berak gravatar imageberak ( 2015-06-12 01:21:35 -0500 )edit

1 answer

Sort by ยป oldest newest most voted

answered 2015-06-12 01:31:11 -0500

Karthikeyan gravatar image

From the openCV documentation,

First of all, you should use only cvInRangeS for using the Scalar limits. The reason you are getting the exception is that, the destination Matrix should be of single channel. You are creating it as a 3 channel.

Change the following line of code from this

IplImage* imageMask = cvCreateImage(cvGetSize(frame), IPL_DEPTH_8U, 3);


IplImage* imageMask = cvCreateImage(cvGetSize(frame), IPL_DEPTH_8U, 1);

And use cvInRangeS, it works. And move to C++ interface of openCV, developers here mostly not using c interface and you may not get the answers.

edit flag offensive delete link more
Login/Signup to Answer

Question Tools


Asked: 2013-06-01 16:00:02 -0500

Seen: 694 times

Last updated: Jun 12 '15