saving captured images to a folder (error in imwrite function)

1 次查看(过去 30 天)
Dear friends,
When I run the following code to save the captured image files to a folder it works. But If I set the n value (FramesPerTrigger) to more than 1 then I get an error message.
vid = videoinput('winvideo', 1, 'RGB24_1920x1080');
src = getselectedsource(vid);
vid.LoggingMode = 'memory';
m=5;
vid.TriggerRepeat = 5;
triggerconfig(vid, 'manual');
vid.TimerFcn = 'trigger(vid)';
vid.TimerPeriod = 1;
n=1;
vid.FramesPerTrigger = n;
start(vid)
mkdir('C:\Documents and Settings\suleyman\My Documents\MATLAB\trial\deneme')
for i=1:m*n
imagename = ['C:\Documents and Settings\bae50742\My Documents\MATLAB\kinking\deneme\deneme5_Sample1_image' num2str(i) '.jpg'];
imwrite(getdata(vid), imagename);
end
stop(vid)
Here is the error message;
Error using writejpg (line 38)
4-D data not supported for JPEG files
Error in imwrite (line 477)
feval(fmt_s.write, data, map, filename, paramPairs{:});
How can I fix this problem? thank you in advance for your help.

回答(2 个)

Youssef  Khmou
Youssef Khmou 2013-3-27
编辑:Youssef Khmou 2013-3-27
hi,
The data you are trying to save are 4D , its a sequence of images as :
Width x height x channels x Frames
I suggest this way : After the acquisition process :
data=getdata(vid);
% Try to look at the size of the data : >>size(data)
N=size(data,4); % N is the number of frames
for x=1:N
filename=strcat('Image',int2str(x),'.jpg'); % OR JPEG AS YOU LIKE
imwrite(data(:,:,:,x),filename);
end
  1 个评论
Suleyman Deveci
Suleyman Deveci 2013-3-27
Hi Youssef, thank you for your reply.
It didn't work because N is always equal to "FramesPerTrigger" value but I normally have "TriggerRepeat x FramesPerTrigger" times number of image file to save.
Regards.
Suleyman

请先登录,再进行评论。


kash
kash 2013-3-27
Try this code
close all
imaqhwinfo
dev_info=imaqhwinfo('winvideo',1)
%info=imaqhwinfo('winvideo')
celldisp(dev_info.SupportedFormats)
vid=videoinput('winvideo',1,'RGB24_1920x1080');
%Open Figure
hFigure=figure(1);
%set parameters for video
%Acquire only one frame each time
triggerconfig(vid,'Manual');
set(vid,'framespertrigger',1);
%Go on forever untill stopped
set(vid,'triggerrepeat',Inf);
%Get a grayscale image
set(vid,'ReturnedColorSpace','RGB');
start(vid);
preview(vid);
data = getsnapshot(vid);
[Save,savename] = uiputfile('*.jpg','save this file')
fname=fullfile(savename,Save);
imwrite(data,fname); % where F is your image
stop(vid),delete(vid),clear('vid')

标签

Community Treasure Hunt

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

Start Hunting!

Translated by