digital image processing practice 3
TRANSCRIPT
Digital Image ProcessingPractice 3
Atsushi OsaYamaguchi Univ.
How to attend this on-line practice1. Download materials for the practice from the course web
site of Digital Image Processing: – http://ds.cc.yamaguchi-u.ac.jp/~samura/web-login/DIP/
2. Watch a video for this practice (on-demand service) indicated in the course web site.
3. Send me your answers for some exercises by e-mail. They are indicated in the video and the slideshow.
4. When you finish today’s exercises, you may close this Webex meeting.
5. The Webex meeting will be closed at the end of the practice.
2
• Let’s watch the video for Practice 3.
• You may close the Webex window as it will interfere with the exercise.
• Visit the Webex meeting when you need it.
3
Preparation 1-download materials-
Download materials for today’s practice from the course web site: http://ds.cc.yamaguchi-u.ac.jp/~samura/web-login/DIP/
1.Slideshow: dip-p3.pdf2.Materials: dip-m3.zip
3
Preparation 2
1. Open your “Documents” folder, and insideopen the “processing” folder.
2. Decompress the “dip-m3.zip” file.3. Move the “dip-m3” folder inside the
“processing” folder.
5
Today’s topics
• Histogram• Contrast change• Automatic gain control
Histogram• The histogram of a digital image with intensity
levels within the range [0,L-1] is a discrete function h(rk) = nk, where rk is the k thintensity value and nk is the number of pixels in the image with intensity rk.
nk
rk0 1 2 3 252 253 254 255
n0
n1n2
n3
・・・
n255
L-1
Exercise 3.1:
Q. What is the summation of nk within the range [0,L-1] ?
A.
Exercise 3.1: Answer
Q. What is the summation of nk within the range [0,L-1] ?
A. The total number of pixels in the image
M rows
N columns
∑−
=
×=1
0
L
kk NMn
Histogram
A histogram shows the basic intensity characteristics of an image: dark, light, low contrast, and high contrast.
Exercise 3.2
• Load the “ex3_2” sketch.• Run the sketch.
Histogram
Input image: f(x,y)
inImage[x][y] in this sketch.
Output image: g(x,y)
outImage[x][y] in this sketch
Histogram of the input image
Histogram of the output image
0rk 255
Attention! Black pixels only in these images
Intensity level: you can select the level
Number of pixels: 200 (width) x 200 (height)
equal
Histogram
nk
rk0 1 2 3 252 253 254 255
n0=40000
n1=0
n2=0・・・
n3=0
nk=0 n255=0
Exercise 3.3
• Change the code as follows:
• Save the sketch with a new name.• Run the sketch.
String fileName=“black.bmp”;
String fileName=“gradation.bmp”;
Histogram of a gradation image
Input image: f(x,y)
inImage[x][y] in this sketch.
Output image: g(x,y)
outImage[x][y] in this sketch
Histogram of the input image
Histogram of the output image
0rk 255
All gray levels have the same number of pixels.
Flat
Exercise 3.4
• Change the code as follows:
• Save the sketch with a new name.• Run the sketch.
String fileName=“gradation.bmp”;
String fileName=“sunflower.bmp”;
Histogram of a high-contrast image
0rk 255
Exercise 3.5: Answer the correct combinations between the images and histograms
0 rk255
A B C D
α
β
γ
δ
A
B
C
D
Send your answers by e-mail now
Pause the video until you send your answers to me.
19
Exercise 3.6
• Change the code as follows:
• Save the sketch as a new name.• Run the sketch.
String fileName=“sunflower.bmp”;
String fileName=“sunflower0.bmp”;
Histogram of a dark image
0rk 255
Exercise 3.7
• Change the code as follows:
• Save the sketch with a new name.• Run the sketch.
String fileName=“sunflower0.bmp”;
String fileName=“sunflower1.bmp”;
Histogram of a light image
0rk 255
Exercise 3.8
• Change the code as follows:
• Save the sketch with a new name.• Run the sketch.
String fileName=“sunflower1.bmp”;
String fileName=“sunflower2.bmp”;
Histogram of a low-contrast image
0rk 255
Histogram Processing
Histogram manipulation can be used for image enhancement.
Basic intensity transformation functionIdentity transformation
Negative transformation
Logarithmic transformation
Power-Law (Gamma) transformation
f
g
g(x, y) = f(x, y)
g(x, y) = L-1 -f(x, y)
i.e. g(x, y) = 255 -f(x, y)
g(x, y) = C log( f(x, y) )
g(x, y) = C f γ(x, y)
Contrast stretching• Contrast stretching expands the range of
intensity levels in an image so that it spans the full intensity range of the recording medium or display device.
input image: f
outp
ut im
age:
g
0 255
255
0
outp
ut im
age:
g
0 255
255
0
input image: f
Identitytransformation
Contrast stretching
f1 f2
Contrast stretchingrange [0,f1] in input image f
range [f1,f2]
range[f2,255]outp
ut im
age:
g
0 255
255
0
input image: f
Contrast stretching
f1 f2
0=g
( )112
255 ffff
g −−
=
255=g
Exercise 3.9
• Read the sketch for exercise 3.8 (low-contrast). • Change the code as follows:
• Save the sketch as a new name.• Run the sketch.
void imageProcessing(){for (int x = 0; x < imageWidth; x++ ) {
for (int y = 0; y < imageHeight; y++ ) {outImage[x][y] = inImage[x][y];
}}
}next page
Exercise 3.9: Step 1void imageProcessing(){
float f1 = 100;float f2 = 150;for(int y=0;y<imageHeight;y++){for(int x=0;x<imageWidth;x++){
if(inImage[x][y]<f1){
}if(inImage[x][y]>=f1 && inImage[x][y]<=f2){
} if(inImage[x][y]>f2){
}}
}}
outp
ut im
age:
g
0 255
255
0
input image: f
f1 f2
question 1 of step 1
I will ask you in Step2
question 2 of step 1Logic Operation
AND
If statement
if(inImage[x][y]<f1){
}
1.Test thisif trueif false
2.Run codes inside these curly brackets
exit
Exercise 3.9: answer of Step 1void imageProcessing(){
float f1 = 100;float f2 = 150;for(int y=0;y<imageHeight;y++){for(int x=0;x<imageWidth;x++){
if(inImage[x][y]<f1){outImage[x][y]=0;
}if(inImage[x][y]>=f1 && inImage[x][y]<=f2){
} if(inImage[x][y]>f2){
outImage[x][y]=255;}
}}
}
I will ask you in Step2
Exercise 3.9: Step 2void imageProcessing(){
float f1 = 100;float f2 = 150;for(int y=0;y<imageHeight;y++){for(int x=0;x<imageWidth;x++){
if(inImage[x][y]<f1){outImage[x][y]=0;
}if(inImage[x][y]>=f1 && inImage[x][y]<=f2){
} if(inImage[x][y]>f2){
outImage[x][y]=255;}
}}
}
Answer the correct code
• Send your answer by e-mail
• Pause the video until you send your answers to me.
Answervoid imageProcessing(){
float f1 = 100;float f2 = 150;for(int y=0;y<imageHeight;y++){for(int x=0;x<imageWidth;x++){
if(inImage[x][y]<f1){outImage[x][y]=0;
}if(inImage[x][y]>=f1 && inImage[x][y]<=f2){
outImage[x][y]=(255/(f2-f1))*(inImage[x][y]-f1);} if(inImage[x][y]>f2){
outImage[x][y]=255;}
}}
}
( )112
255 ffff
g −−
=
Exercise 3.10
• Change the code as follows:
• Change the values of two variables f1 and f2 in order to obtain a high-contrast image.
• Save the sketch with a new name.• Run the sketch.
String fileName=“sunflower2.bmp”;
String fileName=“sunflower0.bmp”;
float f1 = 100;float f2 = 150;
dark image
Ideal f1 and f2 for this image
float f1 = 14;float f2 = 92;
14 92
Exercise 3.11
• Change the code as follows:
• Change the values of the two variables f1 and f2 in order to obtain a high-contrast image.
• Save the sketch with a new name.• Run the sketch.
String fileName=“sunflower0.bmp”;
String fileName=“sunflower1.bmp”;
float f1 = 100;float f2 = 150;
light image
Ideal f1 and f2 for this image
float f1 = 180;float f2 = 241;
180 241
Automatic Gain Control (AGC)
• To find suitable values of f1 and f2 automatically,
nk s are zero in the range [0, min-1] nk s are zero in the range [max+1, 255]min max
Exercise 3.12
• Load the sketch for exercise 3.11. • Change the code as shown on the next slide.• Save the sketch with a new name.• Run the sketch.
void imageProcessing(){float f1 = 100;float f2 = 200;for(int y=0;y<imageHeight;y++){
for(int x=0;x<imageWidth;x++){if(inImage[x][y]<f1){
outImage[x][y]=0;}
if(inImage[x][y]>=f1 && inImage[x][y]<=f2){outImage[x][y]=(255/(f2-f1))*(inImage[x][y]-f1);
} if(inImage[x][y]<f2){
outImage[x][y]=255;}
}}}
float f1=0;float f2=255;for(int i=0;i<256;i++){if(inHistogram[i]>0){f1=i;break;
}}for(int i=255;i>=0;i--){if(inHistogram[i]>0){f2=i;break;
}}
Exercise 3.11
Exercise 3.12
Searching the min and max
Exercise 3.13:
• Change the name of the input image to these other files.
• Save the sketch with a new name.• Run the sketch.
String fileName=“sunflower0.bmp”;
String fileName=“sunflower2.bmp”;
• That’s all for today.• You may close today’s Webex meeting.• If you have any questions, please ask me by
the Webex meeting or e-mail.