Vous êtes sur la page 1sur 66

JADAVPUR UNIVERSITY

SCIENCE CLUB
PRESENTS

INTRODUCTION TO IMAGE
PROCESSING USING MATLAB

Whats on the menu?

Acquainting with the MATLAB environment.


Learn the basics ideas about image processing.
Learn advanced concepts like segmentation, OCR, template
matching and feature extraction.

Images and pixels

Digital image consists of small indivisible elements of a picture


called PIXEL.
In black and white or grayscale picture the darkest black is 0 and
the brightest colour is 255. This is because each pixel is
represented by a 8bit unsigned integer number (uint8).

Some basic important commands to begin with

>>
>>
>>
>>
>>
>>
>>
>>

im=iread(flowers9.png, double, gamma, 2.2)


imshow(im)
idisp(im)
imhist(im) //Only for grayscale images
ihist(im) //For both grayscale and coloured images
about im
colorname(maroon)
bw=rgb2gray(im)

Histogram

Command: ihist(im)
The histogram gives the number of pixels in a picture for a
corresponding pixel value.

Thresholding
Initial image:

Thresholding
>> im= imread(sign.png)
>> im= double(im)
>> im= im/255
>> im2= im>0.7
The im2 is a 2 dimentional
binary matrix of datatype
logical.

Noise Removal (Salt & Pepper Noise)

An easy solution: 3x3 mean/ median


filter

Mean filter
>> im=imread('sign.png');
>> imnoisy=imnoise(im,'salt &
pepper',0.1);
>> f=fspecial('average')
f=
0.1111
0.1111
0.1111

0.1111
0.1111
0.1111

0.1111
0.1111
0.1111

>> f1=filter2(f,imnoisy);
>> imshow(uint8(f1))

Median filter
>> med=medfilt2(imnoisy);
>> imshow(med)

A median filter is more preferred over mean filtered as it


gives importance to the central tendency and tends to
neglect the effect of extremum values.

Problem solved!!!

Gaussian Noise and Gaussian Filter


>> im=imread('sign.png');
>> imn=imnoise(im,'gaussian',0.01);
>> sigma=3;
>> cutoff=ceil(3*sigma);
>>
h=fspecial('gaussian',2*cutoff+1,sigma
);
>> surf(1:2*cutoff+1, 1:2*cutoff+1, h)
>> out=conv2(double(imn),h,'same');
>> figure, imshow(uint8(out))

Wiener Filter

Command:
>> w= wiener2(imn,[5 5]);
>> imshow(w)
The Weiner Filter is much preferred over Gaussian Filter as the
Gaussian Filter blurs the image.
The Weiner Filter filters out the noise from an image by minimizing
the mean square error.

Concept of Filtering

In image processing whenever we are applying a transformation on


the picture matrix for blurring, sharpening, embossing, edgedetection, etc. we actually convolve the image matrix with a
kernel or convolution matrix or mask.
For example this simple kernel can be used for edge detection!!!
0

-1

-1

-1

-1

A 5x5 Gaussian Kernel


The Gaussian Kernel acts as a Low Pass Filter and the
frequency response of the filter is a Gaussian curve
resembling a bell-jar.

Ringing Effect in Fourier Domain


>>
>>
>>
>>
>>
>>
>>
>>
>>

[x,y]=meshgrid(-128:127,-128:127);
z=sqrt(x.^2+y.^2);
figure, imshow(uint8(z))
zt=(z<20);
figure, imshow(zt)
ztf=fftshift(fft2(zt));
ztf1=log(1+abs(ztf));
m=max(ztf1(:));
figure, imshow(im2uint8(ztf1/m))

Simple Remedy

Apply a low pass filter !!!


>> b=1./(1+(z./15).^2);
>> figure, imshow(b)
>> bf=fftshift(fft2(b));
>> bf1=log(1+abs(bf));
>> b=1./(1+(z./15).^2);
>> figure, imshow(b)
>> bf=fftshift(fft2(b));
>> bf1=log(1+abs(bf));

Edge detection

>> im = edge(I, method, threshold, direction, sigma)


This detects edges in image I, where method specifies edge
detection method used, threshold specifies sensitivity to edges,
direction specifies the direction in which the function looks for
edges: horizontally, vertically or both (goes only with Sobel and
Prewitt methods), sigmaspecifies standard deviation of the filter
(used with Canny and Laplacian of Gaussian methods).

Colour !!!
>>
im=imread(flowers8.png
);
>> about a
a [uint8] : 426x640x3
(817.9 kB)

Colour Image Indexing!!!


>>
im=imread('flowers8.png')
;
>> im(100,200,:)
ans(:,:,1) =
208
ans(:,:,2) =
154
ans(:,:,3) =
178

>>
squeeze(im(100,200,:))
ans =
208
154
178

Histogram of colour planes!!!


>> ihist(im)

Dividing Components of Coloured


Image
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>

R = im(:,:,1);
G = im(:,:,2) ;
B=im(:,:,3);
R=double(R);
G=double(G);
B=double(B);
Y=R+G+B;
r= R./Y ;
g=G./Y ;
b=B./Y;

Chromaticity Classification
In the next step let us use the
command:
>> bin=(r>0.6) & (g<0.4);
What does this do?

Bingo!!!
We just separated the
red portion in the
picture!!!

Getting details about the tomato!!!


>> m=iblobs(bin, 'area', [20 Inf], 'class', 1)
m=
(1) area=70718, cent=(158.7,120.2), theta=3.14, b/a=0.744,
class=1, label=1, touch=1, parent=1, perim=1108.0, circ=0.805
(2) area=1255, cent=(156.8,61.1), theta=-3.12, b/a=0.733, class=1,
label=2, touch=0, parent=1, perim=141.1, circ=0.882
(3) area=3683, cent=(175.6,126.8), theta=3.11, b/a=0.714, class=1,
label=3, touch=0, parent=1, perim=307.1, circ=0.546

Finding the blobs!!!


>> idisp(bin)
>> m.plot('b*')
>> m.plot_box('g')
>> [m(1).uc m(1).vc]
ans =
158.6974 120.1634

Lets Play!!!

Just 5 lines!!! Thats all!!!

Green Screen

Colour Segmentation

Object Segmentation (Nonoverlapping)

Goal: Separate the coins from the


image.

Object Segmentation Code: Labelling the coins


>>
A=imread('coins.png')
;
>> figure, imshow(A)
>> title('Original
Image');
>> B=im2bw(A);
>> figure, imshow(B)
>> C=imfill(B,'holes');
>> figure, imshow(C)

>> label=bwlabel(C);
>> max(max(label));
>> im1=(label==1);
>> figure, imshow(im1)
>> figure,
imshow(label==6)
>> figure,
imshow(label==10)

Getting back the lost money !!!


for j=1:max(max(label))
[row,col]=find(label==j);
len=max(row)-min(row)
+2;
brd=max(col)-min(col)+2;
target=uint8(zeros([len
brd]));
sy=min(col)-1;
sx=min(row)-1;

for i=1:size(row,1)
x=row(i,1)-sx;
y=col(i,1)-sy;
target(x,y)=A(row(i,1),col(i,1)
);
end
mytitle=strcat('Object
Number:',num2str(j));
figure, imshow(target);
title(mytitle);
end

EUREKA!!!

Introduction to OCR in MATLAB

EUREKA!!!

Locating A Particular Word With OCR

Result!!!

Sliding Window

Goal!!!

Supervised Learning for Pedestrian


Detection

Sliding Window

Text Detection

Understanding the text!!!

OCR Scheme
Training
Data

Pre-processing

Pre-processing

Feature
Extraction

Feature
Extraction

Model
Estimation

Classification

Test
Data

Face Detection
Basic Idea: Slide a window across image and evaluate a face
model at every location
Challenges:
Sliding window detector must evaluate tens of thousands of
location/scale combinations
Faces are rare: 010 per image

Face Detection
Face recognition is a visual pattern recognition problem
A face is a three-dimensional object subject to varying

illumination, pose, expression is to be identified based on its


two-dimensional image ( or three- dimensional images
obtained by laser scan).
A face recognition system generally consists of 4 modules detection, alignment, feature extraction, and matching.

System Architecture for Face


Detection
Insert RGB
Image

Skin
Segmentation
RGB to YCbCr
RGB to HSV
Threshold to
determine skin
region

Skin
pixels

Multi solution
iterative
template
matching

Classifier

Face/Nonface

Skin Segmentation and Thresholding

Process of finding skin pixels in the image.


Reject as much non-skin segment as possible.
Detects skin regions in images and remove background part using
threshold.
Remove other body parts by using binary image processing.

Preliminary Face Detection

Electrons et Photons

Eye Detection!!!

Aye it works!!!

A Contemporary Research Topic


Detecting malignant cells from a CT scan/ X-ray image of the lungs/
marry glands/ brain!!!

The Approach
Preprocessin
g

Feature
Extraction

Classification

Median Filtering (Noise Removal)


Sharpening
Enhancement
Segmentation (Threshold/ Watershedding)
Histogram equalisation

Morphological
Colorometric
Texture

Neural Network
Support Vector Machine (SVM)
Fuzzy Clustering

Voila!!!

Other Uses!!!
Cell count
Quality control in manufacturing
Studying genome and mutation in genes
Autonomous driving vehicle

Lane Detection!!!

Template Matching
Image:

Template:

Template Matching

Template Matching

THANK YOU!!!