为什么我不能在Open CV中正确显示矩形?

时间:2013-10-18 17:46:27

标签: c++ opencv face-recognition

我正在使用Open CV测试已经是经典的人脸检测代码,我在vector Rect中获得 faces 。所以我想让获得的Rects显示在图像上。

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) {

    }

}

我永远不会显示任何矩形。我的rectangle()功能出了什么问题?

我进行了建议编辑,这就是现在的检测周期:

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;
        }

    }

1 个答案:

答案 0 :(得分:1)

你试图将rgb颜色绘制到灰度图像上,你还需要在矩形图之后进行imshow(),然后使用waitKey()来更新窗口

尝试:

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;
}