Introduction to Computer Vision Roger S. Gaborski 1 Lecture 4 Dr. Roger S. Gaborski
Roger S. Gaborski 1
Introduction to Computer Vision
Lecture 4Dr. Roger S. Gaborski
Roger S. Gaborski 2
HW#2 Due 02/13
Roger S. Gaborski 3
In Class Exercise Review1. Given the following MATLAB code:>> image1 = rand([3])image1 = 0.9500 0.6555 0.0318 0.7431 0.1712 0.2769 0.3922 0.7060 0.0462 >> image2 = imadjust(image1, [ .1,.75],[.2, .6]) Carefully draw the transformation map specified by the imadjust statement. Label the x and y axis.
4Roger S. Gaborski
Chapter 3 www.prenhall.com/gonzalezwoodseddins
Brighter Output (gamma<1) Darker Output (gamma>1)
Gamma specifies the shape of the curve
Characteristics:• gamma >1 : all pixels become darker• gamma <1 : all pixels become brighter• gamma =1 : linear transform
5Roger S. Gaborski
image1 =
0.9500 0.6555 0.0318 0.7431 0.1712 0.2769 0.3922 0.7060 0.0462 >> image2 = imadjust(image1, [ .1,.75],[.2, .6])
image2 =
0.6000 0.5418 0.2000 0.5958 0.2438 0.3089 0.3798 0.5729 0.2000
Roger S. Gaborski 6
Example0
.1
2
.3
.4
.
5
.6
.7
.8
.9
1.0
OU
TPU
T
0 .1 .2 .3 .4 .5 .6 .7 .8 .9 1.0INPUT
Roger S. Gaborski 7
image2 = imadjust(image1, [ .1,.75],[.2, .6],1)0
.1
2
.3
.4
.
5
.6
.7
.8
.9
1.0
OU
TPU
T
0 .1 .2 .3 .4 .5 .6 .7 .8 .9 1.0INPUT
Roger S. Gaborski 8
image2 = imadjust(image1, [ .1,.75],[.2, .6],1)0
.1
.2
.3
.4
.
5
.6
.7
.8
.9
1.0
OU
TPU
T
0 .1 .2 .3 .4 .5 .6 .7 .8 .9 1.0INPUT
9
Gray Scale Ramp Image
Roger S. Gaborski
Minimum gray value = .01Maximum gray value = 1.0
10Roger S. Gaborski
11
Ramp Image
Roger S. Gaborski
image_ramp = zeros(100); for i = 1:100 image_ramp(:,i) = i*.01;end fprintf('minimum gray value = %d, \n', min(image_ramp(:)));fprintf('maximun gray value = %d, \n', max(image_ramp(:)));figure, plot(image_ramp(50,:)), xlabel('pixel position'), ylabel('pixel value') title('values')pause
figure, imshow(image_ramp),title('ramp intensity image')pause
12
Map .01 to .5 and 1.0 to .75
Roger S. Gaborski
13Roger S. Gaborski
disp('Use imadjust to map to .50 to .75');
image1 = imadjust(image_ramp,[.01, 1.0],[.5, 0.75]); %map to .5 to .75
fprintf('minimum gray value = %d, \n', min(image1(:)));fprintf('maximun gray value = %d, \n', max(image1(:)));
figure, plot(image1(50,:)), xlabel('pixel position'), ylabel('pixel value') title('values')axis([0,100,0,1])Grid
pause figure, imshow(image1, [0,1]);
pause
14
Map .01 to .5 and 1.0 to .75
Roger S. Gaborski
15
Map values in range <=.25 to .35 and >=.50 to .65');
Roger S. Gaborski
16
Map values in range <=.25 to .35 and >=.50 to .65');
Roger S. Gaborski
disp('Use imadjust to map values in range <=.25 to .35 and >=.50 to .65');image2 = imadjust(image_ramp,[.25, .50],[.35, 0.65]);fprintf('minimum gray value = %d, \n', min(image2(:)));fprintf('maximun gray value = %d, \n', max(image2(:)));figure, plot(image2(50,:)), xlabel('pixel position'), ylabel('pixel value') title('values')axis([0,100,0,1])grid pause figure, imshow(image2, [0,1]);
17
Map values in range <=.25 to .35 and >=.50 to .65');
Roger S. Gaborski
18
Contrast Stretching Transformation• Creates an image with higher contrast than the input
image:
– r: intensities of input; – s: intensities of output;– m: threshold point (see graph) ; – E: controls slope.
Roger S. Gaborski
1( )1 ( )E
s T r mr
19Roger S. Gaborski
Chapter 3 www.prenhall.com/gonzalezwoodseddins
E controls the slope of the function
Roger S. Gaborski 20
1( )1 ( )E
s T r mr
Input(r)
Output(s)
Introduction to Computer Vision
Lecture 4Dr. Roger S. Gaborski
22Roger S. Gaborski
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
An image shows the spatialdistribution of gray level values
We can summary this informationby only retaining the distributionif gray level values:
value count
1 0
40 8
…
…
104 32
181 45
255 0
PARTIAL IMAGE INFO:
Intensity image is simplya matrix of numbers
23
Image Histogram
Roger S. Gaborski
Plot of Pixel Count as a Function of Gray Level Value
20 40 60 80 100 120 140 160 180 200 2200
0.5
1
1.5
2
2.5
3
Gray Level Value
Pixel Count
24
Histogram
• Histogram consists of– Peaks: high concentration of gray level values– Valleys: low concentration– Flat regions
Roger S. Gaborski
25
Formally, Image Histograms
Histogram:• Digital image• L possible intensity levels in range [0,G]• Defined: h(rk) = nk
– Where rk is the kth intensity level in the interval [0,G] and nk is the number of pixels in the image whose level is rk .
– G: uint8 255 uint16 65535
double 1.0
Roger S. Gaborski
26
Notation
• L levels in range [0, G]• For example:• 0, 1, 2, 3, 4, in this case G = 4, L = 5
– Since we cannot have an index of zero,• In this example, index of:
Index 1 maps to gray level 02 maps to 13 maps to 24 maps to 35 maps to 4
Roger S. Gaborski
27
Normalized Histogram
• Normalized histogram is obtained by dividing elements of h(rk) by the total number of pixels in the image (n):
for k = 1, 2,…, L
p(rk) is an estimate of the probability of occurrence of intensity level rk
Roger S. Gaborski
( )( ) ,k kk
h r np rn n
28
MATLAB Histogram
• h = imhist( f, b )– h is the histogram, h(rk) – f is the input image– b is the number of bins (default is 256)
• Normalized histogram
Roger S. Gaborski
( , )( )
imhist f bpnumel f
29
Color and Gray Scale Images
Roger S. Gaborski
30
Background: Gray Image
Roger S. Gaborski
>> I = imread('Flags.jpg');>> figure, imshow(I) % uint8
>> Im = im2double(I); % convert to double>> Igray = (Im(:,:,1)+Im(:,:,2)+Im(:,:,3))/3;>> figure, imshow(Igray)
There is also the rgb2gray function that resultsin a slightly different image
31
Gray Scale Histogram
Roger S. Gaborski
32
Plots
• bar(horz, v, width)– v is row vector
• points to be plotted– horz is a vector same dimension as v
• increments of horizontal scale• omitted axis divided in units 0 to length(v)
– width number in [0 1]• 1 bars touch• 0 vertical lines• 0.8 default
Roger S. Gaborski
33Roger S. Gaborski
p= imhist(Igray)/numel(Igray);>> h1 = p(1:10:256);>> horz = (1:10:256);>> figure, bar(horz,h1)
Review other examplesin text and in MATLABdocumentation
34Roger S. Gaborski
Chapter 3 www.prenhall.com/gonzalezwoodseddins
35
Color and Gray Scale ImagesRecall from Previous Slide
Roger S. Gaborski
Gray Scale Histogram
Roger S. Gaborski 36
Normalized Gray Scale Histogram
Roger S. Gaborski 37
>> p= imhist(Igray)/numel(Igray);>> figure, plot(p)
Normalized Gray Scale Histogram
Roger S. Gaborski 38
imhist(Igray)/numel(Igray); imhist(Igray,32)/numel(Igray)
256 bins 32 bins
Normalized Gray Scale Histogram
Roger S. Gaborski 39
>> p= imhist(Igray)/numel(Igray);>> figure, plot(p)
Gray level values
prob
abili
ty
Roger S. Gaborski 40
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
Contract enhancement
• How could we transform the pixel values of an image so that they occupy the whole range of values between 0 and 255?
Roger S. Gaborski 41
Gray Scale Transformation
• How could we transform the pixel values of an image so that they occupy the whole range of values between 0 and 255?
• If they were uniformly distributed between 0 and x we could multiply all the gray level values by 255/x
• BUT – what if they are not uniformly distributed??
Roger S. Gaborski 42
Cumulative Distribution Function
Roger S. Gaborski 43
Histogram CDF
Histogram Equalization(HE)
• HE generates an image with equally likely intensity values• Transformation function: Cumulative Distribution
Function (CDF)• The intensity values in the output image cover the full
range, [0 1]• The resulting image has higher dynamic range• The values in the normalized histogram are approximately
the probability of occurrence of those values
Roger S. Gaborski 44
Histogram Equalization• Let pr(rj), j = 1, 2, … , L denote the histogram associated with
intensity levels of a given image
• Values in normalized histogram are approximately equal to the probability of occurrence of each intensity level in image
• Equalization transformation is:
Roger S. Gaborski 45
k = 1,2,…,Lsk is intensity valueof output rk is input value
Sum of probability up to k value
1 1
( ) ( )k k
jk k r j
j j
ns T r p r
n
Histogram Equalization Example
• g = histeq(f, nlev) where f is the original image and nlev number of intensity levels in output image
Roger S. Gaborski 46
Roger S. Gaborski 47
Original ImageINPUT
Transformation
Roger S. Gaborski 48
Input Gray Level Value
x255
Out
put G
ray
Leve
l Val
ue
Roger S. Gaborski 49
OUTPUT
Equalization of Original Image
Roger S. Gaborski 50
histogram Equalization of Light Image
Roger S. Gaborski 51
histogram Equalization of Dark Image
52
Simple Histogram Equalization Example
Roger S. Gaborski
Histogram Equalization
Roger S. Gaborski 53
Input Image Output Image
Adaptive Equalization
• g = adapthisteq(f, parameters..)• Contrast-limited adaptive histogram equalization• Process small regions of the image (tiles) individually• Can limit contrast in uniform areas to avoid noise
amplification
Roger S. Gaborski 54
55Roger S. Gaborski
>> help adapthisteq adapthisteq Contrast-limited Adaptive Histogram Equalization (CLAHE). adapthisteq enhances the contrast of images by transforming the values in the intensity image I. Unlike HISTEQ, it operates on small data regions (tiles), rather than the entire image. Each tile's contrast is enhanced, so that the histogram of the output region approximately matches the specified histogram. The neighboring tiles are then combined using bilinear interpolation in order to eliminate artificially induced boundaries. The contrast, especially in homogeneous areas, can be limited in order to avoid amplifying the noise which might be present in the image.
J = adapthisteq(I) Performs CLAHE on the intensity image I. J = adapthisteq(I,PARAM1,VAL1,PARAM2,VAL2...) sets various parameters. Parameter names can be abbreviated, and case does not matter. Each string parameter is followed by a value as indicated below: 'NumTiles' Two-element vector of positive integers: [M N]. [M N] specifies the number of tile rows and columns. Both M and N must be at least 2. The total number of image tiles is equal to M*N. Default: [8 8].
Adaptive Histogram Equalization
Roger S. Gaborski 56
Grayscale Adaptive Histogram EqualizedGrayscale Histogram Equalized
Default, 8x8 tiles
Adaptive Equalization
Roger S. Gaborski 57
Grayscale Adaptive Histogram Equalized
Number of Tiles: 24x24
Grayscale Adaptive Histogram Equalized
Number of Tiles: 24x24, ClipLimit = .05
Roger S. Gaborski 58
Chapter 3 www.prenhall.com/gonzalezwoodseddins
Roger S. Gaborski 59
Chapter 3 www.prenhall.com/gonzalezwoodseddins
Create a ‘color image’
Roger S. Gaborski 60
>> red = rand(5)
red =
0.0294 0.0193 0.3662 0.7202 0.0302 0.7845 0.3955 0.2206 0.4711 0.2949 0.7529 0.1159 0.6078 0.9778 0.5959 0.1586 0.1674 0.5524 0.9295 0.1066 0.7643 0.6908 0.3261 0.5889 0.1359
>> green = rand(5)
green =
0.2269 0.5605 0.6191 0.0493 0.1666 0.0706 0.4051 0.3297 0.7513 0.6484 0.9421 0.0034 0.8243 0.7023 0.8097 0.8079 0.5757 0.6696 0.9658 0.8976 0.0143 0.3176 0.6564 0.1361 0.0754
>> blue = rand(5)
blue =
0.6518 0.0803 0.8697 0.6260 0.9642 0.5554 0.2037 0.8774 0.5705 0.6043 0.8113 0.8481 0.5199 0.0962 0.8689 0.5952 0.2817 0.6278 0.7716 0.8588 0.5810 0.9290 0.2000 0.1248 0.7606
First create three color planes of data
Roger S. Gaborski 61
>> colorIm(:,:,1)=red;>> colorIm(:,:,2)=green;>> colorIm(:,:,3)=blue;>> colorIm
figure imshow(colorIm, 'InitialMagnification', 'fit')
colorIm(:,:,1) =
0.0294 0.0193 0.3662 0.7202 0.0302 0.7845 0.3955 0.2206 0.4711 0.2949 0.7529 0.1159 0.6078 0.9778 0.5959 0.1586 0.1674 0.5524 0.9295 0.1066 0.7643 0.6908 0.3261 0.5889 0.1359
colorIm(:,:,2) =
0.2269 0.5605 0.6191 0.0493 0.1666 0.0706 0.4051 0.3297 0.7513 0.6484 0.9421 0.0034 0.8243 0.7023 0.8097 0.8079 0.5757 0.6696 0.9658 0.8976 0.0143 0.3176 0.6564 0.1361 0.0754
colorIm(:,:,3) =
0.6518 0.0803 0.8697 0.6260 0.9642 0.5554 0.2037 0.8774 0.5705 0.6043 0.8113 0.8481 0.5199 0.0962 0.8689 0.5952 0.2817 0.6278 0.7716 0.8588 0.5810 0.9290 0.2000 0.1248 0.7606
colorIm
Roger S. Gaborski 62
colorIm(1,1,: )
colorIm(4,4,: )
Roger S. Gaborski 63
colorIm(:,:,1) =
0.0294 0.0193 0.3662 0.7202 0.0302 0.7845 0.3955 0.2206 0.4711 0.2949 0.7529 0.1159 0.6078 0.9778 0.5959 0.1586 0.1674 0.5524 0.9295 0.1066 0.7643 0.6908 0.3261 0.5889 0.1359
colorIm(:,:,2) =
0.2269 0.5605 0.6191 0.0493 0.1666 0.0706 0.4051 0.3297 0.7513 0.6484 0.9421 0.0034 0.8243 0.7023 0.8097 0.8079 0.5757 0.6696 0.9658 0.8976 0.0143 0.3176 0.6564 0.1361 0.0754
colorIm(:,:,3) =
0.6518 0.0803 0.8697 0.6260 0.9642 0.5554 0.2037 0.8774 0.5705 0.6043 0.8113 0.8481 0.5199 0.0962 0.8689 0.5952 0.2817 0.6278 0.7716 0.8588 0.5810 0.9290 0.2000 0.1248 0.7606
• What are two methods to convert from a color image to a gray scale image?
Roger S. Gaborski 64
RECALL
• What are two methods to convert from a color image to a gray scale image?– Average red, green and blue pixels
Roger S. Gaborski 65
Average
• For example:
Roger S. Gaborski 66
>> colorImAverage = ( colorIm(:,:,1) + colorIm(:,:,2) + colorIm(:,:,3) )/3
colorImAverage =
0.3027 0.2200 0.6183 0.4651 0.3870 0.4701 0.3348 0.4759 0.5976 0.5159 0.8354 0.3224 0.6507 0.5921 0.7582 0.5206 0.3416 0.6166 0.8890 0.6210 0.4532 0.6458 0.3942 0.2833 0.3240
>> figure, imshow(colorImAverage, 'InitialMagnification', 'fit')
Gray scale version of color image
Roger S. Gaborski 67
.5976
.5921
Color and Gray scale Images
Roger S. Gaborski 68
Color and Gray scale Images
Roger S. Gaborski 69
Conversion to gray scale results in a loss of information
• What are two methods to convert from a color image to a gray scale image?– Average red, green and blue pixels– Matlab’s rgb2gray function
Roger S. Gaborski 70
MATLAB’s rgb2gray Function
Roger S. Gaborski 71
>> colorIm_rgb2gray = rgb2gray(colorIm)
colorIm_rgb2gray =
0.2163 0.3439 0.5721 0.3156 0.2168 0.3393 0.3792 0.3596 0.6469 0.5377 0.8706 0.1333 0.7249 0.7155 0.7525 0.5895 0.4202 0.6298 0.9328 0.6567 0.3031 0.4989 0.5056 0.2702 0.1716
colorIm and rgb2gray(colorIm)
Roger S. Gaborski 72
How does rgb2gray work?
Roger S. Gaborski 73
rgb2gray converts RGB values to grayscale values by forming a weighted sum of the R, G, and B components:
Gray = 0.2989 * R + 0.5870 * G + 0.1140 * B
Color and Gray Scale Images
Roger S. Gaborski 74