How to have different preview windows for a single video object?

5 views (last 30 days)
I have a single video object from a webcam that I want to be previewed in two windows at the same time, how can I accomplish this?

Accepted Answer

Siddharth Bhutiya
Siddharth Bhutiya on 17 Aug 2018
When you preview the video from a "videoinput" object, MATLAB will continuously retrieve the image data from the webcam and display it on the screen. So, you will not be able to create another preview using the same videoinput object as that object will already be in use by the previous preview window.
However, in order to display two preview windows simultaneously you can try the following workaround and see if it fits your workflow.
vid = videoinput('winvideo',1);
figure;
im_handle1 = image(zeros(1280,720));
figure;
global im_handle2
im_handle2 = image(zeros(1280,720));
setappdata(im_handle1, 'UpdatePreviewWindowFcn',@my_callback_fcn);
preview(vid,im_handle1);
function my_callback_fcn(obj, event, himage)
global im_handle2
set(himage,'CData',event.Data);
set(im_handle2,'CData',event.Data);
end
The "UpdatePreviewWindowFcn" callback function will be executed every time a new frame is obtained from the webcam. So, we can create two image handles that will act as our preview windows and inside the callback function we update the "CData" for both the image handles, so that it displays the most recent frame. In this way you can display the same video on two different windows simultaneously.
Please note that if a frame is retrieved before the "UpdatePreviewWindowFcn" callback for the previous frame is completed, then that frame will be dropped.

More Answers (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!