Ask Your Question

meg's profile - activity

2020-01-15 11:35:02 -0600 received badge  Notable Question (source)
2020-01-15 11:35:02 -0600 received badge  Popular Question (source)
2017-04-01 13:17:51 -0600 asked a question How to set 'nmixures' and 'bShadowDetection' values in 'BackgroundSubtractorMOG2'?

I am using OpenCV 3.1 but the problem is that i am unable to make object of BackgroundSubtractorMOG2 class. I have written a function to detect face, eyes, nose and hand(with fingers and count the fingers). The problem is that visual studio gives an error saying that BackgroundSubtractorMOG2 is abstract class. Can someone please help me with that, I am also getting an error int the following lines:

 bg.operator ()(frame, fore); 
 bg.operator()(frame, fore, 0);

Following is my code. How can i resolve this issue. Thank in advance.

#include <opencv/cv.h>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/objdetect.hpp>
#include <opencv2/video/background_segm.hpp>

#include <iostream>
#include<algorithm>
#include <vector>

using namespace std;
using namespace cv;

String face_cascade_name = "haarcascade_frontalface_alt.xml";
String eyes_cascade_name = "haarcascade_eye_tree_eyeglasses.xml";
String nose_cascade_name = "haarcascade_mcs_nose.xml";
CascadeClassifier face_cascade;
CascadeClassifier eyes_cascade;
CascadeClassifier nose_cascade;
double dist(Point x, Point y)
{
    return (x.x - y.x)*(x.x - y.x) + (x.y - y.y)*(x.y - y.y);
}
pair<Point, double> circleFromPoints(Point p1, Point p2, Point p3)
{
    double offset = pow(p2.x, 2) + pow(p2.y, 2);
    double bc = (pow(p1.x, 2) + pow(p1.y, 2) - offset) / 2.0;
    double cd = (offset - pow(p3.x, 2) - pow(p3.y, 2)) / 2.0;
    double det = (p1.x - p2.x) * (p2.y - p3.y) - (p2.x - p3.x)* (p1.y - p2.y);
    double TOL = 0.0000001;
    if (abs(det) < TOL) { cout << "POINTS TOO CLOSE" << endl; return make_pair(Point(0, 0), 0); }

    double idet = 1 / det;
    double centerx = (bc * (p2.y - p3.y) - cd * (p1.y - p2.y)) * idet;
    double centery = (cd * (p1.x - p2.x) - bc * (p2.x - p3.x)) * idet;
    double radius = sqrt(pow(p2.x - centerx, 2) + pow(p2.y - centery, 2));

    return make_pair(Point(centerx, centery), radius);
}
void detectAndDisplay(Mat image)
{
    std::vector<Rect> faces;
    Mat image_gray;

    cvtColor(image, image_gray, CV_BGR2GRAY);
    equalizeHist(image_gray, image_gray);

    face_cascade.detectMultiScale(image_gray, faces, 1.1, 2, 0 | CV_HAAR_SCALE_IMAGE, Size(30, 30));

    cout << faces.size() << std::endl;

    for (size_t i = 0; i < faces.size(); i++) {
        rectangle(image, Point(faces[i].x, faces[i].y), Point(faces[i].x + faces[i].width, faces[i].y + faces[i].height), Scalar(255, 0, 0), 3, 8);

        Mat faceROI = image_gray(faces[i]);
        vector<Rect> eyes;
        vector<Rect> noses;
        eyes_cascade.detectMultiScale(faceROI, eyes, 1.1, 2, 0 | CV_HAAR_SCALE_IMAGE, Size(30, 30));
        nose_cascade.detectMultiScale(faceROI, noses, 1.1, 2, 0 | CV_HAAR_SCALE_IMAGE, Size(30, 30));
        cout << noses.size() << std::endl;
        for (size_t j = 0; j < eyes.size(); j++) {
            Point center(faces[i].x + eyes[j].x + eyes[j].width*0.5, faces[i].y + eyes[j].y + eyes[j].height*0.5);
            int radius = cvRound((eyes[j].width + eyes[j].height)*0.25);
            circle(image, center, radius, Scalar(0, 255, 0), 4, 8, 0);
        }
        for (size_t j = 0; j < noses.size(); j++) {
            Point center(faces[i].x + noses[j].x + noses[j].width*0.5, faces[i].y + noses[j].y + noses[j].height*0.5);
            int radius = cvRound((noses ...
(more)
2017-03-29 10:53:10 -0600 commented answer Assertion failed (!empty()) in cv::CascadeClassifier::detectMultiScale thrown at runtime

Thanks a lot that saved my life

2017-03-29 10:52:49 -0600 received badge  Scholar (source)
2017-03-29 10:19:09 -0600 asked a question Assertion failed (!empty()) in cv::CascadeClassifier::detectMultiScale thrown at runtime

I am trying to do real-time face, nose and eyes detection the following is my code. The problem is that nose_cascade.detectMultiScale(faceROI, noses, 1.1, 2, 0 | CV_HAAR_SCALE_IMAGE, Size(30, 30)); statement generates run-time exception and I don't know the reason.

The exception is: Assertion failed (!empty()) in cv::CascadeClassifier::detectMultiScale

What is the problem can someone please tell me. I have the xml files in the right directories and the rest of the code is working fine except that particular line.

#include "opencv2/objdetect/objdetect.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"

#include <iostream>

using namespace std;
using namespace cv;

/** Function Headers */
void detectAndDisplay(Mat frame);

/** Global variables */
String face_cascade_name = "haarcascade_frontalface_alt.xml";
String eyes_cascade_name = "haarcascade_eye_tree_eyeglasses.xml";
String nose_cascade_name = "haarcascade_mcs_nose.xml";
CascadeClassifier face_cascade;
CascadeClassifier eyes_cascade;
CascadeClassifier nose_cascade;
string window_name = "Capture - Face detection";
RNG rng(12345);

/** @function main */
int main(int argc, const char** argv)
{
    VideoCapture vcap(0);
    if (!vcap.isOpened()) {
        cout << "Error opening video stream" << endl;
        return -1;
    }
    Mat frame;

    //-- 1. Load the cascades
    if (!face_cascade.load(face_cascade_name)) { printf("--(!)Error loading\n"); return -1; };
    if (!eyes_cascade.load(eyes_cascade_name)) { printf("--(!)Error loading\n"); return -1; };

    //-- 2. Read the video stream

        while (true)
        {
            vcap >> frame;
            //-- 3. Apply the classifier to the frame
            if (!frame.empty())
            {
                detectAndDisplay(frame);
            }
            else
            {
                printf(" --(!) No captured frame -- Break!"); break;
            }

            int c = waitKey(10);
            if ((char)c == 'c') { break; }
        }
    return 0;
}

/** @function detectAndDisplay */
void detectAndDisplay(Mat frame)
{
    std::vector<Rect> faces;
    Mat frame_gray;

    cvtColor(frame, frame_gray, CV_BGR2GRAY);
    equalizeHist(frame_gray, frame_gray);

    //-- Detect faces
    face_cascade.detectMultiScale(frame_gray, faces, 1.1, 2, 0 | CV_HAAR_SCALE_IMAGE, Size(30, 30));
    for (size_t i = 0; i < faces.size(); i++)
    {
        Point center(faces[i].x + faces[i].width*0.5, faces[i].y + faces[i].height*0.5);
        rectangle(frame, Point(faces[i].x, faces[i].y), Point(faces[i].x + faces[i].width, faces[i].y + faces[i].height), Scalar(255, 0, 0), 3, 8);
        Mat faceROI = frame_gray(faces[i]);
        vector<Rect> eyes;
        vector<Rect> noses;
        //-- In each face, detect eyes
        eyes_cascade.detectMultiScale(faceROI, eyes, 1.1, 2, 0 | CV_HAAR_SCALE_IMAGE, Size(30, 30));
        for (size_t j = 0; j < eyes.size(); j++)
        {
            Point center(faces[i].x + eyes[j].x + eyes[j].width*0.5, faces[i].y + eyes[j].y + eyes[j].height*0.5);
            int radius = cvRound((eyes[j].width + eyes[j].height)*0.25);
            circle(frame, center, radius, Scalar(255, 0, 0), 4, 8, 0);
        }
        nose_cascade.detectMultiScale(faceROI, noses, 1.1, 2, 0 | CV_HAAR_SCALE_IMAGE, Size(30, 30));
        for (size_t j = 0; j < noses.size(); j++) {
            Point center(faces[i].x + noses[j].x + noses[j].width*0.5, faces[i].y + noses[j].y + noses[j].height*0.5);
            int radius = cvRound((noses[j].width + noses[j].height)*0.25);
            circle(frame, center, radius, Scalar(0, 0, 255), 4, 8, 0);
        }
    }
    //-- Show what you got
    imshow(window_name, frame);
}