Use pre-acquired images instead of an image acquisition device

1 次查看(过去 30 天)
I have a function code for GUI which works on images acquired from the webcam or other accessible image acquisition device. I want to use the same code on pre-acquired images.
Any suggestions ? ? ?

回答(2 个)

Image Analyst
Image Analyst 2011-11-29
I do this all the time. Just have a flag called useSampleImages or useDiskImages or usingCamera or something like that. Set it depending on whether you are using a camera or not. When it comes time to snap your picture either call the video camera snap picture function or call a function you write called ReadImageFromDisk() where you ask the user for a filename, call imread, and pass back the image array.
  1 个评论
Suhas Deshpande
Suhas Deshpande 2011-11-29
In the code I need an object vid as input
now I am using my webcam so :
vid = videoinput('winvideo', 1, 'RGB24_640x480');
function_name(vid)
If I am not using a videoinput what object should I create.
Can I create a pseudo videoinput source which acquires image from disk ?

请先登录,再进行评论。


Image Analyst
Image Analyst 2011-11-29
Look at my code for SnapImage:
%--------------------------------------------------------------------------------------------------------------
% Snap an image from the camera, or if UserSettings.useSamplePictures = true, ask user to select one from disk.
function snappedImage = SnapImage(handles)
% Declare imgOriginal. It might be filled with values here.
global imgOriginal; % Declare global so that other functions can see it, if they also declare it global.
global vidobj; % Video camera object.
global UserSettings;
% global cameraNumber;
snappedImage = []; % Initialize to null
if UserSettings.useSamplePictures
% Disable all controls. Re-enable later with GroupSet
WasEnabled = DisableAllControls_Pointer(handles, 'watch');
% Don't disable the Exit button.
set(handles.btnExit, 'Enable', 'on');
% startingFolder = 'C:\Documents and Settings\user\My Documents\My Pictures';
startingFolder = handles.ImageFolder;
if ~exist(startingFolder, 'dir')
startingFolder = pwd;
end
[baseFileName, folder] = uigetfile({'*.jpg;*.tif;*.png;*.gif;*.bmp','Image Files (jpg,tif,png,bmp,gif)';...
'*.*','All Files (*.*)' },'Specify image file', startingFolder);
if baseFileName ~= 0
fullFileName = fullfile(folder, baseFileName);
snappedImage = imread(fullFileName);
% Enable the "Begin" button.
set(handles.btnBegin, 'Enable', 'on');
% Change folder of listbox if it's different.
% Note: folder will have a trailing backslash.
folderWithoutTrialingBackslash = folder(1:end-1);
if ~strcmpi(handles.ImageFolder, folderWithoutTrialingBackslash)
% From the help:
% "Save the changed version of data with the command guidata(object_handle,data)."
% Store the new folder in handles.ImageFolder.
guidata(handles.ImageFolder, folderWithoutTrialingBackslash);
% Refresh image list.
LoadImageList(handles);
end
end
% Re-enable all those controls we disabled with DisableAllControls_Pointer
GroupSet(handles, WasEnabled, 'Enable', 'on');
set(gcf,'Pointer','arrow');
drawnow; % Cursor won't change right away unless you do this.
if baseFileName == 0
% They clicked Cancel.
return;
end
else
% Snap picture from camera.
if isempty(vidobj), return, end;
snappedImage = getsnapshot(vidobj); % This works.
% lucamShowPreview(cameraNumber); % This does not work.
% lucamTakeVideo(1, cameraNumber); % This does not work, either with or without lucamShowPreview
end
try
axes(handles.axesImage);
hold off;
cla reset;
axis auto;
imshow(snappedImage, 'InitialMagnification', 'fit');
imgOriginal = snappedImage;
catch ME
errorMessage = sprintf('Error in function SnapImage.\nError Message:\n%s', ME.message);
set(handles.txtInfo, 'String', errorMessage);
msgboxw(errorMessage);
end
return; % from SnapImage()
If, upon startup, I determined that a camera wasn't present, I set the variable UserSettings.useSamplePictures = true. Then in SnapImage I either snap a photo from the camera, or ask the user to pick one from disk. You'll of course have to make a few modifications since I make some low level calls to my Lumenera camera and for enabling and disabling controls, which you don't have.
  3 个评论
Walter Roberson
Walter Roberson 2011-11-30
Hint: things would be easier if you design a MATLAB OOP Class derived from the handle class. The class would have to support several methods that are specific to the above code, as that code tends to use the object as if it were a structure. (You cannot just a structure without modifying the code, as the code does not explicitly write back the changes it makes to the object.)

请先登录,再进行评论。

产品

Community Treasure Hunt

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

Start Hunting!

Translated by