【问题标题】:Displaying an outline on top of an image在图像顶部显示轮廓
【发布时间】:2014-03-07 10:11:10
【问题描述】:

我正在尝试绘制面部轮廓并将其覆盖在网络摄像头图像之上。

但到最后,我认为我以错误的方式使用addWeighted,因为我的程序崩溃了。

能否请您帮助我了解我在使用 imshowaddWeighted 时做错了什么?

int main( int argc, const char** argv )
{
  VideoCapture camera;
  camera.open(0);

  if( !camera.isOpened() )
  {
    cerr << "Could not access the camera!" << endl;
    return  1;
  }

  while( true )
  {
    Mat cameraFrame;
    camera >> cameraFrame;

    if( cameraFrame.empty() )
    {
      cerr << "Could not grab a camera frame!" << endl;
      return  1;
    }

    Mat gray;
    cvtColor( cameraFrame, gray, CV_BGR2GRAY );
    Size size = cameraFrame.size();
    Mat faceOutline = Mat::zeros( size, CV_8UC3 );      // Draw a black canvas.
    Scalar color = CV_RGB( 255, 255, 0 );               // Yellow
    int thickness = 4;
    ellipse( faceOutline, Point(320, 240), Size(320, 240), 0, 0, 360, color, thickness, CV_AA );
    addWeighted( gray, 1.0, faceOutline, 0.7, 0, gray, CV_8UC3 );
    imshow( "final image", gray );

    char keypress = waitKey(20);
    if( keypress == 27 ) break;
  }
}

【问题讨论】:

    标签: opencv image-processing video-capture


    【解决方案1】:

    这很好用:

    int main( int argc, const char** argv )
    {
        VideoCapture camera;
        camera.open(0);
    
        if( !camera.isOpened() )
        {
            cerr << "Could not access the camera!" << endl;
            return  1;
        }
    
        while( true )
        {
            Mat cameraFrame;
            camera >> cameraFrame;
    
            if( cameraFrame.empty() )
            {
                cerr << "Could not grab a camera frame!" << endl;
                return  1;
            }
    
            Mat gray;
            cvtColor( cameraFrame, gray, cv::COLOR_BGR2GRAY );
            Size size = cameraFrame.size();
            Mat faceOutline = Mat::zeros( size, CV_8UC3 );      // Draw a black canvas.
            Scalar color = Scalar( 255, 255, 0 );               // Yellow
            int thickness = 4;
            cvtColor( gray, gray, cv::COLOR_GRAY2BGR );
            ellipse( faceOutline, Point(320, 240), Size(320, 240), 0, 0, 360, color, thickness );
            addWeighted( gray, 1.0, faceOutline, 0.7, 0, gray, CV_8UC3 );
            imshow( "final image", gray );
            char keypress = waitKey(20);
            if( keypress == 27 ) break;
        }
    }
    

    【讨论】:

      【解决方案2】:
      • 为什么不直接将椭圆绘制到 cameraFrame 中?

        椭圆( cameraFrame , Point(320, 240), Size(320, 240), 0, 0, 360, 颜色, 厚度, CV_AA );

      • 如果你想使用 addWeighted,

        1. 两个输入图像的类型必须匹配(不能为灰度图像添加颜色)
        2. 因子之和必须为 1.0
        3. 最后一个参数是深度,而不是类型(即您可以在此处将其转换为浮点数,但不能更改通道数)

        addWeighted(cameraFrame, 0.7, faceOutline, 0.3, 0, cameraFrame);

      【讨论】:

      • 我不知道可以在同一张相机图像上“绘图”。感谢您的详细和有益的评论。我尝试了我以前的代码并使用相同的窗口,现在我可以正确地做到这一点。
      【解决方案3】:

      我想你的灰色图像是单通道的,而你的 faceOutline 图像有 3 个通道。

      来自documentation

      src2 – 与 src1 大小和通道号相同的第二个输入数组。

      尝试mixChannels切换多通道图像的单通道

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-11-15
        • 2022-10-05
        • 2021-01-05
        • 2020-07-22
        • 2020-11-07
        • 1970-01-01
        • 2015-04-24
        • 1970-01-01
        相关资源
        最近更新 更多