First time here? Check out the FAQ!

Ask Your Question
0

A problem met in using cvInRange

asked Jun 1 '13

Torlek gravatar image

updated Jun 2 '13

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),
IPL_DEPTH_8U, 3);
 //get the points
 cvInRange( frame, 
&color1, 
&color2,
imageMask);
 //cvInRangeS fails as well...
 /*cvInRangeS( frame, 
color1, 
color2,
imageMask);*/
 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?
Preview: (hide)

Comments

1

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 (Jun 12 '15)edit

1 answer

Sort by » oldest newest most voted
0

answered Jun 12 '15

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);


To


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.

Preview: (hide)

Question Tools

Stats

Asked: Jun 1 '13

Seen: 952 times

Last updated: Jun 12 '15