Skip to content

Commit

Permalink
deploy: a8cc80f
Browse files Browse the repository at this point in the history
  • Loading branch information
nekiwo committed Oct 9, 2024
1 parent d468e4f commit dc3fe0e
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 4 deletions.
36 changes: 35 additions & 1 deletion dev/PreseasonTraining/OpenCV_ColorSegmentation/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -389,7 +389,7 @@ <h3>Converting frames<a class="headerlink" href="#converting-frames" title="Perm
</section>
<section id="masking">
<h3>Masking<a class="headerlink" href="#masking" title="Permalink to this heading"></a></h3>
<p>In graphic design and computer vision, masking is the action of hiding unnecessary part of an image. In a sense, it is image segmentation in our program. You can create a color based mask using this function.</p>
<p>In graphic design and computer vision, masking is the action of hiding unnecessary part of an image. In a sense, it is image segmentation in our program. You can create a color based mask using this function. Note that all three values are the size of a byte, meaning the values should be unsigned integers from 0 to 255 inclusive.</p>
<div class="highlight-py notranslate"><div class="highlight"><pre><span></span><span class="n">mask</span> <span class="o">=</span> <span class="n">cv</span><span class="o">.</span><span class="n">inRange</span><span class="p">(</span><span class="n">your_frame</span><span class="p">,</span> <span class="n">lower_threshold</span><span class="p">,</span> <span class="n">upper_threshold</span><span class="p">)</span>
</pre></div>
</div>
Expand All @@ -404,6 +404,30 @@ <h3>Contours<a class="headerlink" href="#contours" title="Permalink to this head
</div>
</section>
</section>
<section id="calibration">
<h2>Calibration<a class="headerlink" href="#calibration" title="Permalink to this heading"></a></h2>
<section id="intro">
<h3>Intro<a class="headerlink" href="#intro" title="Permalink to this heading"></a></h3>
<p>Since each camera has different physical properties and lenses, they need to be calibrated individually. More often than not, constants such as field of view of a specific webcam are not available online. For this reason, we use a calibration script that scans images of an AruCo chessboard with known dimentions to calculate these constants. For this lesson, you will only need to calibrate your camera for the distance challenge.</p>
</section>
<section id="running">
<h3>Running<a class="headerlink" href="#running" title="Permalink to this heading"></a></h3>
<p>Download the <code class="docutils literal notranslate"><span class="pre">opencv-python-contrib</span></code> package using pip and run <a class="reference external" href="https://github.com/titan2022/Training2024/blob/calibration/calib.py">this file</a> to calibrate your camera. The console output will show your X and Y field of view in degrees. More data such as the camera matrix and distortion coefficients are available in the script.</p>
<p>When running the program, show your AruCo chessboard (which should be taped to a flat surface) at various angles and press SPACE key to take a picture. Take approximately 10 to 20 pictures. Press ESC key to exit and copy the values printed to the console.</p>
</section>
</section>
<section id="challenges">
<h2>Challenges<a class="headerlink" href="#challenges" title="Permalink to this heading"></a></h2>
<p>After completing your initial problem, feel free to try the following challenges to expand your program.</p>
<section id="finding-the-center">
<h3>Finding the center<a class="headerlink" href="#finding-the-center" title="Permalink to this heading"></a></h3>
<p>For use in locating the specific object, its position in the frame needs to be obtained. The best representation of the position would be the center of the contour in pixels on the image frame. For this challenge, use the available contour data to calculate its center in pixels. The <code class="docutils literal notranslate"><span class="pre">contours</span></code> object is an array that contains each contour (often you might have multiple) in a form of another array of 2D vertices. Print it to the console or <a class="reference external" href="https://docs.opencv.org/3.4/d4/d73/tutorial_py_contours_begin.html">read more about it online</a> to figure out the structure of the array and use the data points to calculate the center.</p>
</section>
<section id="calculating-object-distance">
<h3>Calculating object distance<a class="headerlink" href="#calculating-object-distance" title="Permalink to this heading"></a></h3>
<p>Given that the object you want to detect is a near perfect sphere with constant radius, you can use trigonometry to calculate the distance of the sphere from the camera. Use the calibration script to get your field of view of your webcam and use all your known variables to calculate for depth. In a sense, you are trying to translate 2D pixel input (radius of circle in pixels) to 3D distance output. Read this <a class="reference external" href="https://en.wikipedia.org/wiki/3D_projection">Wikipedia article</a> about perspective or search more online resources for help.</p>
</section>
</section>
</section>

</article>
Expand Down Expand Up @@ -481,6 +505,16 @@ <h3>Contours<a class="headerlink" href="#contours" title="Permalink to this head
<li><a class="reference internal" href="#contours">Contours</a></li>
</ul>
</li>
<li><a class="reference internal" href="#calibration">Calibration</a><ul>
<li><a class="reference internal" href="#intro">Intro</a></li>
<li><a class="reference internal" href="#running">Running</a></li>
</ul>
</li>
<li><a class="reference internal" href="#challenges">Challenges</a><ul>
<li><a class="reference internal" href="#finding-the-center">Finding the center</a></li>
<li><a class="reference internal" href="#calculating-object-distance">Calculating object distance</a></li>
</ul>
</li>
</ul>
</li>
</ul>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ new_frame = cv.cvtColor(frame, cv.COLOR_BGR2HSV) # BGR to HSV

### Masking

In graphic design and computer vision, masking is the action of hiding unnecessary part of an image. In a sense, it is image segmentation in our program. You can create a color based mask using this function.
In graphic design and computer vision, masking is the action of hiding unnecessary part of an image. In a sense, it is image segmentation in our program. You can create a color based mask using this function. Note that all three values are the size of a byte, meaning the values should be unsigned integers from 0 to 255 inclusive.

```py
mask = cv.inRange(your_frame, lower_threshold, upper_threshold)
Expand All @@ -122,4 +122,28 @@ To visualize our mask on top of the existing imaage, we can get the countours of
```py
contours, _ = cv.findContours(mask, cv.RETR_EXTERNAL, cv.CHAIN_APPROX_SIMPLE)
cv.drawContours(your_frame, contours, -1, (0, 255, 0), 3)
```
```

## Calibration

### Intro

Since each camera has different physical properties and lenses, they need to be calibrated individually. More often than not, constants such as field of view of a specific webcam are not available online. For this reason, we use a calibration script that scans images of an AruCo chessboard with known dimentions to calculate these constants. For this lesson, you will only need to calibrate your camera for the distance challenge.

### Running

Download the `opencv-python-contrib` package using pip and run [this file](https://github.com/titan2022/Training2024/blob/calibration/calib.py) to calibrate your camera. The console output will show your X and Y field of view in degrees. More data such as the camera matrix and distortion coefficients are available in the script.

When running the program, show your AruCo chessboard (which should be taped to a flat surface) at various angles and press SPACE key to take a picture. Take approximately 10 to 20 pictures. Press ESC key to exit and copy the values printed to the console.

## Challenges

After completing your initial problem, feel free to try the following challenges to expand your program.

### Finding the center

For use in locating the specific object, its position in the frame needs to be obtained. The best representation of the position would be the center of the contour in pixels on the image frame. For this challenge, use the available contour data to calculate its center in pixels. The `contours` object is an array that contains each contour (often you might have multiple) in a form of another array of 2D vertices. Print it to the console or [read more about it online](https://docs.opencv.org/3.4/d4/d73/tutorial_py_contours_begin.html) to figure out the structure of the array and use the data points to calculate the center.

### Calculating object distance

Given that the object you want to detect is a near perfect sphere with constant radius, you can use trigonometry to calculate the distance of the sphere from the camera. Use the calibration script to get your field of view of your webcam and use all your known variables to calculate for depth. In a sense, you are trying to translate 2D pixel input (radius of circle in pixels) to 3D distance output. Read this [Wikipedia article](https://en.wikipedia.org/wiki/3D_projection) about perspective or search more online resources for help.
Loading

0 comments on commit dc3fe0e

Please sign in to comment.