Ask Your Question
-1

my code doesn't work.plzz help me

asked 2016-06-05 04:11:09 -0500

supun gravatar image

Error 1 error C2259: 'cv::BackgroundSubtractorMOG2' : cannot instantiate abstract class c:\Users Error 4 error C2664: 'cv::SimpleBlobDetector::SimpleBlobDetector(const cv::SimpleBlobDetector &)' : cannot convert argument 1 from 'cv::SimpleBlobDetector::Params' to 'const cv::SimpleBlobDetector &' Error 5 error C2664: 'cv::Ptr<cv::simpleblobdetector> cv::SimpleBlobDetector::create(const cv::SimpleBlobDetector::Params &)' : cannot convert argument 1 from 'const char [11]' to 'const cv::SimpleBlobDetector::Params &'
Error 6 error C2039: '()' : is not a member of 'cv::BackgroundSubtractorMOG2'

include<opencv2 opencv.hpp="">

include<iostream>

include<vector>

int main(int argc, char *argv[]) { if (argc < 3) { std::cerr << "Usage: " << argv[0] << " in.file out.file" << std::endl; return -1; }

cv::Mat frame;
cv::Mat back;
cv::Mat fore;
std::cerr << "opening " << argv[1] << std::endl;
cv::VideoCapture cap(argv[1]);
cv::BackgroundSubtractorMOG2 bg;
//bg.nmixtures = 3;
//bg.bShadowDetection = false;

cv::VideoWriter output;
//int ex = static_cast<int>(cap.get(CV_CAP_PROP_FOURCC));
int ex = CV_FOURCC('P', 'I', 'M', '1');
cv::Size size = cv::Size((int)cap.get(CV_CAP_PROP_FRAME_WIDTH),
    (int)cap.get(CV_CAP_PROP_FRAME_HEIGHT));
std::cerr << "saving to " << argv[2] << std::endl;
output.open(argv[2], ex, cap.get(CV_CAP_PROP_FPS), size, true);

std::vector<std::vector<cv::Point> > contours;

cv::namedWindow("Frame");
cv::namedWindow("Fore");
cv::namedWindow("Background");


cv::SimpleBlobDetector::Params params;
params.minThreshold = 40;
params.maxThreshold = 60;
params.thresholdStep = 5;
params.minArea = 100;
params.minConvexity = 0.3;
params.minInertiaRatio = 0.01;
params.maxArea = 8000;
params.maxConvexity = 10;
params.filterByColor = false;
params.filterByCircularity = false;


cv::SimpleBlobDetector blobDtor(params);
blobDtor.create("SimpleBlob");

std::vector<std::vector<cv::Point> >    blobContours;
std::vector<cv::KeyPoint>               keyPoints;
cv::Mat                                 out;

cv::HOGDescriptor hog;
hog.setSVMDetector(cv::HOGDescriptor::getDefaultPeopleDetector());


for (;;)
{
    cap >> frame;

    bg.operator ()(frame, fore);

    bg.getBackgroundImage(back);
    cv::erode(fore, fore, cv::Mat());
    cv::dilate(fore, fore, cv::Mat());

    blobDtor.detect(fore, keyPoints, cv::Mat());

    //cv::imshow("Fore", fore);

    cv::findContours(fore, contours, CV_RETR_EXTERNAL, CV_CHAIN_APPROX_NONE);
    cv::drawContours(frame, contours, -1, cv::Scalar(0, 0, 255), 2);

    std::vector<std::vector<cv::Point> >::const_iterator it = contours.begin();
    std::vector<std::vector<cv::Point> >::const_iterator end = contours.end();
    while (it != end) {
        cv::Rect bounds = cv::boundingRect(*it);
        cv::rectangle(frame, bounds, cv::Scalar(0, 255, 0), 2);

        ++it;
    }

    cv::drawKeypoints(fore, keyPoints, out, CV_RGB(0, 255, 0), cv::DrawMatchesFlags::DEFAULT);
    cv::imshow("Fore", out);


    std::vector<cv::Rect> found, found_filtered;
    hog.detectMultiScale(frame, found, 0, cv::Size(8, 8), cv::Size(32, 32), 1.05, 2);
    for (int i = 0; i < found.size(); ++i) {
        cv::Rect r = found[i];
        int j = 0;
        for (; j < found.size(); ++j) {
            if (j != i && (r & found[j]) == r) {
                break;
            }
        }
        if (j == found.size()) {
            found_filtered.push_back(r);
        }
    }

    for (int i = 0; i < found_filtered.size(); ++i) {
        cv::Rect r = found_filtered[i];
        cv::rectangle(frame, r.tl(), r.br(), cv::Scalar(255, 0, 0), 3);
    }


    output << frame;

    cv::resize(frame, frame, cv::Size(1280, 720));
    cv::imshow("Frame", frame);

    cv::resize(back, back, cv::Size(1280, 720));
    cv::imshow("Background", back);



    if (cv::waitKey(30) >= 0) break;
}
return 0;

}

edit retag flag offensive close merge delete

Comments

I'm not 100% sure, but for OpenCV >3.0.0 I think that you have to use cv::SimpleBlobDetector and cv::BackgroundSubtractorMOG2 with cv::Ptr<>, because they are derived from cv::Algorithm class. SimpleBlobDetector documentation

Try something like this

Ptr<SimpleBlobDetector> blobDtor = SimpleBlobDetector::create(params);

and

Ptr<BackgroundSubtractorMOG2> bg = BackgroundSubtractorMOG2::create();
matman gravatar imagematman ( 2016-06-05 06:18:59 -0500 )edit

Please edit your question. The title has absolutely no meaning and you havn't even invested one second to describe your problem.

FooBar gravatar imageFooBar ( 2016-06-05 06:36:57 -0500 )edit

1 answer

Sort by ยป oldest newest most voted
0

answered 2016-06-05 07:00:14 -0500

there is a similar code i have posted before you can also take a look at http://answers.opencv.org/question/80580

here is your code adopted to run with OpenCV 3.1

#include <opencv2/opencv.hpp>
#include <iostream>
#include <vector>

int main(int argc, char *argv[])
{
    if (argc < 3)
    {
        std::cerr << "Usage: " << argv[0] << " in.file out.file" << std::endl;
        return -1;
    }

    cv::Mat frame;
    cv::Mat back;
    cv::Mat fore;
    std::cerr << "opening " << argv[1] << std::endl;
    cv::VideoCapture cap(argv[1]);
    cv::Ptr<cv::BackgroundSubtractor> bg = cv::createBackgroundSubtractorMOG2();
//bg.nmixtures = 3;
//bg.bShadowDetection = false;

    cv::VideoWriter output;
//int ex = static_cast<int>(cap.get(CV_CAP_PROP_FOURCC));
    int ex = CV_FOURCC('P', 'I', 'M', '1');
    cv::Size size = cv::Size((int)cap.get(CV_CAP_PROP_FRAME_WIDTH),
                             (int)cap.get(CV_CAP_PROP_FRAME_HEIGHT));
    std::cerr << "saving to " << argv[2] << std::endl;
    output.open(argv[2], ex, cap.get(CV_CAP_PROP_FPS), size, true);

    std::vector<std::vector<cv::Point> > contours;

    cv::namedWindow("Frame");
    cv::namedWindow("Fore");
    cv::namedWindow("Background");


    cv::SimpleBlobDetector::Params params;
    params.minThreshold = 40;
    params.maxThreshold = 60;
    params.thresholdStep = 5;
    params.minArea = 100;
    params.minConvexity = 0.3;
    params.minInertiaRatio = 0.01;
    params.maxArea = 8000;
    params.maxConvexity = 10;
    params.filterByColor = false;
    params.filterByCircularity = false;


    cv::Ptr<cv::SimpleBlobDetector> blobDtor = cv::SimpleBlobDetector::create(params);

    std::vector<std::vector<cv::Point> >    blobContours;
    std::vector<cv::KeyPoint>               keyPoints;
    cv::Mat                                 out;

    cv::HOGDescriptor hog;
    hog.setSVMDetector(cv::HOGDescriptor::getDefaultPeopleDetector());


    for (;;)
    {
        cap >> frame;

        bg->apply(frame, fore);

        bg->getBackgroundImage(back);
        cv::erode(fore, fore, cv::Mat());
        cv::dilate(fore, fore, cv::Mat());

        blobDtor->detect(fore, keyPoints, cv::Mat());

        //cv::imshow("Fore", fore);

        cv::findContours(fore, contours, CV_RETR_EXTERNAL, CV_CHAIN_APPROX_NONE);
        cv::drawContours(frame, contours, -1, cv::Scalar(0, 0, 255), 2);

        std::vector<std::vector<cv::Point> >::const_iterator it = contours.begin();
        std::vector<std::vector<cv::Point> >::const_iterator end = contours.end();
        while (it != end)
        {
            cv::Rect bounds = cv::boundingRect(*it);
            cv::rectangle(frame, bounds, cv::Scalar(0, 255, 0), 2);

            ++it;
        }

        cv::drawKeypoints(fore, keyPoints, out, CV_RGB(0, 255, 0), cv::DrawMatchesFlags::DEFAULT);
        cv::imshow("Fore", out);


        std::vector<cv::Rect> found, found_filtered;
        hog.detectMultiScale(frame, found, 0, cv::Size(8, 8), cv::Size(32, 32), 1.05, 2);
        for (int i = 0; i < found.size(); ++i)
        {
            cv::Rect r = found[i];
            int j = 0;
            for (; j < found.size(); ++j)
            {
                if (j != i && (r & found[j]) == r)
                {
                    break;
                }
            }
            if (j == found.size())
            {
                found_filtered.push_back(r);
            }
        }

        for (int i = 0; i < found_filtered.size(); ++i)
        {
            cv::Rect r = found_filtered[i];
            cv::rectangle(frame, r.tl(), r.br(), cv::Scalar(255, 0, 0), 3);
        }


        output << frame;

        cv::resize(frame, frame, cv::Size(1280, 720));
        cv::imshow("Frame", frame);

        cv::resize(back, back, cv::Size(1280, 720));
        cv::imshow("Background", back);



        if (cv::waitKey(30) >= 0) break;
    }
    return 0;
}
edit flag offensive delete link more

Question Tools

1 follower

Stats

Asked: 2016-06-05 04:11:09 -0500

Seen: 602 times

Last updated: Jun 05 '16