<html> <head> <meta content="text/html; charset=ISO-8859-1" http-equiv="Content-Type"> <title>mmcwatershed</title> <link href="../tbxdok.css" rel="stylesheet"> </head> <body> <table class="topNav"> <tr> <td class="index"> [<a href="../morph/mmcthin.html"><tt>mmcthin</tt></a>] [<a href="index.html">Up</a>] [<a href="../morph/mmskiz.html"><tt>mmskiz</tt></a>] </td> <td class="title">Thinning And Thickening</td> </tr> </table> <h1>mmcwatershed <br> <span class="subtitle">Detection of watershed from markers. </span> </h1> <div class="synopsis"> <H2>Synopsis</H2> <div class="H2"> <div class="prototype">Y = <span class="fun">mmcwatershed</span>( <span class="par">f</span>, <span class="par">g</span>, <span class="par">Bc</span> = None, <span class="par">LINEREG</span> = "LINES" ) </div> <p>Implemented in <b>Python.</b> </p> <div class="input"> <H3>Input</H3> <div class="H3"> <table class="deflist"> <tbody valign="baseline"> <tr> <td class="term"><span class="par">f</span></td> <td class="def"><span class="type"><a href="../mmtypes/mmImage.html">Image</a></span> Gray-scale (uint8 or uint16) image. </td> </tr> <tr> <td class="term"><span class="par">g</span></td> <td class="def"><span class="type"><a href="../mmtypes/mmImage.html">Image</a></span> Gray-scale (uint8 or uint16) or binary image. <p>marker image: binary or labeled.</p></td> </tr> <tr> <td class="term"><span class="par">Bc</span></td> <td class="def"><span class="type"><a href="../mmtypes/mmSe.html">Structuring Element</a></span><p>(watershed connectivity)</p><p>Default: <code>None</code> (3x3 elementary cross) </p></td> </tr> <tr> <td class="term"><span class="par">LINEREG</span></td> <td class="def"><span class="type"><a href="../mmtypes/mmString.html">String</a></span><p>'LINES' or ' REGIONS'.</p><p>Default: <code>"LINES"</code> </p></td> </tr> </tbody> </table> </div> </div> <div class="output"> <H3>Output</H3> <div class="H3"> <table class="deflist"> <tbody valign="baseline"> <tr> <td class="term"><span class="par">Y</span></td> <td class="def"><span class="type"><a href="../mmtypes/mmImage.html">Image</a></span> Gray-scale (uint8 or uint16) or binary image. </td> </tr> </tbody> </table> </div> </div> </div> </div> <div class="descr"> <H2>Description</H2> <div class="H2"> <p> <span class="fun">mmcwatershed</span> creates the image <code>y</code> by detecting the domain of the catchment basins of <code>f</code> indicated by the marker image <code>g</code>, according to the connectivity defined by <code>Bc</code>. According to the flag LINEREG <code>y</code> will be a labeled image of the catchment basins domain or just a binary image that presents the watershed lines. To know more about watershed and watershed from markers, see BeucMeye:93. The implementation of this function is based on LotuFalc:00. WARNING: There is a common mistake related to the marker image <code>g</code>. If this image contains only zeros and ones, but it is not a binary image, the result will be an image with all ones. If the marker image is binary, you have to set this explicitly using the <code>logical</code> function. </p> </div> </div> <div class="examples"> <H2>Examples</H2> <div class="H2"> <p> <div class="example"> <div class="listing"> <pre class="user">>>> a = uint8([\ [10, 10, 10, 10, 10, 10, 10],\ [10, 9, 6, 18, 6, 5, 10],\ [10, 9, 6, 18, 6, 8, 10],\ [10, 9, 9, 15, 9, 9, 10],\ [10, 9, 9, 15, 12, 10, 10],\ [10, 10, 10, 10, 10, 10, 10]])</pre> <pre class="computer"></pre> <pre class="user">>>> b = mmcmp(a,'==',uint8(6))</pre> <pre class="computer"></pre> <pre class="user">>>> print mmcwatershed(a,b)</pre> <pre class="computer">[[0 0 0 1 0 0 0] [0 0 0 1 0 0 0] [0 0 0 1 0 0 0] [0 0 0 1 0 0 0] [0 0 0 1 0 0 0] [0 0 0 0 1 0 0]]</pre> <pre class="user">>>> print mmcwatershed(a,b,mmsecross(),'REGIONS')</pre> <pre class="computer">[[1 1 1 1 2 2 2] [1 1 1 1 2 2 2] [1 1 1 1 2 2 2] [1 1 1 1 2 2 2] [1 1 1 1 2 2 2] [1 1 1 1 2 2 2]]</pre> </div> </div> <div class="example"> <div class="listing"> <pre class="user">>>> f=mmreadgray('astablet.tif')</pre> <pre class="computer"></pre> <pre class="user">>>> grad=mmgradm(f)</pre> <pre class="computer"></pre> <pre class="user">>>> mark=mmregmin(mmhmin(grad,17))</pre> <pre class="computer"></pre> <pre class="user">>>> w=mmcwatershed(grad,mark)</pre> <pre class="computer"></pre> <pre class="user">>>> mmshow(grad)</pre> <pre class="computer"></pre> <pre class="user">>>> mmshow(mark)</pre> <pre class="computer"></pre> <pre class="user">>>> mmshow(w)</pre> <pre class="computer"></pre> </div> <table class="images"> <tbody align="center"> <tr class="image" valign="bottom"> <td><img width="251" src="../images/img_mmcwatershed_001.jpg"></td> <td><img width="251" src="../images/img_mmcwatershed_002.jpg"></td> <td class="spare"></td> </tr> <tr class="title" valign="baseline"> <td><a href="../images/img_mmcwatershed_001.jpg">grad</a></td> <td><a href="../images/img_mmcwatershed_002.jpg">mark</a></td> <td class="spare"></td> </tr> </tbody> </table> <table class="images"> <tbody align="center"> <tr class="image" valign="bottom"> <td><img width="251" src="../images/img_mmcwatershed_003.jpg"></td> <td class="spare"></td> </tr> <tr class="title" valign="baseline"> <td><a href="../images/img_mmcwatershed_003.jpg">w</a></td> <td class="spare"></td> </tr> </tbody> </table> </div> </p> </div> </div> <div class="equation"> <H2>Equation</H2> <div class="H2"> <p> <div class="bridge">Equation (watershed regions):</div> <div class="eqfig"> <img src="../images/eq_mmcwatershed001.png"> </div> <div class="bridge">Minimum length of a point to a set:</div> <div class="eqfig"> <img src="../images/eq_mmcwatershed002.png"> </div> <div class="bridge">Length of a point to a set:</div> <div class="eqfig"> <img src="../images/eq_mmcwatershed003.png"> </div> <div class="bridge">Where:</div> <div class="eqfig"> <img src="../images/eq_mmcwatershed004.png"> </div>is the path between the point <i>x</i> and subset <i>X</i> under connectivity <i>Bc</i> </p> </div> </div> <div class="sourcecode"> <H2>Source Code</H2> <div class="H2"> <pre class="listing"> def mmcwatershed(f, g, Bc=None, LINEREG="LINES"): from Numeric import ones, zeros, nonzero, array, put, take, argmin, transpose, compress, concatenate if Bc is None: Bc = mmsecross() return g print 'starting' withline = (LINEREG == 'LINES') if mmis(g,'binary'): g = mmlabel(g,Bc) print 'before 1. mmpad4n' status = mmpad4n(uint8(zeros(f.shape)),Bc, 3) f = mmpad4n( f,Bc,0) #pad input image print 'before 2. mmpad4n' y = mmpad4n( g,Bc,0) # pad marker image with 0 if withline: y1 = mmintersec(mmbinary(y), 0) costM = mmlimits(f)[1] * ones(f.shape) # cummulative cost function image mi = nonzero(mmgradm(y,mmsebox(0),Bc).flat) # 1D index of internal contour of marker print 'before put costM' put(costM.flat,mi, 0) HQueue=transpose([mi, take(costM.flat, mi)]) # init hierarquical queue: index,value print 'before mmse2list0' Bi=mmse2list0(f,Bc) # get 1D displacement neighborhood pixels x,v = mmmat2set(Bc) while HQueue: print 'Hq=',HQueue i = argmin(HQueue[:,1]) # i is the index of minimum value print 'imin=',i pi = HQueue[i,0] print 'pi=',pi ii = ones(HQueue.shape[0]) ii[i] = 0 print 'ii=',ii HQueue = transpose(array([compress(ii,HQueue[:,0]), compress(ii,HQueue[:,1])])) # remove this pixel from queue print 'H=',HQueue put(status.flat, pi, 1) # make it a permanent label for qi in pi+Bi : # for each neighbor of pi if (status.flat[qi] != 3): # not image border if (status.flat[qi] != 1): # if not permanent cost_M = max(costM.flat[pi], f.flat[qi]) if cost_M < costM.flat[qi]: print 'qi=',qi costM.flat[qi] = cost_M y.flat[qi] = y.flat[pi] # propagate the label aux = zeros(array(HQueue.shape) + [1,0]) aux[:-1,:] = HQueue aux[-1,:]=[qi, cost_M] HQueue = aux # insert pixel in the queue print 'insert H=',HQueue elif (withline and (y.flat[qi] != y.flat[pi]) and (y1.flat[pi] == 0) and (y1.flat[qi] == 0) ): y1.flat[pi] = 1 if withline: Y = y1 else: Y = y return Y </pre> </div> </div> <div class="seealso"> <H2>See also</H2> <div class="H2"> <table class="deflist"> <tbody valign="baseline"> <tr> <td class="term"><a href="../morph/mmfreedom.html">mmfreedom</a></td> <td class="def">Control automatic data type conversion.</td> </tr> <tr> <td class="term"><a href="../morph/mmwatershed.html">mmwatershed</a></td> <td class="def">Watershed detection.</td> </tr> <tr> <td class="term"><a href="../morph/mmsebox.html">mmsebox</a></td> <td class="def">Create a box structuring element.</td> </tr> <tr> <td class="term"><a href="../morph/mmsecross.html">mmsecross</a></td> <td class="def">Diamond structuring element and elementary 3x3 cross.</td> </tr> <tr> <td class="term"><a href="../morph/mmswatershed.html">mmswatershed</a></td> <td class="def">Detection of similarity-based watershed from markers.</td> </tr> <tr> <td class="term"><a href="../mmdemos/mmdcalc.html">mmdcalc</a></td> <td class="def">Extract the keys of a calculator.</td> </tr> </tbody> </table> </div> </div> <center> <table class="botNav"> <tr> <td class="index"> [<a href="../morph/mmcthin.html"><tt>mmcthin</tt></a>] [<a href="index.html">Up</a>] [<a href="../morph/mmskiz.html"><tt>mmskiz</tt></a>] </td> <td rowspan="2" class="xhtml"><a href="http://www.python.org"><img width="55" alt="Python" height="22" src="../PythonPoweredSmall.gif"></a></td> </tr> <tr> <td class="copyright">Copyright (c) 2003, Roberto A. Lotufo, UNICAMP-University of Campinas; Rubens C. Machado, CenPRA-Renato Archer Research Center.</td> </tr> </table> </center> </body> </html>