绑定前景对象

时间:2015-07-14 17:32:25

标签: opencv

我想用矩形绑定前景对象,我该怎么做呢?

我尝试通过收集白色像素来限制矩形,但它限制了整个屏幕......

我该如何解决这个问题?

enter image description here

//defined later
vector<Point> points;
RNG rng(12345);

int main(int argc, char* argv[])
{
    //create GUI windows
    namedWindow("Frame");
    namedWindow("FG Mask MOG 2");
    pMOG2 = createBackgroundSubtractorMOG2();

    VideoCapture capture(0);
        //update the background model
        pMOG2->apply(frame, fgMaskMOG2);
        frame_check = fgMaskMOG2.clone();
        erode(frame_check, frame_check, getStructuringElement(MORPH_ELLIPSE, Size(5, 5)));
        dilate(frame_check, frame_check, getStructuringElement(MORPH_ELLIPSE, Size(5, 5)));

        vector<vector<Point> > contours;
        vector<Vec4i> hierarchy;

        /// Detect edges using Threshold
        //threshold(frame_check, frame_check, 100, 255, THRESH_BINARY);
        //find contours
        findContours(frame_check, contours, hierarchy, CV_RETR_TREE, CV_CHAIN_APPROX_SIMPLE, Point(0, 0));

        //points
        for (size_t i = 0; i < contours.size(); i++) {
            for (size_t j = 0; j < contours[i].size(); j++) {
                if (contours[i].size() > 2000)
                {
                    cv::Point p = contours[i][j];
                    points.push_back(p);
                }
            }
        }



            if (points.size() > 0){
                Rect brect = cv::boundingRect(cv::Mat(points).reshape(2));
                rectangle(frame, brect.tl(), brect.br(), CV_RGB(0, 255, 0), 2, CV_AA);
                cout << points.size() << endl;

            }
            imshow("Frame", frame);
            imshow("FG Mask MOG 2", fgMaskMOG2);
            //get the input from the keyboard
            keyboard = waitKey(1000);
        }
    }

2 个答案:

答案 0 :(得分:1)

试试这个,它来自我正在研究的项目。为了隔离对象,我使用彩色蒙版生成二进制图像,但在你的情况下,在前景图像上有一个适当的阈值(过滤后),你可以获得相同的结果。

// Morphological opening
erode(binary, binary, getStructuringElement(MORPH_ELLIPSE, filterSize));
dilate(binary, binary, getStructuringElement(MORPH_ELLIPSE, filterSize));

// Morphological closing
dilate(binary, binary, getStructuringElement(MORPH_ELLIPSE, filterSize));
erode(binary, binary, getStructuringElement(MORPH_ELLIPSE, filterSize));

// Find contours
vector<vector<Point>> contours;
findContours(binary, contours, CV_RETR_EXTERNAL, CV_CHAIN_APPROX_SIMPLE);

vector<Rect> rectangles;
for(auto& contour : contours)
{
    // Produce a closed polygon with object's contours
    vector<Point> polygon;
    approxPolyDP(contour, polygon, 3, true);

    // Get polygon's bounding rectangles
    Rect rect = boundingRect(polygon);
    rectangles.push_back(rect);
}

答案 1 :(得分:1)

这是一个关于如何使用轮廓点在前景对象周围绘制边界框的小例子。这是OpenCV 2.9,所以如果你使用的是OpenCV 3.0,你可能需要更改BackgroundSubtractorMOG2的初始化。

#include <opencv2\opencv.hpp>
using namespace cv;

int main(int argc, char *argv[])
{
   BackgroundSubtractorMOG2 bg = BackgroundSubtractorMOG2(30, 16.0, false);
   VideoCapture cap(0);
   Mat3b frame;
   Mat1b fmask;

   for (;;)
   {
       cap >> frame;
       bg(frame, fmask, -1);

       vector<vector<Point>> contours;
       findContours(fmask.clone(), contours, CV_RETR_TREE, CV_CHAIN_APPROX_SIMPLE);

       for(int i=0; i<contours.size(); ++i)
       {
           if(contours[i].size() > 200)
           {
               Rect roi = boundingRect(contours[i]);
               drawContours(frame, contours, i, Scalar(0,0,255));
               rectangle(frame, roi, Scalar(0,255,0));  
           }
       }

       imshow("frame", frame);
       imshow("mask", fmask);
       if (cv::waitKey(30) >= 0) break;
   }
   return 0;
}