Regions.html 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  2. "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  3. <html xmlns="http://www.w3.org/1999/xhtml">
  4. <head>
  5. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  6. <title>Regions &#8212; image features extraction 1.0.0 documentation</title>
  7. <link rel="stylesheet" href="../_static/alabaster.css" type="text/css" />
  8. <link rel="stylesheet" href="../_static/pygments.css" type="text/css" />
  9. <script type="text/javascript">
  10. var DOCUMENTATION_OPTIONS = {
  11. URL_ROOT: '../',
  12. VERSION: '1.0.0',
  13. COLLAPSE_INDEX: false,
  14. FILE_SUFFIX: '.html',
  15. HAS_SOURCE: true,
  16. SOURCELINK_SUFFIX: '.txt'
  17. };
  18. </script>
  19. <script type="text/javascript" src="../_static/jquery.js"></script>
  20. <script type="text/javascript" src="../_static/underscore.js"></script>
  21. <script type="text/javascript" src="../_static/doctools.js"></script>
  22. <link rel="index" title="Index" href="../genindex.html" />
  23. <link rel="search" title="Search" href="../search.html" />
  24. <link rel="stylesheet" href="../_static/custom.css" type="text/css" />
  25. <meta name="viewport" content="width=device-width, initial-scale=0.9, maximum-scale=0.9" />
  26. </head>
  27. <body>
  28. <div class="document">
  29. <div class="documentwrapper">
  30. <div class="bodywrapper">
  31. <div class="body" role="main">
  32. <h1>Source code for Regions</h1><div class="highlight"><pre>
  33. <span></span><span class="kn">import</span> <span class="nn">pandas</span> <span class="k">as</span> <span class="nn">pd</span>
  34. <span class="kn">from</span> <span class="nn">image_features_extraction</span> <span class="k">import</span> <span class="n">my_iterator</span>
  35. <span class="kn">from</span> <span class="nn">image_features_extraction</span> <span class="k">import</span> <span class="n">Region</span>
  36. <span class="kn">from</span> <span class="nn">image_features_extraction</span> <span class="k">import</span> <span class="n">MyException</span>
  37. <span class="kn">from</span> <span class="nn">skimage.measure</span> <span class="k">import</span> <span class="n">label</span><span class="p">,</span> <span class="n">regionprops</span>
  38. <div class="viewcode-block" id="Regions"><a class="viewcode-back" href="../code.html#Regions.Regions">[docs]</a><span class="k">class</span> <span class="nc">Regions</span><span class="p">(</span><span class="n">my_iterator</span><span class="o">.</span><span class="n">my_iterator</span><span class="p">):</span>
  39. <span class="sd">&quot;&quot;&quot;</span>
  40. <span class="sd"> This class represent a collection of regions: segmented image elements</span>
  41. <span class="sd"> It cannot be instanced directly. It is returned from the object :class:`Image` through the function</span>
  42. <span class="sd"> Regions(...)</span>
  43. <span class="sd"> :example:</span>
  44. <span class="sd"> &gt;&gt;&gt; import image_features_extraction as fe</span>
  45. <span class="sd"> &gt;&gt;&gt; imgs = fe.Images(folder_name)</span>
  46. <span class="sd"> &gt;&gt;&gt; img = imgs.item(1)</span>
  47. <span class="sd"> &gt;&gt;&gt; regs = img.Regions()</span>
  48. <span class="sd"> &quot;&quot;&quot;</span>
  49. <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">obj_regions</span><span class="p">):</span>
  50. <span class="k">try</span><span class="p">:</span>
  51. <span class="bp">self</span><span class="o">.</span><span class="n">__iterator_init__</span><span class="p">()</span>
  52. <span class="bp">self</span><span class="o">.</span><span class="n">__obj_regions_org</span> <span class="o">=</span> <span class="n">obj_regions</span>
  53. <span class="bp">self</span><span class="o">.</span><span class="n">__obj_regions</span> <span class="o">=</span> <span class="n">regionprops</span><span class="p">(</span><span class="n">obj_regions</span><span class="p">)</span> <span class="c1"># used regionprops from skimage</span>
  54. <span class="bp">self</span><span class="o">.</span><span class="n">count_update</span><span class="p">(</span><span class="nb">len</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">__obj_regions</span><span class="p">))</span>
  55. <span class="k">except</span> <span class="n">MyException</span><span class="o">.</span><span class="n">MyException</span> <span class="k">as</span> <span class="n">e</span><span class="p">:</span>
  56. <span class="nb">print</span><span class="p">(</span><span class="n">e</span><span class="o">.</span><span class="n">args</span><span class="p">)</span>
  57. <span class="k">def</span> <span class="nf">__regions_obj</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
  58. <span class="sd">&quot;&quot;&quot;</span>
  59. <span class="sd"> This function returns the Internal object regions. it is used only for debugging</span>
  60. <span class="sd"> &quot;&quot;&quot;</span>
  61. <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">__obj_regions_org</span>
  62. <div class="viewcode-block" id="Regions.item"><a class="viewcode-back" href="../code.html#Regions.Regions.item">[docs]</a> <span class="k">def</span> <span class="nf">item</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">i</span><span class="p">):</span>
  63. <span class="sd">&quot;&quot;&quot;</span>
  64. <span class="sd"> Item(..) returns the i-th image element of the regions.</span>
  65. <span class="sd"> :param i: the i-th element of the collection region</span>
  66. <span class="sd"> :type i: int</span>
  67. <span class="sd"> :returns: Region</span>
  68. <span class="sd"> :rtype: object</span>
  69. <span class="sd"> :example:</span>
  70. <span class="sd"> &gt;&gt;&gt; import image_features_extraction as fe</span>
  71. <span class="sd"> &gt;&gt;&gt; imgs = fe.Images(folder_name)</span>
  72. <span class="sd"> &gt;&gt;&gt; img = imgs.item(1)</span>
  73. <span class="sd"> &gt;&gt;&gt; regs = img.Regions()</span>
  74. <span class="sd"> &gt;&gt;&gt; reg = regs.item(1)</span>
  75. <span class="sd"> &quot;&quot;&quot;</span>
  76. <span class="k">try</span><span class="p">:</span>
  77. <span class="k">if</span> <span class="n">i</span> <span class="o">&gt;=</span> <span class="bp">self</span><span class="o">.</span><span class="n">count</span><span class="p">():</span>
  78. <span class="k">raise</span> <span class="n">MyException</span><span class="o">.</span><span class="n">MyException</span><span class="p">(</span><span class="s2">&quot;error: index out of bound&quot;</span><span class="p">)</span>
  79. <span class="k">return</span> <span class="n">Region</span><span class="o">.</span><span class="n">Region</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">__obj_regions</span><span class="p">[</span><span class="n">i</span><span class="p">])</span>
  80. <span class="k">except</span> <span class="n">MyException</span><span class="o">.</span><span class="n">MyException</span> <span class="k">as</span> <span class="n">e</span><span class="p">:</span>
  81. <span class="nb">print</span><span class="p">(</span><span class="n">e</span><span class="o">.</span><span class="n">args</span><span class="p">)</span>
  82. <span class="k">return</span> <span class="kc">None</span></div>
  83. <div class="viewcode-block" id="Regions.prop_values"><a class="viewcode-back" href="../code.html#Regions.Regions.prop_values">[docs]</a> <span class="k">def</span> <span class="nf">prop_values</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">prop_name</span><span class="p">):</span>
  84. <span class="sd">&quot;&quot;&quot;</span>
  85. <span class="sd"> prop_values(...) returns the values of the specified property/measure name (e.g., &#39;area&#39;) for all image</span>
  86. <span class="sd"> elements contained in the object Regions. For a list of property names refer to &quot;regionprops &lt;http://scikit-image.org/docs/dev/api/skimage.measure.html#skimage.measure.regionprops&gt;&quot;_.</span>
  87. <span class="sd"> :param prop_name: name of the property to measure (e.g, &#39;area&#39;)</span>
  88. <span class="sd"> :type prop_name: string</span>
  89. <span class="sd"> :returns: values of the property name in input</span>
  90. <span class="sd"> :rtype: List</span>
  91. <span class="sd"> :example:</span>
  92. <span class="sd"> &gt;&gt;&gt; import image_features_extraction as fe</span>
  93. <span class="sd"> &gt;&gt;&gt; imgs = fe.Images(folder_name)</span>
  94. <span class="sd"> &gt;&gt;&gt; img = imgs.item(1)</span>
  95. <span class="sd"> &gt;&gt;&gt; regs = img.Regions()</span>
  96. <span class="sd"> &gt;&gt;&gt; areas = regs.prop_values(&#39;area&#39;)</span>
  97. <span class="sd"> &quot;&quot;&quot;</span>
  98. <span class="k">try</span><span class="p">:</span>
  99. <span class="n">vals</span> <span class="o">=</span> <span class="p">[]</span>
  100. <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">__obj_regions</span><span class="p">:</span>
  101. <span class="n">vals</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="nb">getattr</span><span class="p">(</span><span class="n">i</span><span class="p">,</span> <span class="n">prop_name</span><span class="p">))</span>
  102. <span class="k">return</span> <span class="n">vals</span>
  103. <span class="k">except</span> <span class="ne">Exception</span> <span class="k">as</span> <span class="n">e</span><span class="p">:</span>
  104. <span class="nb">print</span><span class="p">(</span><span class="n">e</span><span class="o">.</span><span class="n">args</span><span class="p">)</span>
  105. <span class="k">return</span> <span class="kc">None</span></div>
  106. <div class="viewcode-block" id="Regions.get_features"><a class="viewcode-back" href="../code.html#Regions.Regions.get_features">[docs]</a> <span class="k">def</span> <span class="nf">get_features</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">features</span><span class="p">,</span> <span class="n">class_value</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">class_name</span><span class="o">=</span><span class="s1">&#39;class_name&#39;</span><span class="p">):</span>
  107. <span class="sd">&quot;&quot;&quot;</span>
  108. <span class="sd"> get_features(...) returns a table with all values for the property names given in input, and supplies an</span>
  109. <span class="sd"> additional parameter for feature classification</span>
  110. <span class="sd"> :param features: list of property/measure names (e.g, &#39;area&#39;, &#39;centroid&#39;, etc )</span>
  111. <span class="sd"> :type features: List</span>
  112. <span class="sd"> :param class_value: classification label</span>
  113. <span class="sd"> :type class_value: int, string (default=None)</span>
  114. <span class="sd"> :returns: table cointaining all property values (columns) for all elements in the regions object (rows)</span>
  115. <span class="sd"> :rtype: Pandas.DataFrame</span>
  116. <span class="sd"> :example:</span>
  117. <span class="sd"> &gt;&gt;&gt; import image_features_extraction as fe</span>
  118. <span class="sd"> &gt;&gt;&gt; imgs = fe.Images(folder_name)</span>
  119. <span class="sd"> &gt;&gt;&gt; img = imgs.item(1)</span>
  120. <span class="sd"> &gt;&gt;&gt; regs = img.Regions()</span>
  121. <span class="sd"> &gt;&gt;&gt; df = regs.get_features([&#39;label&#39;, &#39;area&#39;,&#39;perimeter&#39;, &#39;centroid&#39;], class_value=1)</span>
  122. <span class="sd"> &quot;&quot;&quot;</span>
  123. <span class="n">df</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">DataFrame</span><span class="p">()</span>
  124. <span class="k">try</span><span class="p">:</span>
  125. <span class="k">for</span> <span class="n">f</span> <span class="ow">in</span> <span class="n">features</span><span class="p">:</span>
  126. <span class="n">df</span><span class="p">[</span><span class="n">f</span><span class="p">]</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">prop_values</span><span class="p">(</span><span class="n">f</span><span class="p">)</span>
  127. <span class="k">if</span> <span class="n">class_value</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
  128. <span class="n">df</span><span class="p">[</span><span class="n">class_name</span><span class="p">]</span> <span class="o">=</span> <span class="n">class_value</span>
  129. <span class="k">return</span> <span class="n">df</span>
  130. <span class="k">except</span> <span class="ne">Exception</span> <span class="k">as</span> <span class="n">e</span><span class="p">:</span>
  131. <span class="nb">print</span><span class="p">(</span><span class="s2">&quot;one or more input labels might be wrong:</span><span class="si">{}</span><span class="s2">&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">e</span><span class="p">))</span>
  132. <span class="k">return</span> <span class="kc">None</span></div></div>
  133. </pre></div>
  134. </div>
  135. </div>
  136. </div>
  137. <div class="sphinxsidebar" role="navigation" aria-label="main navigation">
  138. <div class="sphinxsidebarwrapper">
  139. <h1 class="logo"><a href="../index.html">image features extraction</a></h1>
  140. <h3>Navigation</h3>
  141. <p class="caption"><span class="caption-text">Contents:</span></p>
  142. <ul>
  143. <li class="toctree-l1"><a class="reference internal" href="../intro.html">Introduction</a></li>
  144. <li class="toctree-l1"><a class="reference internal" href="../tutorial.html">Tutorial</a></li>
  145. <li class="toctree-l1"><a class="reference internal" href="../code.html">Classes/Modules</a></li>
  146. </ul>
  147. <div class="relations">
  148. <h3>Related Topics</h3>
  149. <ul>
  150. <li><a href="../index.html">Documentation overview</a><ul>
  151. <li><a href="index.html">Module code</a><ul>
  152. </ul></li>
  153. </ul></li>
  154. </ul>
  155. </div>
  156. <div id="searchbox" style="display: none" role="search">
  157. <h3>Quick search</h3>
  158. <form class="search" action="../search.html" method="get">
  159. <div><input type="text" name="q" /></div>
  160. <div><input type="submit" value="Go" /></div>
  161. <input type="hidden" name="check_keywords" value="yes" />
  162. <input type="hidden" name="area" value="default" />
  163. </form>
  164. </div>
  165. <script type="text/javascript">$('#searchbox').show(0);</script>
  166. </div>
  167. </div>
  168. <div class="clearer"></div>
  169. </div>
  170. <div class="footer">
  171. &copy;2017, Remigio Picone.
  172. |
  173. Powered by <a href="http://sphinx-doc.org/">Sphinx 1.6.3</a>
  174. &amp; <a href="https://github.com/bitprophet/alabaster">Alabaster 0.7.10</a>
  175. </div>
  176. </body>
  177. </html>