image1.jpg, image2.jpg.....etc how?

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.
here's my simple code.
vid = videoinput('winvideo',1);
set(vid, 'ReturnedColorSpace', 'RGB');
img = getsnapshot(vid);
imshow(img);
imwrite(img,'C:\Users\Sony Vaio\Documents\Appendix\images\image.jpg');
thank you :)

 채택된 답변

Jeff E
Jeff E 2013년 9월 11일

0 개 추천

counter = 1; %initialize filename increment
vid = videoinput('winvideo',1);
set(vid, 'ReturnedColorSpace', 'RGB');
img = getsnapshot(vid);
imshow(img);
savename = strcat('C:\Users\Sony Vaio\Documents\Appendix\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
Instead of using a counter, you could also use a timestamp, as from CLOCK or DATESTR.

댓글 수: 4

Lloyd
Lloyd 2013년 9월 11일
thank you for replying Sir Jeff EA, I tried your code but it's stuck at image_1.jpg and it's overwriting it. :(
Jeff E
Jeff E 2013년 9월 11일
Sounds like you're calling this block of code every time you want to take a snapshot. With my solution, and ImageAnalyst's below, you'll need to figure out a way to increment the counter variable.
Or use a timestamp.
Lloyd
Lloyd 2013년 9월 11일
yes, that's what I'm trying to say. hehe anyway, thank you for helping me Sir Jeff EA :)
Lloyd
Lloyd 2013년 9월 11일
one more thing sir, Is it possible to loop the whole block of code? I can't find any solutions to put on "if (insert loop rofl)"

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Image Analyst
Image Analyst 2013년 9월 11일

0 개 추천

Lloyd:
Use sprintf() to construct a unique filename based on a loop counter. See the FAQ for details: http://matlab.wikia.com/wiki/FAQ#How_can_I_process_a_sequence_of_files.3F
baseFileName = sprintf('Frame %d.png', frameCounter);
fullFileName = fullfile(folder, baseFileName);
imwrite(img, fullFileName);
frameCounter = frameCounter + 1;
Put the above in a loop that keeps snapping and saving images until you stop it somehow.

댓글 수: 3

Lloyd
Lloyd 2013년 9월 11일
I can't seem to make it work Sir/Ma'am Image Analyst. I just need a simple 1st hit the pushbutton and 1 image1.jpg, 2nd hit and image2.jpg....
I'm sort of troubled with the sequentially numbered process and sequence processing with the directory.
I used your code:
frameCounter = 1; %initialize filename increment vid = videoinput('winvideo',2); set(vid, 'ReturnedColorSpace', 'RGB'); img = getsnapshot(vid); imshow(img); baseFileName = sprintf('Frame %d.jpg', frameCounter); fullFileName = fullfile('C:\Users\Sony Vaio\Documents\Task\0.1 Systems\System 1 - edited\Appendix\images', baseFileName); imwrite(img, fullFileName); frameCounter = frameCounter + 1;
It's stuck too at Frame1 and keeps on overwriting Frame1 :(
I'm still new and learning Sir/Ma'am. I'm sorry for troubling you..if ever I am :(
Lloyd
Lloyd 2013년 9월 11일
Sir/Ma'am Image Analyst,
I saw your answer somewhere here about the global continueSnapping;
can I use that to call the whole block of code to a loop?
btw: I wanted to Accept your answer too since your answer is identical to JEFF EA but I didn't know that only 1 answer is allowed to be accepted. I'm sorry :(
Image Analyst
Image Analyst 2013년 9월 12일
So it's not in a loop - it's in a pushbutton callback. So you can either make frameCounter global or persistent, or you can get a directory listing, figure out what the highest number so far is, and then increment it (which is the most complicated and longest method but probably the most robust and reliable.)

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Deep Learning Toolbox에 대해 자세히 알아보기

질문:

2013년 9월 11일

Community Treasure Hunt

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

Start Hunting!

Translated by