Ask Your Question
0

How to resize boundingRect to fix size?

asked 2013-12-04 18:35:24 -0600

Shaban gravatar image

updated 2013-12-04 20:20:41 -0600

Hi guys, I wanna resize boundingRect to fix size and have the object inside the center of rect but I don't get some help after googling it. And try some several ways but didn't work, like:

cv::Rect r0 = cv::boundingRect(contours[cnum]);
r0.x = r0.x - 5;
r0.y = r0.y - 5;
r0.height = 60
r0.width = 100;
cv::rectangle(blob, r0, cv::Scalar(255, 0, 0));
human.detectMultiScale(frame(r0),rects);

Here we go my full code, look at the last line:

    cv::namedWindow("Frame");   
    cv::namedWindow("Background Model");
    cv::namedWindow("Blob");

    cv::VideoCapture cap("skenario c/3.avi");   

    cv::BackgroundSubtractorMOG2 bgs;                       
        bgs.nmixtures = 3;
        bgs.history = 1000;
        bgs.bShadowDetection = true;                            
        bgs.nShadowDetection = 0;                               
        bgs.fTau = 0.25;    

    std::vector<std::vector<cv::Point>> contours;               

    cv::CascadeClassifier human;
    assert(human.load("hogcascade_pedestrians.xml"));
    for(;;){
        cap >> frame;   

        cv::GaussianBlur(frame,blurred,cv::Size(3,3),0,0,cv::BORDER_DEFAULT);

        bgs.operator()(blurred,fg,0);                           
        bgs.getBackgroundImage(bgmodel);    

        cv::erode(fg,fg,cv::Mat(),cv::Point(-1,-1),1);                         
        cv::dilate(fg,fg,cv::Mat(),cv::Point(-1,-1),3); 

        cv::threshold(fg,threshfg,70.0f,255,CV_THRESH_BINARY);

        cv::findContours(threshfg,contours,CV_RETR_EXTERNAL,CV_CHAIN_APPROX_SIMPLE);
        cv::cvtColor(threshfg,blob,CV_GRAY2RGB);
        cv::drawContours(blob,contours,-1,cv::Scalar(255,255,255),CV_FILLED,8);
        blob.copyTo(blobarray[(int)cap.get(CV_CAP_PROP_POS_FRAMES)]);

        int cmin = 20; 
        int cmax = 1000;
        bool FOD1 = true;
        bool FOD2 = true;
        std::vector<cv::Rect> rects;

        for(int cnum = 0; cnum < contours.size(); cnum++){

            if(contours[cnum].size() > cmin && contours[cnum].size() < cmax){       


        //I WANNA RESIZE HERE:
        human.detectMultiScale(frame(cv::boundingRect(contours[cnum])),rects);

I'll appreciate any help here, thanks. :)

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
0

answered 2014-07-20 17:13:57 -0600

You need check the boundaries against the image:

        cv::Rect superbounds;
        superbounds.x = (int) (bounds.x - dst.cols / 6);
        superbounds.y = (int) (bounds.y - dst.rows / 6);
        superbounds.width = (int) (bounds.width + dst.cols / 3);
        superbounds.height = (int) (bounds.height + dst.rows / 3);

        superbounds.x = (superbounds.x > 0) ? superbounds.x : 0;
        superbounds.y = (superbounds.y > 0) ? superbounds.y : 0;
        superbounds.height = ((superbounds.height + superbounds.y) > dst.rows) ? (dst.rows - superbounds.y) : superbounds.height;
        superbounds.width = ((superbounds.width + superbounds.x) > dst.cols) ? (dst.cols - superbounds.x) : superbounds.width;
edit flag offensive delete link more

Question Tools

Stats

Asked: 2013-12-04 18:35:24 -0600

Seen: 5,610 times

Last updated: Jul 20 '14