Download Presentation
Introduction to Computer Vision

Loading in 2 Seconds...

1 / 91

# Introduction to Computer Vision - PowerPoint PPT Presentation

Introduction to Computer Vision. Lecture 15 Morphological Processing Dr. Roger S. Gaborski. In class exercise Tuesday before Thursday’s Quiz. What is the equation for the covariance matrix? Cov (x,y) =. What is the equation for the covariance matrix?. variance (x) =

I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
Download Presentation

## Introduction to Computer Vision

An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.

- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript

### Introduction to Computer Vision

Lecture 15

Morphological Processing

Dr. Roger S. Gaborski

What is the equation for the covariance matrix?

variance (x) =

covariance (x, y) =

First calculate means for red, green and blue data

Then calculate individual covariance values

Roger S. Gaborski

Roger S. Gaborski

5

Covariance

MATLAB: cov

Diagonal: variances

Matrix is symmetric

Roger S. Gaborski

Roger S. Gaborski

6

Covariance

MATLAB: cov

Diagonal: variances

Matrix is symmetric

Roger S. Gaborski

Roger S. Gaborski

7

Image1 has the follow mean values:

Red mean value: Mr = 5,

Green mean value: Mg = 7

Blue mean value: Mb = 6.

In vector format: M = [5; 7; 6]

A new pixel P has the following values:

R= 7, G= 6, B= 1, In vector format: P= [7;6;1]

What is the equation for the Euclidean distance using the terms given above?

dEuc(P,M) =

= [ (P-M)T (P-M) ]1/2

or

= [(Pr-MR)2 + (Pg-Mg)2 + (Pb-Mb)2]1/2

dEuc(P,M) =( [7-5,6-7,1-6]*[7-5; 6-7; 1-6] )1/2 = (30)1/2 = 5.5

Need inverse of covariance matrix

Then:

DM(P,M) = [ (P-M)T C-1(P-M) ]1/2

DM(P,M) = [ (P-M)T C-1(P-M) ]1/2

Assume C-1 = [1 0 .5; 0 1 0; .5 0 1]

First part:

[7-5,6-7,1-6]*[1 0 .5; 0 1 0; .5 0 1] =

[2, -1, -5] * [1 0 .5; 0 1 0; .5 0 1] =

= [-.5 -1 -4]

Second part:

[-.5 -1 -4] *[2; -1; -5] = 20,

DM(P,M) = sqrt(20)

Agenda
• Binary morphological processing
• Erosion and dilation
• Opening and closing
• Gray-scale morphological processing
• Erosion and dilation
• Morphological gradients
• Example of Background Removal

Roger S. Gaborski

12

Introduction
• Morphology: a branch of biology dealing with the form and structure of creatures
• Mathematical morphology:
• Extract image components based on shape

e.g. boundaries, skeletons, convex hull, etc

• Image denoise

e.g. reduce noise after edge detection

• Remove Background variation

Roger S. Gaborski

13

Binary Morphological Processing

Non-linear image processing technique

Order of sequence of operations is important

Linear: (3+2)*3 = (5)*3=15

3*3+2*3=9+6=15

Non-linear: (3+2)2 = (5)2 =25 [sum, then square]

(3)2 + (2)2 =9+4=13 [square, then sum]

Based on geometric structure

Used for edge detection, noise removal and feature extraction

Used to ‘understand’ the shape/form of a binary image

Roger S. Gaborski

14

Image – Set of Pixels

Basic idea is to treat an object within an image as a set of pixels (or coordinates of pixels)

In binary images

Background pixels are set to 0 and appear black

Foreground pixels (objects) are 1 and appear white

Roger S. Gaborski

15

Chapter 9

Morphological Image Processing

A-B = A- (A∩B)

From: Digital Image Processing, Gonzalez,Woods

And Eddins

Roger S. Gaborski

16

DILATION

A

A1= A B

B

B

a

a

Object B is one point located at (a,0) A1: Object A is translated by object B

Since dilation is the union of all the translations, A B = U Atwhere the set union U is for all the b’s in B, the dilation of rectangle A in the positive x direction by a results in rectangle A1 (same size as A, just translated to the right)

Roger S. Gaborski

17

DILATION – B has 2 Elements

A

A2 A1

(part of A1 is under A2)

-a a -a a

Object B is 2 points, (a,0), (-a,0)

There are two translations of A as result of two elements in B

Dilation is defined as the UNION of the objects A1 and A2.

NOT THE INTERSECTION

Roger S. Gaborski

18

DILATION

Countless

translation

Vectors

Rounded corners

Image (A)

SE (B)

Dilation

Roger S. Gaborski

19

DILATION

Another approach

Image (A)

SE (B)

Round Structuring Element (SE) can be interpreted

as rolling the SE around the contour of the object.

New object has rounded corners and is larger by

½ width of the SE

Dilation

Roger S. Gaborski

20

DILATION

Countless

translation

vectors

Square corners

Image (A)

SE (B)

Dilation

Roger S. Gaborski

21

DILATION

Another approach

Square corners

Image (A)

SE (B)

Square Structuring Element (SE) can be interpreted

as moving the SE around the contour of the object.

New object has square corners and is larger by

½ width of the SE

Dilation

Roger S. Gaborski

22

DILATION

The shape of B determines the final shape of the dilated object. B acts as a geometric filter that changes the geometric structure of A

Roger S. Gaborski

23

Chapter 9

Morphological Image Processing

From: Digital Image Processing, Gonzalez,Woods

And Eddins

Roger S. Gaborski

24

Chapter 9

Morphological Image Processing

Image A

Image B

~ A

A U B

A ∩B

A-B = A ∩(~B)

From: Digital Image Processing, Gonzalez,Woods

And Eddins

Roger S. Gaborski

25

SE

Original Image

Translation Process

Dilated

Image

From: Digital Image Processing, Gonzalez,Woods

And Eddins

Roger S. Gaborski

26

imdilate
• IM2 = IMDILATE(IM,NHOOD) dilates the image IM, where NHOOD is a
• matrix of 0s and 1s that specifies the structuring element
• neighborhood. This is equivalent to the syntax IIMDILATE(IM,
• STREL(NHOOD)). IMDILATE determines the center element of the
• neighborhood by FLOOR((SIZE(NHOOD) + 1)/2).
• >> se = imrotate(eye(3),90)
• se =
• 0 0 1
• 0 1 0
• 1 0 0
• >> ctr=floor(size(se)+1)/2
• ctr =
• 2 2

1

2

3

1 2 3

Roger S. Gaborski

27

MATLAB Dilation Example

Im (original image)

Im2 (dialated image)

>> Im = zeros([13 19]);

>> Im(6,6:8)=1;

>> Im2 = imdilate(Im,se);

1

2

3

Roger S. Gaborski

28

1 2 3

MATLAB Dilation Example

INPUT IMAGE

DILATED IMAGE

>> I = zeros([13 19]);

>> I(6, 6:12)=1;

>> SE = imrotate(eye(5),90);

>> I2=imdilate(I,SE);

>> figure, imagesc(I)

>> figure, imagesc(SE)

>> figure, imagesc(I2)

1

2

3

4

5

SE

1 2 3 4 5

Roger S. Gaborski

29

MATLAB Dilation Example

INPUT IMAGE

DILATED IMAGE

I I2

1

2

3

4

5

>> I(6:9,6:13)=1;

>> figure, imagesc(I)

>> I2=imdilate(I,SE);

>> figure, imagesc(I2)

SE

1 2 3 4 5

Roger S. Gaborski

30

MATLAB Dilation Example

DILATED IMAGE

INPUT IMAGE

I I2

SE =

1 1 1

1 1 1

1 1 1

Roger S. Gaborski

31

Dilation and Erosion

DILATION: Adds pixels to the boundary of an object

EROSIN: Removes pixels from the boundary of an object

Number of pixels added or removed depends on size and shape of structuring element

Roger S. Gaborski

32

EROSIN

SE

Original Image

Translation Process

Eroded

Image

From: Digital Image Processing, Gonzalez,Woods

And Eddins

Roger S. Gaborski

33

MATLAB Erosion Example

ERODED IMAGE

2 pixel wide

INPUT IMAGE

>> I=zeros(13, 19); I(6:9,6:13)=1;

>> figure, imagesc(I)

>> I2=imerode(I,SE);

>> figure, imagesc(I2)

1

2

3

1 2 3

34

Roger S. Gaborski

SE = 3x1

Chapter 9

Morphological Image Processing

Original Image

Erosion with a disk of radius 10

From: Digital Image Processing, Gonzalez,Woods

And Eddins

Erosion with a disk

of radius 5

Erosion with a disk

of radius 20

35

Roger S. Gaborski

Combinations

In most morphological applications dilation and erosion are used in combination

May use same or different structuring elements

Roger S. Gaborski

36

Morphological Opening and Closing

Opening (o) of A by B

A o B = (AO B)  B; imopen(A, B)

Erosion of A by B, followed by the dilation of the result by B

Closing ()of A by B 

A B = (A  B) O B; imclose(A, B)

Dilation of A by B, followed by the erosion of the result by B

Roger S. Gaborski

37

MATLAB Function strel

strel constructs structuring elements with various shapes and sizes

Syntax: se = strel(shape, parameters)

Example:

se = strel(‘octagon’, R);

R is the dimension – see help function

Roger S. Gaborski

38

Opening of A by B  A B

• Erosion of A by B, followed by the dilation of the result by B

Erosion- if any element of structuring element overlaps with background output is 0

f (original image)

fe (eroded image)

FIRST - EROSION

>> se = strel('square', 20);

fe = imerode(f,se);

figure, imagesc(fe),title('fe')

Roger S. Gaborski

39

Dilation of Previous Result

Outputs 1 at center of SE when at least one element of SE overlaps object

fe (eroded image)

fd (dilated image)

SECOND - DILATION

>> se = strel('square', 20);

fd = imdilate(fe,se);

figure, imagesc(fd),title('fd')

Roger S. Gaborski

40

FO = imopen(f,se);

figure, imagesc(FO),title('FO')

FO (opened image)

Original Image

Roger S. Gaborski

41

What if we increased size of SE for DILATION operation??

se = 25 se = 30

se = strel('square', 30);

fd = imdilate(fe,se);

figure, imagesc(fd),title('fd')

se = strel('square', 25);

fd = imdilate(fe,se);

figure, imagesc(fd),title('fd')

Roger S. Gaborski

42

Closing of A by B  A B

Dilation of A by B

Outputs 1 at center of SE when at least one element of SE overlaps object

se = strel('square', 20);

fd = imdilate(f,se);

figure, imagesc(fd),title('fd')

Roger S. Gaborski

43

Erosion of the result by B

Erosion- if any element of structuring element overlaps with background

output is 0

Roger S. Gaborski

44

ORIGINAL

OPENING CLOSING

Roger S. Gaborski

45

Chapter 9

Morphological Image Processing

original image

opening

opening + closing

From: Digital Image Processing, Gonzalez,Woods

And Eddins

Roger S. Gaborski

46

Hit or Miss Transformation

Usage: to identify specified configuration of pixels, e.g.

isolated foreground pixels

pixels at end of lines (end points)

Definition

A B = (A B1) ∩(Ac ΘB2) A eroded by B1, intersects A complement eroded by B2 (two different structuring elements: B1 , B2)

Roger S. Gaborski

47

Hit or Miss Example

Find cross shape pixel configuration:

MATLAB Function: C = bwhitmiss(A, B1, B2)

Roger S. Gaborski

48

Original Image A and B1

A eroded by B1

Complement of Original

Image and B2

Erosion of A complement

And B2

Intersection of eroded images

Roger S. Gaborski

49

From: Digital Image Processing, Gonzalez,Woods

And Eddins

Original Image A and B1

A eroded by B1

Complement of Original

Image and B2

Erosion of A complement

And B2

Intersection of eroded images

Roger S. Gaborski

50

From: Digital Image Processing, Gonzalez,Woods

And Eddins

Original Image A and B1

A eroded by B1

Complement of Original

Image and B2

Erosion of A complement

And B2

Intersection of eroded images

Roger S. Gaborski

51

From: Digital Image Processing, Gonzalez,Woods

And Eddins

Original Image A and B1

A eroded by B1

Complement of Original

Image and B2

Erosion of A complement

And B2

Intersection of eroded images

Roger S. Gaborski

52

From: Digital Image Processing, Gonzalez,Woods

And Eddins

Original Image A and B1

A eroded by B1

Complement of Original

Image and B2

Erosion of A complement

byB2

Intersection of eroded images

Roger S. Gaborski

53

From: Digital Image Processing, Gonzalez,Woods

And Eddins

Original Image A and B1

A eroded by B1

Complement of Original

Image and B2

Erosion of A complement

And B2

Intersection of eroded images

Roger S. Gaborski

54

From: Digital Image Processing, Gonzalez,Woods

And Eddins

Hit or Miss

Have all the pixels in B1 (hits all pixels in B1), but none of the pixels in B2 (misses all pixels in B2)

More precisely, hit-and-miss operation

Roger S. Gaborski

55

Hit or Miss Example #2

Locate upper left hand corner pixels of objects in an image

Pixels that have east and south neighbors (Hits) and no NE, N, NW, W, SW Pixels (Misses)

B1 = B2 =

Don’t

Care about

SE

Roger S. Gaborski

56

Chapter 9

Morphological Image Processing

G = bwhitmiss(f, B1, B2);

Figure, imshow(g)

From: Digital Image Processing, Gonzalez,Woods

And Eddins

Roger S. Gaborski

57

bwmorph(f, operation, n)

Implements various morphological operations based on combinations of dilations, erosions and look up table operations.

f: input binary image

operation: a string specifying the desired operation

n: a positive integer specifying iteration times (default: 1)

Roger S. Gaborski

58

Example 1: thinning
• To reduce binary objects or shapes in an image to strokes whose width is 1 pixel
• Matlab implementation

>> f = imread(‘fingerprint_cleaned.tif’);

>> g = bwmorph(f, ‘thin’, 1);

>> g2 = bwmorph(f, ‘thin’, 2);

>> g3 = bwmorph(f, ‘thin’, Inf);

Roger S. Gaborski

59

Input

Thinned once

Thinned twice

Thinned until stability

Roger S. Gaborski

60

From: Digital Image Processing, Gonzalez,Woods

And Eddins

Chapter 9

Morphological Image Processing

Example 2: skeletonization

From: Digital Image Processing, Gonzalez,Woods

And Eddins

Roger S. Gaborski

62

Grayscale Morphological Processing

Similar to spatial convolution

Dilation: local maximum operator

Erosion: local minimum operator

Roger S. Gaborski

63

Original Color Image

Cropped, Grayscale and Resized

Roger S. Gaborski

64

max min

Roger S. Gaborski

65

se7 = strel('square' , 7);

max min

Roger S. Gaborski

66

Grayscale Morphological Processing

Morphological Gradient:

Dilated image – eroded image

Half-gradient by erosion:

Image – eroded image

Half-gradient by dilation:

Image – dilated image

Roger S. Gaborski

67

Morphological Gradient: Dilated image – Eroded image

Increase size of SE: more difference area in morph-gradient image

se = ones(7)

Roger S. Gaborski

69

Directional GradientsFunction of Structure Element

horizontal_gradient

= imdilate(I,seh) - imerode(I,seh);

vertical_gradient

= imdilate(I,sev) - imerode(I,sev);

Where

I : the input image

seh = strel([1 1 1]);

sev = strel([1;1;1]);

Roger S. Gaborski

71

Horizontal GradientGradient Perpendicular to Edge

Vertical edges detected using horizontal gradient

Roger S. Gaborski

72

Vertical GradientGradient Perpendicular to Edge

Horizontal edges detected using vertical gradient

Roger S. Gaborski

74

Thresholded

Binary

Grayscale

With binary processing

Removing background

resulted in loss of small stripes

Used gray scale gradient

Processing to find stripes

Morphological Processing Background Estimation

I = imread('rice.png');

imshow(I)

I = im2double(I);

NOTE VARIATION

IN BACKGROUND

>> %attempt to threshold using graythresh

lev = graythresh(I)

figure, imshow(I>lev), title('Thresholded with graythresh')

lev =

0.5137

background = imopen(I,strel('disk',15));

figure, imshow(background, [])

figure, surf(double(background(1:8:end,1:8:end))),zlim([0 1]);

set(gca,'ydir','reverse');

%subtract background estimate from original image

I2 = I - background;

figure, imshow(I2), title('Image with background removed')

level = graythresh(I2);

bw = im2bw(I2,level);

figure, imshow(bw),title('threshold')

>> help bwconncomp

bwconncomp Find connected components in binary image.

CC = bwconncomp(BW) returns the connected components CC found in BW.

BW is a binary image that can have any dimension. CC is a structure

with four fields:

Connectivity Connectivity of the connected components (objects).

ImageSize Size of BW.

NumObjects Number of connected components (objects) in BW.

PixelIdxList 1-by-NumObjects cell array where the kth element

in the cell array is a vector containing the linear

indices of the pixels in the kth object.

cc = bwconncomp(bw, 4)

cc.NumObjects

ans=

102

grain = false(size(bw)); %256x256 logical zeros

grain(cc.PixelIdxList{50}) = true;

imshow(grain);

labelmatrix Create label matrix from BWCONNCOMP structure.

L = labelmatrix(CC) creates a label matrix from the connected

components structure CC returned by BWCONNCOMP.

labeled = labelmatrix(cc);

RGB_label = label2rgb(labeled, @spring, 'c', 'shuffle');

imshow(RGB_label)

regionprops Measure properties of image regions.

STATS = regionprops(BW,PROPERTIES) measures a set of properties for

each connected component (object) in the binary image BW, which must be

a logical array; it can have any dimension.

Shape Measurements

'Area' 'EulerNumber' 'Orientation'

'BoundingBox' 'Extent' 'Perimeter'

'Centroid' 'Extrema' 'PixelIdxList'

'ConvexArea' 'FilledArea' 'PixelList'

'ConvexHull' 'FilledImage' 'Solidity'

'ConvexImage' 'Image' 'SubarrayIdx'

'Eccentricity' 'MajorAxisLength'

'EquivDiameter' 'MinorAxisLength'

graindata = regionprops(cc,'basic')

graindata(50).Area

grain_areas = [graindata.Area];

nbins = 20;

figure, hist(grain_areas,nbins)

title('Histogram of Rice GrainArea');