How can I handle camera on GUI(axes) by using imaq.VideoDevice?

3 views (last 30 days)
I want to show camera on axes(or uipanel) in GUI but I use 'imaq.VideoDevice' syntax.
I only found answer for 'videoinput'
like this -->>
vid = videoinput('winvideo',1);
hImage=image(zeros(699,1500,1),'Parent',handles.axes1);
preview(vid,hImage);
I want to show it like this but in imaq.VideoDevice way.
Can anybody give me an idea T^T
Thank you...

Answers (3)

Image Analyst
Image Analyst on 2 Aug 2014
For what it's worth, here's my code (a small snippet from a logitech web cam app). It's long because it's more robust and general purpose than yours and also spits out useful information to the command window:
%=====================================================================
function InitializeVideoCamera(handles)
% Initialize the video camera.
global vidobj; % Video camera object.
try
% First see if any video mode is selected in advance. If it is, we should use that if we can.
% Now get the selection from the lstVideoModes listbox.
lstVideoModes = get(handles.lstVideoModes, 'String');
lstSelectedItem = 1; % Flag for indicating no video mode is selected.
if ~isempty(lstVideoModes)
lstSelectedItem = get(handles.lstVideoModes, 'Value');
% Let's see what it is, just for fun.
if ~isempty(lstSelectedItem)
selectedVideoMode = lstVideoModes{lstSelectedItem};
end
end
% Print info to command window:
hardwareInfo = imaqhwinfo % Print what cameras are there.
adaptorNames = hardwareInfo.InstalledAdaptors;
% Might look something like:
% InstalledAdaptors: {'dcam' 'gentl' 'gige' 'lumeneraimaqw64' 'matrox' 'winvideo'}
matches = strfind(adaptorNames, 'winvideo');
% Find out which index is the Lumenera Camera.
winvideoIndex = find(~cellfun(@isempty, matches));
thewinvideoAdaptor = adaptorNames{winvideoIndex}
% Print out other useful information to the command window.
hw2 = imaqhwinfo(thewinvideoAdaptor);
devInfo = hw2.DeviceInfo
numberOfImagingDevices = length(devInfo)
logitechModes = '';
for d = 1 : numberOfImagingDevices
thisDevice = devInfo(d); % A structure.
defaultFormat = thisDevice.DefaultFormat; % A character array.
deviceFileSupported = thisDevice.DeviceFileSupported; % A logical.
devName{d} = thisDevice.DeviceName; % A cell.
devID = thisDevice.DeviceID; % A double
videoInputConstructor = thisDevice.VideoInputConstructor; % A character array.
videoDeviceConstructor = thisDevice.VideoDeviceConstructor; % A character array.
SupportedFormats{d} = thisDevice.SupportedFormats; % A cell array
numberOfSupportedFormats(d) = length(SupportedFormats);
% Print out stuff to command window.
fprintf('\nFor device #%d, named %s, the attributes are:\n DefaultFormat = %s\n DeviceFileSupported = %d\n DeviceID = %d\n VideoInputConstructor = %s\n VideoDeviceConstructor = %s\n',...
d, devName{d}, defaultFormat, deviceFileSupported, devID, videoInputConstructor, videoDeviceConstructor);
% Let's start a counter for the video modes if it's the Logitech camera.
if strfind(lower(devName{d}), 'logitech')
modeCounter = 1;
end
% Print out all the supported formats for this device.
fprintf(' For device named : %s, the supported formats are:\n', devName{d});
for k = 1 : numberOfSupportedFormats(d)
theseFormats = SupportedFormats{k}
for k2 = 1 : length(theseFormats)
thisFormat = theseFormats{k2};
fprintf(' Format #%d = %s\n', k2, thisFormat);
% If the device name contains Logitech, and the format contains RGB, let's add the mode to the listbox.
if ~isempty(strfind(lower(devName{d}), 'logitech')) && ~isempty(strfind(thisFormat, 'RGB'))
logitechModes{modeCounter} = thisFormat;
modeCounter = modeCounter + 1;
end
end
end
end
% Add the modes to the listbox:
if ~isempty(logitechModes)
set(handles.lstVideoModes, 'string', logitechModes);
end
% The device name, devName, might be something like : {'Hauppauge WinTV 885 Video Capture' 'Logitech QuickCam Ultra Vision'}
% Find the one that is for our Logitch camera.
matches = strfind(devName, 'Logitech');
% Find out which index is the Logitech Camera.
LogitechIndex = find(~cellfun(@isempty, matches));
% Get the Logitech device:
LogitechCamera = devInfo(LogitechIndex);
% Get the device ID:
LogitechDeviceID = LogitechCamera.DeviceID;
% Now we know which device is the Logitech camera. Now let's let the user pick some RGB modes;
% Initialize winvideo webcam with the first mode on the list.
% Or the selected mode if one was selected in advance of entering this function.
vidobj = videoinput(thewinvideoAdaptor, LogitechDeviceID, logitechModes{lstSelectedItem});
if ~isempty(vidobj)
src = getselectedsource(vidobj);
vidobj.FramesPerTrigger = 1;
axes(handles.axesImage);
hImage = findobj(handles.axesImage, 'Type', 'image');
preview(vidobj, hImage);
% src.ZoomMode = 'manual';
% Turn on the live video preview. Display the bounding box over it if there is one selected.
TurnOnLiveVideo(handles);
end
% Turn on the live video preview. Display the bounding box over it if there is one selected.
TurnOnLiveVideo(handles);
catch ME
errorMessage = sprintf('Error in function logitech_webcam_OpeningFcn.\nNo Logitech webcam detected!\nMake sure you plug in your webcam BEFORE you start MATLAB, NOT AFTER!\n\nError Message:\n%s', ME.message);
fprintf('%s\n', errorMessage);
set(handles.txtInfo, 'string', errorMessage);
uiwait(warndlg(errorMessage));
end

Geoff Hayes
Geoff Hayes on 1 Aug 2014
The code should be very similar to that for the videoinput object. See imaq.videodevice and preview for details.
An example could be
vidobj = imaq.VideoDevice('winvideo', 1);
hImage=image(zeros(699,1500,1),'Parent',handles.axes1);
preview(vidobj,hImage);
Try the above and see what happens!
  7 Comments
Jia Zhen
Jia Zhen on 2 May 2015
hi, so is there really no way to use imaq.VideoDevice for preview in axes without opening another window as George Hayes had say? I really dont want to use videoinput as it would cause my visioncascadeobject detector to malfunction. Thanks.
Image Analyst
Image Analyst on 2 May 2015
I don't know. I've never used visioncascadeobject and I don't know why that would malfunction if you had a video input object. I suggest you call the tech support.

Sign in to comment.


Ankit Singh
Ankit Singh on 7 Apr 2016
How to close hImage figure is always on

Community Treasure Hunt

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

Start Hunting!