Ask Your Question

Revision history [back]

Operations under detectMultiScales, need help

For some reason, while running my code, it seems that all operations below detectMultiScales stopped working. When I remove the detectMultiScales line in my code, the program works very well. BTW, I am using Visual Studio 2013 Community and here is my code.

Operations under detectMultiScales, need help

For some reason, while running my code, it seems that all operations below detectMultiScales stopped working. When I remove the detectMultiScales line in my code, the program works very well. BTW, I am using Visual Studio 2013 Community and here here is my code.code

#include "stdafx.h"
#include <string>
#include <iostream>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/objdetect/objdetect.hpp>

using namespace std;
using namespace cv;

Mat cam;
VideoCapture camera(0);
CascadeClassifier car_cascade;

void detectAndDisplay(Mat frame) {
    std::vector<Rect> cars;
    Mat frame_gray;
    cvtColor(frame, frame_gray, CV_BGR2GRAY);
    equalizeHist(frame_gray, frame_gray);
    car_cascade.detectMultiScale(frame_gray, cars, 1.1, 2, 0 | CV_HAAR_SCALE_IMAGE, Size(30, 30));
    for (int i = 0; i < cars.size(); i++) {
        Point center(cars[i].x + cars[i].width*0.5, cars[i].y + cars[i].height*0.5);
        ellipse(frame, center, Size(cars[i].height*0.5, cars[i].width*0.5), 0, 0, 360, Scalar(0, 255, 0));
    }
    imshow("Frame", frame);
}

int main()
{
    if (!car_cascade.load("C:\\Users\\---\\Documents\\cascade.xml")) {
        return -1;
    }

    if (!camera.isOpened()) {
        printf("Error: cannot open a camera\n");
    }

    while (true)
    {
        if (camera.isOpened()) {
            camera >> cam;
        }

        if (!cam.empty()) {
            detectAndDisplay(cam);
        }

        if (cv::waitKey(1) == 27) {
            break;
        }

    }

    return 0;

}