You are on page 1of 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:
Eachpixelintheresultingimagehasavalueequaltothesumofthepixelvaluesofthe
originalimagexfiltermaskwhenthemaskiscenteredonthatpixel:imfilter
q=OriginalImage Filtermask
filt=ones(3,3);
filt=filt*1/9;
FilteringimagesinMatlab
Aspatialfilterrequiresafiltermask.Exampleanaveragingfilter:
Eachpixelintheresultingimagehasavalueequaltothesumofthepixelvaluesofthe
originalimagexfiltermaskwhenthemaskiscenteredonthatpixel:imfilter
TryapplyingaGaussianfiltertoyourimage(hint:Gaussiansarereadilymadeusingfspecial)
q=OriginalImage Filtermask
filt=ones(3,3);
filt=filt*1/9;
imfilt(q,filt)
FilteringimagesinMatlab
Aspatialfilterrequiresafiltermask.Exampleanaveragingfilter:
Eachpixelintheresultingimagehasavalueequaltothesumofthepixelvaluesofthe
originalimagexfiltermaskwhenthemaskiscenteredonthatpixel:imfilter
TryapplyingaGaussianfiltertoyourimage(hint:Gaussiansarereadilymadeusingfspecial)
q=OriginalImage Filtermask
filt=ones(3,3);
filt=filt*1/9;
imfilt(q,filt)
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/(NA
obj
+NA
cond
)
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:
}
offset
x
f(x)
IncludedinthedistributedfilesisD1Resids.mfor
fittinga1DGaussiantodata.Letstryandfitoneto
a1Dintensityprofileoftheimagepsf.tif.
Firstmaketheintensityprofilewhichyouwillfit:Ichoseahorizontalslicethroughthemiddle
LeastSquaresFitting
Fittinginvolvesfindingthecurvewhichminimizesthesumofthesquaresoftheoffsets("the
residuals")ofthegivendatapointsfromthecurve:
}
offset
x
f(x)
IncludedinthedistributedfilesisD1Resids.mfor
fittinga1DGaussiantodata.Letstryandfitoneto
a1Dintensityprofileoftheimagepsf.tif.
Firstmaketheintensityprofilewhichyouwillfit:Ichoseahorizontalslicethroughthemiddle
Now,tryminimizingtheresidualswithD1Resids.m
psf=mat2gray(imread(psf.tif'));
psf_profile=psf(18,:);
LeastSquaresFitting
Fittinginvolvesfindingthecurvewhichminimizesthesumofthesquaresoftheoffsets("the
residuals")ofthegivendatapointsfromthecurve:
}
offset
x
f(x)
IncludedinthedistributedfilesisD1Resids.mfor
fittinga1DGaussiantodata.Letstryandfitoneto
a1Dintensityprofileoftheimagepsf.tif.
Firstmaketheintensityprofilewhichyouwillfit:Ichoseahorizontalslicethroughthemiddle
Now,tryminimizingtheresidualswithD1Resids.m
fitp =fminsearch(@(x)D1Resids(x,inputdata),[17,3,.04,.01])
Overlayyourfittedfunctionwithpsf.tif
psf=mat2gray(imread(psf.tif'));
psf_profile=psf(18,:);
LeastSquaresFitting
Fittinginvolvesfindingthecurvewhichminimizesthesumofthesquaresoftheoffsets("the
residuals")ofthegivendatapointsfromthecurve:
}
offset
x
f(x)
IncludedinthedistributedfilesisD1Resids.mfor
fittinga1DGaussiantodata.Letstryandfitoneto
a1Dintensityprofileoftheimagepsf.tif.
Firstmaketheintensityprofilewhichyouwillfit:Ichoseahorizontalslicethroughthemiddle
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')
psf=mat2gray(imread(psf.tif'));
psf_profile=psf(18,:);
Fittingin2D
Youshouldnowhavethepropertoolstofita2DGaussiantopsf.tif
Giveitatry!
Canyoulocalizethequantumdotinpsf.tiftosubpixelresolutiohn?

You might also like