【问题标题】:OpenCV ROI on Real time camera实时相机上的 OpenCV ROI
【发布时间】:2017-04-24 21:08:17
【问题描述】:

我正在尝试在实时相机中设置 ROI 并在 ROI 中复制图片。 但是,我尝试了许多互联网上的方法,但仍然不成功。 我的部分代码如下所示:

 while(!protonect_shutdown)
    {
        listener.waitForNewFrame(frames);      
        libfreenect2::Frame *ir = frames[libfreenect2::Frame::Ir];
        //! [loop start]

     
        cv::Mat(ir->height, ir->width, CV_32FC1, ir->data).copyTo(irmat);
      
 
	    Mat img = imread("button.png");

	    cv::Rect r(1,1,100,200);
		
	   cv::Mat dstroi = img(Rect(0,0,r.width,r.height));
	   irmat(r).convertTo(dstroi, dstroi.type(), 1, 0);
  	   cv::imshow("ir", irmat / 4500.0f);
  
       int key = cv::waitKey(1);

       protonect_shutdown = protonect_shutdown || (key > 0 && ((key & 0xFF) == 27)); 

        listener.release(frames);
    }

我的实时摄像头可以正常显示视频。并且我的程序中没有错误,但图片无法在 ROI 中显示。 有人有什么想法吗? 任何帮助表示赞赏。

【问题讨论】:

  • 我使用了 rectangle(irmat,r,Scalar(0,255,0),2);在我的代码中,矩形可以在实时摄像头中成功显示。但是图片不成功。 :(

标签: c++ opencv roi


【解决方案1】:

我希望我正确理解了您的问题,并且您想要这样的输出:

我在视频源上创建了一个大小为 100x200 的矩形,并在该矩形中显示了一个图像。

代码如下:

int main()
{
    Mat frame,overlayFrame;

    VideoCapture cap("video.avi");//use 0 for webcam 
    overlayFrame=imread("picture.jpg");

    if (!cap.isOpened())
    {
        cout << "Could not capture video";
        return -1;
    }

    Rect roi(1,1,100,200);//creating a rectangle of size 100x200 at point (1,1) on the videofeed

    namedWindow("CameraFeed");

    while ((cap.get(CV_CAP_PROP_POS_FRAMES) + 1) < cap.get(CV_CAP_PROP_FRAME_COUNT))
    {
        cap.read(frame);

        resize(overlayFrame, overlayFrame, resize(overlayFrame, overlayFrame, Size(roi.width, roi.height));//changing the size of the image to fit in the roi

        overlayFrame.copyTo(frame(roi));//copying the picture to the roi

        imshow("CameraFeed", frame);

        if (waitKey(27) >= 0)
            break;
    }
    destroyAllWindows;
    return 0;
}

【讨论】:

  • 感谢您的回复。但是,我的数据来自使用 linux libfreenect2 的 kinect2,所以我不能使用 VideoCapture cap("video.avi");读取相机。
  • 如果我在我的代码上按照您的其他步骤操作,错误是 OpenCV 错误:copyTo 中的断言失败 (channels() == CV_MAT_CN(dtype)),文件 /build/buildd/opencv-2.4。 8+dfsg1/modules/core/src/copy.cpp,第 212 行终止在抛出 'cv::Exception' what() 实例后调用:/build/buildd/opencv-2.4.8+dfsg1/modules/core/ src/copy.cpp:212: error: (-215) channels() == CV_MAT_CN(dtype) in function copyTo
  • 我使用了 resize(img, img, Size(roi.width, roi.height)); img.copyTo(irmat(roi)); cv::imshow("ir", irmat / 4500.0f);
  • irmatimg的频道数好像不一样。所以在videocapture() 中,这意味着一个是BGR 框架,另一个是greyscalebinary 框架。我对 Kinect 不熟悉,但这绝对是问题所在,irmatimg 属于不同类型,而 opencv 函数 copyto 要求它们属于同一类型。
  • 是的,我和你有同样的想法。那么,您认为有什么方法可以解决我的问题吗?
猜你喜欢
  • 1970-01-01
  • 2014-06-19
  • 1970-01-01
  • 1970-01-01
  • 2020-10-11
  • 2015-06-29
  • 2012-10-16
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多