Capturing a blob of white pixels [closed]

asked 2015-07-14 12:21:30 -0600

Katrina_V gravatar image

updated 2015-07-14 12:23:59 -0600

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

#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;
    }
edit retag flag offensive reopen merge delete

Closed for the following reason question is not relevant or outdated by sturkmen
close date 2020-10-21 17:31:22.235690

Comments

lets try something

            vector<vector<Point> > contours_poly(contours.size());
            vector<Rect> boundRect(contours.size());

//try to solve step by step.
//step 1
// by adding the code below you will see red rects. step 2 maybe would gather rects
        for( int i = 0; i< contours.size(); i++ ) // iterate through each contour.
        {

            Rect r = boundingRect(contours[i]);
            rectangle(frame, r, Scalar(0,0,255), 2);

        }
            //Gathering white pixels
            for (size_t i = 0; i < contours.size(); i++) {
                for (size_t j = 0; j < contours[i].size(); j++)
sturkmen gravatar imagesturkmen ( 2015-07-14 17:59:11 -0600 )edit

try the addition code above. if the red rectangles inside the zone you wanted to bound then we will continue

sturkmen gravatar imagesturkmen ( 2015-07-14 18:16:06 -0600 )edit