Previewing video with GUI not fit

3 views (last 30 days)
Yong Guk Kang
Yong Guk Kang on 4 Feb 2022
Answered: Ashutosh Thakur on 24 Jan 2024
I am trying to make GUI that able to preview camera in desired position.
What I want is displaying the video on the GUI axis, with keeping axis ratio.
use 'getsnapshot' - 'image' function displays correctly, but video preview is not possible(too slow when using inside the for-loop)
If I use the preview function, the axis properties are not properly reflected. A very magnified preview appears in the middle of the axes.
How can I handle this?
%VidObj1 : Basler GenI camera connected via USB3 interface
%UIAxes_CamPreview : Axes handle
vidRes = app.vidObj1(1).VideoResolution;
app.UIAxes_CamPreview.DataAspectRatioMode='Manual';
app.UIAxes_CamPreview.DataAspectRatio=[1,1,1];
app.UIAxes_CamPreview.XLimitMethod='tight';
app.UIAxes_CamPreview.YLimitMethod='tight';
axisSize=round(app.UIAxes_CamPreview.Position);
hImage=image(app.UIAxes_CamPreview, zeros(axisSize(4),axisSize(3),3));
%%How can I fit the preview panel to the axes
preview(app.vidObj1(1), hImage)
Result of operation
Desired operation

Answers (1)

Ashutosh Thakur
Ashutosh Thakur on 24 Jan 2024
Hi Yong,
Based on my understanding, you are facing issue in maintaining position of the preview object in the axis. Ideally, the video is previewed at 100% magnification based on its resolution and frame rate. The following documentation link can be followed to understand usage of preview object: https://www.mathworks.com/help/imaq/imaqdevice.preview.html
To achieve the preview frame in the correct position, I suggest changing the position of the object (i.e., axes) where preview of the live video will be presented. You can follow the example code mentioned below for reference:
% Create a video input object.
vid = videoinput('winvideo');
% Create a figure window. This example turns off the default
% toolbar and menubar in the figure.
hFig = figure('Toolbar','none',...
'Menubar', 'none',...
'NumberTitle','Off',...
'Name','My Custom Preview GUI');
% Create the image object in which you want to
% display the video preview data.
vidRes = vid.VideoResolution;
imWidth = vidRes(1);
imHeight = vidRes(2);
nBands = vid.NumberOfBands;
hImage = image( zeros(imHeight, imWidth, nBands) );
% Specify the size of the axes that contains the image object
% so that it displays the image at the right resolution and
% centers it in the figure window.
figSize = get(hFig,'Position');
figWidth = figSize(3);
figHeight = figSize(4);
gca.unit = 'pixels';
gca.position = [ ((figWidth - imWidth)/2)...
((figHeight - imHeight)/2)...
imWidth imHeight ];
preview(vid, hImage);
The above mentioned code creates a figure window and previews the video in it by altering the "position" property of its axes. To know more about the ways in which we can preview the live video data in GUIs you can follow the documentation link present below:
I hope this helps you in having a correct position of preview object in your application.

Products


Release

R2021a

Community Treasure Hunt

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

Start Hunting!