ISRA UNIVERSITY
FACULTY OF ENGINEERING, SCIENCE & TECHNOLOGY
Lab-08
Lab Experiment No. 8
Name: _____________________________________________________ Roll No: ______________
Score:_________________ Signature:__________________________________ Date:___________
Introduction to Image Processing & Interfacing Camera in MATLAB
PERFORMANCE OBJECTIVE:
After the successful completion of this lab, students will be able to:
Interface and use external camera in MATLAB
Capture live image through matlab code and Simulink.
Load previously stored image through code and Simulink.
Manipulate RGB and grayscale images.
Record video frames through matlab code and Simulink.
Saving and playing an AVI file through matlab code and Simulink..
LAB REQUIREMENTS:
PC with Windows XP/2007,Operating System.
MATLAB 2007,2009,2011 or latest with video &Image processing and image acquisition
toolbox
DISCUSSION:
8.3.1 RGB Images
An RGB image, sometimes referred to as a true-color image, is stored in MATLAB as an m-by-n-by-3
data array that defines red, green, and blue color components for each individual pixel. RGB images
do not use a palette. The color of each pixel is determined by the combination of the red, green, and
blue intensities stored in each color plane at the pixel's location. Graphics file formats store RGB
images as 24-bit images, where the red, green, and blue components are 8 bits each. This yields a
potential of 16 million colors. The precision with which a real-life image can be replicated has led to
the commonly used term true-color image. An RGB array can be of class double, uint8, or uint8. In
an RGB array of class double, each color component is a value between 0 and 1. A pixel whose color
components are (0,0,0) is displayed as black, and a pixel whose color components are (1,1,1) is
displayed as white. The three color components for each pixel are stored along the third dimension
of the data array. For example, the red, green, and blue color components of the pixel (10,5) are
stored in RGB(10,5,1), RGB(10,5,2), and RGB(10,5,3), respectively.
Figure 8.1 depicts an RGB image of class double.
Designed By: Engr. Irshad Rahim Memon
Department: Computer Science
ISRA UNIVERSITY
Lab-08
FACULTY OF ENGINEERING, SCIENCE & TECHNOLOGY
_________________________________________________________________________________
Figure 8.1: RGB Image
8.3.2
MATLAB functions
Image Processing Toolbox
Imread:
The imread function reads an image from any supported graphics image file format, in any of the
supported bit depths. Most image file formats use 8 bits to store pixel values. When these are read
into memory, MATLAB stores them as class uint8. For file formats that support 16-bit data, PNG and
TIFF, MATLAB stores the images as class uint16.
For example, this code reads an RGB image into the MATLAB workspace as the variable RGB.
RGB = imread('football.jpg');
Imshow:
The imshow function displays an image.
imshow(RGB);
rgb2gray:
The rgb2gray function converts an RGB image or colormap to grayscale.
I = rgb2gray(RGB)
Image Acquisition Toolbox
Imaqhwinfo:
Returns information about available image acquisition hardware.
out = imaqhwinfo
Designed By: Engr. Irshad Rahim Memon
Department: Computer Science
ISRA UNIVERSITY
Lab-08
FACULTY OF ENGINEERING, SCIENCE & TECHNOLOGY
_________________________________________________________________________________
returns a structure, out, that contains information about the image acquisition adaptors available on
the system. An adaptor is the interface between MATLAB and the image acquisition devices
connected to the system. The adaptor's main purpose is to pass information between MATLAB and
an image acquisition device via its driver.
out = imaqhwinfo('adaptorname')
returns a structure, out, that contains information about the specified adaptor. The information
returned includes adaptor version and available hardware for the specified adaptor. To get a list of
valid adaptor names, use the imaqhwinfo syntax.
out = imaqhwinfo('adaptorname', deviceID)
returns information for the device identified by the numerical device ID deviceID. The deviceID can
be a scalar or a vector. If deviceID is a vector, out is a 1-by-N structure array where N is the length of
deviceID.
Videoinput:
The videoinput function creates a video input object.
Syntax:
obj = videoinput('adaptorname')
obj = videoinput('adaptorname', deviceID)
'adaptorname' - String specifying the device adaptor obj is associated with. Valid values can be
determined by using the imaqhwinfo function.
'deviceID' - Numerical device identifier. If deviceID is not specified, the first available device ID is
used.
Preview:
Activate a live image preview window.
Syntax:
preview(obj)
Figure 8.2: Components of a Video Preview Window
getsnapshot:
Designed By: Engr. Irshad Rahim Memon
Department: Computer Science
ISRA UNIVERSITY
Lab-08
FACULTY OF ENGINEERING, SCIENCE & TECHNOLOGY
_________________________________________________________________________________
Immediately return a single image frame.
Syntax:
frame = getsnapshot(obj)
immediately returns one single image frame, from the video input object obj. The frame of data
returned is independent of the video input object.
closepreview:
Close image preview windows.
Syntax:
closepreview(obj)
closes the image preview window associated with image acquisition object obj. closepreview
closes all image preview windows for all image acquisition objects.
Recording a video through matlab code
start:
syntax:
start(obj)
obtains exclusive use of the image acquisition device associated with the video input object OBJ and
locks the device's configuration. Starting an object is a necessary first step to acquire image data.
getdata:
syntax:
data =getdata(obj)
Return acquired image frames to MATLAB workspace.
Imaqmontage:
Syntax:
imaqmontage(frames)
frames can be any data set returned by getdata, peekdata, or getsnapshot.
Imaqmontage is used to view multiple frames at once or to Display a sequence of image frames as a
montage.
Implay:
syntax:
implay(‘filename’)
opens the IMPLAY movie player, displaying the content of the file specified by 'filename'. The file
can be an AVI file.
Delete:
Syntax:
delete(obj)
Deletes file or object.
Designed By: Engr. Irshad Rahim Memon
Department: Computer Science
ISRA UNIVERSITY
Lab-08
FACULTY OF ENGINEERING, SCIENCE & TECHNOLOGY
_________________________________________________________________________________
Example 8.1: Create an avi file using following code and observe in the current directory that it is
saved there. Also play the avi file and check its quality after recording.
obj=videoinput('winvideo')
kk = uint8(zeros(120,160,3,20));
for frame=1:50
[kk(:,:,:,frame)] = getsnapshot(obj);
end
mov = immovie(kk);
implay(mov);
movie2avi(mov, 'kk2.avi','fps',5);
8.3.4 Image and video acquisition using Simulink
For image and video acquisition and their visualization we will use’ image acquisition toolbox’ and
‘video and image processing blockset’
Example 8.2: Construct the following simulink model as shown in Figure 8.2 to load a previously
stored image in a variable a and display it on a video display.
a
Image
Image From Workspace
Image
To Video
Display
To Video Display
Figure 8.3: Simulink model for loading and displaying stored image
Example 8.3 Construct the simulink model as shown in fig dash to capture live image or video using’
from video device’ block and display the captured data on dispaly device.
Figure 8.4: Simulink model for capturing and displaying live image
Designed By: Engr. Irshad Rahim Memon
Department: Computer Science
ISRA UNIVERSITY
Lab-08
FACULTY OF ENGINEERING, SCIENCE & TECHNOLOGY
_________________________________________________________________________________
From Video Input block
The From Video Device block lets you acquire image and video data streams from image acquisition
devices, such as cameras and frame grabbers, in order to bring the image data into a Simulink
model. The block also lets you configure and preview the acquisition directly from Simulink.
The From Video Device block opens, initializes, configures, and controls an acquisition device. The
opening, initializing, and configuring occur once, at the start of the model's execution. During the
model's run time, the block buffers image data, delivering one image frame for each simulation time
step.
The block has no input ports. You can configure the block to have either one output port, or three
output ports corresponding to the uncompressed color bands, such as red, green, and blue, or Y, Cb,
Cr. Figure 8.5 shows both configurations.
HP Webcam-101
YUY2_160x120
i nput1
From Vi deo Device
HP Webcam-101 Y
YUY2_160x120 Cb
i nput1
Cr
From Vi deo Device1
Figure 8.5: From video Device block with one port and 3 ports configuration
Dialog Box
In the Source Block Parameters dialog box, the options that show up are dependent on the device
you are using. Figure 8.6 illustrates the fields that may appear if your device supports camera files
and hardware triggering.
Figure 8.6: Source Block Parameters: From Video Device1
Designed By: Engr. Irshad Rahim Memon
Department: Computer Science
ISRA UNIVERSITY
Lab-08
FACULTY OF ENGINEERING, SCIENCE & TECHNOLOGY
_________________________________________________________________________________
Example 8.4 Construct the simulink model as shown in figure 8.7 to record an avi file directly or live
from video camera. After recording observe and play the avi file.
Y
HP Webcam-101
YUY2_640x480 Cb
input1
Cr
From Video Device
R
G
B
output.avi
Audio
To Multimedia File
Figure 8.7: Simulink model for recording live video as an avi file
8.4
Exercises
Exercise 8.1
Design a GUI in MATLAB consisting of 4 push buttons. The task of each push button is stated here:
1. Push button 1 should activate the camera attached to your work station and display the
video feed.
2. Push button 2 should capture an image from the video feed and close the video preview.
3. Push button 3 should convert the captured color image into grayscale and display it.
4. Push button 4 should capture a live video of 10 seconds.
5. Push button 5 should display the frames as montage.
6. Push button 6 should display the recorded video.
7. Push button 7 should clear the screen, close all the existing figures, remove the variables
from the memory and exit.
Please attach the hardcopy of your code along with handout for grading.
Designed By: Engr. Irshad Rahim Memon
Department: Computer Science
ISRA UNIVERSITY
Lab-08
FACULTY OF ENGINEERING, SCIENCE & TECHNOLOGY
_________________________________________________________________________________
8.4
Review Questions
1. What is the purpose of ‘getsnapshot’ function in MATLAB?
______________________________________________________________________
______________________________________________________________________
2. What is the purpose of ‘Imaqmontage’ function in MATLAB?
______________________________________________________________________
______________________________________________________________________
3. What are various parameters and their functions in ‘From Video Device1’ Block?
______________________________________________________________________
______________________________________________________________________
4. What is the purpose of immovie, implay and movie2avi functions in MATLAB?
______________________________________________________________________
______________________________________________________________________
______________________________________________________________________
Designed By: Engr. Irshad Rahim Memon
Department: Computer Science
ISRA UNIVERSITY
FACULTY OF ENGINEERING, SCIENCE & TECHNOLOGY
Lab - 08
Designed By: Engr. Irshad Rahim Memon
-9-
Department: Computer Science
© Copyright 2026 Paperzz