reconstruction.html 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  1. <!DOCTYPE html>
  2. <!--[if IE 8]><html class="no-js lt-ie9" lang="en" > <![endif]-->
  3. <!--[if gt IE 8]><!--> <html class="no-js" lang="en" > <!--<![endif]-->
  4. <head>
  5. <meta charset="utf-8">
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  7. <title>ipfml.processing.reconstruction &mdash; ipfml v0.4.2 documentation</title>
  8. <link rel="stylesheet" href="../../../_static/css/theme.css" type="text/css" />
  9. <link rel="stylesheet" href="../../../_static/pygments.css" type="text/css" />
  10. <link rel="index" title="Index" href="../../../genindex.html" />
  11. <link rel="search" title="Search" href="../../../search.html" />
  12. <script src="../../../_static/js/modernizr.min.js"></script>
  13. </head>
  14. <body class="wy-body-for-nav">
  15. <div class="wy-grid-for-nav">
  16. <nav data-toggle="wy-nav-shift" class="wy-nav-side">
  17. <div class="wy-side-scroll">
  18. <div class="wy-side-nav-search">
  19. <a href="../../../index.html" class="icon icon-home"> ipfml
  20. </a>
  21. <div class="version">
  22. 0.4.2
  23. </div>
  24. <div role="search">
  25. <form id="rtd-search-form" class="wy-form" action="../../../search.html" method="get">
  26. <input type="text" name="q" placeholder="Search docs" />
  27. <input type="hidden" name="check_keywords" value="yes" />
  28. <input type="hidden" name="area" value="default" />
  29. </form>
  30. </div>
  31. </div>
  32. <div class="wy-menu wy-menu-vertical" data-spy="affix" role="navigation" aria-label="main navigation">
  33. <p class="caption"><span class="caption-text">Contents:</span></p>
  34. <ul>
  35. <li class="toctree-l1"><a class="reference internal" href="../../../description.html">Description</a></li>
  36. <li class="toctree-l1"><a class="reference internal" href="../../../ipfml.html">Documentation</a></li>
  37. <li class="toctree-l1"><a class="reference internal" href="../../../examples.html">Examples</a></li>
  38. <li class="toctree-l1"><a class="reference internal" href="../../../contributing.html">Contributing</a></li>
  39. </ul>
  40. </div>
  41. </div>
  42. </nav>
  43. <section data-toggle="wy-nav-shift" class="wy-nav-content-wrap">
  44. <nav class="wy-nav-top" aria-label="top navigation">
  45. <i data-toggle="wy-nav-top" class="fa fa-bars"></i>
  46. <a href="../../../index.html">ipfml</a>
  47. </nav>
  48. <div class="wy-nav-content">
  49. <div class="rst-content">
  50. <div role="navigation" aria-label="breadcrumbs navigation">
  51. <ul class="wy-breadcrumbs">
  52. <li><a href="../../../index.html">Docs</a> &raquo;</li>
  53. <li><a href="../../index.html">Module code</a> &raquo;</li>
  54. <li>ipfml.processing.reconstruction</li>
  55. <li class="wy-breadcrumbs-aside">
  56. </li>
  57. </ul>
  58. <hr/>
  59. </div>
  60. <div role="main" class="document" itemscope="itemscope" itemtype="http://schema.org/Article">
  61. <div itemprop="articleBody">
  62. <h1>Source code for ipfml.processing.reconstruction</h1><div class="highlight"><pre>
  63. <span></span><span class="sd">&quot;&quot;&quot;</span>
  64. <span class="sd">Functions for reconstruction process of image using reduction/compression methods</span>
  65. <span class="sd">&quot;&quot;&quot;</span>
  66. <span class="c1"># main imports</span>
  67. <span class="kn">import</span> <span class="nn">numpy</span> <span class="k">as</span> <span class="nn">np</span>
  68. <span class="c1"># image processing imports</span>
  69. <span class="kn">from</span> <span class="nn">numpy.linalg</span> <span class="k">import</span> <span class="n">svd</span> <span class="k">as</span> <span class="n">np_svd</span>
  70. <span class="kn">from</span> <span class="nn">sklearn.decomposition</span> <span class="k">import</span> <span class="n">FastICA</span><span class="p">,</span> <span class="n">IncrementalPCA</span>
  71. <span class="c1"># ipfml imports</span>
  72. <span class="kn">from</span> <span class="nn">ipfml.processing</span> <span class="k">import</span> <span class="n">transform</span>
  73. <div class="viewcode-block" id="svd"><a class="viewcode-back" href="../../../ipfml/ipfml.processing.reconstruction.html#ipfml.processing.reconstruction.svd">[docs]</a><span class="k">def</span> <span class="nf">svd</span><span class="p">(</span><span class="n">image</span><span class="p">,</span> <span class="n">interval</span><span class="p">):</span>
  74. <span class="sd">&quot;&quot;&quot;Reconstruct an image from SVD compression using specific interval of Singular Values</span>
  75. <span class="sd"> Args:</span>
  76. <span class="sd"> image: PIL Image, Numpy array or path of 3D image</span>
  77. <span class="sd"> interval: Interval used for reconstruction</span>
  78. <span class="sd"> Returns:</span>
  79. <span class="sd"> Reconstructed image</span>
  80. <span class="sd"> Example:</span>
  81. <span class="sd"> &gt;&gt;&gt; from PIL import Image</span>
  82. <span class="sd"> &gt;&gt;&gt; import numpy as np</span>
  83. <span class="sd"> &gt;&gt;&gt; from ipfml.processing import reconstruction</span>
  84. <span class="sd"> &gt;&gt;&gt; image_values = Image.open(&#39;./images/test_img.png&#39;)</span>
  85. <span class="sd"> &gt;&gt;&gt; reconstructed_image = reconstruction.svd(image_values, (100, 200))</span>
  86. <span class="sd"> &gt;&gt;&gt; reconstructed_image.shape</span>
  87. <span class="sd"> (200, 200)</span>
  88. <span class="sd"> &quot;&quot;&quot;</span>
  89. <span class="n">begin</span><span class="p">,</span> <span class="n">end</span> <span class="o">=</span> <span class="n">interval</span>
  90. <span class="n">lab_img</span> <span class="o">=</span> <span class="n">transform</span><span class="o">.</span><span class="n">get_LAB_L</span><span class="p">(</span><span class="n">image</span><span class="p">)</span>
  91. <span class="n">lab_img</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">(</span><span class="n">lab_img</span><span class="p">,</span> <span class="s1">&#39;uint8&#39;</span><span class="p">)</span>
  92. <span class="n">U</span><span class="p">,</span> <span class="n">s</span><span class="p">,</span> <span class="n">V</span> <span class="o">=</span> <span class="n">np_svd</span><span class="p">(</span><span class="n">lab_img</span><span class="p">,</span> <span class="n">full_matrices</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>
  93. <span class="c1"># reconstruction using specific interval</span>
  94. <span class="n">smat</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">zeros</span><span class="p">((</span><span class="n">end</span> <span class="o">-</span> <span class="n">begin</span><span class="p">,</span> <span class="n">end</span> <span class="o">-</span> <span class="n">begin</span><span class="p">),</span> <span class="n">dtype</span><span class="o">=</span><span class="nb">complex</span><span class="p">)</span>
  95. <span class="n">smat</span><span class="p">[:,</span> <span class="p">:]</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">diag</span><span class="p">(</span><span class="n">s</span><span class="p">[</span><span class="n">begin</span><span class="p">:</span><span class="n">end</span><span class="p">])</span>
  96. <span class="n">output_img</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">dot</span><span class="p">(</span><span class="n">U</span><span class="p">[:,</span> <span class="n">begin</span><span class="p">:</span><span class="n">end</span><span class="p">],</span> <span class="n">np</span><span class="o">.</span><span class="n">dot</span><span class="p">(</span><span class="n">smat</span><span class="p">,</span> <span class="n">V</span><span class="p">[</span><span class="n">begin</span><span class="p">:</span><span class="n">end</span><span class="p">,</span> <span class="p">:]))</span>
  97. <span class="k">return</span> <span class="n">output_img</span></div>
  98. <div class="viewcode-block" id="fast_ica"><a class="viewcode-back" href="../../../ipfml/ipfml.processing.reconstruction.html#ipfml.processing.reconstruction.fast_ica">[docs]</a><span class="k">def</span> <span class="nf">fast_ica</span><span class="p">(</span><span class="n">image</span><span class="p">,</span> <span class="n">components</span><span class="p">):</span>
  99. <span class="sd">&quot;&quot;&quot;Reconstruct an image from Fast ICA compression using specific number of components to use</span>
  100. <span class="sd"> Args:</span>
  101. <span class="sd"> image: PIL Image, Numpy array or path of 3D image</span>
  102. <span class="sd"> components: Number of components used for reconstruction</span>
  103. <span class="sd"> Returns:</span>
  104. <span class="sd"> Reconstructed image</span>
  105. <span class="sd"> Example:</span>
  106. <span class="sd"> &gt;&gt;&gt; from PIL import Image</span>
  107. <span class="sd"> &gt;&gt;&gt; import numpy as np</span>
  108. <span class="sd"> &gt;&gt;&gt; from ipfml.processing import reconstruction</span>
  109. <span class="sd"> &gt;&gt;&gt; image_values = Image.open(&#39;./images/test_img.png&#39;)</span>
  110. <span class="sd"> &gt;&gt;&gt; reconstructed_image = reconstruction.fast_ica(image_values, 25)</span>
  111. <span class="sd"> &gt;&gt;&gt; reconstructed_image.shape</span>
  112. <span class="sd"> (200, 200)</span>
  113. <span class="sd"> &quot;&quot;&quot;</span>
  114. <span class="n">lab_img</span> <span class="o">=</span> <span class="n">transform</span><span class="o">.</span><span class="n">get_LAB_L</span><span class="p">(</span><span class="n">image</span><span class="p">)</span>
  115. <span class="n">lab_img</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">(</span><span class="n">lab_img</span><span class="p">,</span> <span class="s1">&#39;uint8&#39;</span><span class="p">)</span>
  116. <span class="n">ica</span> <span class="o">=</span> <span class="n">FastICA</span><span class="p">(</span><span class="n">n_components</span><span class="o">=</span><span class="mi">50</span><span class="p">)</span>
  117. <span class="c1"># run ICA on image</span>
  118. <span class="n">ica</span><span class="o">.</span><span class="n">fit</span><span class="p">(</span><span class="n">lab_img</span><span class="p">)</span>
  119. <span class="c1"># reconstruct image with independent components</span>
  120. <span class="n">image_ica</span> <span class="o">=</span> <span class="n">ica</span><span class="o">.</span><span class="n">fit_transform</span><span class="p">(</span><span class="n">lab_img</span><span class="p">)</span>
  121. <span class="n">restored_image</span> <span class="o">=</span> <span class="n">ica</span><span class="o">.</span><span class="n">inverse_transform</span><span class="p">(</span><span class="n">image_ica</span><span class="p">)</span>
  122. <span class="k">return</span> <span class="n">restored_image</span></div>
  123. <div class="viewcode-block" id="ipca"><a class="viewcode-back" href="../../../ipfml/ipfml.processing.reconstruction.html#ipfml.processing.reconstruction.ipca">[docs]</a><span class="k">def</span> <span class="nf">ipca</span><span class="p">(</span><span class="n">image</span><span class="p">,</span> <span class="n">components</span><span class="p">,</span> <span class="n">_batch_size</span><span class="o">=</span><span class="mi">25</span><span class="p">):</span>
  124. <span class="sd">&quot;&quot;&quot;Reconstruct an image from IPCA compression using specific number of components to use and batch size</span>
  125. <span class="sd"> Args:</span>
  126. <span class="sd"> image: PIL Image, Numpy array or path of 3D image</span>
  127. <span class="sd"> components: Number of components used for reconstruction</span>
  128. <span class="sd"> batch_size: Batch size used for learn (default 25)</span>
  129. <span class="sd"> Returns:</span>
  130. <span class="sd"> Reconstructed image</span>
  131. <span class="sd"> Example:</span>
  132. <span class="sd"> &gt;&gt;&gt; from PIL import Image</span>
  133. <span class="sd"> &gt;&gt;&gt; import numpy as np</span>
  134. <span class="sd"> &gt;&gt;&gt; from ipfml.processing import reconstruction</span>
  135. <span class="sd"> &gt;&gt;&gt; image_values = Image.open(&#39;./images/test_img.png&#39;)</span>
  136. <span class="sd"> &gt;&gt;&gt; reconstructed_image = reconstruction.ipca(image_values, 20)</span>
  137. <span class="sd"> &gt;&gt;&gt; reconstructed_image.shape</span>
  138. <span class="sd"> (200, 200)</span>
  139. <span class="sd"> &quot;&quot;&quot;</span>
  140. <span class="n">lab_img</span> <span class="o">=</span> <span class="n">transform</span><span class="o">.</span><span class="n">get_LAB_L</span><span class="p">(</span><span class="n">image</span><span class="p">)</span>
  141. <span class="n">lab_img</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">(</span><span class="n">lab_img</span><span class="p">,</span> <span class="s1">&#39;uint8&#39;</span><span class="p">)</span>
  142. <span class="n">transformer</span> <span class="o">=</span> <span class="n">IncrementalPCA</span><span class="p">(</span>
  143. <span class="n">n_components</span><span class="o">=</span><span class="n">components</span><span class="p">,</span> <span class="n">batch_size</span><span class="o">=</span><span class="n">_batch_size</span><span class="p">)</span>
  144. <span class="n">transformed_image</span> <span class="o">=</span> <span class="n">transformer</span><span class="o">.</span><span class="n">fit_transform</span><span class="p">(</span><span class="n">lab_img</span><span class="p">)</span>
  145. <span class="n">restored_image</span> <span class="o">=</span> <span class="n">transformer</span><span class="o">.</span><span class="n">inverse_transform</span><span class="p">(</span><span class="n">transformed_image</span><span class="p">)</span>
  146. <span class="k">return</span> <span class="n">restored_image</span></div>
  147. </pre></div>
  148. </div>
  149. </div>
  150. <footer>
  151. <hr/>
  152. <div role="contentinfo">
  153. <p>
  154. &copy; Copyright 2019, Jérôme BUISINE
  155. </p>
  156. </div>
  157. Built with <a href="http://sphinx-doc.org/">Sphinx</a> using a <a href="https://github.com/rtfd/sphinx_rtd_theme">theme</a> provided by <a href="https://readthedocs.org">Read the Docs</a>.
  158. </footer>
  159. </div>
  160. </div>
  161. </section>
  162. </div>
  163. <script type="text/javascript" id="documentation_options" data-url_root="../../../" src="../../../_static/documentation_options.js"></script>
  164. <script type="text/javascript" src="../../../_static/jquery.js"></script>
  165. <script type="text/javascript" src="../../../_static/underscore.js"></script>
  166. <script type="text/javascript" src="../../../_static/doctools.js"></script>
  167. <script type="text/javascript" src="../../../_static/language_data.js"></script>
  168. <script type="text/javascript" src="../../../_static/js/theme.js"></script>
  169. <script type="text/javascript">
  170. jQuery(function () {
  171. SphinxRtdTheme.Navigation.enable(true);
  172. });
  173. </script>
  174. </body>
  175. </html>