04 CV BinaryImages 1002 - users.ics.forth.grusers.ics.forth.gr/~argyros/cs472_spring20/04_CV... ·...

25
04 – Binary Images CS472 - Computer Vision 1 Binary Images Monday, 10/02/2020 Antonis Argyros e-mail: [email protected] 1 Today… From gray level to binary images Processing of binary images Mathematical morphology 2

Transcript of 04 CV BinaryImages 1002 - users.ics.forth.grusers.ics.forth.gr/~argyros/cs472_spring20/04_CV... ·...

Page 1: 04 CV BinaryImages 1002 - users.ics.forth.grusers.ics.forth.gr/~argyros/cs472_spring20/04_CV... · ±%lqdu\ ,pdjhv &6 &rpsxwhu 9lvlrq ,whudwlyh . 0hdqv &oxvwhulqj iru wkuhvkroglqj)rup

04 – Binary Images

CS472 - Computer Vision 1

Binary Images

Monday, 10/02/2020Antonis Argyros

e-mail: [email protected]

1

Today…

• From gray level to binary images

• Processing of binary images• Mathematical morphology

2

Page 2: 04 CV BinaryImages 1002 - users.ics.forth.grusers.ics.forth.gr/~argyros/cs472_spring20/04_CV... · ±%lqdu\ ,pdjhv &6 &rpsxwhu 9lvlrq ,whudwlyh . 0hdqv &oxvwhulqj iru wkuhvkroglqj)rup

04 – Binary Images

CS472 - Computer Vision 2

Thresholding

T: threshold

Threshold selection:• User-defined• Αutomatic (e.g., Ηistogram partitioning, Otsu method, k-

means, …)3

A thresholded image1 if ( , )

( , )0 if ( , )

I x y TO x y

I x y T

(objects)

(background)

Simple Global Thresholding

• Partition the image by using a single threshold T.

• Then the image is scanned and labels are assigned.

• This technique is successful in highly controlled environments.

Page 3: 04 CV BinaryImages 1002 - users.ics.forth.grusers.ics.forth.gr/~argyros/cs472_spring20/04_CV... · ±%lqdu\ ,pdjhv &6 &rpsxwhu 9lvlrq ,whudwlyh . 0hdqv &oxvwhulqj iru wkuhvkroglqj)rup

04 – Binary Images

CS472 - Computer Vision 3

Sometimes works fine…

Sometimes works fine…

• When?• The population of foreground and background pixels are

well separated in the image histogram6

Page 4: 04 CV BinaryImages 1002 - users.ics.forth.grusers.ics.forth.gr/~argyros/cs472_spring20/04_CV... · ±%lqdu\ ,pdjhv &6 &rpsxwhu 9lvlrq ,whudwlyh . 0hdqv &oxvwhulqj iru wkuhvkroglqj)rup

04 – Binary Images

CS472 - Computer Vision 4

...some times it doesn’t…

• When?• A global threshold does not separate well foreground

from background7

Idea to solve the problem: adaptive thresholding

Key idea: The global histogram may not have distinctive peaks, but subimages might have….

• Split the image into subregions…… of what size?

• “Very small” subimages: Do not cover both foreground and background pixels

• “Very large” subimages: Violate the assumption of approximately uniform illumination

• Find a local threshold by statistically examining the intensity values of the local neighborhood of each pixel.• The statistic which is most appropriate depends largely on the

input image. • Simple and fast functions include the mean of the local intensity

distribution, the median value, or the mean of the minimum and maximum values 8

Page 5: 04 CV BinaryImages 1002 - users.ics.forth.grusers.ics.forth.gr/~argyros/cs472_spring20/04_CV... · ±%lqdu\ ,pdjhv &6 &rpsxwhu 9lvlrq ,whudwlyh . 0hdqv &oxvwhulqj iru wkuhvkroglqj)rup

04 – Binary Images

CS472 - Computer Vision 5

Thresholding: inputInteractive demo: http://homepages.inf.ed.ac.uk/rbf/HIPR2/adapthreshdemo.htm

9

Global thresholding

10

Page 6: 04 CV BinaryImages 1002 - users.ics.forth.grusers.ics.forth.gr/~argyros/cs472_spring20/04_CV... · ±%lqdu\ ,pdjhv &6 &rpsxwhu 9lvlrq ,whudwlyh . 0hdqv &oxvwhulqj iru wkuhvkroglqj)rup

04 – Binary Images

CS472 - Computer Vision 6

Adaptive thresholding, mean, 7x7 regions

11

Adaptive thresholding, mean, 17x17 regions

12

Page 7: 04 CV BinaryImages 1002 - users.ics.forth.grusers.ics.forth.gr/~argyros/cs472_spring20/04_CV... · ±%lqdu\ ,pdjhv &6 &rpsxwhu 9lvlrq ,whudwlyh . 0hdqv &oxvwhulqj iru wkuhvkroglqj)rup

04 – Binary Images

CS472 - Computer Vision 7

Adaptive thresholding, “mean minus C", 7x7, C=7

13

Adaptive thresholding, median-C, 7x7 regions, C=4

14

Page 8: 04 CV BinaryImages 1002 - users.ics.forth.grusers.ics.forth.gr/~argyros/cs472_spring20/04_CV... · ±%lqdu\ ,pdjhv &6 &rpsxwhu 9lvlrq ,whudwlyh . 0hdqv &oxvwhulqj iru wkuhvkroglqj)rup

04 – Binary Images

CS472 - Computer Vision 8

Automatic thresholding methods

1. Otsu’s method

2. K-means clustering

Best choices for T

Otsu’s method

Automatic thresholding method

• automatically picks “best” threshold T given an image histogram

Assumes 2 groups are present in the image:

1. Those that are <= T

2. Those that are > T

Page 9: 04 CV BinaryImages 1002 - users.ics.forth.grusers.ics.forth.gr/~argyros/cs472_spring20/04_CV... · ±%lqdu\ ,pdjhv &6 &rpsxwhu 9lvlrq ,whudwlyh . 0hdqv &oxvwhulqj iru wkuhvkroglqj)rup

04 – Binary Images

CS472 - Computer Vision 9

Otsu’s method

For every possible TA. Calculate within group variances:

1. probability of being in group 1; probability of being in group 2

2. determine mean of group 1; determine mean of group 2

3. calculate variance for group 1; calculate variance for group 2

4. calculate weighted sum of group variances

B. Remember which T gave rise to minimum.

Otsu’s method: probability of being in each group

10

max

21

T

i

i T

q T p i

q T p i

p(i) is the histogram value for intensity i

Page 10: 04 CV BinaryImages 1002 - users.ics.forth.grusers.ics.forth.gr/~argyros/cs472_spring20/04_CV... · ±%lqdu\ ,pdjhv &6 &rpsxwhu 9lvlrq ,whudwlyh . 0hdqv &oxvwhulqj iru wkuhvkroglqj)rup

04 – Binary Images

CS472 - Computer Vision 10

Otsu’s method: mean of individual groups

1 10

max

2 21

/

/

T

i

i T

T i p i q T

T i p i q T

Otsu’s method: variance of individual groups

221 1 1

0

max22

2 2 21

/

/

T

i

i T

T i T p i q T

T i T p i q T

Page 11: 04 CV BinaryImages 1002 - users.ics.forth.grusers.ics.forth.gr/~argyros/cs472_spring20/04_CV... · ±%lqdu\ ,pdjhv &6 &rpsxwhu 9lvlrq ,whudwlyh . 0hdqv &oxvwhulqj iru wkuhvkroglqj)rup

04 – Binary Images

CS472 - Computer Vision 11

Otsu’s method:weighted sum of group variances

Calculate for all t’s and minimize.

2 2 21 1 2 2W T q T T q T T

* 2arg min | 0 maxWT T T

Otsu’s method

Page 12: 04 CV BinaryImages 1002 - users.ics.forth.grusers.ics.forth.gr/~argyros/cs472_spring20/04_CV... · ±%lqdu\ ,pdjhv &6 &rpsxwhu 9lvlrq ,whudwlyh . 0hdqv &oxvwhulqj iru wkuhvkroglqj)rup

04 – Binary Images

CS472 - Computer Vision 12

Iterative K-Means Clustering for thresholding

Form two clusters from a set of pixel gray values.

1. Set ic = 1 (iteration count).2. Choose two random gray values as the initial K

means, m1(1) and m2(1).

3. For each pixel gray value xi compute fabs(xi,mj(ic)) for each j=1,2.

4. Assign xi to the cluster Cj with the nearest mean.5. ic =ic+1; update the means to get a new set m1(ic),

m2(ic).

6. Repeat steps 3 to 5 until Cj(ic+1) = Cj(ic) for all j.

Iterative K-Means Clustering for thresholding

Example.

m1(1)=260.83, m2(1)=539.00m1(2)=39.37, m2(2)=1045.65m1(3)=52.29, m2(3)=1098.63m1(4)=54.71, m2(4)=1106.28m1(5)=55.04, m2(5)=1107.24m1(6)=55.10, m2(6)=1107.44m1(7)=55.10, m2(7)=1107.44...

Page 13: 04 CV BinaryImages 1002 - users.ics.forth.grusers.ics.forth.gr/~argyros/cs472_spring20/04_CV... · ±%lqdu\ ,pdjhv &6 &rpsxwhu 9lvlrq ,whudwlyh . 0hdqv &oxvwhulqj iru wkuhvkroglqj)rup

04 – Binary Images

CS472 - Computer Vision 13

K-Means

K-Means readily generalizes to:1. arbitrary number of classes (K)

2. can easily be extended to many features (i.e., feature vectors instead of only gray values/higher dimensions)

K-Means will find a local optimum, but that may not be the global optimum!

Processing of binary (=two valued) images

Images with only two values: 0 (usually background) and 1 (usually foreground)

26

… says “HI”…

Page 14: 04 CV BinaryImages 1002 - users.ics.forth.grusers.ics.forth.gr/~argyros/cs472_spring20/04_CV... · ±%lqdu\ ,pdjhv &6 &rpsxwhu 9lvlrq ,whudwlyh . 0hdqv &oxvwhulqj iru wkuhvkroglqj)rup

04 – Binary Images

CS472 - Computer Vision 14

Morphological Filtering

Main ideaExamine the geometrical structure of an image by

matching it with small patterns called structuring elements at various locations

By varying the size and shape of the matching patterns, we can extract useful information about the shape of the different parts of the image and their interrelations.

27

Example: thresholded image of erythrocytes

• Several objects• Some touching each other• Some with noisy

boundaries• Can we “improve” this

image?

28

Page 15: 04 CV BinaryImages 1002 - users.ics.forth.grusers.ics.forth.gr/~argyros/cs472_spring20/04_CV... · ±%lqdu\ ,pdjhv &6 &rpsxwhu 9lvlrq ,whudwlyh . 0hdqv &oxvwhulqj iru wkuhvkroglqj)rup

04 – Binary Images

CS472 - Computer Vision 15

Improving the thresholded image

• Removal of contour pixels for better object separation

• Filling of small gaps

• Removal of small objects and extrusions

29

Morphological filtering Noisy image will break down OCR systems

Noisy image

30

Restored image

By applying MF, we increase the OCR accuracy!

Page 16: 04 CV BinaryImages 1002 - users.ics.forth.grusers.ics.forth.gr/~argyros/cs472_spring20/04_CV... · ±%lqdu\ ,pdjhv &6 &rpsxwhu 9lvlrq ,whudwlyh . 0hdqv &oxvwhulqj iru wkuhvkroglqj)rup

04 – Binary Images

CS472 - Computer Vision 16

Mathematical morphology

Two basic operations (dual to each other)…

• Dilation: enlarges foreground, shrinks background

• Erosion: shrinks foreground, enlarges background

… and several composite

• closing

• opening

• . . .31

Dilation

Let A and B are subsets in 2D space.

A: image undergoing analysis

B: Structuring element

denotes dilation

The translation of A by x is defined as

The dilation of A by B can be computed as the union of translation of A by the elements of B (or vice versa)

2( ) { , }xA c Z c a x for some a A

( ) ( )b ab B a A

A B A B

32

Page 17: 04 CV BinaryImages 1002 - users.ics.forth.grusers.ics.forth.gr/~argyros/cs472_spring20/04_CV... · ±%lqdu\ ,pdjhv &6 &rpsxwhu 9lvlrq ,whudwlyh . 0hdqv &oxvwhulqj iru wkuhvkroglqj)rup

04 – Binary Images

CS472 - Computer Vision 17

Dilation, example

••• •

• •• •• • •

• •

• •B

A BA

33

Dilation

••• •

••• •

•• •• •• • •

• •

)0,0(A )1,0(A

BA

• • B

34

Page 18: 04 CV BinaryImages 1002 - users.ics.forth.grusers.ics.forth.gr/~argyros/cs472_spring20/04_CV... · ±%lqdu\ ,pdjhv &6 &rpsxwhu 9lvlrq ,whudwlyh . 0hdqv &oxvwhulqj iru wkuhvkroglqj)rup

04 – Binary Images

CS472 - Computer Vision 18

Dilation

xB)(

BA

A

( ) ( )b ab B a A

A B A B

35

Example of Dilation

Pablo Picasso, Pass with the Cape, 1960

StructuringElement

36

Page 19: 04 CV BinaryImages 1002 - users.ics.forth.grusers.ics.forth.gr/~argyros/cs472_spring20/04_CV... · ±%lqdu\ ,pdjhv &6 &rpsxwhu 9lvlrq ,whudwlyh . 0hdqv &oxvwhulqj iru wkuhvkroglqj)rup

04 – Binary Images

CS472 - Computer Vision 19

Properties of Dilation

Commutative

Associative

Dilation of a subset

DBDAimpliesBA

ABBA

CBACBA )()(

37

Properties of Dilation Translation Invariance

Linearity

Containment

Decomposition of structuring element

xx BABA )()(

( ) ( ) ( )A B C A C B C

( ) ( ) ( )A B C A C B C

( ) ( ) ( )A B C A B A C

38

Page 20: 04 CV BinaryImages 1002 - users.ics.forth.grusers.ics.forth.gr/~argyros/cs472_spring20/04_CV... · ±%lqdu\ ,pdjhv &6 &rpsxwhu 9lvlrq ,whudwlyh . 0hdqv &oxvwhulqj iru wkuhvkroglqj)rup

04 – Binary Images

CS472 - Computer Vision 20

Erosion Erosion is the morphological dual to dilation

Let denote the erosion of A by B. Then:BA

))({ 2 ABZxBA x

39

2D example of erosion

• • • • ••••

• •

B

A BA

• • • •

40

Page 21: 04 CV BinaryImages 1002 - users.ics.forth.grusers.ics.forth.gr/~argyros/cs472_spring20/04_CV... · ±%lqdu\ ,pdjhv &6 &rpsxwhu 9lvlrq ,whudwlyh . 0hdqv &oxvwhulqj iru wkuhvkroglqj)rup

04 – Binary Images

CS472 - Computer Vision 21

Erosion

xB)(

A

BA

))({ 2 ABZxBA x

41

Example of Erosion

StructuringElement

42

Page 22: 04 CV BinaryImages 1002 - users.ics.forth.grusers.ics.forth.gr/~argyros/cs472_spring20/04_CV... · ±%lqdu\ ,pdjhv &6 &rpsxwhu 9lvlrq ,whudwlyh . 0hdqv &oxvwhulqj iru wkuhvkroglqj)rup

04 – Binary Images

CS472 - Computer Vision 22

Erosion

Original image Eroded image

43

Erosion

Eroded once Eroded twice

44

Page 23: 04 CV BinaryImages 1002 - users.ics.forth.grusers.ics.forth.gr/~argyros/cs472_spring20/04_CV... · ±%lqdu\ ,pdjhv &6 &rpsxwhu 9lvlrq ,whudwlyh . 0hdqv &oxvwhulqj iru wkuhvkroglqj)rup

04 – Binary Images

CS472 - Computer Vision 23

Erosion

Original image Eroded image

45

Erosion

Eroded once Eroded twice

46

Page 24: 04 CV BinaryImages 1002 - users.ics.forth.grusers.ics.forth.gr/~argyros/cs472_spring20/04_CV... · ±%lqdu\ ,pdjhv &6 &rpsxwhu 9lvlrq ,whudwlyh . 0hdqv &oxvwhulqj iru wkuhvkroglqj)rup

04 – Binary Images

CS472 - Computer Vision 24

Counting Coins…

• Counting coins is difficult because they touch each other!

• Solution: Thresholding and Erosion separates them!

47

Duality Relationship

, the reflection of B, is defined as

Erosion and Dilation Duality Theorem

2ZB B

{ , }B x x b b B

( )c cA B A B

48

Page 25: 04 CV BinaryImages 1002 - users.ics.forth.grusers.ics.forth.gr/~argyros/cs472_spring20/04_CV... · ±%lqdu\ ,pdjhv &6 &rpsxwhu 9lvlrq ,whudwlyh . 0hdqv &oxvwhulqj iru wkuhvkroglqj)rup

04 – Binary Images

CS472 - Computer Vision 25

Duality

Erosion of the foreground (white) results in dilation of the background (black)

49

Duality Relationship Dilation and Erosion: what one does to image

foreground, the other does to the image background.

50