56
DREAM PLAN IDEA IMPLEMENTATION 1

DREAM PLAN IDEA IMPLEMENTATION 1. 2 3 Introduction to Image Processing Dr. Kourosh Kiani Email: [email protected]@yahoo.com Email: [email protected]@aut.ac.ir

Embed Size (px)

Citation preview

Page 1: DREAM PLAN IDEA IMPLEMENTATION 1. 2 3 Introduction to Image Processing Dr. Kourosh Kiani Email: kkiani2004@yahoo.comkkiani2004@yahoo.com Email: Kourosh.kiani@aut.ac.irKourosh.kiani@aut.ac.ir

1

DREAMDREAM

PLANPLANIDEAIDEA

IMPLEMENTATIONIMPLEMENTATION

Page 3: DREAM PLAN IDEA IMPLEMENTATION 1. 2 3 Introduction to Image Processing Dr. Kourosh Kiani Email: kkiani2004@yahoo.comkkiani2004@yahoo.com Email: Kourosh.kiani@aut.ac.irKourosh.kiani@aut.ac.ir

3

Introduction to Image ProcessingIntroduction to Image Processing

Dr. Kourosh KianiEmail: [email protected]: [email protected]: [email protected]: www.kouroshkiani.com

Present to:Amirkabir University of Technology (Tehran

Polytechnic) & Semnan University

Page 4: DREAM PLAN IDEA IMPLEMENTATION 1. 2 3 Introduction to Image Processing Dr. Kourosh Kiani Email: kkiani2004@yahoo.comkkiani2004@yahoo.com Email: Kourosh.kiani@aut.ac.irKourosh.kiani@aut.ac.ir

4

Lecture 02

Histogram

Page 12: DREAM PLAN IDEA IMPLEMENTATION 1. 2 3 Introduction to Image Processing Dr. Kourosh Kiani Email: kkiani2004@yahoo.comkkiani2004@yahoo.com Email: Kourosh.kiani@aut.ac.irKourosh.kiani@aut.ac.ir

12

THE IMAGE HISTOGRAM

H(k) specifies the # of pixels with gray-value k

Let N be the number of pixels: P(k) = H(k)/N defines the normalized

histogram defines the accumulated

histogram

Occurrence(# of pixels)

Gray Level

k

kHN )(

k

i

iHkC1

)()(

Page 13: DREAM PLAN IDEA IMPLEMENTATION 1. 2 3 Introduction to Image Processing Dr. Kourosh Kiani Email: kkiani2004@yahoo.comkkiani2004@yahoo.com Email: Kourosh.kiani@aut.ac.irKourosh.kiani@aut.ac.ir

131 2 3 4 5 6 7 8 9 100

0.2

0.4

0.6

0.8

1

gray level

1 2 3 4 5 6 7 8 9 100

5000

10000

15000

gray level

1 2 3 4 5 6 7 8 9 100

0.05

0.1

0.15

0.2

0.25

gray level

Histogram

Normalized Histogram

Accumulated Histogram

Page 14: DREAM PLAN IDEA IMPLEMENTATION 1. 2 3 Introduction to Image Processing Dr. Kourosh Kiani Email: kkiani2004@yahoo.comkkiani2004@yahoo.com Email: Kourosh.kiani@aut.ac.irKourosh.kiani@aut.ac.ir

14

The image histogram does not fully represent the image

P(I)

I

P(I)

I

11

0.5

EXAMPLES

H(I)

I

0.1

H(I)

I

0.1

Pixel permutation of the left image

Page 15: DREAM PLAN IDEA IMPLEMENTATION 1. 2 3 Introduction to Image Processing Dr. Kourosh Kiani Email: kkiani2004@yahoo.comkkiani2004@yahoo.com Email: Kourosh.kiani@aut.ac.irKourosh.kiani@aut.ac.ir

15

EXAMPLE

2 3 3 2

4 2 4 3

3 2 3 5

2 4 2 4

4x4 image

Gray scale = [0,9]histogram

0 1

1

2

2

3

3

4

4

5

5

6

6

7 8 9

No. of pixels

Gray level

Page 16: DREAM PLAN IDEA IMPLEMENTATION 1. 2 3 Introduction to Image Processing Dr. Kourosh Kiani Email: kkiani2004@yahoo.comkkiani2004@yahoo.com Email: Kourosh.kiani@aut.ac.irKourosh.kiani@aut.ac.ir

16

Gray Level(j)

0 1 2 3 4 5 6 7 8 9

No. of pixels 0 0 6 5 4 1 0 0 0 0

0 0 6 11 15 16 16 16 16 16

0 06

/16

11 /16

15 /16

16 /16

16 /16

16 /16

16 /16

16 /16

s x 9 0 03.33

6.16

8.48

9 9 9 9 9

k

jjn

0

k

j

j

n

ns

0

EXAMPLE

Page 17: DREAM PLAN IDEA IMPLEMENTATION 1. 2 3 Introduction to Image Processing Dr. Kourosh Kiani Email: kkiani2004@yahoo.comkkiani2004@yahoo.com Email: Kourosh.kiani@aut.ac.irKourosh.kiani@aut.ac.ir

17

Grayscale Histogram

Page 18: DREAM PLAN IDEA IMPLEMENTATION 1. 2 3 Introduction to Image Processing Dr. Kourosh Kiani Email: kkiani2004@yahoo.comkkiani2004@yahoo.com Email: Kourosh.kiani@aut.ac.irKourosh.kiani@aut.ac.ir

18

WHAT IS HISTOGRAM ?

(3 ,8 ,5)

Page 20: DREAM PLAN IDEA IMPLEMENTATION 1. 2 3 Introduction to Image Processing Dr. Kourosh Kiani Email: kkiani2004@yahoo.comkkiani2004@yahoo.com Email: Kourosh.kiani@aut.ac.irKourosh.kiani@aut.ac.ir

20

HISTOGRAM

0 1 1 2 4

2 1 0 0 2

5 2 0 0 4

1 1 2 4 1 0

1

2

3

4

5

6

7

0 1 2 3 4 5 6

image histogram

Page 21: DREAM PLAN IDEA IMPLEMENTATION 1. 2 3 Introduction to Image Processing Dr. Kourosh Kiani Email: kkiani2004@yahoo.comkkiani2004@yahoo.com Email: Kourosh.kiani@aut.ac.irKourosh.kiani@aut.ac.ir

21

117 83 59 59 68 77 84 94 82 67 62 70 83 86 85 81 71 65 77 89 86 82 76 67 72 90 97 86 66 54 68 104 121 107 85 46 58 89 138 165 137 91 38 80 147 200 211 187 138 40 80 149 197 202 187 146 56 76 114 159 181 160 113

value count

1 0

40 8

104 32

181 45

255 0

PARTIAL IMAGE INFO:

HISTOGRAM

Page 22: DREAM PLAN IDEA IMPLEMENTATION 1. 2 3 Introduction to Image Processing Dr. Kourosh Kiani Email: kkiani2004@yahoo.comkkiani2004@yahoo.com Email: Kourosh.kiani@aut.ac.irKourosh.kiani@aut.ac.ir

22

IMAGE HISTOGRAM

20 40 60 80 100 120 140 160 180 200 2200

0.5

1

1.5

2

2.5

3

Plot of Pixel Count as a Function of Gray Level Value

Page 23: DREAM PLAN IDEA IMPLEMENTATION 1. 2 3 Introduction to Image Processing Dr. Kourosh Kiani Email: kkiani2004@yahoo.comkkiani2004@yahoo.com Email: Kourosh.kiani@aut.ac.irKourosh.kiani@aut.ac.ir

23

IMAGE HISTOGRAMS

The histogram of an image shows us the distribution of grey levels in the imageMassively useful in image processing, especially in segmentation

Grey Levels

Fre

quen

cies

Page 24: DREAM PLAN IDEA IMPLEMENTATION 1. 2 3 Introduction to Image Processing Dr. Kourosh Kiani Email: kkiani2004@yahoo.comkkiani2004@yahoo.com Email: Kourosh.kiani@aut.ac.irKourosh.kiani@aut.ac.ir

24

Page 27: DREAM PLAN IDEA IMPLEMENTATION 1. 2 3 Introduction to Image Processing Dr. Kourosh Kiani Email: kkiani2004@yahoo.comkkiani2004@yahoo.com Email: Kourosh.kiani@aut.ac.irKourosh.kiani@aut.ac.ir

27

0 50 100 150 200 250 3000

0.005

0.01

0.015

0.02

0.025

0.03

0 50 100 150 200 250 3000

0.01

0.02

0.03

0.04

0.05

0.06

0 50 100 150 200 250 3000

0.01

0.02

0.03

0.04

0.05

0.06

Original Dark Light

Page 29: DREAM PLAN IDEA IMPLEMENTATION 1. 2 3 Introduction to Image Processing Dr. Kourosh Kiani Email: kkiani2004@yahoo.comkkiani2004@yahoo.com Email: Kourosh.kiani@aut.ac.irKourosh.kiani@aut.ac.ir

29

HISTOGRAM

An image has low contrast when the complete range of possible values is not used.  Inspection of the histogram shows this lack of contrast.

Page 31: DREAM PLAN IDEA IMPLEMENTATION 1. 2 3 Introduction to Image Processing Dr. Kourosh Kiani Email: kkiani2004@yahoo.comkkiani2004@yahoo.com Email: Kourosh.kiani@aut.ac.irKourosh.kiani@aut.ac.ir

31-50 0 50 100 150 200 250 3000

500

1000

1500

2000

2500

rgbI = imread('kourosh.jpg');grayI = rgb2gray(rgbI);%get image histogram[counts bins]=imhist(grayI); %show histogrambar(bins,counts);

Page 32: DREAM PLAN IDEA IMPLEMENTATION 1. 2 3 Introduction to Image Processing Dr. Kourosh Kiani Email: kkiani2004@yahoo.comkkiani2004@yahoo.com Email: Kourosh.kiani@aut.ac.irKourosh.kiani@aut.ac.ir

32

rgbI = imread('kouroshdark.jpg');grayI = rgb2gray(rgbI);%get image histogram[counts bins]=imhist(grayI); %show histogrambar(bins,counts);

-50 0 50 100 150 200 250 3000

2

4

6

8

10

12x 10

4

Page 33: DREAM PLAN IDEA IMPLEMENTATION 1. 2 3 Introduction to Image Processing Dr. Kourosh Kiani Email: kkiani2004@yahoo.comkkiani2004@yahoo.com Email: Kourosh.kiani@aut.ac.irKourosh.kiani@aut.ac.ir

33

rgbI = imread('kouroshbright.jpg');grayI = rgb2gray(rgbI);%get image histogram[counts bins]=imhist(grayI); %show histogrambar(bins,counts);

-50 0 50 100 150 200 250 3000

1

2

3

4

5

6x 10

4

Page 34: DREAM PLAN IDEA IMPLEMENTATION 1. 2 3 Introduction to Image Processing Dr. Kourosh Kiani Email: kkiani2004@yahoo.comkkiani2004@yahoo.com Email: Kourosh.kiani@aut.ac.irKourosh.kiani@aut.ac.ir

34

I = imread('pic01.png');%get image histogram[counts bins]=imhist(I); %show histogrambar(bins,counts);

-50 0 50 100 150 200 250 3000

100

200

300

400

500

600

700

Page 35: DREAM PLAN IDEA IMPLEMENTATION 1. 2 3 Introduction to Image Processing Dr. Kourosh Kiani Email: kkiani2004@yahoo.comkkiani2004@yahoo.com Email: Kourosh.kiani@aut.ac.irKourosh.kiani@aut.ac.ir

35

I = imread('pic03.png');%get image histogram[counts bins]=imhist(I); %show histogrambar(bins,counts);

-50 0 50 100 150 200 250 3000

100

200

300

400

500

600

700

800

900

Page 36: DREAM PLAN IDEA IMPLEMENTATION 1. 2 3 Introduction to Image Processing Dr. Kourosh Kiani Email: kkiani2004@yahoo.comkkiani2004@yahoo.com Email: Kourosh.kiani@aut.ac.irKourosh.kiani@aut.ac.ir

36

HISTOGRAM EQUALIZATION

The image shows the spatial distribution of gray values.

The image histogram discards the spatial information and shows the relative frequency of occurrence of the gray values.

0 3 3 2 5 5

1 1 0 3 4 5

2 2 2 4 4 4

3 3 4 4 5 5

3 4 5 5 6 6

7 6 6 6 6 5

0 2 .05 1 2 .05 2 4 .11 3 6 .17 4 7 .20 5 8 .22 6 6 .17 7 1 .03

Image CountGray Value

Rel. Freq.

Sum= 36 1.00

Page 37: DREAM PLAN IDEA IMPLEMENTATION 1. 2 3 Introduction to Image Processing Dr. Kourosh Kiani Email: kkiani2004@yahoo.comkkiani2004@yahoo.com Email: Kourosh.kiani@aut.ac.irKourosh.kiani@aut.ac.ir

37

The histogram typically plots the absolute pixel count as a function of gray value:

0 1 2 3 4 5 6 70

1

2

3

4

5

6

7

8

Pix

el C

ount

Gray Value

For an image with dimensions M by N

MNiHI

Ii

min

min

)(I max

HISTOGRAM EQUALIZATION

Page 38: DREAM PLAN IDEA IMPLEMENTATION 1. 2 3 Introduction to Image Processing Dr. Kourosh Kiani Email: kkiani2004@yahoo.comkkiani2004@yahoo.com Email: Kourosh.kiani@aut.ac.irKourosh.kiani@aut.ac.ir

38

PROBABILITY INTERPRETATION

The graph of relative frequency of occurrence as a function of gray value is also called a histogram:

Interpreting the relative frequency histogram as a probability distribution,

then:

0 1 2 3 4 5 6 70

0.05

0.1

0.15

0.2

0.25

Rela

tive F

requency

Gray Value

P(I(x,y) = i) = H(i)/(MxN)

Page 39: DREAM PLAN IDEA IMPLEMENTATION 1. 2 3 Introduction to Image Processing Dr. Kourosh Kiani Email: kkiani2004@yahoo.comkkiani2004@yahoo.com Email: Kourosh.kiani@aut.ac.irKourosh.kiani@aut.ac.ir

39

CUMULATIVE DENSITY FUNCTION Interpreting the relative frequency

histogram as a probability distribution, then:

Curve is called the cumulative distribution function

0 1 2 3 4 5 6 70

0.05

0.1

0.15

0.2

0.25

Rela

tive F

requency

Gray Value

0 1 2 3 4 5 6 70

0.1

0.2

0.3

0.4

0.5

0.6

0.7

0.8

0.9

1

Gray Value

P(I(x,y) = i) = H(i)/(MxN)

i

k

kPiQ0

)()(

i

k

kHiCH0

)()(

Page 40: DREAM PLAN IDEA IMPLEMENTATION 1. 2 3 Introduction to Image Processing Dr. Kourosh Kiani Email: kkiani2004@yahoo.comkkiani2004@yahoo.com Email: Kourosh.kiani@aut.ac.irKourosh.kiani@aut.ac.ir

400 50 100 150 200 250

0

500

1000

1500

2000

2500

3000

3500

4000

Image histograms consist of peaks, valleys, and low plains

Peaks = many pixels concentrated in a few grey levels

Plains = small number of pixels distributed over a wider range of grey levels

HISTOGRAM EQUALIZATION

Page 41: DREAM PLAN IDEA IMPLEMENTATION 1. 2 3 Introduction to Image Processing Dr. Kourosh Kiani Email: kkiani2004@yahoo.comkkiani2004@yahoo.com Email: Kourosh.kiani@aut.ac.irKourosh.kiani@aut.ac.ir

41

HISTOGRAM EQUALIZATION The goal is to modify the gray levels of an image so

that the histogram of the modified image is flat. Expand pixels in peaks over a wider range of

gray-levels. “Squeeze” low plains pixels into a narrower

range of gray levels. Utilizes all gray values equally Example Histogram:

Note low utilization of small gray values

0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 150

200400600800

100012001400160018002000

Pix

el C

ount

Gray Value

Page 42: DREAM PLAN IDEA IMPLEMENTATION 1. 2 3 Introduction to Image Processing Dr. Kourosh Kiani Email: kkiani2004@yahoo.comkkiani2004@yahoo.com Email: Kourosh.kiani@aut.ac.irKourosh.kiani@aut.ac.ir

42

DESIRED HISTOGRAM

All gray levels are used equally. Has a tendency to enhance contrast.

0 1 2 3 4 5 6 7 8 9 1011 12 13 14 150

50

100

150

200

250

300

Pix

el C

ount

Gray Value

Page 43: DREAM PLAN IDEA IMPLEMENTATION 1. 2 3 Introduction to Image Processing Dr. Kourosh Kiani Email: kkiani2004@yahoo.comkkiani2004@yahoo.com Email: Kourosh.kiani@aut.ac.irKourosh.kiani@aut.ac.ir

43

BRUTE FORCE

0 0 256 22 from 7, 234 from 8 1 0 256 1 from 8, 255 from 9 2 0 256 195 from 9, 61 from 10 3 0 256 256 from 10 4 0 256 256 from 10 5 0 256 256 from 10 6 0 256 256 from 10 7 22 256 256 from 10 8 235 256 256 from 10 9 450 256 256 from 10

10 1920 256 67 from 10, 189 from 11 11 212 256 23 from 11, 10 from 12, 223 from 13 12 10 256 10 from 13, 246 from 14 13 233 256 256 from 14 14 672 256 170 from 14, 86 from 15 15 342 256 256 from 15

Gray Actual Desired How to get it Scale Count Count

Sum 4096 4096

How are the gray levels in the original image changed to produce the enhanced image?

Method 1. Choose points randomly.

Method 2. Choice depends on the graylevels of their neighboring points.

Computationally expensive.

Approximations.

Page 44: DREAM PLAN IDEA IMPLEMENTATION 1. 2 3 Introduction to Image Processing Dr. Kourosh Kiani Email: kkiani2004@yahoo.comkkiani2004@yahoo.com Email: Kourosh.kiani@aut.ac.irKourosh.kiani@aut.ac.ir

44

HISTOGRAM EQUALIZATION Mapping from one set of grey-levels, I, to a new

set, O. Ideally, the number of pixels, Np, occupying each

grey level should be:

To approximate this, apply the transform

Where CH is the cumulative histogram (see next slide)

j is the gray value in the source image i is the gray value in the equalized image

Np = M*N

G

i = MAX 0 ,roundCH(j)

Np

-1

Page 45: DREAM PLAN IDEA IMPLEMENTATION 1. 2 3 Introduction to Image Processing Dr. Kourosh Kiani Email: kkiani2004@yahoo.comkkiani2004@yahoo.com Email: Kourosh.kiani@aut.ac.irKourosh.kiani@aut.ac.ir

45

EXAMPLE

800 600 400 200 0

0 1 2 3 4 5 6 8

ideal

800 600 400 200 0

G=8MxN=2400Np=300

j H(j) CH(j) i

0 100 01 800 22 700 43 500 64 100 65 100 76 100 77 0 7

100900160021002200230024002400

CH(j) = S H(i)i=0

j

0 1 2 3 4 5 6 8

Page 46: DREAM PLAN IDEA IMPLEMENTATION 1. 2 3 Introduction to Image Processing Dr. Kourosh Kiani Email: kkiani2004@yahoo.comkkiani2004@yahoo.com Email: Kourosh.kiani@aut.ac.irKourosh.kiani@aut.ac.ir

46

EXAMPLErgbI = imread('ship1.png');J = histeq(rgbI);imshow(rgbI)figure, imshow(J)%get image histogram[counts bins]=imhist(rgbI);%show histogramfigure(3)bar(bins,counts);%get image histogram[counts bins]=imhist(J); %show histogramfigure(4)bar(bins,counts);

Page 47: DREAM PLAN IDEA IMPLEMENTATION 1. 2 3 Introduction to Image Processing Dr. Kourosh Kiani Email: kkiani2004@yahoo.comkkiani2004@yahoo.com Email: Kourosh.kiani@aut.ac.irKourosh.kiani@aut.ac.ir

47

-50 0 50 100 150 200 250 3000

500

1000

1500

2000

2500

3000

3500

4000

4500

5000

-50 0 50 100 150 200 250 3000

500

1000

1500

2000

2500

3000

3500

4000

4500

5000

Page 48: DREAM PLAN IDEA IMPLEMENTATION 1. 2 3 Introduction to Image Processing Dr. Kourosh Kiani Email: kkiani2004@yahoo.comkkiani2004@yahoo.com Email: Kourosh.kiani@aut.ac.irKourosh.kiani@aut.ac.ir

48

EXAMPLE

Page 49: DREAM PLAN IDEA IMPLEMENTATION 1. 2 3 Introduction to Image Processing Dr. Kourosh Kiani Email: kkiani2004@yahoo.comkkiani2004@yahoo.com Email: Kourosh.kiani@aut.ac.irKourosh.kiani@aut.ac.ir

49

Histogram Equalization: Example

Page 50: DREAM PLAN IDEA IMPLEMENTATION 1. 2 3 Introduction to Image Processing Dr. Kourosh Kiani Email: kkiani2004@yahoo.comkkiani2004@yahoo.com Email: Kourosh.kiani@aut.ac.irKourosh.kiani@aut.ac.ir

50

EXAMPLE

Page 51: DREAM PLAN IDEA IMPLEMENTATION 1. 2 3 Introduction to Image Processing Dr. Kourosh Kiani Email: kkiani2004@yahoo.comkkiani2004@yahoo.com Email: Kourosh.kiani@aut.ac.irKourosh.kiani@aut.ac.ir

51

COLOR IMAGE

Page 52: DREAM PLAN IDEA IMPLEMENTATION 1. 2 3 Introduction to Image Processing Dr. Kourosh Kiani Email: kkiani2004@yahoo.comkkiani2004@yahoo.com Email: Kourosh.kiani@aut.ac.irKourosh.kiani@aut.ac.ir

52

Page 53: DREAM PLAN IDEA IMPLEMENTATION 1. 2 3 Introduction to Image Processing Dr. Kourosh Kiani Email: kkiani2004@yahoo.comkkiani2004@yahoo.com Email: Kourosh.kiani@aut.ac.irKourosh.kiani@aut.ac.ir

53

clc; % Clear the command window.close all; % Close all figures (except those of imtool.)imtool close all; % Close all imtool figures.clear; % Erase all existing variables.workspace; % Make sure the workspace panel is showing.fontSize = 20; % Read in standard MATLAB color demo image.rgbImage = imread('peppers.png');[rows columns numberOfColorBands] = size(rgbImage);figure (1)imshow(rgbImage, []); redPlane = rgbImage(:, :, 1);greenPlane = rgbImage(:, :, 2);bluePlane = rgbImage(:, :, 3); % Let's get its histograms.[pixelCountR grayLevelsR] = imhist(redPlane);figure(2)%show histogrambar(grayLevelsR,pixelCountR,'r');xlim([0 grayLevelsR(end)]); % Scale x axis manually.title('Histogram of red plane', 'Fontsize', fontSize);grid on;[pixelCountG grayLevelsG] = imhist(greenPlane);figure(3)%show histogrambar(grayLevelsG,pixelCountG,'g');xlim([0 grayLevelsG(end)]); % Scale x axis manually.title('Histogram of green plane', 'Fontsize', fontSize);grid on;[pixelCountB grayLevelsB] = imhist(bluePlane);figure(4)%show histogrambar(grayLevelsB,pixelCountB,'b');xlim([0 grayLevelsB(end)]); % Scale x axis manually.title('Histogram of blue plane', 'Fontsize', fontSize);grid on;

Page 54: DREAM PLAN IDEA IMPLEMENTATION 1. 2 3 Introduction to Image Processing Dr. Kourosh Kiani Email: kkiani2004@yahoo.comkkiani2004@yahoo.com Email: Kourosh.kiani@aut.ac.irKourosh.kiani@aut.ac.ir

540 50 100 150 200 250

0

1000

2000

3000

4000

5000

6000

7000

Histogram of green plane

0 50 100 150 200 2500

2000

4000

6000

8000

10000

12000

Histogram of blue plane

0 50 100 150 200 2500

2000

4000

6000

8000

10000

12000

Histogram of red plane

Page 55: DREAM PLAN IDEA IMPLEMENTATION 1. 2 3 Introduction to Image Processing Dr. Kourosh Kiani Email: kkiani2004@yahoo.comkkiani2004@yahoo.com Email: Kourosh.kiani@aut.ac.irKourosh.kiani@aut.ac.ir

55

Questions? Discussion? Suggestions?

Page 56: DREAM PLAN IDEA IMPLEMENTATION 1. 2 3 Introduction to Image Processing Dr. Kourosh Kiani Email: kkiani2004@yahoo.comkkiani2004@yahoo.com Email: Kourosh.kiani@aut.ac.irKourosh.kiani@aut.ac.ir

56