0

I am trying to set ROI in real time camera and copy a picture in the ROI. However, I tried many methods from Internet but it is still unsuccessful. Part of my code is shown below:

 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);
    }

My real time camera can show the video normally. And no bugs in my program, but the picture cannot be shown in the ROI. Does anyone have some ideas? Any help is appreciate.

xuxin wang
  • 7
  • 1
  • 6
  • I used rectangle(irmat,r,Scalar(0,255,0),2); in my code, the rectangle can be shown successfully in the real time camera. But the picture is unsuccessful. :( – xuxin wang Apr 24 '17 at 21:27

1 Answers1

1

I hope I understood your question right and you want an output something like this:

enter image description here

I have created a rectangle of size 100x200 on the video feed and displaying an image in that rectangle.

Here is the code:

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;
}
  • Thanks for your reply. However, my data is from kinect2 using linux libfreenect2, so I cannot use VideoCapture cap("video.avi"); to read the camera. – xuxin wang Apr 25 '17 at 17:31
  • If I follow your other steps on my code, the error is OpenCV Error: Assertion failed (channels() == CV_MAT_CN(dtype)) in copyTo, file /build/buildd/opencv-2.4.8+dfsg1/modules/core/src/copy.cpp, line 212 terminate called after throwing an instance of '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 – xuxin wang Apr 25 '17 at 17:40
  • I used resize(img, img, Size(roi.width, roi.height)); img.copyTo(irmat(roi)); cv::imshow("ir", irmat / 4500.0f); – xuxin wang Apr 25 '17 at 18:47
  • It looks like the number of channels of `irmat` and `img` are different. So in a `videocapture()` it would mean one is `BGR` frame and the other is `greyscale` or `binary` frame. I'm not familiar with Kinect but that is definitely the issue, the `irmat` and `img` are of different types and the opencv fiunction, `copyto` requires them to be of the same type. – Abhijith Bagepalli Apr 26 '17 at 05:30
  • Yes, I have the same idea with you. So, do you think there is some way to solve my problem? – xuxin wang Apr 26 '17 at 19:12
  • Did you try using the `converTo` function in the new code, like you've posted in the question? – Abhijith Bagepalli Apr 27 '17 at 05:29