【问题标题】:Why can't I get a rectangle to be displayed properly in Open CV?为什么我不能在 Opencv 中正确显示一个矩形?
【发布时间】:2013-10-27 16:03:34
【问题描述】:

我正在使用 Open CV 测试已经很经典的人脸检测代码,我在vectorof 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;
        }

    }

【问题讨论】:

  • 我实际上可以正确检测到人脸。

标签: c++ opencv face-recognition


【解决方案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;
}

【讨论】:

  • 我现在仍然可以显示任何矩形,但我认为我应该检查调整矩形的点值,因为检测是在缩小的图像上执行的......
  • 对不起,我做错了,我再次尝试绘制灰度和均衡图像。现在我只需要做反向缩放。谢谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-08-31
  • 1970-01-01
  • 1970-01-01
  • 2012-04-13
  • 1970-01-01
相关资源
最近更新 更多