21
Introduction to Image Introduction to Image Processing with MATLAB Processing with MATLAB Wally Block Wally Block BME / Medical Physics 530 BME / Medical Physics 530 Medical Imaging Systems Medical Imaging Systems

Introduction to Image Processing with MATLAB Wally Block BME / Medical Physics 530 Medical Imaging Systems

Embed Size (px)

Citation preview

Page 1: Introduction to Image Processing with MATLAB Wally Block BME / Medical Physics 530 Medical Imaging Systems

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

Page 2: Introduction to Image Processing with MATLAB Wally Block BME / Medical Physics 530 Medical 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

Page 3: Introduction to Image Processing with MATLAB Wally Block BME / Medical Physics 530 Medical Imaging Systems

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

Page 4: Introduction to Image Processing with MATLAB Wally Block BME / Medical Physics 530 Medical Imaging Systems

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

Page 5: Introduction to Image Processing with MATLAB Wally Block BME / Medical Physics 530 Medical Imaging Systems

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

Page 6: Introduction to Image Processing with MATLAB Wally Block BME / Medical Physics 530 Medical Imaging Systems

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)

Page 7: Introduction to Image Processing with MATLAB Wally Block BME / Medical Physics 530 Medical Imaging Systems

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

Page 8: Introduction to Image Processing with MATLAB Wally Block BME / Medical Physics 530 Medical Imaging Systems

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’)

Page 9: Introduction to Image Processing with MATLAB Wally Block BME / Medical Physics 530 Medical Imaging Systems

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

Page 10: Introduction to Image Processing with MATLAB Wally Block BME / Medical Physics 530 Medical Imaging Systems

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

Page 11: Introduction to Image Processing with MATLAB Wally Block BME / Medical Physics 530 Medical Imaging Systems

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

Page 12: Introduction to Image Processing with MATLAB Wally Block BME / Medical Physics 530 Medical Imaging Systems

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

Page 13: Introduction to Image Processing with MATLAB Wally Block BME / Medical Physics 530 Medical Imaging Systems

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

Page 14: Introduction to Image Processing with MATLAB Wally Block BME / Medical Physics 530 Medical Imaging Systems

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]

Page 15: Introduction to Image Processing with MATLAB Wally Block BME / Medical Physics 530 Medical Imaging Systems

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

Page 16: Introduction to Image Processing with MATLAB Wally Block BME / Medical Physics 530 Medical Imaging Systems

fftshift in 2Dfftshift in 2D

Use fftshift for 2D functionsUse fftshift for 2D functions >>smiley2 = fftshift(smiley);>>smiley2 = fftshift(smiley);

Page 17: Introduction to Image Processing with MATLAB Wally Block BME / Medical Physics 530 Medical Imaging Systems

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)

Page 18: Introduction to Image Processing with MATLAB Wally Block BME / Medical Physics 530 Medical Imaging Systems

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)

Page 19: Introduction to Image Processing with MATLAB Wally Block BME / Medical Physics 530 Medical Imaging Systems

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

Page 20: Introduction to Image Processing with MATLAB Wally Block BME / Medical Physics 530 Medical Imaging Systems

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

Page 21: Introduction to Image Processing with MATLAB Wally Block BME / Medical Physics 530 Medical Imaging Systems

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