Intensity Transformations and Spatial Filtering

advertisement
Digital Image Processing
Session 3
Dr. Z. R. Ghassabi
z.r.ghassabi@gmail.com
Tehran shomal University
Spring 2015
1
Outline
•
•
•
•
•
•
•
•
•
•
•
Introduction
Digital Image Fundamentals
Intensity Transformations and Spatial Filtering
Filtering in the Frequency Domain
Image Restoration and Reconstruction
Color Image Processing
Wavelets and Multi resolution Processing
Image Compression
Morphological Operation
Object representation
Object recognition
2
Outline of Chapter 3
• Basic Intensity Transformation Functions
• Negative, Log, Gamma
• Piecewise-Linear Transformation Functions
• Contrast stretching, contrast slicing, bit-plane slicing
• Histogram Processing
• Histogram Stretching, Histogram Shrink, Histogram Sliding, Histogram
equalization, adaptive local histogram, histogram matching, local histogram
equalization, histogram statistics
• Fundamentals of Spatial Filtering
• Smoothing Spatial Filters, Sharpening Spatial Filters, Combining Spatial
Enhancement Tools
Image Enhancement
• Methods
– Spatial Domain:
• Linear
• Nonlinear
– Frequency Domain:
• Linear
• Nonlinear
Image Enhancement
• Spatial Domain
g  x, y   T  f  x, y  
Image Enhancement
• Example: Image Subtraction for enhancing Differences
g x , y   f x , y   h x , y 
Image Enhancement
• Frequency Domain
Image Transforms
Image Enhancement in spatial Domain (Transformation)
•
•
For 11 neighborhood: s  T  r 
– Contrast Enhancement/Stretching/Point process
For w w neighborhood:
– Filtering/Mask/Kernel/Window/Template Processing
Image Enhancement in spatial Domain
Negative
nth root
Log
nth power
Identity
Inverse Log
Input gray level, r
Some Basic Intensity
Transformation Functions
Image Negatives
• Image Negatives:
s  L 1  r
y
y=L-x
L
0
L
x
Image Negatives
Log Transformation
Log Transformation
y
y  c log 10 (1  x)
0
L x
c=100
Log Transformation
Range Compression
Power-Law(Gamma) Transformations
s  c r   

Power-Law(Gamma) Transformations
Gamma Correction:
r1.8
r
1.8
2.5 
2.5
 1
r1.8
2.5
Power-Law(Gamma) Transformations (Effect of decreasing gamma)
Original
 =0.6
 =0.4
 =0.3
Power-Law(Gamma) Transformations
(Effect of increasing gamma)
Power-Law(Gamma) Transformations
• Medical Example
Piecewise-Linear Transformation Functions
• Contrast Stretching
• Contrast slicing
• Bite-Plane slicing
Contrast Stretching
y
x


y    ( x  a )  ya
  ( x  b)  y
b

0 xa
a xb
bxL
yb
ya
0
a b
a  50, b  150,   0.2,   2,   1, ya  30, yb  200
L
x
Contrast stretching
Original
C. S.
THR.
Contrast Stretching
Contrast Stretching
y
Clipping:
0


y   ( x  a)
  (b  a )

0 xa
a xb
bxL
0
a b
a  50, b  150,   2
L
x
MATLAB Tutorial
• imadjust(I, [low-in, high-in],[low-out, high-out])
Output Image
high-out
low-out
low-in
high-in
Input Image
MATLAB Tutorial
• imadjust(I, [low-in, high-in],[low-out, high-out], gamma)
Output Image
high-out
low-out
low-in
high-in
Input Image
• imadjust(I, [0.1, 0.7],[0, 1], gamma)
‫مناسب ترین مقدار چیست؟‬
MATLAB Tutorial
• Use Min and Max gray-levels
– Low-in: Double(min(I(:))/255)
– max-in: Double(max(I(:))/255)
• Use stretchlim(I)
imadjust(I,stretchlim(I) ,[low-out, high-out], gamma)
MATLAB Tutorial
Output Image
high-out
Y= 𝐿𝑜 + (ℎ𝑜 −
𝑥−𝐿𝑖
𝐿𝑜 ) (
)
ℎ𝑖 −𝐿𝑖
low-out
Input Image
low-in
high-in
Y=
𝑥−𝐿𝑖 𝛾
𝐿𝑜 + (ℎ𝑜 − 𝐿𝑜 )(
)
ℎ𝑖 −𝐿𝑖
Y=
log(𝑓+1) 𝛾
𝐿𝑜 + (ℎ𝑜 − 𝐿𝑜 )(
)
log 2
Gray-level Slicing
Gray-level Slicing
Gray-level Slicing
Gray-level Slicing
Gray-level Slicing
Bit-plane Slicing
Highlighting the contribution made to total image appearance by specific bits
Suppose each pixel is represented by 8 bits
Higher-order bits contain the majority of the visually significant data
Useful for analyzing the relative importance played by each bit of the image
Bit-plane Slicing
Bit-plane Slicing
The (binary) image for bit-plane 7 can be obtained by
processing the input image with a thresholding gray-level
transformation.
Map all levels between 0 and 127 to 0
Map all levels between 129 and 255 to 255
Bit-plane Slicing
Fractal Image
Bit-plane Slicing
Fractal Image
Bit-plane 7
Bit-plane 6
Bit-plane 5
Bit-plane 4
Bit-plane 3
Bit-plane 2
Bit-plane 1
Bit-plane 0
Bit-plane Slicing
Histogram Processing
Enhancement based on statistical Properties: Local, Global
Histogram Definition
h(rk)=nk
Where rk is the kth gray level and nk is the number of pixels in the image having gray level rk
Normalized histogram:
P(rk)=nk/n
Histogram of an image represents the relative frequency of occurrence of various gray levels in the
image
Histogram Example
MATLAB Tutorial
• Hist(double(I(:)),50)
• imhist(I)
Histogram Examples
• Histogram Visual Meaning:
– Dark
– Bright
– Low Contrast
– High Contrast
Histogram Modification
• Histogram Stretching
• Histogram Shrink
• Histogram Sliding
Histogram Stretching
Stretch (I(r,c))=
𝐼 𝑟, 𝑐
𝑀𝐴𝑋
𝐼 𝑟,𝑐 −𝐼 𝑟,𝑐 𝑀𝐼𝑁
𝐼 𝑟,𝑐 𝑀𝐴𝑋 −𝐼 𝑟,𝑐 𝑀𝐼𝑁
𝑀𝐴𝑋 − 𝑀𝐼𝑁 + 𝑀𝐼𝑁
is the largest gray-value in the image I(r,c)
𝐼 𝑟, 𝑐
𝑀𝐼𝑁
is the smallest gray-level in I(r,c)
𝑀𝐴𝑋 𝑎𝑛𝑑 𝑀𝐼𝑁 𝑐𝑜𝑟𝑟𝑒𝑠𝑝𝑜𝑛𝑑𝑠 𝑡𝑜 𝑡ℎ𝑒 𝑚𝑎𝑥𝑖𝑚𝑢𝑚 𝑎𝑛𝑑 𝑚𝑖𝑛𝑖𝑚𝑢𝑚
𝑔𝑟𝑎𝑦 − 𝑙𝑒𝑣𝑒𝑙 𝑣𝑎𝑙𝑢𝑒𝑠 𝑝𝑜𝑠𝑖𝑏𝑙𝑒 (for an 8-bit image these are 0 and 255)
Histogram Stretching
Histogram Stretching
Histogram Shrinking
Shrink (I(r,c))=
𝐼 𝑟, 𝑐
𝑆ℎ𝑟𝑖𝑛𝑘𝑀𝐴𝑋 −𝑆ℎ𝑟𝑖𝑛𝑘𝑀𝐼𝑁
𝐼 𝑟,𝑐 𝑀𝐴𝑋 −𝐼 𝑟,𝑐 𝑀𝐼𝑁
𝑀𝐴𝑋
((𝑟, 𝑐) − 𝐼(𝑟, 𝑐)𝑀𝐼𝑁 + 𝑆ℎ𝑟𝑖𝑛𝑘𝑀𝐼𝑁
is the largest gray-value in the image I(r,c)
𝐼 𝑟, 𝑐
𝑀𝐼𝑁
is the smallest gray-level in I(r,c)
Histogram Shrinking
Histogram Sliding
‫‪Histogram Equalization‬‬
‫متغیر تصادفی ‪U‬‬
‫‪1‬‬
‫‪PDF‬‬
‫‪CDF‬‬
‫‪x‬‬
‫‪0‬‬
‫تناظر بین ‪ x‬و ‪ U‬اکیدا صعودی بودن ‪ CDF‬هست‪.‬‬
‫برای اکیدا صعودی بودن تابع ‪ CDF‬باید مشتق آن یعنی ‪ PDF‬همیشه بزرگتر از صفر باشد‪.‬‬
‫]‪F: X-------->[0,1‬‬
‫)‪U=F(X‬‬
‫چون تابع یک به یک هست پس معکوس هم دارد‪X=F-1(U) .‬‬
Histogram Equalization
s  T ( r ), 0  r  1
r  T 1 ( s), 0  s  1
‫‪Histogram Equalization‬‬
‫• توزیع آماری ‪ u‬مستقل از مقدار ‪x‬‬
‫و همیشه یکنواخت بین ‪ 0‬و یک هست‪.‬‬
‫)‪• u~U(0,1‬‬
‫• ‪ X‬های ورودی را بین ‪ 0‬و یک توزیع‬
‫میکند‪.‬‬
‫هیستوگرام ‪U‬‬
‫‪Histogram Equalization‬‬
‫هیستوگرام تصویر ورودی‬
‫‪PDF‬‬
‫‪1‬‬
‫‪Pr‬‬
‫)‪S=T(r‬‬
‫‪CDF‬‬
‫‪r‬‬
‫‪0‬‬
‫هیستوگرام ‪U‬‬
‫‪ r‬روشنایی تصویر ورودی‬
‫سطح زیر منحنی‬
‫𝑟‬
‫)‪S~U(0,1‬‬
‫=𝑠‬
‫𝜌𝑑 𝜌 𝑟𝑃‬
‫‪0‬‬
‫هیستوگرام تصویر ورودی‬
Pr
PDF
Histogram Equalization
1
S=T(r)
CDF
‫ روشنایی تصویر ورودی‬r
r
0
ℎ𝑖𝑠𝑡𝑜𝑔𝑟𝑎𝑚: (𝑟𝑗 , 𝑛𝑗 )
𝑛𝑗
Pr{𝑟 = 𝑟𝑗 } =
𝑛
𝑗
𝑆𝑗 =
𝑘=0
𝑛𝑗
𝑛
≤1
Histogram Equalization
Histogram Equalization
MATLAB Tutorial
• histeq(I)
Histogram Equalization
Adaptive Contrast Enhancement (ACE)
𝐴𝐶𝐸 = 𝑘1
𝑚𝐼 𝑟,𝑐
𝐼 𝑟, 𝑐 − 𝑚𝐿 (𝑟, 𝑐) + 𝑘2 (𝑚𝐿 (𝑟, 𝑐))
𝜎𝐿 (𝑟, 𝑐)
𝑚𝐿 𝑟, 𝑐 : 𝑚𝑒𝑎𝑛 𝑓𝑜𝑟 𝑡ℎ𝑒 𝑒𝑛𝑡𝑖𝑟𝑒 𝑖𝑚𝑎𝑔𝑒 𝐼(𝑟, 𝑐)
𝜎𝐿 : Local standard Deviation (in the window under consideration)
𝑚𝐿 = Local mean (average mean in the window under consideration)
Adaptive Contrast Enhancement (ACE)
Adaptive Contrast Enhancement (ACE)
MATLAB Tutorial
• I2=adapthisteq( I,'clipLimit',0.015,'Distribution','rayleigh');
clear all;
close all;
clc;
[f1, pp1] = uigetfile('*.jpg', 'Pick a image');
I1Name = sprintf('%s%s',pp1,f1); I=imread(I1Name);
if ndims(I)==3
I1=rgb2gray(I);
end
I2=adapthisteq( I1,'clipLimit',0.015,'Distribution','rayleigh');
figure();
subplot(1,2,1); imshow(I1);
subplot(1,2,2); imshow(I2);
Adaptive Histogram Equalization
Histogram Matching
pr
Histeq(I,hgram)
pz
1
exp (− 2𝜎2 𝑥 − 𝜇 2 )
𝜇
1
1
𝑤1 exp (− 2𝜎 2 𝑥 − 𝜇1 2 ) + 𝑤2 exp ( − 2𝜎 2 𝑥 − 𝜇2 2 )
1
2
Pr
Histogram Matching
S
PDF
CDF
S=T(r)
0
0
r
‫هیستوگرام تصویر ورودی‬
pz
r
Histogram matching:
Obtain the histogram of the given image, s=T(r)
Precompute a mapped level Sk for each level rk
k
k
nj
j 0
n
sk  T (rk )  ( L  1) pr (rj )  ( L  1)
j 0
, k  0,1,2,..., L  1
Obtain the transformation functionk G from the given pz(z)
G ( z k )  ( L  1) p z ( zi ) sk , k  0,1,2,..., L  1
i 0
Precompute Zk for each value of rk
Map rk to its corresponding level Sk; then map level Sk into the final level Zk
z k  G 1[T ( rk )],
k  0,1,2,..., L  1
‫یستوگرام دلخواه‬
‫)‪Histogram Matching (Specification‬‬
‫برای یک تصویر ‪ 64*64‬هیستوگرام طوری تبدیل شود که دارای‬
‫مقادیر مشخص شده در شکل ‪ b‬باشد‪.‬‬
‫)‪Histogram Matching (Specification‬‬
‫هر پیکسلی که مقدارش در تصویر تعدیل هیستوگرام یک هست‬
‫به مقداری برابر سه در هیستوگرام مشخص شده‬
‫‪S0=1 , G(z3)=1,‬‬
‫می‪s0---‬‬
‫نگاشت‪>z3‬‬
‫شود‪.‬‬
Histogram Matching (Specification)
Image is dominated by large, dark areas, resulting in a histogram characterized by a large
concentration of pixels in pixels in the dark end of the gray scale
Histogram Matching (Specification)
Notice that the output histogram’s low end has shifted right
toward the lighter region of the gray scale as desired.
Histogram Matching (Specification)
Desired
Initial CDF
Modified CDF
Local Histogram Processing
a)
b)
c)
Original image
global histogram equalization
local histogram equalization using 7x7 neighborhood.
Local Histogram Processing
Histogram using a local 3*3 neighborhood
Using Histogram Statistics
Use of histogram statistics for image enhancement:
r denotes a discrete random variable
P(ri) denotes the normalized histogram component corresponding to the ith value of r
Mean:
m
L 1
 r p(r )
i 0
The nth moment:
i
i
L 1
 n ( r )   ( ri  m) n p ( ri )
i 0
The second moment:
L 1
 2 ( r )   ( ri  m) 2 p ( ri )
i 0
Using Histogram Statistics
Global enhancement: The global mean and variance are measured over an entire image
Local enhancement: The local mean and variance are used as the basis for making changes
rs,t is the gray level at coordinates (s,t) in the neighborhood
P(rs,t) is the neighborhood normalized histogram component
mean:
mS xy 
local variance:
 S2 
xy
r
s ,t
( s ,t )S xy
 [r
( s ,t )S xy
s ,t
p ( rs ,t )
 mS xy ]2 p ( rs ,t )
Using Histogram Statistics
Mapping:

 E. f  x, y  mS  x, y   K 0 M G and k1DG   S  x, y   k 2 DG
g  x, y   
O.W

 f  x, y 
E,K0,K1,K2 are specified parameters
MG is the global mean
DG is the global standard deviation
Using Histogram Statistics
• A SEM sample images:
Using Histogram Statistics
Local Mean
Local Var
E or one
Using Histogram Statistics
• Enhanced Images:
Fundamental of Spatial Filtering
Fundamental of Spatial Filtering
The Mechanics of Spatial Filtering:
g ( x, y )  w( 1,1) f ( x  1, y  1) 
w( 1,0) f ( x  1, y )   
w(0,0) f ( x, y )   
w(1,0) f ( x  1, y ) 
w(1,1) f ( x  1, y  1)
Correlation
g ( x, y ) 
a

b
 w( s, t ) f ( x  s, y  t )
-a
s   at   b
Image size: M×N, x= 0,1,2,…,M-1 and y= 0,1,2,…,N-1
Mask size: m×n, a=(m-1)/2 and b=(n-1)/2
+a
-b
+b
Fundamental of Spatial Filtering
Spatial Correlation and Convolution
Spatial Correlation and Convolution
Vector Representation ofof
Linear
Filtering
Vector Representation
Linear
Filtering
R  w1 z1  w2 z 2  ...  w9 z9

9
w z
i 1
i
i
Smoothing Spatial Filters
Smoothing Linear Filters :
Noise reduction
Smoothing of false contours
Reduction of irrelevant detail
a
g ( x, y ) 
b
  w( s, t ) f ( x  s, y  t )
s   at   b
a
b
  w( s, t )
s   at   b
1 9
R   zi
9 i 1
Smoothing Spatial Filters
Image smoothing with masks of various sizes.
Smoothing Spatial
FiltersFilters
Smoothing
Spatial
MATLAB Tutorial
• W=repmat(1/9,3,3);
• W=1/9*ones(3,3);
• a=2;
• W=ones(2*a+1)
• W=W/sum(W(:));
• Img1=imread(‘image1.jpg’);
• Img2=imfilter(Img1,W);
MATLAB Tutorial
• Img2=imfilter(Img1,W,’symmetric’);
Order-Static (Nonlinear) Filters
Order-statistic filters:
Max
Min
Median filter: Replace the value of a pixel by the median of the gray levels in the
neighborhood of that pixel
 Noise-reduction
Median Filter
Sharpening Spatial Filters
The first-order derivative:
f
 f ( x  1)  f ( x )
x
Zero in flat region
Non-zero at start of step/ramp region
Non-zero along ramp
The second-order derivative
2 f
 f ( x  1)  f ( x  1)  2 f ( x)
x 2
Zero in flat region
Non-zero at start/end of step/ramp region
Zero along ramp
Sharpening Spatial Filters
Sharpening Spatial Filters
Sharpening Spatial Filters
Use of second derivatives for enhancement-The Laplacian:
Development of the method
2 f
2 f
 f 

x 2
y 2
2
2 f
 f ( x  1, y )  f ( x  1, y )  2 f ( x, y )
x 2
2 f
 f ( x, y  1)  f ( x, y  1)  2 f ( x, y )
y 2
 2 f   f  x  1, y   f  x, y  1  f  x  1, y   f  x, y  1  4 f  x, y  
Sharpening Spatial Filters
Practically use:
Sharpening Spatial Filters

f
g  x, y   

f
 x, y    2 f  x , y 
 x, y    2 f  x , y 
0 1 0 
1 4 1  90 isotropic


0 1 0 
0
 1


0
1
5
1
0
1

0

90 isotropic
 sign
 sign
1 1 1
1 8 1 45 isotropic


1 1 1
 1
 1


 1
1
9
1
1
1

1

45 isotropic
Sharpening Spatial Filters
 f  x, y    2 f  x, y   sign
g  x, y   
2
f
x
,
y


f  x, y   sign



SEM image
Two L. Mask
a. Mask Result
Sharpening Spatial Filters
b. Mask Result
(Sharper)
Unsharp Masking and Highboost Filtering
Unsharp masking
Subtract a blurred version of an image from the image itself
g mask ( x, y )  f ( x, y )  f ( x, y )
f(x,y) : The image, f̄(x,y): The blurred image
High boost Filtering:
g ( x, y )  f ( x, y )  k * g mask ( x, y )
g ( x, y)  f ( x, y)  k * g mask ( x, y)
,k 1
,k 1
Unsharp Masking and Highboost Filtering
Unsharp Masking and Highboost Filtering
f HB  x, y   Af  x, y   f  x, y  ,
A 1
f HB  x, y    A  1 f  x, y   f S  x, y 
 Af  x, y    2 f  x, y   sign
f HB  x, y   
2
 Af  x, y    f  x, y   sign
Unsharp Masking and Highboost Filtering
Original
Laplacian (A=1)
Laplacian (A=0)
Laplacian (A=1.7)
Using First-Order Derivative for (Nonlinear) Image
Sharpening - The Gradient
Using first-order derivatives for (nonlinear) image sharpening, The gradient:
The gradient:
 f 
 x 
gx 
f  
 

f 
g y 



y




The magnitude is rotation invariant (isotropic)

M ( x, y )  mag (f )  G x2  G y2
 f  2  f  2 
 
 
 y 
 
x 



 


1

1
2
2
M ( x, y )  g x  g y
g x  ( z9  z5 )
and
g y  ( z8  z 6 )

M ( x, y)  ( z9  z5 ) 2  ( z8  z6 ) 2
M ( x, y)  z9  z5  z8  z6
Roberts Cross Gradient
Sobel
(2 1 for prewitt)

1
2
Using the Sobel
Gradient
for
Image
Gradient
Sharpening
Combining Spatial Enhancement Tools
Bone Scan
Original +Laplacian
Laplacian
Soble of
Original
Combining Spatial Enhancement Tools
Smoothed Sobel
Orig.+
(Orig. + L.)*S.Sobel
(Orig. + L.)*S.Sobel
Apply Power-Law
MATLAB Tutorial
•
Img0=im2double(img0);
•
•
•
•
•
w=fspecial(type, parameters)
W=fspecial(‘disk’,3);
W=fspecial(‘gaussian’,101,10);
W=fspecial(‘laplacian’,0);
W=fspecial(‘log’,10,1);
•
•
•
Img1=imfilter(Img0,W);
figure();imshow(normalize(Img1));
C=-1; figure();imshow(img0+C*Img1);
•
•
Wp=fspecial(‘prewitt’); Ws=fspecial(‘prewitt’);
Img1=imfilter(Img0,Wp); Img2=imfilter(Img0,Ws);
•
W=0.3*Wp+0.7*Ws; Img1=imfilter(Img0,W);
•
•
Img1=imfilter(Img0,Wp); Img2=imfilter(Img0,Wp’);
Imshow(sqrt(Img1.^2+Img2.^2)
MATLAB Tutorial
function XN=Normalize(X)
Xmin=min(X(:));
Xmax=max(X(:));
XN=((X-Xmin)/(Xmax-Xmin)).^beta;
end
MATLAB Tutorial
Img0=im2double(imread(‘….’));
M=3;N=5;
Domain=ones(M,N);
Img1=ordfilt2(img0,M*N, Domain);
Img2=ordfilt2(img0,0, Domain);
Img3=ordfilt2(img0,(M+N+1)/2, Domain);
figure();
subplot(1,3,1); imshow(Img1);
subplot(1,3,2); imshow(Img2);
subplot(1,3,3); imshow(Img3);
MATLAB Tutorial
Img0=im2double(imread(‘….’));
M=3;N=5;
Domain=ones(M,N);
Img1=imnoise(img0,’salt & pepper’);
Img2=ordfilt2(img1,(M+N+1)/2, Domain);
Img3=medfilt2(img1,[M N]);
figure();
subplot(2,2,1); imshow(Img0);
subplot(2,2,2); imshow(Img1);
subplot(2,2,3); imshow(Img2);
subplot(2,2,3); imshow(Img3);
MATLAB Tutorial
• MATLAB Command:
– Image Statistics:
• means2, std2, corr2, imhist, regionprops
– Image Intensity Adjustment:
• imadjust, histeq, adapthisteq, imnoise
– Linear Filter:
• imfilter, fspecial, conv2, corr2,
– Nonlinear filter:
• medfilt2, ordfilt2,
MATLAB Tutorial
End of Session 3
Download