Ask Your Question

Error when Cropping Human Image after detectMultiScale

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

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;

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

 return 0;

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

edit retag flag offensive close merge delete


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

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

2 answers

Sort by ยป oldest newest most voted

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

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



OMG! It works! Thanks! :D

Shaban gravatar imageShaban ( 2013-09-02 01:15:52 -0500 )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 -0500 )edit

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 -0500 )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 -0500 )edit

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

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

For that You will need one loop:

vector<Mat> people;
for(int i=0;i<rects.size();i++)
     //or draw green rectangle over image
edit flag offensive delete link more

Question Tools


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

Seen: 588 times

Last updated: Sep 02 '13