Skip to content

Commit

Permalink
Updated tutorial
Browse files Browse the repository at this point in the history
  • Loading branch information
andres-patrignani committed Apr 10, 2024
1 parent fc53dce commit b2f78fd
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 31 deletions.
40 changes: 25 additions & 15 deletions docs/exercises/count_seeds.html
Original file line number Diff line number Diff line change
Expand Up @@ -840,7 +840,12 @@ <h1 class="title"><span class="chapter-number">76</span>&nbsp; <span class="chap

</header>

<p>In this exercise we will learn how to use image analysis to identify and count seeds from an image collected with a mobile device.</p>
<p>In this exercise we will learn how to use image analysis to identify and count seeds from an image collected with a mobile device. In this process we will learn about the concept of image thresholdding, histograms, erosion, dilation, area opening, and binary closing.</p>
<p><strong>Otsu’s thresholding</strong> is an image segmentation method used to automatically determine the optimal threshold value that maximizes the between-class variance and minimizes the intra-class variance of the pixel intensities in a grayscale image.</p>
<p><strong>Erosion</strong> is used to smooth the boundaries of foreground objects (i.e., objects in white on a black background) in an image. It works by moving a structuring element (a small “kernel” or “mask”) over the image and only keeping the pixel in the output image if all the pixels under the structuring element are white. Erosion can be used to remove small white noise from a binary image.</p>
<p><strong>Dilation</strong> is used to expand the boundaries of foreground objects in an image. It works by moving the structuring element over the image and setting the output pixel to white if at least one pixel under the structuring element is white. Dilation can be used to fill small gaps in a binary image or to merge adjacent objects.</p>
<p><strong>Area opening</strong> is used to remove small objects or noise from an image while preserving the larger structures. It involves eroding the image and then dilating it with the same structuring element. This operation can be used, for example, to remove small white specks from a black background.</p>
<p><strong>Binary closing</strong> is used to fill small holes or gaps in objects in an image. It involves dilating the image and then eroding it with the same structuring element. This operation can be used to close small gaps between letters in a text image, for instance.</p>
<div class="cell" data-execution_count="1">
<div class="sourceCode cell-code" id="cb1"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb1-1"><a href="#cb1-1" aria-hidden="true" tabindex="-1"></a><span class="im">import</span> numpy <span class="im">as</span> np</span>
<span id="cb1-2"><a href="#cb1-2" aria-hidden="true" tabindex="-1"></a><span class="im">import</span> matplotlib.pyplot <span class="im">as</span> plt</span>
Expand Down Expand Up @@ -921,15 +926,20 @@ <h1 class="title"><span class="chapter-number">76</span>&nbsp; <span class="chap
</div>
</div>
<div class="cell" data-execution_count="9">
<div class="sourceCode cell-code" id="cb9"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb9-1"><a href="#cb9-1" aria-hidden="true" tabindex="-1"></a><span class="co"># Remove small areas (remove noise)</span></span>
<span id="cb9-2"><a href="#cb9-2" aria-hidden="true" tabindex="-1"></a>image_binary <span class="op">=</span> area_opening(image_binary, area_threshold<span class="op">=</span><span class="dv">1000</span>, connectivity<span class="op">=</span><span class="dv">2</span>)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="sourceCode cell-code" id="cb9"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb9-1"><a href="#cb9-1" aria-hidden="true" tabindex="-1"></a><span class="co"># Area opening to remove small areas (smaller than the typical seed size)</span></span>
<span id="cb9-2"><a href="#cb9-2" aria-hidden="true" tabindex="-1"></a>pixel_area <span class="op">=</span> <span class="dv">1000</span> <span class="co"># area to remove in total pixels</span></span>
<span id="cb9-3"><a href="#cb9-3" aria-hidden="true" tabindex="-1"></a>image_binary <span class="op">=</span> area_opening(image_binary, </span>
<span id="cb9-4"><a href="#cb9-4" aria-hidden="true" tabindex="-1"></a> area_threshold<span class="op">=</span>pixel_area,</span>
<span id="cb9-5"><a href="#cb9-5" aria-hidden="true" tabindex="-1"></a> connectivity<span class="op">=</span><span class="dv">2</span>)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</div>
<div class="cell" data-execution_count="10">
<div class="sourceCode cell-code" id="cb10"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb10-1"><a href="#cb10-1" aria-hidden="true" tabindex="-1"></a><span class="co"># Closing (performs a dilation followed by an erosion. Connect small bright patches)</span></span>
<span id="cb10-2"><a href="#cb10-2" aria-hidden="true" tabindex="-1"></a>image_binary <span class="op">=</span> binary_closing(image_binary, disk(<span class="dv">5</span>))</span>
<span id="cb10-3"><a href="#cb10-3" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb10-4"><a href="#cb10-4" aria-hidden="true" tabindex="-1"></a><span class="co"># Let's inspect the structuring element</span></span>
<span id="cb10-5"><a href="#cb10-5" aria-hidden="true" tabindex="-1"></a><span class="bu">print</span>(disk(<span class="dv">5</span>))</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell" data-execution_count="18">
<div class="sourceCode cell-code" id="cb10"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb10-1"><a href="#cb10-1" aria-hidden="true" tabindex="-1"></a><span class="co"># Closing to fill in holes or connect small, nearby patches</span></span>
<span id="cb10-2"><a href="#cb10-2" aria-hidden="true" tabindex="-1"></a>kernel_size <span class="op">=</span> <span class="dv">5</span></span>
<span id="cb10-3"><a href="#cb10-3" aria-hidden="true" tabindex="-1"></a>image_binary <span class="op">=</span> binary_closing(image_binary, </span>
<span id="cb10-4"><a href="#cb10-4" aria-hidden="true" tabindex="-1"></a> disk(kernel_size))</span>
<span id="cb10-5"><a href="#cb10-5" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb10-6"><a href="#cb10-6" aria-hidden="true" tabindex="-1"></a><span class="co"># Let's inspect the structuring element</span></span>
<span id="cb10-7"><a href="#cb10-7" aria-hidden="true" tabindex="-1"></a><span class="bu">print</span>(disk(kernel_size))</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>[[0 0 0 0 0 1 0 0 0 0 0]
[0 0 1 1 1 1 1 1 1 0 0]
Expand All @@ -944,7 +954,7 @@ <h1 class="title"><span class="chapter-number">76</span>&nbsp; <span class="chap
[0 0 0 0 0 1 0 0 0 0 0]]</code></pre>
</div>
</div>
<div class="cell" data-execution_count="11">
<div class="cell" data-execution_count="19">
<div class="sourceCode cell-code" id="cb12"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb12-1"><a href="#cb12-1" aria-hidden="true" tabindex="-1"></a><span class="co"># Display inverted and denoised binary image</span></span>
<span id="cb12-2"><a href="#cb12-2" aria-hidden="true" tabindex="-1"></a>plt.figure(figsize<span class="op">=</span>(<span class="dv">4</span>,<span class="dv">4</span>))</span>
<span id="cb12-3"><a href="#cb12-3" aria-hidden="true" tabindex="-1"></a></span>
Expand All @@ -958,17 +968,17 @@ <h1 class="title"><span class="chapter-number">76</span>&nbsp; <span class="chap
<p><img src="count_seeds_files/figure-html/cell-11-output-1.png" class="img-fluid"></p>
</div>
</div>
<div class="cell" data-execution_count="12">
<div class="cell" data-execution_count="22">
<div class="sourceCode cell-code" id="cb13"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb13-1"><a href="#cb13-1" aria-hidden="true" tabindex="-1"></a><span class="co"># Identify seed boundaries</span></span>
<span id="cb13-2"><a href="#cb13-2" aria-hidden="true" tabindex="-1"></a>contours <span class="op">=</span> find_contours(image_binary, <span class="dv">0</span>)</span>
<span id="cb13-2"><a href="#cb13-2" aria-hidden="true" tabindex="-1"></a>contours <span class="op">=</span> find_contours(image_binary)</span>
<span id="cb13-3"><a href="#cb13-3" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb13-4"><a href="#cb13-4" aria-hidden="true" tabindex="-1"></a><span class="co"># Print number of seeds in image</span></span>
<span id="cb13-5"><a href="#cb13-5" aria-hidden="true" tabindex="-1"></a><span class="bu">print</span>(<span class="st">'Image contains'</span>,<span class="bu">len</span>(contours),<span class="st">'seeds'</span>)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>Image contains 41 seeds</code></pre>
</div>
</div>
<div class="cell" data-execution_count="13">
<div class="cell" data-execution_count="23">
<div class="sourceCode cell-code" id="cb15"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb15-1"><a href="#cb15-1" aria-hidden="true" tabindex="-1"></a><span class="co"># Plot seed contours</span></span>
<span id="cb15-2"><a href="#cb15-2" aria-hidden="true" tabindex="-1"></a>plt.figure(figsize<span class="op">=</span>(<span class="dv">4</span>,<span class="dv">4</span>))</span>
<span id="cb15-3"><a href="#cb15-3" aria-hidden="true" tabindex="-1"></a>plt.imshow(image_binary, cmap<span class="op">=</span><span class="st">'gray'</span>)</span>
Expand All @@ -987,7 +997,7 @@ <h1 class="title"><span class="chapter-number">76</span>&nbsp; <span class="chap
<span id="cb16-2"><a href="#cb16-2" aria-hidden="true" tabindex="-1"></a>label_image <span class="op">=</span> label(image_binary)</span>
<span id="cb16-3"><a href="#cb16-3" aria-hidden="true" tabindex="-1"></a>image_label_overlay <span class="op">=</span> label2rgb(label_image, image<span class="op">=</span>image_binary)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</div>
<div class="cell" data-execution_count="15">
<div class="cell" data-execution_count="24">
<div class="sourceCode cell-code" id="cb17"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb17-1"><a href="#cb17-1" aria-hidden="true" tabindex="-1"></a><span class="co"># Display image regions on top of original image</span></span>
<span id="cb17-2"><a href="#cb17-2" aria-hidden="true" tabindex="-1"></a>plt.figure(figsize<span class="op">=</span>(<span class="dv">4</span>, <span class="dv">4</span>))</span>
<span id="cb17-3"><a href="#cb17-3" aria-hidden="true" tabindex="-1"></a>plt.imshow(image_label_overlay)</span>
Expand All @@ -998,7 +1008,7 @@ <h1 class="title"><span class="chapter-number">76</span>&nbsp; <span class="chap
<p><img src="count_seeds_files/figure-html/cell-15-output-1.png" class="img-fluid"></p>
</div>
</div>
<div class="cell" data-execution_count="16">
<div class="cell" data-execution_count="25">
<div class="sourceCode cell-code" id="cb18"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb18-1"><a href="#cb18-1" aria-hidden="true" tabindex="-1"></a><span class="co"># Display contour for a single seed</span></span>
<span id="cb18-2"><a href="#cb18-2" aria-hidden="true" tabindex="-1"></a>plt.figure(figsize<span class="op">=</span>(<span class="dv">12</span>, <span class="dv">8</span>))</span>
<span id="cb18-3"><a href="#cb18-3" aria-hidden="true" tabindex="-1"></a></span>
Expand Down
Binary file modified docs/exercises/count_seeds_files/figure-html/cell-13-output-1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified docs/exercises/count_seeds_files/figure-html/cell-16-output-1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading

0 comments on commit b2f78fd

Please sign in to comment.