界定前景对象 [英] Bounding a foreground object

查看:36
本文介绍了界定前景对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用矩形框住前景对象,我该怎么做?

I want to bound the foreground object by a rectangle, how shall I go about doing it?

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

I tried bounding the rectangle by collecting the white pixels , but it bounds the whole screen...

我该如何解决这个问题?

How shall I solve this problem?

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

推荐答案

这是一个关于如何使用轮廓点围绕前景对象绘制边界框的小示例.它与 OpenCV 2.9 一起使用,因此如果您使用的是 OpenCV 3.0,您可能需要更改 BackgroundSubtractorMOG2 的初始化.

This is a small example on how to draw a bounding box around the foreground objects using the points of the contour. It's with OpenCV 2.9, so you probably need to change the initialization of the BackgroundSubtractorMOG2 if you're using OpenCV 3.0.

#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;
}

这篇关于界定前景对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆