Take Snapshots Using WebCam in MATLAB

In this post, I am going,
  1. open webcam
  2. capture image
  3. save the captured image using MATLAB.

As the first step, we want to install the support tool. So we have to click "Add-Ons." After that, we have to select "Get Hardware Support Packages." Next, we have to search for MATLAB Support Package for USB Webcams and install it. (before installing Add-Ons, you want to log in to MATLAB) After installing the support tool, You can use these codes.

captureimg.m
clear;
cam = webcam; %start webcam
cam.Resolution = '640x360'; %resize the webcam resolution
preview(cam); %preview the webcam
pause(5); %pause the webcam
img = snapshot(cam); %take snapshot
closePreview(cam); %close the preview screen
imshow(img); %show the image
savecapture(img); %call function savecapture
clear; %stop the webcam

savecapture.m
function savecapture(img)
    persistent k %static value k for naming variables 
    if isempty(k)
        k = 0;
    end
    baseFileName = sprintf('img_web(%d).jpg', k); %set variable name
    imwrite(img,baseFileName); %save image in current file location
    k=k+1;

Comments

Popular posts from this blog

C# Analog Clock

Basic Prolog (List)

SOLID Principles