Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Capturing a blob of white pixels

Hi, 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...

image description

Capturing a blob of white pixels

Hi, 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, pixels , but it bounds the whole screen...

image description

#include <opencv2/opencv.hpp>
#include "opencv2/imgcodecs.hpp"
#include "opencv2/imgproc.hpp"
#include "opencv2/videoio.hpp"
#include <opencv2/highgui.hpp>
#include <opencv2/video.hpp>
#include <stdio.h>
//C++
#include <iostream>
#include <sstream>
using namespace cv;
using namespace std;


//defined later
vector<Point> points;



RNG rng(12345);

// Global variables
Mat frame; //current frame
Mat fgMaskMOG2; //fg mask fg mask generated by MOG2 method
Mat frame_check;
Ptr<BackgroundSubtractor> pMOG2; //MOG2 Background subtractor
int keyboard; //input from keyboard
void help();
//void processVideo(char* videoFilename);
void processVideo();
void processImages(char* firstFrameFilename);
void help()
{
    cout
        << "--------------------------------------------------------------------------" << endl
        << "This program shows how to use background subtraction methods provided by " << endl
        << " OpenCV. You can process both videos (-vid) and images (-img)." << endl
        << endl
        << "Usage:" << endl
        << "./bs {-vid <video filename>|-img <image filename>}" << endl
        << "for example: ./bs -vid video.avi" << endl
        << "or: ./bs -img /data/images/1.png" << endl
        << "--------------------------------------------------------------------------" << endl
        << endl;
}
int main(int argc, char* argv[])
{
    //print help information
    help();

    //create GUI windows
    namedWindow("Frame");
    namedWindow("FG Mask MOG 2");
    //create Background Subtractor objects
    pMOG2 = createBackgroundSubtractorMOG2();

    VideoCapture capture(0);

    if (!capture.isOpened()){
        //error in opening the video input
        cerr << "Unable to open video file: " << endl;
        system("pause");
        exit(EXIT_FAILURE);
    }
    //read input data. ESC or 'q' for quitting
    while ((char)keyboard != 'q'){
        //read the current frame
        if (!capture.read(frame)) {
            cerr << "Unable to read next frame." << endl;
            cerr << "Exiting..." << endl;
            system("pause");
            exit(EXIT_FAILURE);
        }
        //update the background model
        pMOG2->apply(frame, fgMaskMOG2);
        frame_check = fgMaskMOG2.clone();

        //process here

        //morphological opening (remove small objects from the foreground)
        erode(frame_check, frame_check, getStructuringElement(MORPH_ELLIPSE, Size(5, 5)));
        dilate(frame_check, frame_check, getStructuringElement(MORPH_ELLIPSE, Size(5, 5)));

        ////morphological closing (fill small holes in the foreground)
        dilate(frame_check, frame_check, getStructuringElement(MORPH_ELLIPSE, Size(5, 5)));
        erode(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));

        /// Approximate contours to polygons + get bounding rects and circles
        vector<vector<Point> > contours_poly(contours.size());
        vector<Rect> boundRect(contours.size());


        //Gathering white pixels
        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(30);
            keyboard = waitKey(1000);
        }
        //delete capture object
        capture.release();
        system("pause");



        //destroy GUI windows
        destroyAllWindows();
        return EXIT_SUCCESS;
    }