preview live data from image acquisition device
introduction
after you connect matlab® to the image acquisition device you can view the live video stream using the video preview window. previewing the video data can help you make sure that the image being captured is satisfactory.
for example, by looking at a preview, you can verify that the lighting and focus are correct. if you change characteristics of the image, by using video input object and video source object properties, the image displayed in the video preview window changes to reflect the new property settings.
the following sections provide more information about using the video preview window.
instead of using the toolbox's video preview window, you can display the live video preview stream in any handle graphics® image object you specify. in this way, you can include video previewing in a gui of your own creation. the following sections describe this capability.
opening a video preview window
to open a video preview window, use the preview
function. the
video preview window displays the live video stream from the device. you can only
open one preview window per device. if multiple devices are used, you can open
multiple preview windows at the same time.
the following example creates a video input object and then opens a video preview window for the video input object.
vid = videoinput('winvideo');
preview(vid);
the following figure shows the video preview window created by this example. the
video preview window displays the live video stream. the size of the preview image
is determined by the value of the video input object's
roiposition
property. the video preview window displays the
video data at 100% magnification.
in addition to the preview image, the video preview window includes information about the image, such as the timestamp of the video frame, the video resolution, the frame rate, and the current status of the video input object.
note
because video formats typically express resolution as width-by-height, the video preview window expresses the size of the image frame as column-by-row, rather than the standard matlab row-by-column format.
note
the image acquisition toolbox™ preview
window supports the display of up to 16-bit image data. the preview window was
designed to only show 8-bit data, but many cameras return 10-, 12-, 14-, or
16-bit data. the preview window display supports these higher bit-depth cameras.
however, larger bit data is scaled to 8-bit for the purpose of displaying
previewed data. to capture the image data in the preview window in its full bit
depth for grayscale images, set the previewfullbitdepth
property to 'on'
.
stopping the preview video stream
when you use the preview
function to start previewing image
data, the video preview window displays a view of the live video stream coming from
the device. to stop the updating of the live video stream, call the
stoppreview
function.
this example creates a video input object and opens a video preview window. the
example then calls the stoppreview
function on this video input
object. the video preview window stops updating the image displayed and stops
updating the timestamp. the status displayed in the video preview window also
changes to indicate that previewing has been stopped.
vid = videoinput('winvideo');
preview(vid)
stoppreview(vid)
to restart the video stream in the video preview window, call
preview
again on the same video input object.
preview(vid)
closing a video preview window
to close a particular video preview window, use the function, specifying the video input object as an argument. you do not need to stop the live video stream displayed in the video preview window before closing it.
closepreview(vid)
to close all currently open video preview windows, use the
closepreview
function without any arguments.
closepreview
note
when called without an argument, the closepreview
function
only closes video preview windows. the closepreview
function
does not close any other figure windows in which you have directed the live
preview video stream. for more information, see previewing data in custom guis.
previewing data in custom guis
instead of using the toolbox's video preview window, you can use the
preview
function to direct the live video stream to any
handle graphics image object. in this way, you can incorporate the toolbox's
previewing capability in a gui of your own creation. (you can also perform custom
processing as the live video is displayed. for information, see performing custom processing of previewed data.)
to use this capability, create an image object and then call the
preview
function, specifying a handle to the image object as
an argument. the preview
function outputs the live video stream
to the image object you specify.
the following example creates a figure window and then creates an image object in
the figure, the same size as the video frames. the example then calls the
preview
function, specifying a handle to the image
object.
% create a video input object. vid = videoinput('winvideo'); % create a figure window. this example turns off the default % toolbar, menubar, and figure numbering. figure('toolbar','none',... 'menubar', 'none',... 'numbertitle','off',... 'name','my preview window'); % create the image object in which you want to display % the video preview data. make the size of the image % object match the dimensions of the video frames. vidres = vid.videoresolution; nbands = vid.numberofbands; himage = image( zeros(vidres(2), vidres(1), nbands) ); % display the video data in your gui. preview(vid, himage);
when you run this example, it creates the gui shown in the following figure.
custom preview
performing custom processing of previewed data
when you specify an image object to the preview
function (see
previewing data in custom guis), you can optionally
also specify a function that preview
executes every time it
receives an image frame.
to use this capability, follow these steps:
create the function you want executed for each image frame, called the update preview window function. for information about this function, see creating the update preview window function.
create an image object.
configure the value of the image object's
'updatepreviewwindowfcn'
application-defined data to be a function handle to your update preview window function. for more information, see specifying the update preview function.call the
preview
function, specifying the handle of the image object as an argument.
note
if you specify an update preview window function, in addition to whatever
processing your function performs, it must display the video data in the image
object. you can do this by updating the cdata
of the image
object with the incoming video frames. for some performance guidelines about
updating the data displayed in an image object, see .
creating the update preview window function
when preview
calls the update preview window function you
specify, it passes your function the following arguments.
argument | description | |
---|---|---|
| handle to the video input object being previewed | |
| a data structure containing the following fields: | |
| current image frame specified as an h-by-w-by-b array,
where h is the image height and w is the image width, as
specified in the | |
| character vector specifying the current image width and
height, as defined by the | |
| character vector describing the status of the video input object | |
| character vector specifying the time associated with
the current image frame, in the format
| |
| character vector specifying the current frame rate of the video input object in frames per second | |
| handle to the image object in which the data is to be displayed |
the following example creates an update preview window function that displays
the timestamp of each incoming video frame as a text label in the custom gui.
the update preview window function uses getappdata
to
retrieve a handle to the text label uicontrol
object from
application-defined data in the image object. the custom gui stores this handle
to the text label uicontrol
object — see specifying the update preview function.
note that the update preview window function also displays the video data by
updating the cdata
of the image object.
function mypreview_fcn(obj,event,himage) % example update preview window function. % get timestamp for frame. tstampstr = event.timestamp; % get handle to text label uicontrol. ht = getappdata(himage,'handletotimestamplabel'); % set the value of the text label. ht.string = tstampstr; % display image data. himage.cdata = event.data
specifying the update preview function
to use an update preview window function, store a function handle to your
function in the 'updatepreviewwindowfcn'
application-defined
data of the image object. the following example uses the
setappdata
function to configure this application-defined
data to a function handle to the update preview window function described in
creating the update preview window function.
this example extends the simple custom preview window created in previewing data in custom guis. this example adds
three push button uicontrol
objects to the gui: start preview, stop
preview, and close
preview.
in addition, to illustrate using an update preview window function, the
example gui includes a text label uicontrol
object to display
the timestamp value. the update preview window function updates this text label
each time a framed is received. the example uses setappdata
to store a handle to the text label uicontrol
object in
application-defined data in the image object. the update preview window function
retrieves this handle to update the timestamp display.
% 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'); % set up the push buttons uicontrol('string', 'start preview',... 'callback', 'preview(vid)',... 'units','normalized',... 'position',[0 0 0.15 .07]); uicontrol('string', 'stop preview',... 'callback', 'stoppreview(vid)',... 'units','normalized',... 'position',[.17 0 .15 .07]); uicontrol('string', 'close',... 'callback', 'close(gcf)',... 'units','normalized',... 'position',[0.34 0 .15 .07]); % create the text label for the timestamp htextlabel = uicontrol('style','text','string','timestamp', ... 'units','normalized',... 'position',[0.85 -.04 .15 .08]); % 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 ]; % set up the update preview window function. setappdata(himage,'updatepreviewwindowfcn',@mypreview_fcn); % make handle to text label available to update function. setappdata(himage,'handletotimestamplabel',htextlabel); preview(vid, himage);
when you run this example, it creates the gui shown in the following figure.
each time preview
receives a video frame, it calls the update
preview window function that you specified, which updates the timestamp text
label in the gui.
custom preview gui with timestamp text label