Ask Your Question

Katrina_V's profile - activity

2019-02-25 06:10:07 -0600 received badge  Popular Question (source)
2018-06-04 11:34:22 -0600 received badge  Popular Question (source)
2015-07-16 06:25:19 -0600 received badge  Enthusiast
2015-07-14 12:21:30 -0600 asked a question 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

#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;
    }
2015-07-14 12:17:35 -0600 commented question Findcontours() no working correctly in background subtraction

Sorry!..you are right, I got it . Thank you!!! But I have got another problem... How to bound the object by a rectangle ? I tried capturing all the points but it bounds the whole screen :P!!!

2015-07-14 11:13:11 -0600 commented question Findcontours() no working correctly in background subtraction

Thanks for the reply. What do you mean ""eats up the image". There is nothin on the link. Also what should I clone?

2015-07-14 10:12:12 -0600 asked a question Findcontours() no working correctly in background subtraction

Hi,

I am trying to obtain the foreground objects from the background using background subtraction model. There will only be a person walking in foreground whom I have to track and draw a bounding box around him. I have written following code for the same.

The Background subtraction works properly when findcontours() function is not added. However on using the function findcontours() function, the background model obtained does not give proper output. Please help me.

without findcontours()

image description

with findcontours()

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;

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 processVideo(char* videoFilename);
void processVideo();
void processImages(char* firstFrameFilename);

int main(int argc, char* argv[])
{

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

        ////approximate contours by rectangles
        //for (int i = 0; i < contours.size(); i++)
        //{
        //  approxPolyDP(Mat(contours[i]), contours_poly[i], 3, true);
        //  boundRect[i] = boundingRect(Mat(contours_poly[i]));
        //}

        ////draw bounded recatangles
        //Mat drawing = Mat::zeros(frame_check.size(), CV_8UC3);
        //Scalar color = Scalar(rng.uniform(0, 255), rng.uniform(0, 255), rng.uniform(0, 255));

        //for (int i = 0; i< contours.size(); i++)
        //{
        //  //drawContours(drawing, contours_poly, i, color, 1, 8, vector<Vec4i>(), 0, Point());
        //  rectangle(drawing, boundRect[i].tl(), boundRect[i].br(), color, 2, 8, 0);
        //  //circle(drawing, center[i], (int)radius[i], color, 2, 8, 0);
        //}

        imshow("Frame", frame);
        imshow("FG Mask MOG 2", frame_check);
        //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;
}
void processVideo() {


}
2015-07-14 09:41:16 -0600 asked a question How to collect all the white pixels from an image

Hi, I have a thresholded image which has black pixels and white pixels. Is there a function/method to obtain all the white pixels in a container such as vector<vector<point> > contours?

2015-07-09 08:03:16 -0600 commented answer waitkey() does not return the value of the character pressed

Thanks a lot! :) !! It works when I create a window using namedWindow() beforehand.

2015-07-09 07:58:06 -0600 received badge  Scholar (source)
2015-07-09 06:52:40 -0600 asked a question waitkey() does not return the value of the character pressed

The code below does not pause indefinitely. Instead prints the '-1' indefinitely. Please help.

#include "opencv2/objdetect/objdetect.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include <opencv2/opencv.hpp>
#include <iostream>
#include <stdio.h>
using namespace std;
using namespace cv;

int main(){

    int ch;

    while (1)
    { 
        ch = waitKey(0);
        cout << ch << endl;
    }
    return 0;
}
2015-06-03 04:42:54 -0600 received badge  Editor (source)
2015-06-03 04:41:17 -0600 commented question Algorithm to measure sleep hours of a patient

Thanks Eduardo, links seem to be quite useful, I will take a look at it :)

2015-06-03 04:39:28 -0600 asked a question How to use single video camera for different .exe files

Hi, I have two binary files containing opencv programs both taking frames from single video camera. But on running both simultaneously gives only one program running whereas other program shows blank window. How to solve this problem. Please help.

2015-05-30 10:52:19 -0600 received badge  Student (source)
2015-05-30 02:16:56 -0600 asked a question Algorithm to measure sleep hours of a patient

Hi, I am a doctor and recently learnt basics of opencv. I wanted to use opencv ( by analysing the video feed from a camera attached in the room) to measure the sleeping hours of a patient in my hospital - time for which the patient is awake, roaming around and time for which he sleeping on bed. There is a nurse as well moving around in the room sometimes. Can you please tell me the way in which I can solve the problem. I tried using the Zdenek Kalal's predator algorithm but its computationally heavy and I have some other analytics also running sideways. Please help me.