Question

I'm testing the already classical face detection code with Open CV, I'm getting the faces in a vectorof Rect. So I want to get the obtained Rects to be displayed on the image.

while (true) {

    camera >> cameraFrame;
    if (cameraFrame.empty ()) {

        cerr << "Error: Could grab any frame!" << endl;
        exit(2);
    }

    imshow("Hola mundo", cameraFrame);
    cameraFrame = shrinkImage(turn2Gray(cameraFrame));
    imshow("Hola mundo gris", cameraFrame);
    equalizeHist(cameraFrame, equalizedImage);
    imshow("Hola mundo surreal y perturbador", equalizedImage);

    int flags = CASCADE_SCALE_IMAGE;
    Size minFeatureSize(20,20);
    float searchScaleFactor = 1.1f;
    int minNeighbors = 4;

    std::vector<Rect> faces;

    faceDetector.detectMultiScale(equalizedImage, faces, searchScaleFactor, minNeighbors, flags, minFeatureSize);
    cout << "Caras: " << faces.size() << endl;

    for (i=0; i< (int) faces.size(); i++) {

        rectangle( equalizedImage, faces[i], CV_RGB(0,255,0), 2, 8, 0 );

    }

    if (waitKey(20) == 27) {

    }

}

I never get any rectangle to be displayed. What's wrong with my rectangle() function?

I did the advised edits, this is how the detection cycle looks like now:

while (true) {

        camera >> cameraFrame;
        if (cameraFrame.empty ()) {

            cerr << "Error: Could grab any frame!" << endl;
            exit(2);
        }

        imshow("Hola mundo", cameraFrame);
        greyFrame = shrinkImage(turn2Gray(cameraFrame));
        imshow("Hola mundo gris", greyFrame);
        equalizeHist(greyFrame, equalizedImage);
        imshow("Hola mundo surreal y perturbador", equalizedImage);



        faceDetector.detectMultiScale(equalizedImage, faces, searchScaleFactor, minNeighbors, flags, minFeatureSize);
        cout << "Caras: " << faces.size() << endl;

        for (i=0; i < faces.size(); i++) {

            rectangle( cameraFrame, faces[i], CV_RGB(0,255,0), 2, 8, 0 );

        }
        imshow("Hola Diego", cameraFrame);

        if (waitKey(20) == 27) {
            break;
        }

    }
Was it helpful?

Solution

you're trying to draw rgb color onto a grayscale img, also you need to do imshow() after the rectangle drawing, and then waitKey(), to update the window

try:

Mat greyFrame = shrinkImage(turn2Gray(cameraFrame)); // make a new grey mat, keep the original for drawing later
imshow("Hola mundo gris", greyFrame);
equalizeHist(greyFrame, equalizedImage);
imshow("Hola mundo surreal y perturbador", equalizedImage);

// ...

for (i=0; i< (int) faces.size(); i++) {
    rectangle( cameraFrame, faces[i], CV_RGB(0,255,0), 2, 8, 0 );
}

imshow("Hola diego", cameraFrame);
if (waitKey(20) == 27) {
    break;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top