You are on page 1of 38

Computer Vision and Digital

Image processing
Digital Image Representation
Outline
• Introduction to MATLAB
• Digital image representation
• Digital image thresholding
Introduction
 Computer Vision and Image Processing -> Always there is a need to
extract information from images.

 Interpret their content has been the driving factor in the development
of image processing.

 Image processing finds use in numerous sectors, including medicine,


industry, military, consumer electronics and so on.
Introducing MATLAB environment

Fig. 1: Common MATLAB working areas


4
Introducing MATLAB environment

1. Command Window

2. Command History

3. Workspace

4. Editor Window

5. HELP

6. Array Editor
5
MATLAB
 Basic Elements/Components of MATLAB

 Data Classes
Data Classes Description
uint8 8-bit unsigned integers (1 byte per element)
uint16 16-bit unsigned integers (2 bytes per element)
uint32 32-bit unsigned integers (4 bytes per element)
int8 8-bit signed integers (1 byte per element)
int16 16-bit signed integers (2 bytes per element)
int32 32-bit unsigned integers (4 bytes per element)
single Single-precision floating numbers (4 bytes per element)
double Double-precision floating numbers (8 bytes per element)
logical Values are 0 (false) and 1(true) (1 byte per element)
char Characters (2 bytes per element)
MATLAB
 M-files: Scripts and Functions

• M-file: scripts that execute a series of MATLAB commands or


statements or functions that can accept arguments (parameters)
and produce one or more output values.
• Scripts: an m-file that consists of a sequence of commands to
be interpreted and executed.
 Created using text editor and stored with a meaningful
name (.m extension) and It can be invoked from the
command line by typing its name.
• Function: an M-file that can accept arguments and produce an
Reading an Image
>> f = imread(‘filename’);
>> imshow(f);
% Remove semicolon in the first code and run it in the command
window to see the contents of the image.
>> imshow(f, [low high]);
% Displays as
black all values less than or equal to low, and
as white all values greater than or equal to high
8
Writing Image and Image Info
>> imwrite(f, ‘filename’); //Writing image
>> imfinfo(’filename’) //Image information

9
Size of an Image
>> f = imread(‘filename’);
>> size(f);

10
Using imtool()
>> f = imread(‘filename’);
>> imtool(f); //display image tool
 Inspect pixel values
 Display image information
 Crop image
 Measure distance
Note: It is also possible to use the interface under plots category!

11
Size Normalization
Resizing the size of an image:
>> f = imread(‘filename’);
>> f = imresize(f, [500 500]);
>> f = imresize(f, 2);
>> figure, imshow(f);
>> title(‘Resized Image’);
>> imsave();

12
Subplots
>> subplot(row, column, current_plot)

E.g. subplot(2,2,1);
Rotate an image
>> imrotate(image, angle)
Converting an image to gray scale image

• Gray = rgb2gray(image)

• converts RGB values to grayscale values by forming a


weighted sum of the R, G, and B components:
0.2989 * R + 0.5870 * G + 0.1140 * B
Image Thresholding (Binarization)
• Is a technique of image converting a grayscale image into a
binary image

• Im2bw(gray_image, level)
• Im2bw(gray_image, graythresh(gray_image))
• Imbinarize(gray_image, level)
Image enhancement
• Noise removal
• Histogram equalization
• Deblurring
Noise Removing
What is Noise?
It is any undesired information that contaminates an image
Types of Noise
Salt &Pepper
•It is caused by sharp, sudden disturbances in the image
•It is randomly scattered white or black (or both) pixels
•It can be modeled by random values added to an image
Gaussian (Adaptive)
•It is an idealized form of white noise, which is caused by random fluctuations
Speckle
•It is a multiplicative noise, very frequent in radar and satellite imagery
•It can be modeled by random values multiplied by pixel values and
•It is very difficult to remove
Matlab Noise
• Adding Noise on the Image
• imnoise(image,’gaussian',0.2,0.01);
• Imnoise(image,’type of noise’, mean, variance)
• imnoise(image,’salt & pepper’,0.02);
• Imnoise(image,’type of noise’, median);
• Imnoise(image, ‘speckle’,0.02);
• Imnoise(image,’type of noise’, median);
Matlab Noise Removal
• There are many noise removing techniques which used to restore the noised
images.
 Median Filter: medfilt2(image)
• It is best to remove salt and pepper noise
 Mean/Gaussian Filter:
 filter2(fspecial(‘average’,[5 5]),img)/255 and
 imfilter(img,fspecial(‘average’,[5 5]))
 Adaptive Filter: wiener2()
• It is best to remove Gaussian noise
• Most noise filtering mechanism are using for two dimensions array
Histogram of an image
• An image histogram is a graphical representation of the distribution of
intensity values in an image.
• It is created by counting the number of pixels in each intensity level,
and then plotting the counts as a bar graph.
• The histogram can be used to identify the overall brightness of an
image, as well as the distribution of brightness values.

• Imhist(image)
Histogram equalization
1. Calculate the histogram of the image.
2. Create a cumulative distribution function (CDF) from the histogram.
3. Apply a function to the CDF to create a new histogram.
4. Use the new histogram to create a new image.

In matlab, you can use


• histeq(image)
Contrast adjustment
• histeq(image):
• Modifies histogram of the output image approximately matches a
specified histogram (uniform distribution by default).
• adapthisteq(image):
• contrast-limited adaptive histogram equalization
• It operates on small data regions (tiles) rather than the entire image.
• imadjust(image):
• Only 1% of the data is saturated at low and high intensities of the input
data.
Deblurring
• Removing a the blur of an image
• Adding blur
• MotionBlur = fspecial('motion',21,11);
• imageDouble = im2double(image);
• blurred = imfilter(imageDouble, MotionBlur,'conv','circular’);
• Removing a blur using wiener filter
• wnr1 = deconvwnr(blurred,MotionBlur);
• Morphological image processing
• Image segmentation
• Matlab GUI
Morphological image processing
• Mathematical morphology is a tool for extracting image components
that are useful in the representation and description of region shape,
such as boundaries, skeletons, and the convex hull.

• Basic Morphological Operations


• Erosion and Dilation
• Opening and Closing
objects and structuring elements
Erosion and Dilation
• Erosion (Shrinking)
• imerode(~image, structuring-element)

• strel(‘shape’,size)
• Square, 3
Erosion and Dilation
• Dilation
• Dilation “grows” or “thickens” objects in a binary image.

• imdilate(image, structuring-element)

• strel(‘shape’,size)
• Square, 3
Opening and Closing
• Opening
• The interpretation that the opening of A by B is the union of all the
translations of B such that B fits entirely within A
Opening and Closing
• Opening
• imopen(image,str_element)

• strel(‘shape’,size)
• Sphere, 3
Opening and Closing
• Closing
• Closing has a similar geometric interpretation, except that now we translate B
outside A. The closing is then the complement of the union of all translations
of B that do not overlap A.
Opening and Closing
• Opening
• imclose(image,str_element)

• strel(‘shape’,size)
• Sphere, 3
Filling hole

• Whenever there is a patch of region that needs to filled out or


removed inside a connected component
• imfill(image,’ holes’)
Image segmentation
1. Label connected components in binary image
• [label,length] = bwlabel(~image,4|8);
Image segmentation
2. Measure properties of image regions
regionprops(label, properties)
• Properties could be
• 'Area'
• 'Orientation'
• 'BoundingBox’
• 'Perimeter'
• 'Centroid’
• ‘All'
Segmentation
3. Differentiate the images and draw a bounding box

• rectangle('Position',prop(i).BoundingBox,'EdgeColor','g’);
GUI
• Get file through the UI
• uigetfile({'*.jpg;*.tif;*.png;*.gif’});
• Returns file, and path – needs to be concatenated with each other to produce
output
• Create App Through
• Home -> New -> APP-> Guide

You might also like