简体   繁体   中英

MatLab (Image Processing, Image Acquisition) How to save captured images by webcam without overwriting the original file name?

I want to save images without overwriting them whenever I hit the pushbutton. Can you please help me how save images without overwriting the original? What I want to do is whenever I'll hit the pushbutton, It will generated 1 image at a time without deleting the original.

Just like in digital cameras, whenever I will hit the trigger button, it will save 1 image and the file name will be image1.jpg . So basically, if I will push trigger again, it will capture 1 image again and the file name will be image2.jpg and so on.

here is my code:

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

My code saves and keeps on overwriting the filename image1.jpg. To make things clear

1 push to the pushbutton, 1 image saves.

it's like it will call the whole block code every hit at pushbutton. I hope you guys can help me. I really troubled right now :( Thank you :)

If this is the code that makes up the callback function for that pushbutton, then yes indeed, it will execute the entire block every time you push it.

If that is the case, you'll need to change it to this:

%// 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;  

or, you could check what files are actually present, and use the next logical filename in the sequence:

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);

Every time you push that button the counter value resets to 1 because of the very first statement:

counter = 1

and hence the error.

counter = length(dir('*.jpg')) + 1; %Counts the number of .jpg files in the directory

That should do the job.

Zaher: I'm online program about image processing and image acquisition from the camera in writing MATLAB. When receiving the image every few seconds I get a picture of the camera. Photos must be stored and processed in statistical process control charts. When the first image after image acquisition program hangs and stops. Please code to get images every 10 seconds online from cameras send images that can be used in statistical process control. Thank

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM