# Introduction to DIPimage

Contents
1 Introduction..............................................................................................................................2 2 MATLAB.................................................................................................................................2 3 DIPimage................................................................................................................................. 2 Edit a MATLAB command file...................................................................................... 3 4 Loading and displa ing an image............................................................................................ 4 ! Image filtering..........................................................................................................................4 Blurring filters.................................................................................................................! Deri"ati"e filters# gradient and Laplace..........................................................................! \$%n&s'arpening................................................................................................................( Local ma)imum and minimum filters ............................................................................* ( Point operations....................................................................................................................... * +istogram,-ased operations ............................................................................................ T'res'olding................................................................................................................... . * Binar morp'olog .................................................................................................................. / . T'e 0ourier transform............................................................................................................11 / Measurements in images........................................................................................................11

Quantitative Imaging Group Department of Imaging Science & Technology Faculty of Applied Sciences Delft niversity of Technology Delft! The "etherlands T# \$%& &' ()* &+&, -# Secr.QI.T"/0tudelft1nl I# http#223331ist1tudelft1nl24i I# http#223331dipli51org2

1

processing en"ironment# MATLAB and t'e DIPimage tool-o) for multi. MATLAB pro"ides onl a command line and grap'ics capa-ilities.on e)perience 2it' image processing.dept' e)planation of all t'e features in t'e soft2are. 2ill call t'at function4 2it' t'e "alues a and b as its parameters.b). T'e one on t'e top.%I contains a menu -ar.b). To facilitate a 5uic3 start4 t'ere 2ill not -e an in. T'is section ta3es ou t'roug' t'e most rele"ant features. If no e)plicit assignment is done4 t'e output of a function 2ill -e put into a "aria-le called ans# max(a. T'e idea is t'at ou gi"e it commands t'roug' t'e command line4 2'ic' are e)ecuted immediatel .d)). Let us start 2it' a s'ort introduction to t'e MATLAB en"ironment.dimensional image processing.  :ou ma 'a"e noticed t'e 2indo2s t'at appeared around t'e screen 2'en ou started MATLAB.rap'ical %ser Interface&. T'e MATLAB s nta) 2ill -ecome clear during t'is la-orator session.  MATLAB is a computing8programming en"ironment especiall designed to 2or3 2it' data sets as a 2'ole suc' as "ectors4 matrices and images.b). 2ill cause 2'ate"er is in "aria-le b \$a scalar or an image& to -e copied into "aria-le a. It is possi-le to use t'e result of a function call as a parameter in anot'er function# a = max(max(a. T'e .left is t'e .& Introduction T'e goal of t'is la-orator 2or3 is to get 'ands. is t'e same as ans = max(a. T'ese data sets can -e treated t'e same as a mat'ematical e)pression of scalar "aria-les. <pend some time e)ploring t'e menus. 6e 'a"e mar3ed t'e sections t'at e)plain somet'ing a-out t'e en"ironment 2it' t'e  s m-ol4 so t'at t'e stand out. If max is t'e name of a function4 t'en a = max(a. % DIPimage DIPimage is t'e tool-o) 2e 2ill -e using under MATLAB to do image processing. +ere 9ust a fe2 s'ort comments# a = b. T'e ot'er 2indo2s are t'e ones used to displa t'e images in. max(c. T'e result of t'e function \$its return "alue& 2ill -e 2ritten into a4 o"er2riting its pre"ious contents.%I \$. If ou omit t'e semicolon at t'e end of t'e command4 t'e ne2 contents of a 2ill -e printed \$scalars4 "ectors and matrices 2ill printed in t'e command 2indo24 2'ereas images displa ed in a separate 2indo2&. 6e 2ill -riefl present t'e t'ings ou need at t'is moment. 6'ate"er 2as in "aria-le a gets lost.b). To do so4 ou 2ill 'a"e to learn t'e image. 6'en ou c'oose one of t'e options4 t'e area -eneat' t'e menu -ar c'anges into a dialog -o) t'at allo2s ou to enter t'e parameters for t'e function ou 'a"e c'osen# 2 . ( 6AT7A8 T'is section is to ma3e ou familiar 2it' MATLAB7 if ou alread are4 s3ip t'is section.

:ou can also e)ecute t'e script . It can -e used to let MATLAB run t'e file currentl -eing edited. 6e 2ill t pe \$or cop 8paste& t'e commands 2e 2ant to e)ecute in it. 6'en sol"ing t'e pro-lems in t'is la-orator session4 2e 2ill -e ma3ing te)t files t'at contain all commands 2e used to get to t'e result \$2e 2ill call t'em e)ercise command files&.T'ere are t2o 2a s of using t'e functions in t'is tool-o). T'ere is a >?un@ menu item under t'e >Tools@ menu. Edit a MATLAB command file To open t'e editor4 t pe edit T'e MATLAB editor 2ill -e started.%I4 2'ic' ma3es it eas to select filters and its parameters. T'e first one is t'roug' t'e . If ou start eac' file 2it' t'e commands clear dipclf t'en t'e "aria-les and t'e figure 2indo2s 2ill -e cleared -efore e)ecuted. 6e recommend ou ma3e a ne2 file for eac' e)ercise4 and gi"e t'em names t'at are eas to recogni=e. 6e 2ill -e using it "er often at first4 -ut graduall less during t'e course of t'e afternoon. T'is a"oids undetected errors. T'is ma3es t'e results reproduci-le. T'e ot'er met'od is t'roug' t'e command line. our commands are 3 . It 2ill also a"oid lots of repetiti"e and tedious 2or3.t ping its name in t'e MATLAB prompt.

aussian filter# t'e standard de"iation in pi)els. As ou 'a"e learned toda 4 t'e Laplace operator is a general second deri"ati"e. T'is 2ill allo2 us to s'o2 ou 'o2 to compute 2it' images."alues. T'e re5uired input image s'ould alread reside in one of t'e "aria-les4 e.directions.mapping t'e "alue 1 to -lac34 and t'e "alue 2!! to 2'ite. B'oose >Based at 1@.aussian filterH 6' canEt ou ma3e t'e results e)actl t'e sameH Ma3e sure ou cop some of t'e function calls ou ma3e to our e)ercise command file.  :ou 2ill notice t'at t'e second deri"ati"e does not ield "er 'ig' gre .separating t'e t2o "alues 2it' a comma4 and surrounding t'e 2'ole t'ing 2it' s5uare -rac3ets# #\$."alues in t'is particular image are \$appro)imatel & -et2een .aussian filter@H 6'at parameters 2ould ou c'oose to ma3e t'e result of t'e uniform filter similar to t'e result from t'e . <ince all gre . Tr out different "alues for it4 and see 2'at 'appens. T'ere are also some more filters -ased on com-inations of deri"ati"es4 suc' as laplace and gradmag. ! . B default4 images are displa ed .31 and 314 t'is mapping is not ade5uate.g. T'e displa loo3s -lac3. T pe an name for t'e output image4 for e)ample b. !%. direction. T'is is t'e 2a t'at arra s are constructed in MATLAB. Derivative filters: gradient and Laplace T'e menu >Differential 0ilters@ contains a general deri"ati"e filter \$ gauss&deri'ati'e&4 and a complete set of first and second order deri"ati"es. Apen t'e >Mappings@ menu. T'is can -e accomplis'ed . 0irst 2e need t'e second deri"ati"es in t'e x and y. It contains a couple of options t'at allo2s ou to c'ange t'e displa mapping. 6'at is t'e difference 2it' t'e >.Blurring filters B'oose >. 6e can c'ange t'is mapping t'roug' t'e menus of t'e figure 2indo2s. Tr out t'e ot'er modes too. T'e name -et2een parent'eses on t'e menu indicates t'e name of t'e function t'at implements t'is filter.  It is possi-le to c'oose a different filter si=e parameter for t'e 'ori=ontal and "ertical -lur. Co2 2e need to c'oose t'e si=e of t'e . T'e first "alue 2ill -e used in t'e x. Put t'em in "aria-les named a&xx and a&yy \$an name is as good as t'e ot'er4 isnEt itH&.direction4 and t'e second one in t'e y. LetEs ma3e one -ased on t'e -asic deri"ati"es onl . Also e)plore t'e >%niform filter@. a. T'is mode 2ill map t'e image "alues linearl -et2een -lac3 and 2'ite4 forcing t'e "alue 1 to !1I gre .aussian filter@.

LetEs compare t'e result 2it' t'e Laplace operator \$ laplace&.t2o4 2e multipl -ot' t'e lo2 and 'ig' fre5uencies.t2o. Tr t'is out on anot'er image too.fre5uenc components are effecti"el multiplied . Put its result in c. Anl t'e 'ig'. But 2e 2ant to ma3e sure t'at t'e difference is =ero e"er 2'ere4 and not 9ust "er small.t'e command# a = readim('"dna ') %ns'arp mas3ing 'as -een defined toda as t'e original image minus t'e Laplace of t'e image. (Un)sharpening Co2 2e 2ill s'arpen t'e image D5dna1E4 load it in "aria-le a . T'e tric3 is t'at t'e uns'arp print mas3s t'e lo2. T'e t2o com-ined produced a s'arper "ersion of t'e p'otograp'.su-tracting t'e t2o images. T'e lo2 fre5uenc components are t'en su-tracted again4 t'us remaining in t'eir original intensit . T'is is accomplis'ed 2it' t'e command b = a&xx ( a&yy \$Cote t'at t'ere is no menu item for adding t2o images&.Co2 2e need to add t'ese t2o deri"ati"es toget'er. fre5uenc components4 -ut not t'e 'ig' fre5uencies7 t'e procedure t'us implements a 'ig'. 6'at t'e used to do 2as print an uns'arp "ersion of t'e image on film4 and use t'at to mas3 t'e negati"e. It is also possi-le to use a constant "alue instead of eit'er image. pass filter. <elect >Pi)el testing@4 and press t'e mouse -utton 2'ile pointing some2'ere in t'e image \$3eep t'e -utton do2n&.  T'e >Actions@ menu allo2s ou to specif t'e mouse action on t'e figure 2indo2. <ince -ot' are e5ual4 t'e result is completel -lac3. Co2 compare b and c . 6e can 2rite t'is "er easil using onl t'e command line# a ) laplace(a) T'e ans2er is put into t'e "aria-le ans. LetEs reproduce t'at tric3 2it' our DcermetE. Ma3e sure ou 'a"e copied e"er command ou e)ecuted to t'e e)ercise command file. Anot'er option on t'e >Actions@ menu is used to =oom in on an image. Tr mo"ing t'e mouse 2'ile 'olding t'e -utton do2n4 and c'ec3 t'at t'e "alues are all indeed =ero. ( . T'e figure caption c'anges to s'o2 t'e coordinates of t'e mouse in t'e image and t'e "alue of t'e pi)el at t'ose coordinates. Tr it out too.p'otograp'ers long -efore t'e da s of computers or image processing. T pe t'is# *+a ) gaussf(a) B multipl ing t'e image . Cote t'at uns'arp mas3ing gets its name from a procedure emplo ed . Images can -e su-tracted4 multiplied and di"ided in a similar 2a .

In 2'at do t'e differH +o2 are t'e ali3eH Local maximum and minimum filters T'e local >minimum and ma)imum filters@ \$minf and maxf& assign respecti"el t'e minimum or t'e ma)imum "alue of a local 2indo2 to t'e current pi)el in t'e output image. Load t'e image DcermetE into "aria-le a. Dilation is a local ma)imum filter4 erosion is a local minimum filter4 opening is t'e se5uence erosion and dilation4 and closing is t'e in"erse se5uence. atan*. T'e act on t'e image pi)el. cos. log*. E)amples are t'e mat'ematical functions: sin. <u-tract t'e original from t'e dilated image and put t'e result in b.. T'e general names for t'ese operations are erosion and dilation4 and can -e found as suc' under t'e >Morp'olog @ menu. etc.& . log !. abs. Cote 'o2 t'e image is made smoot'er4 2it'out ma3ing t'e o-9ect edges less s'arp. imag.. Point operations T'ere e)ist a large num-er of monadic point operations t'at are onl accessi-le t'roug' t'e command line. %sing ma)8min filters \$e5ui"alent to dilation8erosion& 2e 2ill construct a morp'ological gradient t'at 2ill -e modified into a morp'ological s'arpening operation. etc. exp. 0or e)ample4 tr a closing 2it' different si=e parameter on t'e image DcermetE \$2'ic' is a microscopic image 2it' some dar3 o-9ects in it&. angle.gaussf(a)@& are not t'e same. Tr different filter si=es as 2ell.T'e t2o uns'arp filters \$>a-laplace(a)@ and >2*a .. <u-tract t'e eroded image from t'e original and put t'e result in c. Tr t'e ot'er filters on t'e same image and 2atc' t'e si=e of o-9ects. complex. con. log. s"rt. Cotice t'e su-tle difference in t'e e es. * . etc. a = readim('cermet') b = dilation(a) ) a c = a ) erosion(a) Bot' results detect a-rupt c'anges in gre .pi)el. \$?emem-er to 2rite all commands in an e)ercise command file.le"el. Also4 note 'o2 t'e smaller o-9ects are remo"ed from t'e image. tan. real.

T'e o-9ects in it are clearl defined and are eas to segment. Plot t'e 'istogram of t'e ne2 image."alues are not used4 as are t'e upper 4. T'e red portion of t'e segmented image is t'e o-9ect and t'e -lac3 is t'e -ac3ground.'fixed'. <ometimes t'is is not desira-le4 for e)ample 2'en comparing images ac5uired under different lig'ting circumstances. T'is stretc'ing met'od is "er sensiti"e to noise. T'e t-res-old function contains a couple of different algorit'ms to do t'is. :ou 2ill not notice t'e difference. a = readim('maan') b = -ist&e"uali. T'is causes t'e lo2er and upper 1I of t'e gre . T pe# a(!.e(a) Plot t'e 'istogram -efore and after 'istogram e5uali=ation. Appl t'e -ist&e"uali."alues larger t'an t'e t'res'old are considered as part of t'e o-9ect. Ane solution is to in"ert t'e image -efore or after t'res'olding. Plot t'e 'istogram of t'e image EscaE. An t'is image it goes all 2rong# t'e -ac3ground 'as -ecome t'e o-9ect4 and t'e particles 'a"e -ecome D'olesE in t'e o-9ect. T'e simplest one4 Dfi)edE4 re5uires ou to pro"ide a parameter# t'e t'res'old le"el. Is in t'is image a stretc' or an e5uali=ation of t'e 'istogram prefera-leH Thresholding Load and stretc' t'e image DscaE again into "aria-le a.\$under t'e >Point@ menu&. T'at is -ecause gre . T'is is a feature of most images. Borrect t'is using t'e function stretc.istogram!"ased operations T'e function dip-ist \$under t'e ><tatistics@ menu& plots t'e 'istogram of an image. !!)&4 or t'e minus . 0or e)ample4 set a single pi)el in t'e original image a to 11. T'e ot'er t'ree estimate t'is parameter -ased on t'e 'istogram of t'e image in different 2a s. T'e 'istogram of DscaE is also "er poorl distri-uted. It simpl means t'at some gre . To see t'e difference 2it' t'e original image4 ma3e sure t'at t'e displa mode is set to >Cormal@. +o2e"er4 t'e stretc'ing algorit'm 2ill.e(a) ?epeat t'e pre"ious se5uence of commands 2it' t'e image DmaanE4 one of t'e earl images of our moon. :ou 2ill notice t'at t'e lo2er /.!) = ! Co2 plot t'e 'istogram again. Plot t'e 'istogram of t'e stretc'ed image to see t'is."alues to -e clipped -efore stretc'ing. gre . It is not "er important 'o2 t'is 2or3s e)actl 4 since manipulating an image in t'is 2a is not part of t'e course. 6' is t'e lo2er part of t'e 'istogram flatH ?epeat t'e pre"ious se5uence of commands 2it' t'e lo2er and upper percentiles in t'e stretc' function 1 and // respecti"el . Apen a ne2 image# DscaE7 in t'is image are -loodcells of a sic3le cell anaemia patient s'o2n.  .e function \$also on t'e >Point@ menu& to t'e image JscaJ4 and t'en plot t'e 'istogram again.inde)ing. Is in t'is image a stretc' or an e5uali=ation of t'e 'istogram prefera-leH a = readim('sca') b = -ist&e"uali. T'is can -e done 2it' t'e negation operator for t'e -inar image \$/t-res-old(a. 0or t'is image4 t'e parameter is not "er critical7 letEs use 111."alues occur more often in t'e image t'an ot'ers. T'is can -e accomplis'ed .

T'e point operations DnotE \$ /&4 DorE \$2&4 DandE \$3&4 D)orE \$xor& can onl -e issued directl onto t'e command line. 0or e)ample4 2e can use t'e >Binar Apening@ \$ bopening) to remo"e small o-9ects. / . T'is leads us to -inar morp'olog . 6e can use t'e relational operators to do t'res'olding \$04 14 ==4 /=4 etc&. A sic3le cell 2ill s'o2 a straig't line."alue image \$t-res-old()a.t'e red coloring of t'e displa . T'e -inar morp'ological filters can -e found under t'e >Binar 0ilters@ menu.) !!)4 note 'o2 t'e t'res'old "alue s'ould also -e c'anged&. In our case# b = a 0 !! Co2 2e 'a"e a -inar image4 recogni=a-le .operator for t'e gre . 6'at is t'e effect of t'e >Edge condition@H 6'at connecti"it gi"es t'e -est results for t'is imageH 6'at is t'e effect of t'e specified num-er of iterationsH Co2 load t'e image DscaE4 stretc' and t'res'old it \$K1*!&. T'e cells4 2'ic' are not full inside t'e picture s'ould -e remo"ed. %se t'e bs4eleton function \$under t'e >Binar 0ilters@ menu& to create a s3eleton of t'e o-9ects. Ma3e sure t'at t'e t'res'olding operation retains t'e 2'ole o-9ects.t'e function remo"e edge o-9ects \$under >Binar filters@&. 6'at is t'e influence of t'e DEdge BonditionEH 6'at does DEnd.Pi)el BonditionE controlH <ince most of t'e normal cells 'a"e a reflection4 2'ic' is also "isi-le in t'e t'res'olded image4 t'e s3eleton image s'o2s a circle for a normal cell. In t'e same 2a 4 2e can use bclosing to remo"e small 'oles in t'e o-9ects. T'is can -e done . 6e 2ill use t'e s3eleton operations to separate t'e 'ealt' from t'e sic3le cells. Anot'er 2a is to c'ange t'e t'res'olding operation.'fixed'. ) 8inary morphology T'ere are dedicated operations for -inar images.

. Tr linear stretc'ing. T'e d namic range is "er large. T'e "alues are comple). %nder t'e >Transforms@ menu4 ou 2ill find t'e for2ard and in"erse 0ourier transforms. Logarit'mic stretc'ing is usuall emplo ed to loo3 at 0ourier spectra.T'e picture re"eals one straig't line4 se"eral ellipses and some cur"ed lines \$originating from t2o cells o"erlapping&. 11 . Co2 2e 2ill decompose it into a real and an imaginar part. and store t'e 0ourier spectrum in "aria-le b. If ou also o"er2rote it4 clear t'em 2it' clear i . b = ft(a) T'e result loo3s li3e a 2'ite cloud in a -lac3 -ac3ground7 t'is is -ecause t'e default displa mapping is not t'e most ade5uate. If ou o"er2rote "aria-le i 2it' an image4 ou can use . 6'at ou see as an image is 9ust t'e amplitude of t'e spectrum. 6e need to multipl it .is t'e 0ourier spectrum&# re = real(b) im = imag(b)  :ou 2ill notice t'at t'e image im contains real "alues. Appl t'e for2ard transform to t'e image in "aria-le a. 6e 2ill do t'is t'roug' t'e real and imag commands \$t'ese are not in t'e menus&. 6rite t'ese and t'e pre"ious commands into a command file \$2e assume t'at . * The Fourier transform Load t'e image D5dna1E into "aria-le a. Co2 s2itc' on t'e >Pi)el testing@ mode and loo3 at t'e "alues in t'e spectrum.i. Co2 all ou 'a"e left is a single dot in t'e middle.

+o2e"er4 t'e in"erse transforms are not real4 -ut comple). Co2 2rite According to t'e t'eor 4 t'e in"erse transform of re s'ould -e t'e e"en component of t'e original image4 and t'e in"erse transform of im s'ould -e t'e odd component.measuring different o-9ect si=es4 li3e t'e area4 perimeter and lengt's.t'e DBonnecti"it E parameter. 6'at t'is function does is gi"e eac' separate o-9ect in t'e image a different gre . Co2 compute t'e in"erse transform of t'e amplitude and t'e p'ase term \$ma3e sure ou ta3e t'e real part of t'e in"erse transform4 not t'e magnitudeL&. +ere 2e 2ill do t'e same e)ercise .di"iding t'e original spectrum . T'res'old it. T'is is t'e e5ualit operator. real(ift(abs(b))) real(i f t (e x p ( i * a n g l e ( b ) ) )) 6'ic' one contains more informationH 6'at does t'e transform of t'e p'ase term loo3 li3eH +o2 do 2e get t'e original image -ac3H Tr real(i f t ( abs(b)+ex p ( i * a n g l e ( b ) ) )) 9 6easurements in images In section * 2e tried to distinguis' nuts from -olts using -inar morp'olog . real(ift(real(b))) real(ift(i+imag(b))) Co2 t'e displa ed images are not t'e amplitude of t'e comple) images4 so negati"e "alues are "isi-le.its amplitude. B e)amining t'e images4 ou can see t'at t'e imaginar parts are negligi-le small. 0ar more interesting is exp(i+angle(b)) \$2e 2ill call t'is t'e p'ase term&."alue. Load t'e DnutsM-olts1E image again. T'e amplitude is ac5uired using abs4 t'e p'ase using angle. Co2 eac' o-9ect is displa ed in a different color. ?emo"e t'em using t'e real function again. To e)tract o-9ect num-er 3 from t'e image4 2e can no2 do \$assuming la is t'e la-el image&# la == 5 Cote t'e dou-le e5ual sign. 11 . Turn it on. Co2 find t'e function label in t'e >Transforms@ menu.T'is 2ill return t'em to t'eir original use4 t'e imaginar num-er im = i+imag(b) − 1 . +o2e"er4 t'e p'ase itself is not too interesting. 6e can also calculate t'is . 0urt'ermore4 -ot' s'ould -e real. Cote t'at t'ere are onl a small num-er of different colors. 6'at o-9ects are considered separate can -e controlled . 6'ere does t'e difference come fromH Anot'er 2a of separating a comple) image is in amplitude and p'ase. %se t'e >Pi)el testing@ mode on t'e result to c'ec3 2'at "alues eac' o-9ect 'as. T'is ma3es it eas to see if o-9ects 'a"e -een correctl separated or not. If t'ere are more o-9ects4 some 2ill s'are a color. Ma3e sure t'e images are trul e"en and odd4 t'en add t'em up and compare 2it' t'e original image. T'ere is a special displa mode to loo3 at la-eled images.

Co2 2e are read to do some measuring.e is a MATLAB arra 2it' t'e si=es of t'e o-9ects. Co2 t pe dip-ist(s. 6e 2ill use t'e function mrs*ob."alue imageE is t'e original image -efore segmentation7 it 2onEt -e used . Tr t'em out. T'e nuts can -e o-tained .*!!!%. to DpaintE eac' o-9ect 2it' t'eir measured si=e. = data. B'oose t'e la-el image as t'e input4 and data as t'e measurement data. !!) T'is 2ill create a 'istogram for t'e si=es. T'e D. <elect t'e measure function in t'e >Anal sis@ menu.re . <elect 'si.xor. Co2 s.e' as t'e measurement.ing t'e original -inar image and t'e -olts image# nuts = xor(b. LetEs sa t'at si=es up to 1111 are for t'e nuts4 and larger si=es for t'e -olts. 6e can no2 t'res'old t'is image at t'e c'osen "alue of 1111 to retrie"e t'e -olts. Put t'e output in a "aria-le called data.bolts) 6e could do t'e same t'ing 2it' ot'er measurements of t'e o-9ects4 li3e t'e lengt' \$'feret'&4 t'e si=e of -ounding -o) \$'dimension'&4 or t'e perimeter \$'perimeter'&..# . T'ere are o-"iousl t2o si=e categories. If ou lea"e DA-9ect IDsE empt 4 all o-9ects 2ill -e measured. T'ere e)ist 2a s of doing t'is automaticall 4 -ut 2e 2onEt go into t'em no2.si.t'e measurements 2e 2ill ma3e4 so it can -e left out. 12 . T'e DA-9ect imageE is t'e la-eled image la.