0% found this document useful (0 votes)
105 views

AdvancedMatlabTutorial PDF

The document discusses various image processing techniques in Matlab, including loading images as matrices, viewing and manipulating images, applying filters like averaging and Gaussian filters, and performing morphological operations like opening to remove small features. It also discusses segmenting images using thresholding, and analyzing properties of segmented regions like area and perimeter. Key steps include subtracting an uneven background, adjusting contrast, converting to binary, and labeling connected regions.

Uploaded by

Girindra_W
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
105 views

AdvancedMatlabTutorial PDF

The document discusses various image processing techniques in Matlab, including loading images as matrices, viewing and manipulating images, applying filters like averaging and Gaussian filters, and performing morphological operations like opening to remove small features. It also discusses segmenting images using thresholding, and analyzing properties of segmented regions like area and perimeter. Key steps include subtracting an uneven background, adjusting contrast, converting to binary, and labeling connected regions.

Uploaded by

Girindra_W
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 44

ManipulatingimageswithMatlab

MonochromeimagesinMatlab arerepresentedbyandNxM matrix Nistheheightoftheimage,Misthewidth LoadanimageintovariableJ:J=imread(spine.tif); Letsgetsomeinformationabouttheimage:whos J

ManipulatingimageswithMatlab
MonochromeimagesinMatlab arerepresentedbyandNxM matrix Nistheheightoftheimage,Misthewidth LoadanimageintovariableJ:J=imread(spine.tif); Letsgetsomeinformationabouttheimage:whos J Rawdataistypicallyuint butimagesshouldbedoublesbetween0and1 usefunctionmat2gray toconverttheimage.

ManipulatingimageswithMatlab
MonochromeimagesinMatlab arerepresentedbyandNxM matrix Nistheheightoftheimage,Misthewidth LoadanimageintovariableJ:J=imread(spine.tif); Letsgetsomeinformationabouttheimage:whos J Rawdataistypicallyuint butimagesshouldbedoublesbetween0and1 usefunctionmat2gray toconverttheimage. Toviewtheimageuse:imtool(J)(alternatively,imshow(J))

ManipulatingimageswithMatlab
MonochromeimagesinMatlab arerepresentedbyandNxM matrix Nistheheightoftheimage,Misthewidth LoadanimageintovariableJ:J=imread(spine.tif); Letsgetsomeinformationabouttheimage:whos J Rawdataistypicallyuint butimagesshouldbedoublesbetween0and1 usefunctionmat2gray toconverttheimage. Toviewtheimageuse:imtool(J)(alternatively,imshow(J)) Trymanuallysetcontrast:imtool(J,[lower,upper]) Pixelslower areblack;Pixelsupper arewhite automaticallysetlowertotheminimumpixel andupper tothemaximumimtool(J,[]) Youcanalsoadjustthegammaandpixelmapping withimadjust(J,[],[],gamma);

FilteringimagesinMatlab
Aspatialfilterrequiresafiltermask.Exampleanaveragingfilter:

FilteringimagesinMatlab
Aspatialfilterrequiresafiltermask.Exampleanaveragingfilter: filt=ones(3,3); filt=filt*1/9; Eachpixelintheresultingimagehasavalueequaltothesumofthepixelvaluesofthe originalimagexfiltermaskwhenthemaskiscenteredonthatpixel:imfilter q=OriginalImage Filtermask

FilteringimagesinMatlab
Aspatialfilterrequiresafiltermask.Exampleanaveragingfilter: filt=ones(3,3); filt=filt*1/9; Eachpixelintheresultingimagehasavalueequaltothesumofthepixelvaluesofthe originalimagexfiltermaskwhenthemaskiscenteredonthatpixel:imfilter q=OriginalImage Filtermask

imfilt(q,filt)

TryapplyingaGaussianfiltertoyourimage(hint:Gaussiansarereadilymadeusingfspecial)

FilteringimagesinMatlab
Aspatialfilterrequiresafiltermask.Exampleanaveragingfilter: filt=ones(3,3); filt=filt*1/9; Eachpixelintheresultingimagehasavalueequaltothesumofthepixelvaluesofthe originalimagexfiltermaskwhenthemaskiscenteredonthatpixel:imfilter q=OriginalImage Filtermask

imfilt(q,filt)

TryapplyingaGaussianfiltertoyourimage(hint:Gaussiansarereadilymadeusingfspecial) J_gaus=imfilt(J,fspecial(gaussian,11,3));

Anotherexample morphologicaloperations
Readthestockimagerice.png:rice=mat2gray(imread(rice.png)); Viewtheimage,noticethebackgroundisnoteven:imtool(rice,[]); Wewillopenthisimage,usingthefunctionimopen,toremove thesmallfeaturesandgetalookatthebackground. imopen isamorphologicalerosionfollowedbyadilation imclose isthereverse Asanexample,loadtheimagesticksandballs.tif Wecanusetheimopen featuretoremovethesticks Tryopeningthisimageandremovingthestickswithadisc shapedstructuralelement(strel)

Anotherexample morphologicaloperations
Readthestockimagerice.png:rice=mat2gray(imread(rice.png)); Viewtheimage,noticethebackgroundisnoteven:imtool(rice,[]); Wewillopenthisimage,usingthefunctionimopen,toremove thesmallfeaturesandgetalookatthebackground. imopen isamorphologicalerosionfollowedbyadilation imclose isthereverse Asanexample,loadtheimagesticksandballs.tif Wecanusetheimopen featuretoremovethesticks Tryopeningthisimageandremovingthestickswithadisc shapedstructuralelement(strel) sb_open=imopen(sb,strel(disk,5)); Nowletsreturntoourunevenlyilluminatedrice: opentheimagesuchthatonlythebackgroundremains

Anotherexample morphologicaloperations
Readthestockimagerice.png:rice=mat2gray(imread(rice.png)); Viewtheimage,noticethebackgroundisnoteven:imtool(rice,[]); Wewillopenthisimage,usingthefunctionimopen,toremove thesmallfeaturesandgetalookatthebackground. imopen isamorphologicalerosionfollowedbyadilation imclose isthereverse Asanexample,loadtheimagesticksandballs.tif Wecanusetheimopen featuretoremovethesticks Tryopeningthisimageandremovingthestickswithadisc shapedstructuralelement(strel) sb_open=imopen(sb,strel(disk,5)); Nowletsreturntoourunevenlyilluminatedrice: opentheimagesuchthatonlythebackgroundremains rice_open=imopen(rice,strel(disk,20));

Evenriceyeahbaby!
Lookattheoriginalimagewiththeunevenbackgroundsubtracted

Evenriceyeahbaby!
Lookattheoriginalimagewiththeunevenbackgroundsubtracted rice_nobg=ricerice_open imshow(rice_nobg) Butwearentusingtheentirecontrastrangenow,adjusttheimage rice_nobg=imadjust(rice_nobg);

Evenriceyeahbaby!
Lookattheoriginalimagewiththeunevenbackgroundsubtracted rice_nobg=ricerice_open imshow(rice_nobg) Butwearentusingtheentirecontrastrangenow,adjusttheimage rice_nobg=imadjust(rice_nobg); Segmentationisacommonneedinbiologyimageprocessing;letssegmentthericeusinga simplethresholdasacriterionforwhatisriceandwhatisnot. imhist givesahistogramoftheintensityoftheimagepixels choseathresholdbetweenthebackground(around0)andtherice(around1) Usingim2bw makethepixelsbelowthethresholdblackandtheonesabovewhite

Evenriceyeahbaby!
Lookattheoriginalimagewiththeunevenbackgroundsubtracted rice_nobg=ricerice_open imshow(rice_nobg) Butwearentusingtheentirecontrastrangenow,adjusttheimage rice_nobg=imadjust(rice_nobg); Segmentationisacommonneedinbiologyimageprocessing;letssegmentthericeusinga simplethresholdasacriterionforwhatisriceandwhatisnot. imhist givesahistogramoftheintensityoftheimagepixels choseathresholdbetweenthebackground(around0)andtherice(around1) Usingim2bw makethepixelsbelowthethresholdblackandtheonesabovewhite rice_bw=im2bw(rice_nobg,0.6); Eachconnectedregionof1scanbegivenitsownlabelwithbwlabel.

Evenriceyeahbaby!
Lookattheoriginalimagewiththeunevenbackgroundsubtracted rice_nobg=ricerice_open imshow(rice_nobg) Butwearentusingtheentirecontrastrangenow,adjusttheimage rice_nobg=imadjust(rice_nobg); Segmentationisacommonneedinbiologyimageprocessing;letssegmentthericeusinga simplethresholdasacriterionforwhatisriceandwhatisnot. imhist givesahistogramoftheintensityoftheimagepixels choseathresholdbetweenthebackground(around0)andtherice(around1) Usingim2bw makethepixelsbelowthethresholdblackandtheonesabovewhite rice_bw=im2bw(rice_nobg,0.6); Eachconnectedregionof1scanbegivenitsownlabelwithbwlabel. rice_label=bwlabel(rice_bw); Viewtheresultingimage Findtheareaandperimeterofeachlabeledregion withregionprops

Evenriceyeahbaby!
Lookattheoriginalimagewiththeunevenbackgroundsubtracted rice_nobg=ricerice_open imshow(rice_nobg) Butwearentusingtheentirecontrastrangenow,adjusttheimage rice_nobg=imadjust(rice_nobg); Segmentationisacommonneedinbiologyimageprocessing;letssegmentthericeusinga simplethresholdasacriterionforwhatisriceandwhatisnot. imhist givesahistogramoftheintensityoftheimagepixels choseathresholdbetweenthebackground(around0)andtherice(around1) Usingim2bw makethepixelsbelowthethresholdblackandtheonesabovewhite rice_bw=im2bw(rice_nobg,0.6); Eachconnectedregionof1scanbegivenitsownlabelwithbwlabel. rice_label=bwlabel(rice_bw); Viewtheresultingimage Findtheareaandperimeterofeachlabeledregion withregionprops rice_dims=regionprops(rice_label,Area,Perimeter);

Segmentedrice
Nowwecananswerquestionslike: Whatisthemeanareaoftherice?Thevarianceinthemean?Theperimeter?

Segmentedrice
Nowwecananswerquestionslike: Whatisthemeanareaoftherice?Thevarianceinthemean?Theperimeter? areas=[rice_dims.Area] mean(areas) var(areas) hist(areas) andonandon Forfun,trysegmentingthesuppliedimagefish.tif

PointSpreadFunction
Foranidealoptical system,apointsource isnotapointatthe imageplane. Instead,itappearsasa PointSpreadFunction (PSF) akatheimpulse responsefunctionfor theopticalsystem.

Put experimental onehere

PSF:Why?

PSF:Why?
Theobjectivecannotcaptureallofthelight radiatedbythepointsource. Equivalently:highfrequencyFourier componentsarelost. MissingcomponentsintheFourierdomain correspondtouncertaintyinpositionin physicalspace. Howmuchlightiscaptured?Quantifyby NumericalAperture (NA)

NumericalAperture

NA=nsin() n:indexofrefractionofmedium betweenspecimenandobjective. WhydoesNAdependonn?

Puttingittogether:Resolution
RayleighCriterion: conventionfordefining resolution
r=1.22/(NAobj+NAcond) r250nmfortypical imagingconditions

FluorescenceMicroscopy:Howare imagesformed?
TheimagedetectedbyyourCCDisthe convolution ofthePSFwiththepointsources (e.g.,fluorescentproteinmolecules,quantum dots,fluorescentdyemolecules)withinthe fieldofview. Wellexplorethisideainthenextfewslides.

Space:TheFinalFrontier
Loadprovidedimageof pointsources(points.tif)

Space:TheFinalFrontier
Loadprovidedimageof pointsources(points.tif)
P=imread(points.tif); imshow(P)

ExperimentalPSF
Loadprovidedimageof pointspreadfunction (psf.tif)
psf =imread(psf.tif); imshow(psf)

[Aside:improfile]
Useimprofile toestimate theminimumresolvable distanceaccordingto Rayleighcriterion

[Aside:improfile]
Useimprofile toestimate theminimumresolvable distanceaccordingto Rayleighcriterion(recall thisisthedistancefrom centralpeaktofirst minimum).

About7pixelsorso.

Convolvingthepointsources
Nowconvolveyourimageofpoint sourceswiththepointspread function,usingimfilter. (Hint:itsusuallyagoodideato convertallyourimagestodoubles beforefilteringthem,soyoumay wanttouseim2double)

Convolvingthepointsources
Nowconvolveyourimageofpoint sourceswiththepointspread function,usingimfilter. (Hint:itsusuallyagoodideato convertallyourimagestodoubles beforefilteringthem,soyoumay wanttouseim2double)

PD=im2double(P); psfd =im2double(psf); J=imfilter(PD,psfd); imshow(J,[])

BeatingtheLimit:Deconvolution
Weveseenthatevenforanidealoptical system,thereexisthardlimitson performance. Isthereanywaywecouldcleverlygetaround theselimits?

BeatingtheLimit:Deconvolution
Weveseenthatevenforanidealoptical system,thereexisthardlimitson performance. Isthereanywaywecouldcleverlygetaround theselimits? Yes:Deconvolution! Basicidea:weknowwhataPSFlookslike,so extrapolatebackwards

TheMath

Howmanyspots?
Openhowmany.tifinthe tutorialfolder.

Howmanyspots?
Openhowmany.tifinthe tutorialfolder. J=imread(howmany.tif);

Howmanyspots?
Openhowmany.tifinthe tutorialfolder. J=imread(howmany.tif); Now,deconvolve theimage usingdeconvwnr,which performsWiener deconvolutionn. Syntax:deconvwnr(I,psf)

Howmanyspots?
Openhowmany.tifinthe tutorialfolder. J=imread(howmany.tif); Now,deconvolve theimage usingdeconvwnr,which performsWiener deconvolutionn. Syntax:deconvwnr(I,psf) JD=deconvwnr(J,psf);

LeastSquaresFitting
Fittinginvolvesfindingthecurvewhichminimizesthesumofthesquaresoftheoffsets("the residuals")ofthegivendatapointsfromthecurve:

f(x)
offset IncludedinthedistributedfilesisD1Resids.mfor fittinga1DGaussiantodata.Letstryandfitoneto a1Dintensityprofileoftheimagepsf.tif.

}
x

Firstmaketheintensityprofilewhichyouwillfit:Ichoseahorizontalslicethroughthemiddle

LeastSquaresFitting
Fittinginvolvesfindingthecurvewhichminimizesthesumofthesquaresoftheoffsets("the residuals")ofthegivendatapointsfromthecurve:

f(x)
offset IncludedinthedistributedfilesisD1Resids.mfor fittinga1DGaussiantodata.Letstryandfitoneto a1Dintensityprofileoftheimagepsf.tif.

}
x

Firstmaketheintensityprofilewhichyouwillfit:Ichoseahorizontalslicethroughthemiddle psf=mat2gray(imread(psf.tif')); psf_profile=psf(18,:); Now,tryminimizingtheresidualswithD1Resids.m

LeastSquaresFitting
Fittinginvolvesfindingthecurvewhichminimizesthesumofthesquaresoftheoffsets("the residuals")ofthegivendatapointsfromthecurve:

f(x)
offset IncludedinthedistributedfilesisD1Resids.mfor fittinga1DGaussiantodata.Letstryandfitoneto a1Dintensityprofileoftheimagepsf.tif.

}
x

Firstmaketheintensityprofilewhichyouwillfit:Ichoseahorizontalslicethroughthemiddle psf=mat2gray(imread(psf.tif')); psf_profile=psf(18,:); Now,tryminimizingtheresidualswithD1Resids.m fitp =fminsearch(@(x)D1Resids(x,inputdata),[17,3,.04,.01]) Overlayyourfittedfunctionwithpsf.tif

LeastSquaresFitting
Fittinginvolvesfindingthecurvewhichminimizesthesumofthesquaresoftheoffsets("the residuals")ofthegivendatapointsfromthecurve:

f(x)
offset IncludedinthedistributedfilesisD1Resids.mfor fittinga1DGaussiantodata.Letstryandfitoneto a1Dintensityprofileoftheimagepsf.tif.

}
x

Firstmaketheintensityprofilewhichyouwillfit:Ichoseahorizontalslicethroughthemiddle psf=mat2gray(imread(psf.tif')); psf_profile=psf(18,:); Now,tryminimizingtheresidualswithD1Resids.m fitp =fminsearch(@(x)D1Resids(x,inputdata),[17,3,.04,.01]) Overlayyourfittedfunctionwithpsf.tif x=[1:35] y=fitp(3)*exp((xfitp(1)).^2/fitp(2))+fitp(4) plot(psf_profile,'k');hold;plot(x,y,'r')

Fittingin2D
Youshouldnowhavethepropertoolstofita2DGaussiantopsf.tif Giveitatry! Canyoulocalizethequantumdotinpsf.tiftosubpixelresolutiohn?

You might also like