How to save webcam image in gui to a certain folder without push button

I have interfaced webcam with matlab gui and able to take images. I have two push buttons Preview & Take Image. I want to take image with "Take Image" push button & my taken images to be stored with my desired names and folder as we do in imsave normally but here I am unable.Can anyone help me, the closest was below but it is introducing new push button.
function pbTP_Callback(hObject, eventdata, handles)
vid = videoinput('winvideo', 1, 'YUY2_1024x768');
vid.FramesPerTrigger = 1;
vid.ReturnedColorspace = 'rgb';
triggerconfig(vid, 'manual');
vidRes = get(vid, 'VideoResolution');
imWidth = vidRes(1);
imHeight = vidRes(2);
nBands = get(vid, 'NumberOfBands');
hImage = image(zeros(imHeight, imWidth, nBands), 'parent', handles.axPreview)
preview(vid, hImage);
start(vid);
pause(5);
trigger(vid);
stoppreview(vid);
im1 = getdata(vid);
imwrite(im1, 'myimage.jpg');

8 Comments

Snowleopard - what is the purpose of the Take Image push button? Does it save the current image to file? As for the code that you have provided, pbTP_Callback, what does this correspond to? It seems that it previews and then saves the image to file. Are you trying to split this function into two, for each of your buttons?
@ Geoff Hayes. pbTP_Callback is tag for push button with string named "Take image". You understood well it starts preview and takes image in 5 seconds and saves in folder containing the M & gui files. I want to save & name the taken image to my desired location as we do in imsave; without adding any new push button. pbTP_Callback should take image and the "save image" window should appear.
Snowleopard - what does the Preview button do? Does it do the same as the above but just doesn't write the images to file? Is the problem with your above code that
imwrite(im1, 'myimage.jpg');
always overwrites the last saved image? If so, why not just call imsave from within the pbTP_Callback function?
Preview button only turns on Preview.Its not related to pbTP_Callback. I was unable to use imsave. Can you please modify my above code.
Please clarify what you mean by I was unable to use imsave. Did you observe any errors and, if so, what were they?
I get multiple errors,
??? Error using ==> imaqdevice.preview at 179 Multiple VIDEOINPUT objects cannot access the same device simultaneously.
Error in ==> gui_4>pbTP_Callback at 114 preview(vid, hImage);
Error in ==> gui_mainfcn at 96 feval(varargin{:});
Error in ==> gui_4 at 42 gui_mainfcn(gui_State, varargin{:});
Error in ==> @(hObject,eventdata)gui_4('pbTP_Callback',hObject,eventdata,guidata(hObject))
??? Error using ==> pause Error while evaluating uicontrol Callback
??? Error using ==> im1checkhandle at 61 Function IMSAVE expected its first input argument, h, to be a valid handle to a single graphics object.
Error in ==> imsave at 42 im1checkhandle(h,{'image','axes','figure','uipanel'},mfilename,'h',1);
Error in ==> gui_4>pbTP_Callback at 127 imsave(h)
Error in ==> gui_mainfcn at 96 feval(varargin{:});
Error in ==> gui_4 at 42 gui_mainfcn(gui_State, varargin{:});
Error in ==> @(hObject,eventdata)gui_4('pbTP_Callback',hObject,eventdata,guidata(hObject))
??? Error while evaluating uicontrol Callback
According to imsave, it creates a Save Image tool in a separate figure that is associated with the image in the current figure, called the target image. Presumably your current figure is the GUI and so this is probably not the correct approach. Try using either imputfile or uiputfile which will allow you (or the user) to select the filename and folder where he or she wishes to save the file.
Thanks for your support.I tried with uiputfile and got the save window as expected but file was not saved and got following errors as well.
??? Error using ==> imaqdevice.preview at 179 Multiple VIDEOINPUT objects cannot access the same device simultaneously.
Error in ==> gui_4>pbTP_Callback at 114 preview(vid, hImage);
Error in ==> gui_mainfcn at 96 feval(varargin{:});
Error in ==> gui_4 at 42 gui_mainfcn(gui_State, varargin{:});
Error in ==> @(hObject,eventdata)gui_4('pbTP_Callback',hObject,eventdata,guidata(hObject))
??? Error using ==> pause Error while evaluating uicontrol Callback

Sign in to comment.

 Accepted Answer

snowleopoard - Did you get an error message when you tried to save the file, and if so, what was it?
I suspect that the above error is when you pressed the Take Image button a second time. Is that the case? If so, the problem is probably due to
vid = videoinput('winvideo', 1, 'YUY2_1024x768');
You are creating a new video input object without deleting the previous one (created the first time you pressed the button). Either add a line to delete your object before the callback exits (so after you've saved to file), or use a single object that you create once and save to your handles object. Something like
function pbTP_Callback(hObject, eventdata, handles)
if ~isfield(handles,'vidObject')
handles.vidObject = videoinput('winvideo', 1, 'YUY2_1024x768');
guidata(hObject,handles);
end
% now do same as what you have written, replacing vid with handles.vidObject
The above code checks to see if the vidObject has already been created and is part of the handles structure. If not, it creates it and saves it to handles using guidata.

5 Comments

Thanks for your support, I was pressing Take Image twice to end up with this error. Further I was not using uiputfile with complete steps. Now code is,
im1 = getdata(vid);
[Save,savename] = uiputfile('*.jpg','Reference Image')
fname=fullfile(savename,Save);
imwrite(im1,fname);
Thanks Again
I want to change the pause() through an Edit Box where user can enter the time and the image is taken with the entered delay.
pause(get(handles.edit2, 'String') it is giving error.
pause is expecting a numeric input; get(handles.edit2,'String') is returning a string. Try
pause(str2double(handles.edit2,'String'))
In the future, please post the full error message.
I tried above but resulted in errors,
??? Error using ==> str2double
Too many input arguments.
Error in ==> RM_1>PBProcess_Callback at 192
pause(str2double(handles.edit2,'String'));
Error in ==> gui_mainfcn at 96
feval(varargin{:});
Error in ==> RM_1 at 42
gui_mainfcn(gui_State, varargin{:});
I added a push button (pushbutton_9) i-e whenever I push the button value should be updated in pause but again could not run, Pushbutton 9 callback
x = get(handles.edit2,'String');
y=str2double(x)
setappdata(handles.edit1, 'y', y)
Pb_process callback
y = getappdata(handles.pushbutton9 , 'y')
pause(y);
Error in ==> RM_1>pushbutton9_Callback at 300
setappdata(handles.edit1, 'y', y)
Error in ==> gui_mainfcn at 96
feval(varargin{:});
Error in ==> RM_1 at 42
gui_mainfcn(gui_State, varargin{:});
Error in ==> @(hObject,eventdata)RM_1('pushbutton9_Callback',hObject,eventdata,guidata(hObject))
??? Error while evaluating uicontrol Callback
This option does not stop pause & in command window it update y=[] but does not follow the time which I enter, Can you plz correct it
That's my mistake. Change
pause(str2double(handles.edit2,'String'))
to
pause(str2double(get(handles.edit2,'String')))
Note the missing get.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!