Upload
denis-lambert
View
225
Download
1
Tags:
Embed Size (px)
Citation preview
Introduction to Image Introduction to Image Processing with MATLAB Processing with MATLAB
Wally BlockWally Block
BME / Medical Physics 530BME / Medical Physics 530Medical Imaging SystemsMedical Imaging Systems
OverviewOverview
• Getting HelpGetting Help
• 2D Matrix / Image2D Matrix / Image Coordinate systemCoordinate system DisplayDisplay Storing imagesStoring images
• 2D Functions2D Functions
• Discrete Fourier TransformDiscrete Fourier Transform 1D DFT1D DFT fftshiftfftshift 2D DFT: zero-filling and shifting the image2D DFT: zero-filling and shifting the image
Getting StartedGetting Started
• Get a CAE accountGet a CAE account• Log in (Windows or Unix)Log in (Windows or Unix)• Create a directory ‘ImLab’Create a directory ‘ImLab’• Open Webbrowser and goto course pageOpen Webbrowser and goto course page
http://zoot.radiology.wisc.edu/~block/bme_530.html http://zoot.radiology.wisc.edu/~block/bme_530.html
• Goto ‘Lecture Material’Goto ‘Lecture Material’• DownloadDownload
matlab_intro.mmatlab_intro.m example.fig, example.mat, example.tifexample.fig, example.mat, example.tif
• Start MatlabStart Matlab Change to directory ‘Imlab’Change to directory ‘Imlab’
– Either Either >>cd ImLab>>cd ImLab– Or use the path browserOr use the path browser
Getting Help in MatlabGetting Help in Matlab
• Online HelpOnline Help Help Browser (? In toolbar or Help Browser (? In toolbar or >>helpbrowser>>helpbrowser))
Help Functions ( Help Functions ( >>help >>help functionnamefunctionname ))
Matlab website: www.mathworks.comMatlab website: www.mathworks.com
DemosDemos
• DocumentationDocumentation Online as pdf filesOnline as pdf files
Checkout at CAECheckout at CAE
2D Matrix2D Matrix
• 2D matrix has 2D matrix has MM rows and rows and NN columnscolumns
• ExampleExample >>m=zeros(400,200);>>m=zeros(400,200); >>m(1:50,1:100) = 1;>>m(1:50,1:100) = 1;
• Note:Note: Index 1 to M, not 0 to M-1Index 1 to M, not 0 to M-1 Origin of coordinate system is in upper Origin of coordinate system is in upper
left cornerleft corner Row index increases from top to bottomRow index increases from top to bottom Coordinate system is rotated in respect to Coordinate system is rotated in respect to
‘standard’ x-y coordinate system ‘standard’ x-y coordinate system
123
M
1 2 3 N…
…r
c
Image DisplayImage Display
• Display a 2D matrix as an imageDisplay a 2D matrix as an image >>image(m)>>image(m) >>imagesc(m)>>imagesc(m) >>imshow(m)>>imshow(m)
• imshow is recommended by MATLAB imshow is recommended by MATLAB for image processingfor image processing
• Image has MxN pixels = picture Image has MxN pixels = picture elements = dots on displayelements = dots on display
• Test the following commandsTest the following commands >>axis on>>axis on >>colorbar>>colorbar >>colormap(jet)>>colormap(jet) >>colormap(gray)>>colormap(gray)
Image TypesImage TypesMedical images are mostly Medical images are mostly
represented as grayscale images represented as grayscale images Human visual system resolves highest Human visual system resolves highest
spatial resolution for grayscale contrastspatial resolution for grayscale contrast Color is sometimes used to superimpose Color is sometimes used to superimpose
information on anatomical datainformation on anatomical data
Save Image MatrixSave Image Matrix
• MATLAB binary formatMATLAB binary format >>save example m >>save example m
-> writes out example.mat-> writes out example.mat
• Standard image format Standard image format (bmp,tiff,jpeg,png,hdf,pcx,xwd)(bmp,tiff,jpeg,png,hdf,pcx,xwd) >>imwrite(m,’example.tif’,’tif’)>>imwrite(m,’example.tif’,’tif’)
-> writes out example.tif-> writes out example.tif
Warning: imwrite expects data in range [0…1]Warning: imwrite expects data in range [0…1]
If data range outside this [0…1], use mat2grayIf data range outside this [0…1], use mat2gray
>>m_2 = mat2gray(m);>>m_2 = mat2gray(m);
>>imwrite(m_2,’example.tif’,’tif’)>>imwrite(m_2,’example.tif’,’tif’)
Save Figure (incl. labels etc)Save Figure (incl. labels etc)
• Save in Matlab Figure Format: Save in Matlab Figure Format: >File>Save As> in Figure Window>File>Save As> in Figure Window
-> writes out example.fig-> writes out example.fig• Standard image formats Standard image formats
>File>Export in Figure Window>File>Export in Figure Window E.g. jpg, tif, png, eps, …E.g. jpg, tif, png, eps, …
Alternatively, use print, e.g.Alternatively, use print, e.g.
>>print –deps2 example.eps>>print –deps2 example.eps
Loading 2D Data and ImagesLoading 2D Data and Images
• Load matrix from MATLAB binary formatLoad matrix from MATLAB binary format >>load example >>load example (loads example.mat)(loads example.mat)
• Load matrix from standard image formatLoad matrix from standard image format >>m_in = imread(’example.tif’,’tif’);>>m_in = imread(’example.tif’,’tif’); To check on an image:To check on an image:
>>imfinfo(’example.tif’,’tif’)>>imfinfo(’example.tif’,’tif’)
• Load figure from Matlab Figure Format Load figure from Matlab Figure Format (*.fig): (*.fig): >File>Open> ‘example.fig’ in Figure Window>File>Open> ‘example.fig’ in Figure Window
->Check loaded matrices->Check loaded matrices >>whos>>whos
Some Common Image Some Common Image FormatsFormats• TIF (TIFF)TIF (TIFF)
Very common, compatible, all purpose image formatVery common, compatible, all purpose image format Allows for lossless compressionAllows for lossless compression
• JPEGJPEG Allows for lossy compression (small file size)Allows for lossy compression (small file size) Very common for internetVery common for internet
• PNG (portable network graphics)PNG (portable network graphics) Good for saving MATLAB images for importing into Microsoft Good for saving MATLAB images for importing into Microsoft
docuuments such as Worddocuuments such as Word• DicomDicom
THE medical imaging formatTHE medical imaging format Lots of header information (patient name & ID, imaging Lots of header information (patient name & ID, imaging
parameters, exam date, …) can be storedparameters, exam date, …) can be stored Allows for lossy and lossless compressionAllows for lossy and lossless compression Matlab function ‘dicomread’, ‘dicomwrite’Matlab function ‘dicomread’, ‘dicomwrite’
• EPS (Encapsulated Postscript)EPS (Encapsulated Postscript) Graphics format rather than an image formatGraphics format rather than an image format Great for best quality print resultsGreat for best quality print results Large file sizeLarge file size
2D Functions2D Functions
• Create a matrix that Create a matrix that evaluates 2D Gaussian: evaluates 2D Gaussian: exp(-exp(-/2(x/2(x22+y+y22)/)/22)) >>ind = [-32:1:31] / 32;>>ind = [-32:1:31] / 32; >>[x,y] = meshgrid(ind,-1*ind);>>[x,y] = meshgrid(ind,-1*ind); >>z = exp(-pi/2*(x.^2+y.^2)/(.25.^2));>>z = exp(-pi/2*(x.^2+y.^2)/(.25.^2)); >>imshow(z)>>imshow(z) >>colorbar>>colorbar
x’x’
y’y’
xx
yy
64x64 matrix
-1
0
31/32
-1
FT Conventions - 1DFT Conventions - 1D1D Fourier Transform1D Fourier Transform
>>l = z(33,:);>>l = z(33,:); >>L = fft(l);>>L = fft(l);
sample #
10 20 30 40 50 600
0.1
0.2
0.3
0.4
0.5
0.6
0.7
0.8
0.9
1
l
10 20 30 40 50 600
2
4
6
8
10
12
10 20 30 40 50 60-4
-2
0
2
4
sample #
ph
ase
(L)
abs
(L)
1 33 64
u
0 fNfN/2
1 33 64
x [Matlab] 0 xmax
1D FT - fftshift1D FT - fftshift
Use fftshiftUse fftshift >>L1 = fft(fftshift(l));>>L1 = fft(fftshift(l));
sample #
l
sample #
ph
ase
(L1)
abs
(L1)
1 33 64
0 fNfN/2
1 33 64
0 xmax
10 20 30 40 50 600
0.1
0.2
0.3
0.4
0.5
0.6
0.7
0.8
0.9
1
10 20 30 40 50 600
2
4
6
8
10
12
10 20 30 40 50 60-4
-2
0
2
4
ux [Matlab]
1D FT - 2xfftshift1D FT - 2xfftshift
Center Fourier DomainCenter Fourier Domain >>L2 = >>L2 =
fftshift(fft(fftshift(l)));fftshift(fft(fftshift(l)));
sample #
ph
ase
(L2)
abs
(L2)
1 33 64
0-fN/2
10 20 30 40 50 600
2
4
6
8
10
12
10 20 30 40 50 60-4
-2
0
2
4
u
(N-1)/N*fN/2
fftshift in 2Dfftshift in 2D
Use fftshift for 2D functionsUse fftshift for 2D functions >>smiley2 = fftshift(smiley);>>smiley2 = fftshift(smiley);
2D Discrete Fourier 2D Discrete Fourier TransformTransform >>Z=fftshift(fft2(fftshift(z)));>>Z=fftshift(fft2(fftshift(z))); >>whos>>whos >>figure>>figure >>imshow(real(Z))>>imshow(real(Z)) >>imshow(real(Z),[])>>imshow(real(Z),[]) >>colorbar>>colorbar
20 40 60
20
40
60 -2
-1
0
1
2
x 10-14
20 40 60
20
40
600
50
100
20 40 60
20
40
60 -2
-1
0
1
2
x 10-14
>>figure>>figure >>imshow(imag(Z),[])>>imshow(imag(Z),[]) >>colorbar>>colorbar
Real (Z) Imag (Z)
2D DFT - Zero Filling2D DFT - Zero Filling
Interpolating Fourier space by zerofilling in image spaceInterpolating Fourier space by zerofilling in image space >>z_zf = zeros(256);>>z_zf = zeros(256); >>z_zf(97:160,97:160) = z;>>z_zf(97:160,97:160) = z; >>Z_ZF = fftshift(fft2(fftshift(z_zf)));>>Z_ZF = fftshift(fft2(fftshift(z_zf))); >>figure>>figure >>imshow(abs(Z_ZF),[])>>imshow(abs(Z_ZF),[]) >>colorbar>>colorbar
>>figure>>figure >>imshow(angle(Z_ZF),[])>>imshow(angle(Z_ZF),[]) >>colorbar>>colorbar
50 100 150 200 250
50
100
150
200
250
-3
-2
-1
0
1
2
3
50 100 150 200 250
50
100
150
200
250
-3
-2
-1
0
1
2
3
50 100 150 200 250
50
100
150
200
250
0
20
40
60
80
100
120
50 100 150 200 250
50
100
150
200
250
0
20
40
60
80
100
120
Magnitude (Z_ZF) Phase (Z_ZF)
2D DFT - Voxel Shifts2D DFT - Voxel Shifts
Shift image: 5 voxels up and 2 voxels leftShift image: 5 voxels up and 2 voxels left >>z_s = zeros(256);>>z_s = zeros(256); >>z_s(92:155,95:158) = z;>>z_s(92:155,95:158) = z; >>Z_S = fftshift(fft2(fftshift(z_s)));>>Z_S = fftshift(fft2(fftshift(z_s))); >>figure>>figure >>imshow(abs(Z_S),[])>>imshow(abs(Z_S),[]) >>colorbar>>colorbar
>>figure>>figure >>imshow(angle(Z_S),[])>>imshow(angle(Z_S),[]) >>colorbar>>colorbar
50 100 150 200 250
50
100
150
200
250
-3
-2
-1
0
1
2
3
50 100 150 200 250
50
100
150
200
250
0
20
40
60
80
100
120
50 100 150 200 250
50
100
150
200
250
0
20
40
60
80
100
120
Magnitude (Z_S) Phase (Z_S)
50 100 150 200 250
50
100
150
200
250
-3
-2
-1
0
1
2
3
Review of functions …Review of functions …
• Input / OutputInput / Output >>imread>>imread >>imwrite>>imwrite >>imfinfo>>imfinfo
• Image DisplayImage Display >>imshow>>imshow
• OthersOthers >>axis>>axis >>colorbar>>colorbar >>colormap>>colormap >>meshgrid>>meshgrid >>fft, fft2>>fft, fft2 >>fftshift>>fftshift
More useful functions …More useful functions …
• Image DisplayImage Display >>title>>title >>xlabel, ylabel>>xlabel, ylabel >>subimage>>subimage >>zoom>>zoom >>mesh>>mesh
• OthersOthers >>imresize>>imresize >>imrotate>>imrotate >>imhist>>imhist >>conv2>>conv2 >>radon>>radon >>roipoly>>roipoly
• Demos in Image Processing Demos in Image Processing ToolboxToolbox >>help imdemos>>help imdemos