Image MatLab(图像处理,图像采集)如何通过网络摄像头保存捕获的图像而不覆盖原始文件名?

Image MatLab(图像处理,图像采集)如何通过网络摄像头保存捕获的图像而不覆盖原始文件名?,image,matlab,image-processing,process,counter,Image,Matlab,Image Processing,Process,Counter,我希望在按下按钮时保存图像而不覆盖它们。你能帮我保存图像而不覆盖原始图像吗?我想做的是,每当我按下按钮时,它会一次生成一个图像,而不会删除原始图像 就像在数码相机中一样,每当我按下触发器按钮时,它都会保存一幅图像,文件名为image1.jpg。所以基本上,如果我再次按下触发器,它将再次捕获一个图像,文件名将是image2.jpg,依此类推 这是我的密码: counter = 1; %initialize filename increment vid = videoinput('winvideo

我希望在按下按钮时保存图像而不覆盖它们。你能帮我保存图像而不覆盖原始图像吗?我想做的是,每当我按下按钮时,它会一次生成一个图像,而不会删除原始图像

就像在数码相机中一样,每当我按下触发器按钮时,它都会保存一幅图像,文件名为image1.jpg。所以基本上,如果我再次按下触发器,它将再次捕获一个图像,文件名将是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
这应该可以完成任务。

扎赫: 我是用MATLAB编写的关于图像处理和摄像机图像采集的在线程序。 当每隔几秒钟接收一次图像时,我就会得到一张相机的照片。 照片必须在统计过程控制图中存储和处理。 当图像采集程序后的第一幅图像挂起并停止时。 请编码每10秒从摄像头在线获取图像,并发送可用于统计过程控制的图像。
谢谢

我使用了你修改过的代码,但我发现了一个错误。它说:函数定义放错位置或嵌套不正确。在哪里结束if语句?尝试将其置于计数器下方=1;和计数器=计数器+1;但我的相机不再拍摄图像了。奥尔登休斯:上面写着-未定义的函数或变量“savename”。licenseplate>CaptureLogin\u回调(第492行)imwrite(img,savename)中出错;gui_mainfcn(第96行)feval(varargin{:})中出错;licenseplate(第42行)gui_mainfcn(gui_状态,varargin{:})中的错误;@(hObject,eventdata)licenseplate('CaptureLogin_Callback',hObject,eventdata,guidata(hObject))中的错误,在评估uicontrol回调时出现错误这是您的第二个错误code@Lloyd字体你看到错误了吗?这是一个相当小的错误…我在最近的编辑中更正了它。是的,我看到了saveName,但被声明了,而没有被声明。:)哈哈,工作起来很有魅力!:)非常感谢你!你刚刚救了我一命呵呵