0

我正在使用 Open CV 测试已经很经典的人脸检测代码我在vector. Rect所以我想让获得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;
        }

    }
4

1 回答 1

1

您正在尝试将 rgb 颜色绘制到灰度 img 上,还需要在绘制矩形之后执行 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;
}
于 2013-10-18T18:22:46.470 回答