Total Pageviews

Translate

October 7, 2016

Image Acquisition Application Development Using MATLAB...

by 4hathacker  |  in Image Processing at  6:43 PM

Hello Everyone...!!!

I have discussed about face detection in the previous article. Hope you liked the content. In this article, I have tried to cover a gentle introduction of image acquisition using webcam. 


Image Acquisition is the first and foremost step for image processing. Because after this we get an actual image in a respective format to go for the analysis. The way this is done varies significantly from one technology to another. Cameras, whether in the mobile phone or in the PC as a webcam, are the devices used to capture images. These devices contains image sensors which convert EM signals into electrical signals that can be processed, displayed, and interpreted as images. Two technologies that are prominent for image acquisition are CCDs (Charge Coupled Devices) and CMOS (Complementary Metal Oxide Semiconductor) Technologies.

Coming again to MATLAB, I am using my Lenovo Easy Cam for capturing several frames in real time and save them in a folder. Firstly, you have to check for the installed adaptors using the imaqhwinfo command on Command Window. It shows results like given below.

>> imaqhwinfo

ans = 

    InstalledAdaptors: {'winvideo'}
        MATLABVersion: '8.3 (R2014a)'
          ToolboxName: 'Image Acquisition Toolbox'
       ToolboxVersion: '4.7 (R2014a)'


The adaptor shown is 'winvideo' which we would use for acquisition. Some other could be seen as 'matrox', etc. Then use imaqtool command to see for the image acquisition toolbox and camera showing properly.



We can obtain the device ID and available formats for capturing live feed from the window appeared at the bottom left. Further use the comment section to understand the code. In the final section of code, I managed to use the sequence of image frames captured to show in video like feature using implay().


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%         Name: Nitin Sharma
%%%%%%         Code: Funky_cam_app
%%%%%%         MATLAB Version: R2014a
%%%%%%         Toolbox Required: Image Acquisition Toolbox
%%%%%%          Utility: Captures a number of images at a predefined rate set by 
%%%%%%          user in 640x480 size that can be used as a training data
%%%%%%          set for face detection and recognition, and uses implay() 
%%%%%%          for visualisation of sequence of images.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

function image_aquisition     %%% my function name
    tic                                         %%% tic and toc for elapsed time calculation
    
    %%%                vid object created using VIDEOINPUT command... 
    %%%                Use imaqhwinfo and imaqinfo to detect for the present adaptors
    %%%                WINVIDEO is one of the ADAPTORNAME

    vid = videoinput('winvideo',1,'YUY2_640x480');

    %%% Configure the object for manual trigger mode.

    triggerconfig(vid, 'manual');
    
    % Now that the device is configured for manual triggering, call START.
    % This will cause the device to send data back to MATLAB, but will not log
    % frames to memory at this point.
    
    start(vid)
    preview(vid);                         %%% Preview is not necessary but one can observe livefeed.
    %imagesc(snapshot);
    for counter = 1:50                  %%% 50 is the no. of snapshots/frames to be obtained.
        iTime = clock;                    %%% returns six element date and time format
        
        %%%           GETSNAPSHOT is the function that allows for quick acquisition
        %%%           of single video frame. But its not that much quick.
        %%%           It connects to device, configure it, start the acquisition,
        %%%           acquire one frame, stop acquisition and finally close the
        %%%           frame. That's why we have used manual triggering here.
        
        img = getsnapshot(vid); 
        
        ii = ycbcr2rgb(img);  %%% our ycbcr format image converted to rgb
        
        %%%          File handling for sing the images
        baseFileName = sprintf('myfaces_%d.jpg', counter);
        fullFileName = fullfile('E:\New', baseFileName);
        imwrite(ii,fullFileName,'jpg');
        
        %%%           its a practical method to make up for the time taken by
        %%%           snapshot using the etime() to calculate for the pause time 
        %%%           elapsed between the acquisition start and GETSNAPSHOT delay.
         
        elapsed = etime(clock,iTime);   
        pause(5-elapsed);    
         
    end
    
    delete(vid);             %%%            delete the vid object otherwise the webcam 
                                   %%%             still remain in acquisition mode 
    
    elapsedTime = toc
    
    % Computing the time per frame and effective frame rate.
    
    timePerFrame = elapsedTime/20
    effectiveFrameRate = 1/timePerFrame
    
    %%%           Its an additional fun added to function to play sequence of images 
    %%%           captured. Do take care of location of stored images
    
    filelocat = dir('*.jpg');                        %%% As already set to image sequence location 
    for i = 1:length(filelocat)
       images(:,:,:,i) = imread(filelocat(i).name); 
    end
    implay(images,20);                          %%% The number as second argument is the frame rate
    
end

The time elapsed, time per frame and effective frame rate are obtained at the command window. The results are given below for the images stored at desired location.

image_aquisition

elapsedTime =

  252.0720


timePerFrame =

   12.6036


effectiveFrameRate =

    0.0793



Try to use this code for gathering image frames for data analysis, machine learning or statistical tasks and do comments for improvement of code for decreasing elapsed time and increasing efficiency. 

0 comments:

Like Our Facebook Page

Nitin Sharma's DEV Profile
Proudly Designed by 4hathacker.