Image Acquisition using a GUI

1 view (last 30 days)
Lu Yaseen
Lu Yaseen on 27 Apr 2014
Answered: Lu Yaseen on 28 Apr 2014
Hi guys im having a problem acquiring an image using the GUI , so once i click the button i should be able to get a photo from a the live video in the GUI here is my code
if true
% --- Executes on button press in pbCapture.
function pbCapture_Callback(hObject, eventdata, handles)
vid = videoinput('kinect', 2);
vid.ROIPosition = [89 81 137 140];
vid.FramesPerTrigger = 1;
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);
capt1 = getdata(vid);
[FileName, PathName] = uiputfile('*.jpg', 'Save As');
Name = fullfile(PathName,FileName);
imwrite(capt1, Name, 'jpg');
end
i can save the image in PNG but i cant save it in JPEG i get this error
Error using writejpg>set_jpeg_props (line 184) UINT16 image data requires bitdepth specifically set to either 12 or 16.
Error in writejpg (line 50) props = set_jpeg_props(data,varargin{:});
Error in imwrite (line 473) feval(fmt_s.write, data, map, filename, paramPairs{:});
Error in samples_aquasition>pbCapture_Callback (line 100) imwrite(capt1, Name, 'jpg');
Error in gui_mainfcn (line 96) feval(varargin{:});
Error in samples_aquasition (line 42) gui_mainfcn(gui_State, varargin{:});
Error in@(hObject,eventdata)samples_aquasition('pbCapture_Callback',hObject,eventdata,guidata(hObject)) Error while evaluating uicontrol Callback
i really need to save the file in JPEG , and i dont want to read the image again and convert it to JPEG after saving it as PNG. thanks

Accepted Answer

Lu Yaseen
Lu Yaseen on 28 Apr 2014
Thank you image Analyst for the fast replay but i think that i found the solution to my problem :)!
instead of using
capt1 = getdata(vid);
i used
capt1 = getdata(vid,1,'uint8');
because getdata(vid) will get the data as uint 16 by default and jpg works with uint8 so we have to specify that

More Answers (1)

Image Analyst
Image Analyst on 28 Apr 2014
If you're only capturing 1 frame per trigger, then just try using getsnapshot() instead of getdata(). And don't put the file type into imwrite(). Let it figure it out itself from the filename you supply it. Let me know how that works.
  1 Comment
Lu Yaseen
Lu Yaseen on 28 Apr 2014
Edited: Lu Yaseen on 28 Apr 2014
i used getsnapshot() it was the same as getdata() i also removed the file type from imwrite()
the problem is it is only saving tif or png works fine with both ,but its still giving me error when i try to save as jpg
thanks

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!