[英]MatLab (Image Processing, Image Acquisition) How to save captured images by webcam without overwriting the original file name?
我想保存图像而不在每次按下按钮时覆盖它们。 您能帮我如何在不覆盖原始图像的情况下保存图像吗? 我想做的就是每当我按下按钮时,它将一次生成1张图像,而不会删除原始图像。
就像在数码相机中一样,每当我按下触发按钮时,它将保存1张图像,文件名将为image1.jpg 。 因此,基本上,如果我再次按下触发器,它将再次捕获1张图像,文件名将为image2.jpg ,依此类推。
这是我的代码:
counter = 1; %initialize filename increment
vid = videoinput('winvideo',2);
set(vid, 'ReturnedColorSpace', 'RGB');
img = getsnapshot(vid);
imshow(img);
savename = strcat('C:\Users\Sony Vaio\Documents\Task\images\image_' ,num2str(counter), '.jpg'); %this is where and what your image will be saved
imwrite(img, savename);
counter = counter +1; %counter should increment each time you push the button
我的代码保存并继续覆盖文件名image1.jpg。 弄清楚
1按该按钮,保存1张图像。
就像每次按下按钮时,它将调用整个块代码。 我希望你们能帮助我。 我现在真的很困扰:(谢谢:)
如果这是组成该按钮的回调函数的代码,那么确实是的,它将在您每次按下按钮时执行整个块。
如果是这种情况,则需要将其更改为:
%// initialize filename increment
persistent counter;
if isempty(counter)
counter = 1; end
vid = videoinput('winvideo', 2);
set(vid, 'ReturnedColorSpace', 'RGB');
img = getsnapshot(vid);
imshow(img);
%// this is where and what your image will be saved
savename = [...
'C:\Users\Sony Vaio\Documents\Task\images\image_', ...
num2str(counter), '.jpg'];
imwrite(img, savename);
%// counter should increment each time you push the button
counter = counter + 1;
或者,您可以检查实际存在的文件,并使用序列中的下一个逻辑文件名:
vid = videoinput('winvideo', 2);
set(vid, 'ReturnedColorSpace', 'RGB');
img = getsnapshot(vid);
imshow(img);
%// this is where and what your image will be saved
counter = 1;
baseDir = 'C:\Users\Sony Vaio\Documents\Task\images\';
baseName = 'image_';
newName = [baseDir baseName num2str(counter) '.jpg'];
while exist(newName,'file')
counter = counter + 1;
newName = [baseDir baseName num2str(counter) '.jpg'];
end
imwrite(img, newName);
每次按下该按钮,由于第一个语句,计数器值将重置为1:
计数器= 1
因此错误。
counter = length(dir('*.jpg')) + 1; %Counts the number of .jpg files in the directory
那应该做的。
Zaher:我是一个在线程序,涉及编写MATLAB的相机中的图像处理和图像采集。 每隔几秒钟接收一次图像时,我会得到相机的照片。 照片必须在统计过程控制图中存储和处理。 图像获取程序之后的第一个图像挂起并停止时。 请编码以每10秒钟从摄像机在线获取图像,然后发送可用于统计过程控制的图像。 谢谢
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.