introduction to image processing using the...
TRANSCRIPT
Introduction to Medical Image Processing using Matlab
Nov. 28. 2013
HanbeanYoun
Matlab
• For: for - end is a repetition statement providing a loop for automatic iteration over a range of numbers or objects.
• Usage
• Example: calculate 𝑖=110 𝑖
for i=1:1:N
Operation(i);
end
Sum = 0;
for i=1:1:10
Sum = Sum+i;
end
• for-if combination
Sum = 0;
for i=1:1:10
if mod(i,2)==1
Sum = Sum+i;
end
end
Questions?
• Type F1!!!
• or
051)510-3921
010-4567-9371
기계관 3301호
• Image processing toolbox
• Useful functions: imshow, imread, imwrite, imfilter …
• Image I/O
• Raw images
• “jpg”, “png”, “tif” … : “imread” built-in function
• Dicom images (.dcm): “dicomread” built-in function
• Open “ImgIO.m”
[fid]=fopen(FileName,'r',Endian);
Image=fread(fid,ImageSize,DataType);
fclose(fid);
clear fid;
Histogram
• Open “Histogram.m”
Histogram = zeros(max(max(Img)),1);
for i=1:size(Img,1)
for j=1:size(Img,2)
Histogram(Img(i,j)) = Histogram(Img(i,j))+1;
end
end
• Image display in Matlab
figure,imshow(Img, []) figure,imshow(Img, [0 500]) figure,imshow(Img, [500 1000])
Pixel base operations
• Gray level transformations (gamma transformation)
where A is a normalizing constant
• Implementation
• gamma = 0.5 % 1.0 2.0
• g = f.^gamma
• Open “GammaTran.m”
inAfg
𝛾 = 0.5 𝛾 = 1.0 𝛾 = 2.0
Pixel base operations
• Built-in function for pixel based image transforms
g=imadjust(f,[low_in high_in], [low_out high_out], gamma);
Pixel base operations
• Threshoding
• Open “ImgThreshold.m”
g = h>Threshold; g = h<Threshold;
Pixel base operations
• Multi-image operations (dual energy imaging)
• Open “DESub.m”
𝐷𝐸 = log 𝐼𝐻𝐸 −𝑤 log 𝐼𝐿𝐸
𝑤 = 0.47 𝑤 = 0.79
Pixel base operations
• 2d transformations: imtransform
• B = imtransform(A,tform)
• B = imtransform(A,tform,interp)
• imrotate, imresize
I = imread('cameraman.tif');
tform = maketform('affine',[1 0 0; .5 1 0; 0 0 1]);
J = imtransform(I,tform);
imshow(I), figure, imshow(J)
Transform Type Description
'affine' Affine transformation in 2-D or N-D
'projective' Projective transformation in 2-D or N-D
'custom' User-defined transformation that can be N-D to M-D
'box'Independent affine transformation (scale and shift) in each dimensio
n
'composite' Composition of an arbitrary number of more basic transformations
Image filtering
• Convolution filters
• “imfilter”: N-D filtering of multidimensional images
• Usage: B = imfilter(A,h), where A and h denote input image and convolution filter
• Example: averaging filtering
• Open “ImgConv.m”
1/9 1/9 1/9
1/9 1/9 1/9
1/9 1/9 1/9
Filter = ones(3,3)/9;
B = imfilter(A,Filter);
• Example: Sharpen filter
-0.167 -0.667 -0.167
-0.667 4.333 -0.667
-0.167 -0.667 -0.167
Adjust size of blurring filter
Non-linear filter
• Median filtering (open “ImgMedf.m”)
G = medfilt2(DE,[Size Size])
• Comparison
• Median filtering is very suitable for “salt and pepper” noise
• Compare with average filter
Noisy image Average filtering Median filtering
• Unsharp masking
• To enhance edge by emphasizing the high-frequency part and assigning it a higher weight
• Open “ImgUnsharp.m”
Unsharp masking applied to lower part of image
• Implementation
Alpha = 0.5;
Filter = ones(3,3)/9;
Unsharp = Img-imfilter(Img,Filter);
ImgFin = Img+Alpha*Unsharp;
Adjust 𝛼 and size of blurring filter
Frequency domain operations
• 2d Fourier transform in Matlab
• fft2 returns the two-dimensional discrete Fourier transform (DFT) of X.
• The DFT is computed with a fast Fourier transform (FFT) algorithm.
• Usage: Y = fft2(X), Y = fft2(X,m,n)
• Low pass filters
-1
-0.5
0
0.5
1
-1
-0.5
0
0.5
10
0.2
0.4
0.6
0.8
1
Fx
Fy
Magnitude
-1
-0.5
0
0.5
1
-1
-0.5
0
0.5
10.2
0.4
0.6
0.8
1
Fx
Fy
Magnitude
• High pass filters
-1
-0.5
0
0.5
1
-1
-0.5
0
0.5
10
2
4
6
8
Fx
Fy
Magnitude
-1
-0.5
0
0.5
1
-1
-0.5
0
0.5
10
1
2
3
4
5
6
Fx
Fy
Magnitude
M = 128;
N = 128;
u = (0:(M-1));
v = (0:(N-1));
idx = find(u > M/2);
u(idx) = u(idx)-M;
idy = find(v > N/2);
v(idy) = v(idy)-N;
[V, U] = meshgrid(v, u);
D = hypot(U,V);
D0 = 2000;
H = 1-exp(-(D.^2)/(2*D0^2));
figure,surf(fftshift(H));
colormap jet; shading interp
-1
-0.5
0
0.5
1
-1
-0.5
0
0.5
10.2
0.4
0.6
0.8
1
Fx
Fy
Magnitude
실습
• Oepn “ImgFilt.m”
실습
• Filter의모양을바꾸어가면서필터링을해보자
고주파강조필터링
• 고역통과필터는 dc항을 0으로제거하므로, 영상의평균값을 0으로만든다. 따라서에지검출마스크가된다.
• 이를보상하기위해사용되는한가지방법은고역통과필터에오프셋(offset)을더하는것이다.
• 오프셋이필터를 1보다큰상수로곱한것과결합될때, 이접근방식은고주파강조필터링으로불린다.
• 고주파강조필터들은다음의전달함수를갖는다.
• Open “ImgFilt_H.m”
𝐻𝐻𝐹𝐸 𝑢, 𝑣 = 𝑎 + 𝑏𝐻𝐻𝐹(𝑢, 𝑣)
실습
f = ReadRaw('HeadandNeck.raw',[1024 768],'uint16','l');
F=fftshift(fft2(f));
M = 1024;
N = 768;
u = (0:(M-1));
v = (0:(N-1));
idx = find(u > M/2);
u(idx) = u(idx)-M;
idy = find(v > N/2);
v(idy) = v(idy)-N;
[V, U] = meshgrid(v, u);
DSQ = U.^2 + V.^2;
D = hypot(U,V);
D0 = 100;
H = 0.5+2*(1-exp(-(D.^2)/(2*D0^2)));
H = ifftshift(H);
G = F.*H;
g = abs(ifft2(G));
WriteRaw('Result.raw',[1024 768],'double','l');
Gray level operations
• Histogram equalization
• Histogram equalization is a method in image processing of contrast adjustment using the image's histogram.
Original image
Calculate histogram
Cumulative sum and normalize
Gray level re-mapping
Enhanced image
• Implementation
• Histogram calculation
• Cumulative sum
• Normalization
Histogram = zeros(max(max(Img)),1);
for i=1:size(Img,1)
for j=1:size(Img,2)
Histogram(Img(i,j)) = Histogram(Img(i,j))+1;
end
end
for i=1:numel(Histogram);
CumSum(i) = sum(Histogram(1:i));
end
ni = CumSum/size(Img,1)/size(Img,2)*1024;
• Re-mapping
Img2 = zeros(size(Img));
for i=1:size(Img,1)
for j=1:size(Img,2)
Img2(i,j) = round(ni(Img(i,j)))+1;
end
end