Friday, 15 June 2012

On 07:41 by Unknown in    No comments

Here in this post I discuss about how to connect MATLAB? And taking images from Webcam?
So first of all we need a videobject of the webcam video. Here 
obj = videoinput('winvideo',1)” create the videoobject. The description of this line is discussed next.
The winvideo is a video adapter for webcam. And videoinput creates an object of webcam through winvideo adapter, & save it in obj variable. One another thing is the definition of webcam. It is possible that one have more than one webcams. Since the sequence number of webcam defines which webcam you want to connect.  1 is for internal system webcam or 1st webcam. If someone has two webcam & want to connect the 2nd webcam to MATLAB then it will be 2.
After creation the videoobject now I call this object and run the live video through “preview (obj)” command. We have live video, now Images can take easily from this video, “getsapshot(obj)” function is used for this purpose. We can easily convert this image frame to grayscale image through function “rgb2gray(frame)”.  At almost all cases the same size images needed for further procession, since the images are resized to 64 by 64 size. 64 by 64 is so small size, you can resize it to 128 by 128, 320 by 440 etc, this process is done through “imresize(file_name,[128, 128])”.  We can show this image by “imshow(frame)” function. But only the first image will show.
We can also save this image to the system directory through “imwrite(file-name, directory, extention-type-of-image)” function. In the code “int2str(k)” is used to convert the integer value of k to string type, to save it in a directory.
Loop while defines the number of images one can want to take from video. it will be decide by environment and the purposes where the program is used. Here I take 5 images from this video which is done through the highest value of k, “if (k==4)”. Now I pause the first image to the screen to show first image, close the image, & then delete the videoobject, to save memory.

%% Create a video object to the webcame of system.

obj = videoinput('winvideo',1);
preview(obj);
k = 0;
  while(true)
    frame = getsnapshot(obj);
    frame=rgb2gray(frame);

   % Resize Image Size to desired size. I resized it to 64 by 64

    frame=imresize(frame, [64 64]);
    imshow(frame);
    k = k+1;

    % Save the Images to a directory

imwrite(frame,'F:\ImageProcessing\studymaterial\PROJECT\OurProjet\Detection_Queue\image' , int2str(k),'.jpeg']);



    % k defines number of Images take from video,I take 5 Images.

     if(k==4)
         break;
     end  
  end

%% This will remove the video object permanently
% pause will still the image on the screen

pause(1);

% This will remove the showing image from memory

close;

% This will permanently delete the video object from memory

delete(obj);

Live Video Stream
First Image Captured from video
All 4 Images Taking from Video


0 comments:

Post a Comment