123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341 |
- <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
- "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
- <html xmlns="http://www.w3.org/1999/xhtml">
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
- <title>Regions — Image Features Extraction 1.0.0 documentation</title>
- <link rel="stylesheet" href="../_static/alabaster.css" type="text/css" />
- <link rel="stylesheet" href="../_static/pygments.css" type="text/css" />
- <script type="text/javascript">
- var DOCUMENTATION_OPTIONS = {
- URL_ROOT: '../',
- VERSION: '1.0.0',
- COLLAPSE_INDEX: false,
- FILE_SUFFIX: '.html',
- HAS_SOURCE: true,
- SOURCELINK_SUFFIX: '.txt'
- };
- </script>
- <script type="text/javascript" src="../_static/jquery.js"></script>
- <script type="text/javascript" src="../_static/underscore.js"></script>
- <script type="text/javascript" src="../_static/doctools.js"></script>
- <link rel="index" title="Index" href="../genindex.html" />
- <link rel="search" title="Search" href="../search.html" />
-
- <link rel="stylesheet" href="../_static/custom.css" type="text/css" />
-
-
- <meta name="viewport" content="width=device-width, initial-scale=0.9, maximum-scale=0.9" />
- </head>
- <body>
-
- <div class="document">
- <div class="documentwrapper">
- <div class="bodywrapper">
- <div class="body" role="main">
-
- <h1>Source code for Regions</h1><div class="highlight"><pre>
- <span></span><span class="kn">import</span> <span class="nn">pandas</span> <span class="k">as</span> <span class="nn">pd</span>
- <span class="kn">from</span> <span class="nn">image_features_extraction</span> <span class="k">import</span> <span class="n">my_iterator</span>
- <span class="kn">from</span> <span class="nn">image_features_extraction</span> <span class="k">import</span> <span class="n">Region</span>
- <span class="kn">from</span> <span class="nn">image_features_extraction</span> <span class="k">import</span> <span class="n">MyException</span>
- <span class="kn">from</span> <span class="nn">image_features_extraction</span> <span class="k">import</span> <span class="n">Features</span>
- <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>
- <span class="kn">from</span> <span class="nn">image_features_extraction</span> <span class="k">import</span> <span class="n">Utils</span>
- <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>
- <span class="sd">"""</span>
- <span class="sd"> This class represent a collection of regions: segmented image elements</span>
- <span class="sd"> It cannot be instanced directly. It is returned from the object :class:`Image` through the function</span>
- <span class="sd"> Regions(...)</span>
- <span class="sd"> :example:</span>
- <span class="sd"> >>> import image_features_extraction as fe</span>
- <span class="sd"> >>> imgs = fe.Images(folder_name)</span>
- <span class="sd"> >>> img = imgs.item(1)</span>
- <span class="sd"> >>> regs = img.Regions()</span>
- <span class="sd"> """</span>
- <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>
- <span class="k">try</span><span class="p">:</span>
- <span class="c1">#self.__iterator_init__()</span>
- <span class="nb">super</span><span class="p">()</span><span class="o">.</span><span class="fm">__init__</span><span class="p">()</span>
- <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>
- <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>
- <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>
- <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>
- <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>
- <span class="k">def</span> <span class="nf">__regions_obj</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
- <span class="sd">"""</span>
- <span class="sd"> This function returns the Internal object regions. it is used only for debugging</span>
- <span class="sd"> """</span>
- <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">__obj_regions_org</span>
- <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>
- <span class="sd">"""</span>
- <span class="sd"> Item(..) returns the i-th image element of the regions.</span>
- <span class="sd"> :param i: the i-th element of the collection region</span>
- <span class="sd"> :type i: int</span>
- <span class="sd"> :returns: Region</span>
- <span class="sd"> :rtype: object</span>
- <span class="sd"> :example:</span>
- <span class="sd"> >>> import image_features_extraction as fe</span>
- <span class="sd"> >>> imgs = fe.Images(folder_name)</span>
- <span class="sd"> >>> img = imgs.item(1)</span>
- <span class="sd"> >>> regs = img.Regions()</span>
- <span class="sd"> >>> reg = regs.item(1)</span>
- <span class="sd"> """</span>
- <span class="k">try</span><span class="p">:</span>
- <span class="k">if</span> <span class="n">i</span> <span class="o">>=</span> <span class="bp">self</span><span class="o">.</span><span class="n">count</span><span class="p">():</span>
- <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">"error: index out of bound"</span><span class="p">)</span>
- <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>
- <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>
- <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>
- <span class="k">return</span> <span class="kc">None</span></div>
- <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>
- <span class="sd">"""</span>
- <span class="sd"> Measure the values of the specified property/measure name (e.g., 'area') for all</span>
- <span class="sd"> elements contained in the object Regions.</span>
- <span class="sd"> :param prop_name: name of the property to measure (e.g, 'area')</span>
- <span class="sd"> :type prop_name: string</span>
- <span class="sd"> :returns: property name values</span>
- <span class="sd"> :rtype: List</span>
- <span class="sd"> :example:</span>
- <span class="sd"> >>> import image_features_extraction as fe</span>
- <span class="sd"> >>> imgs = fe.Images(folder_name)</span>
- <span class="sd"> >>> img = imgs.item(1)</span>
- <span class="sd"> >>> regs = img.Regions()</span>
- <span class="sd"> >>> areas = regs.prop_values('area')</span>
- <span class="sd"> The following properties can be accessed as attributes or keys:</span>
- <span class="sd"> **area** : int</span>
- <span class="sd"> Number of pixels of region.</span>
- <span class="sd"> **bbox** : tuple</span>
- <span class="sd"> Bounding box ``(min_row, min_col, max_row, max_col)``.</span>
- <span class="sd"> Pixels belonging to the bounding box are in the half-open interval</span>
- <span class="sd"> ``[min_row; max_row)`` and ``[min_col; max_col)``.</span>
- <span class="sd"> **bbox_area** : int</span>
- <span class="sd"> Number of pixels of bounding box.</span>
- <span class="sd"> **centroid** : array</span>
- <span class="sd"> Centroid coordinate tuple ``(row, col)``.</span>
- <span class="sd"> **convex_area** : int</span>
- <span class="sd"> Number of pixels of convex hull image.</span>
- <span class="sd"> **convex_image** : (H, J) ndarray</span>
- <span class="sd"> Binary convex hull image which has the same size as bounding box.</span>
- <span class="sd"> **coords** : (N, 2) ndarray</span>
- <span class="sd"> Coordinate list ``(row, col)`` of the region.</span>
- <span class="sd"> **eccentricity** : float</span>
- <span class="sd"> Eccentricity of the ellipse that has the same second-moments as the</span>
- <span class="sd"> region. The eccentricity is the ratio of the focal distance</span>
- <span class="sd"> (distance between focal points) over the major axis length.</span>
- <span class="sd"> The value is in the interval [0, 1).</span>
- <span class="sd"> When it is 0, the ellipse becomes a circle.</span>
- <span class="sd"> **equivalent_diameter** : float</span>
- <span class="sd"> The diameter of a circle with the same area as the region.</span>
- <span class="sd"> **euler_number** : int</span>
- <span class="sd"> Euler characteristic of region. Computed as number of objects (= 1)</span>
- <span class="sd"> subtracted by number of holes (8-connectivity).</span>
- <span class="sd"> **extent** : float</span>
- <span class="sd"> Ratio of pixels in the region to pixels in the total bounding box.</span>
- <span class="sd"> Computed as ``area / (rows * cols)``</span>
- <span class="sd"> **filled_area** : int</span>
- <span class="sd"> Number of pixels of filled region.</span>
- <span class="sd"> **filled_image** : (H, J) ndarray</span>
- <span class="sd"> Binary region image with filled holes which has the same size as</span>
- <span class="sd"> bounding box.</span>
- <span class="sd"> **image** : (H, J) ndarray</span>
- <span class="sd"> Sliced binary region image which has the same size as bounding box.</span>
- <span class="sd"> **inertia_tensor** : (2, 2) ndarray</span>
- <span class="sd"> Inertia tensor of the region for the rotation around its mass.</span>
- <span class="sd"> **inertia_tensor_eigvals** : tuple</span>
- <span class="sd"> The two eigen values of the inertia tensor in decreasing order.</span>
- <span class="sd"> **intensity_image** : ndarray</span>
- <span class="sd"> Image inside region bounding box.</span>
- <span class="sd"> **label** : int</span>
- <span class="sd"> The label in the labeled input image.</span>
- <span class="sd"> **local_centroid** : array</span>
- <span class="sd"> Centroid coordinate tuple ``(row, col)``, relative to region bounding</span>
- <span class="sd"> box.</span>
- <span class="sd"> **major_axis_length** : float</span>
- <span class="sd"> The length of the major axis of the ellipse that has the same</span>
- <span class="sd"> normalized second central moments as the region.</span>
- <span class="sd"> **max_intensity** : float</span>
- <span class="sd"> Value with the greatest intensity in the region.</span>
- <span class="sd"> **mean_intensity** : float</span>
- <span class="sd"> Value with the mean intensity in the region.</span>
- <span class="sd"> **min_intensity** : float</span>
- <span class="sd"> Value with the least intensity in the region.</span>
- <span class="sd"> **minor_axis_length** : float</span>
- <span class="sd"> The length of the minor axis of the ellipse that has the same</span>
- <span class="sd"> normalized second central moments as the region.</span>
- <span class="sd"> **moments** : (3, 3) ndarray</span>
- <span class="sd"> Spatial moments up to 3rd order::</span>
- <span class="sd"> m_ji = sum{ array(x, y) * x^j * y^i }</span>
- <span class="sd"> where the sum is over the `x`, `y` coordinates of the region.</span>
- <span class="sd"> **moments_central** : (3, 3) ndarray</span>
- <span class="sd"> Central moments (translation invariant) up to 3rd order::</span>
- <span class="sd"> mu_ji = sum{ array(x, y) * (x - x_c)^j * (y - y_c)^i }</span>
- <span class="sd"> where the sum is over the `x`, `y` coordinates of the region,</span>
- <span class="sd"> and `x_c` and `y_c` are the coordinates of the region's centroid.</span>
- <span class="sd"> **moments_hu** : tuple</span>
- <span class="sd"> Hu moments (translation, scale and rotation invariant).</span>
- <span class="sd"> **moments_normalized** : (3, 3) ndarray</span>
- <span class="sd"> Normalized moments (translation and scale invariant) up to 3rd order::</span>
- <span class="sd"> nu_ji = mu_ji / m_00^[(i+j)/2 + 1]</span>
- <span class="sd"> where `m_00` is the zeroth spatial moment.</span>
- <span class="sd"> **orientation** : float</span>
- <span class="sd"> Angle between the X-axis and the major axis of the ellipse that has</span>
- <span class="sd"> the same second-moments as the region. Ranging from `-pi/2` to</span>
- <span class="sd"> `pi/2` in counter-clockwise direction.</span>
- <span class="sd"> **perimeter** : float</span>
- <span class="sd"> Perimeter of object which approximates the contour as a line</span>
- <span class="sd"> through the centers of border pixels using a 4-connectivity.</span>
- <span class="sd"> **solidity** : float</span>
- <span class="sd"> Ratio of pixels in the region to pixels of the convex hull image.</span>
- <span class="sd"> **weighted_centroid** : array</span>
- <span class="sd"> Centroid coordinate tuple ``(row, col)`` weighted with intensity</span>
- <span class="sd"> image.</span>
- <span class="sd"> **weighted_local_centroid** : array</span>
- <span class="sd"> Centroid coordinate tuple ``(row, col)``, relative to region bounding</span>
- <span class="sd"> box, weighted with intensity image.</span>
- <span class="sd"> **weighted_moments** : (3, 3) ndarray</span>
- <span class="sd"> Spatial moments of intensity image up to 3rd order::</span>
- <span class="sd"> wm_ji = sum{ array(x, y) * x^j * y^i }</span>
- <span class="sd"> where the sum is over the `x`, `y` coordinates of the region.</span>
- <span class="sd"> **weighted_moments_central** : (3, 3) ndarray</span>
- <span class="sd"> Central moments (translation invariant) of intensity image up to</span>
- <span class="sd"> 3rd order::</span>
- <span class="sd"> wmu_ji = sum{ array(x, y) * (x - x_c)^j * (y - y_c)^i }</span>
- <span class="sd"> where the sum is over the `x`, `y` coordinates of the region,</span>
- <span class="sd"> and `x_c` and `y_c` are the coordinates of the region's weighted</span>
- <span class="sd"> centroid.</span>
- <span class="sd"> **weighted_moments_hu** : tuple</span>
- <span class="sd"> Hu moments (translation, scale and rotation invariant) of intensity</span>
- <span class="sd"> image.</span>
- <span class="sd"> **weighted_moments_normalized** : (3, 3) ndarray</span>
- <span class="sd"> Normalized moments (translation and scale invariant) of intensity</span>
- <span class="sd"> image up to 3rd order::</span>
- <span class="sd"> wnu_ji = wmu_ji / wm_00^[(i+j)/2 + 1]</span>
- <span class="sd"> where ``wm_00`` is the zeroth spatial moment (intensity-weighted area).</span>
- <span class="sd"> .. [1] http://scikit-image.org/docs/dev/api/skimage.measure.html#skimage.measure.regionprops</span>
- <span class="sd"> """</span>
- <span class="k">try</span><span class="p">:</span>
- <span class="n">vals</span> <span class="o">=</span> <span class="p">[]</span>
- <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>
- <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>
- <span class="k">return</span> <span class="n">vals</span>
- <span class="k">except</span> <span class="ne">Exception</span> <span class="k">as</span> <span class="n">e</span><span class="p">:</span>
- <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>
- <span class="k">return</span> <span class="kc">None</span></div>
- <div class="viewcode-block" id="Regions.features"><a class="viewcode-back" href="../code.html#Regions.Regions.features">[docs]</a> <span class="k">def</span> <span class="nf">features</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">feature_list</span><span class="p">):</span>
- <span class="sd">"""</span>
- <span class="sd"> get_features(...) returns a table with all values for the property names given in input, and supplies an</span>
- <span class="sd"> additional parameter for feature classification</span>
- <span class="sd"> :param features: list of property/measure names (e.g, 'area', 'centroid', etc )</span>
- <span class="sd"> :type features: List</span>
- <span class="sd"> :param class_value: classification label</span>
- <span class="sd"> :type class_value: int, string (default=None)</span>
- <span class="sd"> : param image_mask: expernal Image mask to be used for the segmentation</span>
- <span class="sd"> :type image_mask: Image</span>
- <span class="sd"> :returns: table cointaining all property values (columns) for all elements in the regions object (rows)</span>
- <span class="sd"> :rtype: Pandas.DataFrame</span>
- <span class="sd"> :example:</span>
- <span class="sd"> >>> import image_features_extraction as fe</span>
- <span class="sd"> >>> imgs = fe.Images(folder_name)</span>
- <span class="sd"> >>> img = imgs.item(1)</span>
- <span class="sd"> >>> regs = img.Regions()</span>
- <span class="sd"> >>> feature = regs.get_features(['label', 'area','perimeter', 'centroid'], class_value=1)</span>
- <span class="sd"> >>></span>
- <span class="sd"> >>> # external image mask</span>
- <span class="sd"> >>> img_masks = fe.Images(folder_name)</span>
- <span class="sd"> >>> features = regs.get_features(['label', 'area','perimeter', 'centroid'], class_value=1, image_mask=img_masks.item(1))</span>
- <span class="sd"> """</span>
- <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>
- <span class="k">try</span><span class="p">:</span>
- <span class="k">for</span> <span class="n">feature_name</span> <span class="ow">in</span> <span class="n">feature_list</span><span class="p">:</span>
- <span class="n">values</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">feature_name</span><span class="p">)</span>
- <span class="n">Utils</span><span class="o">.</span><span class="n">insert_values</span><span class="p">(</span><span class="n">feature_name</span><span class="p">,</span> <span class="n">df</span><span class="p">,</span> <span class="n">values</span><span class="p">)</span>
- <span class="k">return</span> <span class="n">Features</span><span class="o">.</span><span class="n">Features</span><span class="p">(</span><span class="n">df</span><span class="p">)</span>
- <span class="k">except</span> <span class="ne">Exception</span> <span class="k">as</span> <span class="n">e</span><span class="p">:</span>
- <span class="nb">print</span><span class="p">(</span><span class="s2">"one or more input labels might be wrong:</span><span class="si">{}</span><span class="s2">"</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">e</span><span class="p">))</span>
- <span class="k">return</span> <span class="kc">None</span></div></div>
- </pre></div>
- </div>
- </div>
- </div>
- <div class="sphinxsidebar" role="navigation" aria-label="main navigation">
- <div class="sphinxsidebarwrapper">
- <h1 class="logo"><a href="../index.html">Image Features Extraction</a></h1>
- <h3>Navigation</h3>
- <ul>
- <li class="toctree-l1"><a class="reference internal" href="../tutorial.html">Tutorial</a></li>
- <li class="toctree-l1"><a class="reference internal" href="../code.html">Classes/Modules</a></li>
- </ul>
- <div class="relations">
- <h3>Related Topics</h3>
- <ul>
- <li><a href="../index.html">Documentation overview</a><ul>
- <li><a href="index.html">Module code</a><ul>
- </ul></li>
- </ul></li>
- </ul>
- </div>
- <div id="searchbox" style="display: none" role="search">
- <h3>Quick search</h3>
- <form class="search" action="../search.html" method="get">
- <div><input type="text" name="q" /></div>
- <div><input type="submit" value="Go" /></div>
- <input type="hidden" name="check_keywords" value="yes" />
- <input type="hidden" name="area" value="default" />
- </form>
- </div>
- <script type="text/javascript">$('#searchbox').show(0);</script>
- </div>
- </div>
- <div class="clearer"></div>
- </div>
- <div class="footer">
- ©2017, Remigio Picone.
-
- |
- Powered by <a href="http://sphinx-doc.org/">Sphinx 1.6.3</a>
- & <a href="https://github.com/bitprophet/alabaster">Alabaster 0.7.10</a>
-
- </div>
-
-
- </body>
- </html>
|