Got Questions? Get Answers.
Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
imwrite by loop

Subject: imwrite by loop

From: Eng_Bandar

Date: 5 Oct, 2010 12:24:05

Message: 1 of 17

Hi all,
I want take images from webcam and save them. problem if I want take more images

this is my code

vid = videoinput('winvideo', 1);
preview(vid);
for i=1:5
data= getsnapshot(vid);
pause(1);
imshow(data);
imwrite(data,'data1.jpg');
end

In this code I want save only five images How do this ?
How make loop for this ?

Thank you.

Subject: imwrite by loop

From: Grzegorz Knor

Date: 5 Oct, 2010 12:35:22

Message: 2 of 17

"Eng_Bandar " <nnnnhss@hotmail.com> wrote in message <i8f5d5$5ra$1@fred.mathworks.com>...
> Hi all,
> I want take images from webcam and save them. problem if I want take more images
>
> this is my code
>
> vid = videoinput('winvideo', 1);
> preview(vid);
> for i=1:5
> data= getsnapshot(vid);
> pause(1);
> imshow(data);
> imwrite(data,'data1.jpg');
> end
>
> In this code I want save only five images How do this ?
> How make loop for this ?
>
> Thank you.

Read this thread.

Grzegorz

Subject: imwrite by loop

From: Cris Luengo

Date: 5 Oct, 2010 12:37:04

Message: 3 of 17

"Eng_Bandar " <nnnnhss@hotmail.com> wrote in message <i8f5d5$5ra$1@fred.mathworks.com>...
> Hi all,
> I want take images from webcam and save them. problem if I want take more images
>
> this is my code
>
> vid = videoinput('winvideo', 1);
> preview(vid);
> for i=1:5
> data= getsnapshot(vid);
> pause(1);
> imshow(data);
> imwrite(data,'data1.jpg');
> end
>
> In this code I want save only five images How do this ?
> How make loop for this ?
>
> Thank you.

imwrite(data,['data',num2str(i),'.jpg']);

Subject: imwrite by loop

From: Grzegorz Knor

Date: 5 Oct, 2010 13:33:20

Message: 4 of 17

"Grzegorz Knor" <gknor@o2.pl> wrote in message <i8f62a$ie3$1@fred.mathworks.com>...
> Read this thread.
>
> Grzegorz

Sorry, I forgot a link:
http://matlabwiki.mathworks.com/MATLAB_FAQ#How_can_I_process_a_sequence_of_files.3F

Grzegorz

Subject: imwrite by loop

From: Eng_Bandar

Date: 5 Oct, 2010 13:54:04

Message: 5 of 17

"Cris Luengo" <cris.luengo@google.for.my.name.to.contact.me> wrote in message <i8f65g$ohm$1@fred.mathworks.com>...
> "Eng_Bandar " <nnnnhss@hotmail.com> wrote in message <i8f5d5$5ra$1@fred.mathworks.com>...

> imwrite(data,['data',num2str(i),'.jpg']);

WoW, Thank you so much for helping it is work perfectlly.
Now How can I put these images in certain folder and How make loop until webcam stop. I want take images if webcam run how do this?

Thank you again.

Subject: imwrite by loop

From: Cris Luengo

Date: 5 Oct, 2010 15:05:24

Message: 6 of 17

"Eng_Bandar " <nnnnhss@hotmail.com> wrote in message <i8fals$n31$1@fred.mathworks.com>...
> "Cris Luengo" <cris.luengo@google.for.my.name.to.contact.me> wrote in message <i8f65g$ohm$1@fred.mathworks.com>...
> > "Eng_Bandar " <nnnnhss@hotmail.com> wrote in message <i8f5d5$5ra$1@fred.mathworks.com>...
>
> > imwrite(data,['data',num2str(i),'.jpg']);
>
> WoW, Thank you so much for helping it is work perfectlly.
> Now How can I put these images in certain folder and How make loop until webcam stop. I want take images if webcam run how do this?
>
> Thank you again.

I don't know how you can tell from MATLAB that the webcam is turned off. I don't have that 'getsnapshot' function. If it were to return an empty array when it's off, for example, you could do:

ii = 1;
while 1
   data= getsnapshot(vid);
   if isempty(data)
      break;
   end
   ...
   imwrite(data,['data',num2str(ii),'.jpg']);
   ii = ii+1;
end

Subject: imwrite by loop

From: Eng_Bandar

Date: 5 Oct, 2010 19:42:08

Message: 7 of 17

"Cris Luengo" <cris.luengo@google.for.my.name.to.contact.me> wrote in message <i8ferk$6ho$1@fred.mathworks.com>...
> "Eng_Bandar " <nnnnhss@hotmail.com> wrote in message <i8fals$n31$1@fred.mathworks.com>...
> > "Cris Luengo" <cris.luengo@google.for.my.name.to.contact.me> wrote in message <i8f65g$ohm$1@fred.mathworks.com>...
> > > "Eng_Bandar " <nnnnhss@hotmail.com> wrote in message <i8f5d5$5ra$1@fred.mathworks.com>...
> >
> > > imwrite(data,['data',num2str(i),'.jpg']);
> >
> > WoW, Thank you so much for helping it is work perfectlly.
> > Now How can I put these images in certain folder and How make loop until webcam stop. I want take images if webcam run how do this?
> >
> > Thank you again.
>
> I don't know how you can tell from MATLAB that the webcam is turned off. I don't have that 'getsnapshot' function. If it were to return an empty array when it's off, for example, you could do:
>
> ii = 1;
> while 1
> data= getsnapshot(vid);
> if isempty(data)
> break;
> end
> ...
> imwrite(data,['data',num2str(ii),'.jpg']);
> ii = ii+1;
> end

Thank you so much
Do you know how can I save these images in certain folder?

Subject: imwrite by loop

From: Sean

Date: 5 Oct, 2010 19:53:04

Message: 8 of 17


> > imwrite(data,['data',num2str(ii),'.jpg']);

> Thank you so much
> Do you know how can I save these images in certain folder?

Add the folder path to your file name e.g:
%
pathname = '/Volumes/ext1/my_folder/';
imwrite(data,[pathname, 'data',num2str(ii),'.jpg']);
%
That was a sample path for a Mac change it to whatever yours is.

Subject: imwrite by loop

From: Eng_Bandar

Date: 6 Oct, 2010 08:08:04

Message: 9 of 17

"Sean " <sean.dewolski@nospamplease.umit.maine.edu> wrote in message <i8fvn0$npt$1@fred.mathworks.com>...
>
> > > imwrite(data,['data',num2str(ii),'.jpg']);
>
> > Thank you so much
> > Do you know how can I save these images in certain folder?
>
> Add the folder path to your file name e.g:
> %
> pathname = '/Volumes/ext1/my_folder/';
> imwrite(data,[pathname, 'data',num2str(ii),'.jpg']);
> %
> That was a sample path for a Mac change it to whatever yours is.

Thank you so much
It is work perfectlly.
This is my code now

vid = videoinput('winvideo', 1);
preview(vid);
for i=1:5
data= getsnapshot(vid);
imshow(data);
pathname = 'C:\Users\Bandar\Desktop\1111';
imwrite(data,[pathname, 'data',num2str(i),'.jpg']);
end

where 1111 name of folder but images not save in it, it is saving in Desktop Why?
And also name of each image chagned to 1111data1 like this, Why?

Thanks guys for your helping

Subject: imwrite by loop

From: ImageAnalyst

Date: 6 Oct, 2010 12:01:23

Message: 10 of 17

I wouldn't do it that way. This is the way I'd do it (untested),
because this way lets you not have to worry about trailing slashes
(which was your problem).

% Define the folder outside of the loop.
pathName = 'C:\Users\Bandar\Desktop\1111';
% Create it if it doesn't exist.
if ~exist(pathName, 'dir')
  mkdir(pathName);
  % This will create to the full depth of the path.
  % Upper folder levels don't have to exist yet.
end

% Change your loop variable from i to frameNumber . i is not a good
choice.
for frameNumber = 1 : 5
  data = getsnapshot(vid);
  imshow(data);
  baseFileName = sprintf('data%d.jpg', frameNumber);
  fullFileName = fullfile(pathName, baseFileName);
  imwrite(data, fullFileName);
end % of the for loop

Subject: imwrite by loop

From: Eng_Bandar

Date: 7 Oct, 2010 22:45:05

Message: 11 of 17

ImageAnalyst <imageanalyst@mailinator.com> wrote in message <e4bee7cc-fba2-4fe9-9e3e-629bf6d0b8bc@p26g2000yqb.googlegroups.com>...
> I wouldn't do it that way. This is the way I'd do it (untested),
> because this way lets you not have to worry about trailing slashes
> (which was your problem).
>
> % Define the folder outside of the loop.
> pathName = 'C:\Users\Bandar\Desktop\1111';
> % Create it if it doesn't exist.
> if ~exist(pathName, 'dir')
> mkdir(pathName);
> % This will create to the full depth of the path.
> % Upper folder levels don't have to exist yet.
> end
>
> % Change your loop variable from i to frameNumber . i is not a good
> choice.
> for frameNumber = 1 : 5
> data = getsnapshot(vid);
> imshow(data);
> baseFileName = sprintf('data%d.jpg', frameNumber);
> fullFileName = fullfile(pathName, baseFileName);
> imwrite(data, fullFileName);
> end % of the for loop

Thank you so much. really you are clever.

Subject: imwrite by loop

From: Eng_Bandar

Date: 7 Oct, 2010 23:05:04

Message: 12 of 17

"Cris Luengo" <cris.luengo@google.for.my.name.to.contact.me> wrote in message <i8ferk$6ho$1@fred.mathworks.com>...
> "Eng_Bandar " <nnnnhss@hotmail.com> wrote in message <i8fals$n31$1@fred.mathworks.com>...
> > "Cris Luengo" <cris.luengo@google.for.my.name.to.contact.me> wrote in message <i8f65g$ohm$1@fred.mathworks.com>...
> > > "Eng_Bandar " <nnnnhss@hotmail.com> wrote in message <i8f5d5$5ra$1@fred.mathworks.com>...
> >
> > > imwrite(data,['data',num2str(i),'.jpg']);
> >
> > WoW, Thank you so much for helping it is work perfectlly.
> > Now How can I put these images in certain folder and How make loop until webcam stop. I want take images if webcam run how do this?
> >
> > Thank you again.
>
> I don't know how you can tell from MATLAB that the webcam is turned off. I don't have that 'getsnapshot' function. If it were to return an empty array when it's off, for example, you could do:
>
> ii = 1;
> while 1
> data= getsnapshot(vid);
> if isempty(data)
> break;
> end
> ...
> imwrite(data,['data',num2str(ii),'.jpg']);
> ii = ii+1;
> end

Thank you man it is very good. It work perfectlly.

Subject: imwrite by loop

From: Eng_Bandar

Date: 7 Oct, 2010 23:14:03

Message: 13 of 17

Now guys every thing is ok with folllowing code
_______________
vid = videoinput('winvideo', 1);
preview(vid);
% Define the folder outside of the loop.
pathName = 'C:\Users\Bandar\Desktop\1111';
% Create it if it doesn't exist.
if ~exist(pathName, 'dir')
  mkdir(pathName);
  % This will create to the full depth of the path.
  % Upper folder levels don't have to exist yet.
end

% Change your loop variable from i to frameNumber . i is not a good
%choice.
frameNumber=1;
while 1
    data= getsnapshot(vid);
    if isempty(data)
      break;
    else
        imshow(data);
        baseFileName = sprintf('data%d.jpg', frameNumber);
        fullFileName = fullfile(pathName, baseFileName);
        imwrite(data, fullFileName);
        frameNumber=frameNumber+1;
    end
end
_______________

I run it and every thing OK but if I use stop(vid) still work ( not camera but capture not stop ) I want it stop if I stop camera How do this ?
this is last step

Thank you for all

Subject: imwrite by loop

From: Eng_Bandar

Date: 7 Oct, 2010 23:24:04

Message: 14 of 17

I so sorry for this big mistake I put
data= getsnapshot(vid);
in first line in endless loop of course it is not stop ( hahaha )
sorry I will try to solve it and also I need your help you are very nice

Thank you again

Subject: imwrite by loop

From: Image Analyst

Date: 8 Oct, 2010 01:49:04

Message: 15 of 17

Well, how do you want to exit/stop the loop? When the user hits a key? When you get to a certain number of iterations? When you check a checkbox, or push a button? Some other way?

Subject: imwrite by loop

From: Eng_Bandar

Date: 8 Oct, 2010 08:42:04

Message: 16 of 17

"Image Analyst" <imageanalyst@mailinator.com> wrote in message <i8ltag$t5e$1@fred.mathworks.com>...
> Well, how do you want to exit/stop the loop? When the user hits a key? When you get to a certain number of iterations? When you check a checkbox, or push a button? Some other way?

Thank you
I want it by push button (In GUI)

Subject: imwrite by loop

From: ImageAnalyst

Date: 8 Oct, 2010 12:37:17

Message: 17 of 17

On Oct 8, 4:42 am, "Eng_Bandar " <nnnn...@hotmail.com> wrote:
> "Image Analyst" <imageanal...@mailinator.com> wrote in message <i8ltag$t5...@fred.mathworks.com>...
> > Well, how do you want to exit/stop the loop?  When the user hits a key?  When you get to a certain number of iterations?  When you check a checkbox, or push a button?  Some other way?
>
> Thank you
> I want it by push button (In GUI)

------------------------------------------------
One way to do it is to set a global variable when you push the button,
and have the loop check for it.
Button callback says
global continueSnapping ;
continueSnapping = false;

and your loop says
global continueSnapping ;
continueSnapping = true;
while continueSnapping
% snap images
end

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us