You are on page 1of 7

“MCT-453: Machine Vision

Image/Video acquisition, and its Conversions in MATLAB


Lab Session 3: (Open Ended Lab)
Reg#2017-MC-__303__

Reg#2017-MC-__317_

Task 01: Reading Webcam

Cameras are starting point for any real world practical machine vision application. Luckily your laptop
has a built in camera to get started with machine vision.
Write a MATLAB code to start your webcam, preview camera image, capture image and save it in a
folder.
NOTE: Code font size should be 10 pts.

Code:

https://www.mathworks.com/help/supportpkg/usbwebcams/ug/webcam.html
webcamlist
cam = webcam %to open the web camera
preview(cam)
closePreview(cam)
img = snapshot(cam); % to capture the image
imshow(img) % to display the captured image

clear('cam'); % to turn off the webcam


Use mobile camera in Matlab:

Are you interested in using your cell phone camera (a high resolution) in your engineering project?
If yes, you are at the right place! In this tutorial, we will configure mobile camera for any robotic or
vision project in Matlab.

Configure your mobile:

Install IP Webcam app from Android app store.


After successful installation, open IP Webcam app and scroll down to the Start Server option and begin
server, a video streming will start and you will see a local host address(http://192.168.8.100:8080) on
the screen.
Open an internet browser and enter above local host address.
Click on Flash and you would be able to see your mobile camera streaming.
Right-click on screen and copy image address, you would be using this address in Matlab.
https://www.mathworks.com/help/supportpkg/ipcamera/ug/ipcam.html

To watch above demo,

https://www.youtube.com/channel/UCJA5xHsVppYct6zYSKePC9w

Task 02: Reading Mobile Camera

Write a MATLAB code to start your mobile cam, preview camera image, capture image and save it in a
folder.

Code:

IP Webcam link and browsing of images from mobile phone


CODE
cam=ipcam('http://192.168.1.101:8080/video') % code copied from IP webcam server
preview(p)
closePreview(p)
img = snapshot(cam); % snapshot of image captured through mobile phone
imshow(img) % display image snapshot
set(gca,'Visible','on');

Task 03:
Write a MATLAB function to

 acquire images from webcam at camera resolution (320x240) for 60 frames.


 store them with naming 202MC01.jpg on local storage(102 is replaced with your registration
number and 01 is image number).
 Convert images frames into an avi video file with 20 fps to save on a local storage.

After successfully doing above steps, now make it a generic function as

MyImageCapture( camResolution, noFrames, videoName, storageLocation )

Upon execution,

MyImageCapture(‘320x240’, ‘60’,’machineVision’,’ D:\UET\MachineVision\’)

Code:
R=resolution, F=number of frames (60), D= Directory, Fr= Frame rate
(Separate File saved)
MC_303_317('320x240',60,'machineVision','C:\Users\hubiaijaz\Desktop\soft copies\
8th semester\Machine vision\lab 3',20)
(File saved by the name: MC_303_317)
function MyVid1 = MC_303_317(R,F,S,D,Fr)
clear cam
cam = webcam
cd ('C:\Users\hubia ijaz\Desktop\soft copies\8th semester\Machine vision\lab 3')
cam.Resolution = R;
for i=1:F % F= 60
img=snapshot(cam);
imwrite(img,['303_317MC1',num2str(i),'.jpg']); % Snapshot from image 1 to 60
end
for i=1:F
img=imread(strcat('303_317MC1',num2str(i),'.jpg'));
Myvid(:,:,:,i) = img; % Reading snapshots
end
n=VideoWriter('Video');
n.FrameRate=Fr;
open(n);
writeVideo(n,Myvid);
close(n)
end

60 images and a video will be stored in given directory.

2 Snapshot of your Images

Image 18 Image 55
60 Images

Video screenshot

Task 04: Video to Image conversion and Image to video conversion


MATLAB have a video traffic.avi in its directory, Write a code to save video frames as images in a
folder, then convert back all images to a video save it in a folder.

Code:

v=VideoReader('traffic.avi'); % to read the video from the directory


cd ('C:\Users\hubia ijaz\Desktop\soft copies\8th semester\Machine vision\lab 3')
% directory to save images from video
n=0;
while hasFrame(v)
n=n+1;
img=readFrame(v);
imwrite(img,strcat('traffic',num2str(n),'.png'));
end
for i=1:120
img=imread(strcat('traffic',num2str(i),'.png'));
Myvid(:,:,:,i) = img;
end
n=VideoWriter('TrafficVideo');
n.FrameRate=20; % frames / sec in video
open(n);
writeVideo(n,Myvid);
close(n)
Figure: 120 images and video in mentioned directory

Guidelines:

You can make a group of two students from same section. Try to Code efficiently with minimum
number of lines. Report your own results as Plagiarism can leads to zero marks.

Due date: Respective lab session 03

Rumi: Write down one famous teaching, or poem verse of Rumi from which you are influenced!

“ When the world pushes you to your knees, you’re in the perfect position to pray.”

Rumi

You might also like