Ask Your Question
0

Error when Cropping Human Image after detectMultiScale

asked 2013-09-01 23:24:59 -0600

Shaban gravatar image

Hi guys, I wanna crop detected human image using cv::Rect. This is my code:

int main(int argc, char *argv[])
{
 cv::Mat image = cv::imread("Man.jpg",1);
 cv::CascadeClassifier human;
 assert(human.load("hogcascade_pedestrians.xml"));


 std::vector<cv::Rect> rects;
 human.detectMultiScale(image, rects);   
 cv::Mat imgroi = image(rects);          //Error! Can't Convert Vector to Rect!

 cv::namedWindow("Original");
 cv::namedWindow("Cropped");
 cv::imshow("Original",image);
 cv::imshow("Cropped",imgroi);
 cv::waitKey(0);
 return 0;
}

I'll appreciate any help here. Thanks! :)

edit retag flag offensive close merge delete

Comments

My Assumption: there's only 1 person in the picture...

Shaban gravatar imageShaban ( 2013-09-01 23:27:30 -0600 )edit

2 answers

Sort by ยป oldest newest most voted
3

answered 2013-09-02 00:42:30 -0600

Michael Burdinov gravatar image

When cropping image you should provide only one Rect. You are trying to provide vector of Rect. This won't compile (even if you know for sure that there only single Rect in your vector). You should use something like this:

Mat imgroi;
if (rects.size() == 1)
     imgroi = image(rects[0]);
edit flag offensive delete link more

Comments

1

OMG! It works! Thanks! :D

Shaban gravatar imageShaban ( 2013-09-02 01:15:52 -0600 )edit

That will give you only first detection. If you have 10, you will miss other 9.

Spas Hristov gravatar imageSpas Hristov ( 2013-09-03 03:55:40 -0600 )edit
1

Spas, in current example if he has 10 detection, he will miss all 10. It creates ROI if and only if he has exactly one detection. Anyway the question was "how to solve compilation error". So this is just a simple example of code that will compile. He can take it from here and update it according to his problem.

Michael Burdinov gravatar imageMichael Burdinov ( 2013-09-03 04:39:14 -0600 )edit

Yeah, I just need first detection and my assumption there's only 1 person in the picture (and in the ROI of course)

Shaban gravatar imageShaban ( 2013-09-07 23:21:29 -0600 )edit
1

answered 2013-09-02 03:52:33 -0600

updated 2013-09-02 03:54:49 -0600

For that You will need one loop:

vector<Mat> people;
for(int i=0;i<rects.size();i++)
{
     people.push_back((image(rects[i])).clone());
     //or draw green rectangle over image
     //rectangle(image,rects[i],Scalar(0,255,0),1,8,0);
}
edit flag offensive delete link more

Question Tools

Stats

Asked: 2013-09-01 23:24:59 -0600

Seen: 719 times

Last updated: Sep 02 '13