c++ - 实时相机上的 OpenCV ROI

标签 c++ opencv roi

我正在尝试在实时相机中设置 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 中显示。 有没有人有一些想法? 任何帮助表示赞赏。

最佳答案

我希望我理解你的问题,你想要这样的输出:

enter image description here

我在视频源上创建了一个大小为 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;
}

关于c++ - 实时相机上的 OpenCV ROI,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43597744/

相关文章:

c++ - 为什么构造函数调用取决于默认析构函数的存在?

python - 仅检查 OpenCV 中视频源的特定部分

python - 如何在图像中找到签名?

java - OpenCV 检测 ROI,创建 submat 并复制到原始 mat

c++ - 使用无 header 的 AES 加密

c++ - 将表面法 vector 投影到 xy 平面上

ios - 使用 OpenCV 图像识别为 Objective-C 自定义视频摄像头应用程序添加亮度/ Gamma 控制

c++ - 遍历区域并在 OpenCV 中获取平均像素值?

python - OpenCV 的 selectROI 坐标如何工作?

c++ - "Side-By-Side"Visual Studio C++ 错误?