<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Ali Almutawakel on Medium]]></title>
        <description><![CDATA[Stories by Ali Almutawakel on Medium]]></description>
        <link>https://medium.com/@almutawakel.ali?source=rss-a5c00d648667------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*83Fg9oUGjrrpvCYR.</url>
            <title>Stories by Ali Almutawakel on Medium</title>
            <link>https://medium.com/@almutawakel.ali?source=rss-a5c00d648667------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Tue, 01 Dec 2026 21:35:58 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@almutawakel.ali/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[OpenCV: Edge detection]]></title>
            <link>https://medium.com/@almutawakel.ali/opencv-edge-detection-afacc62ec623?source=rss-a5c00d648667------2</link>
            <guid isPermaLink="false">https://medium.com/p/afacc62ec623</guid>
            <category><![CDATA[python]]></category>
            <category><![CDATA[edge-computing]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[computer-vision]]></category>
            <category><![CDATA[opencv]]></category>
            <dc:creator><![CDATA[Ali Almutawakel]]></dc:creator>
            <pubDate>Mon, 10 Apr 2017 04:08:46 GMT</pubDate>
            <atom:updated>2017-04-10T04:22:50.565Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/781/1*Ckj8XetY4zYOyFHsFpdHkQ.gif" /></figure><p>This is the <strong>3rd</strong> introduction for OpenCV. This is a <strong>code-along</strong> tutorial to learn OpenCV in <strong>Python</strong>.<strong> </strong>We will be looking at edge detection concepts and <strong>Canny edge detection function </strong>in OpenCV.</p><p>Edges define the boundaries of an object. A lot of an image’s detail can be taken out and the object can still be identified through its edges. Take the example below of the dog. On the left is the original image, and on the right is the binary form of it. A lot of the detail like colors and background had been taken out but the edges had preserved a lot of details that are useful to identify what’s the object in the image.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/362/1*BAK87CD6NgXYcrLoQfBIFg.png" /></figure><p>Computers love binary. It is fast to process and straight forward (1 or 0). The technique of getting an image’s edges is very useful when it comes to preprocessing an image for object detection. It converts the image to its simplest forms removing unnecessary details that aren’t useful and leaving the interesting aspect of it.</p><p>To our eyes, it is very easy to pickup what are the edges of the dog. However, how can we make the computer do the same?</p><h3>Edge detection concept</h3><p>When processing an image, almost every algorithm relies on the concept of neighbouring pixels. Comparing pixels to each other can get us a lot of insights about the image. Comparisons can be applied on simply one pixel’s values or a group of pixels’ values against another group.</p><p>With edge detection, neighbouring pixels play a great role. Let’s take the following example to demonstrate how it can be done.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/629/1*o_44T3nWnss5SL7s8AHtoQ.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*WXNxAe17w9Zb1kd8ew5qQA.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/690/1*B5rJova44RzpcPcNzNzC8g.png" /></figure><p>Assume we are processing an image to find the edges. Your program is running through all the pixels in that blue line. By comparing the intensity value of one pixel to its previous pixel, we will end up in a graph like the one on the left. That said, we can define edges now as <strong>sudden intensity changes in an image.</strong> That sudden discontinuity of a particular value is an example of a cue to detect an edge.</p><h3>Canny Edge Algorithm</h3><p>The most popular algorithm for edge detection is Canny Edge detection. Developed by John F. Canny in 1986, it became the most widely used algorithm due its accuracy and low error rate. An image undergoes few stages of processing before the final result is produced.</p><ol><li>Gaussian blur is applied to remove noise.</li><li>Finding the intensity (brightness) gradient of the image like the simple example above.</li><li>Apply non-maximum suppression to get rid of pixels that are not edges for processing.</li><li>Applies hysteresis. Suppose we have two thresholds <strong>U</strong> &amp; <strong>L</strong>. Anything above <strong>U</strong> is an edge and anything lower than <strong>L</strong> is not. Anything in between them can possibly be. With hysteresis, we can tell if that a potential edge candidate that is connected to a verified strong edge is considered an edge because edges tend to be continued in real life.</li></ol><h3>In OpenCV</h3><p>Fortunately, OpenCV provides few edge detection functions as part of their API and one of them is Canny(image, L, U). In my example, it takes three parameters:</p><ul><li><strong>Image</strong></li><li><strong>L</strong>: Lower threshold value. Any value below <strong>L </strong>is considered not to be an edge.</li><li><strong>U</strong>: Upper threshold value. Any value above <strong>U</strong> is considered to be an edge.</li></ul><pre><strong>import cv2</strong></pre><pre># Reading the image in grey scale indicated by the 0 parameter<br><strong>image = cv2.imread(&#39;images/input.jpg&#39;,0)</strong></pre><pre># Showing original image<br><strong>cv2.imshow(&#39;Original&#39;, image)</strong><br><strong>cv2.waitKey(0)</strong></pre><pre># Computing edges with Canny with thresholds 50 and 120<br># then showing it<br><strong>canny = cv2.Canny(image, 50, 120)<br>cv2.imshow(&#39;Canny&#39;, canny)</strong></pre><pre><strong>cv2.destroyAllWindows()</strong></pre><p>Results:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*3CnibJ7D9Sv6izPAQS7Rvg.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*1kDBM_OIkEu5lbGP2uNAMw.png" /></figure><blockquote>What’s Next?</blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/320/1*UZPqQ-vdJ47a2C_sC--sWw.gif" /></figure><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=afacc62ec623" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[OpenCV: Filters & Arithmetic Operations]]></title>
            <link>https://medium.com/@almutawakel.ali/opencv-filters-arithmetic-operations-2f4ff236d6aa?source=rss-a5c00d648667------2</link>
            <guid isPermaLink="false">https://medium.com/p/2f4ff236d6aa</guid>
            <category><![CDATA[opencv]]></category>
            <category><![CDATA[computer-vision]]></category>
            <category><![CDATA[programming]]></category>
            <dc:creator><![CDATA[Ali Almutawakel]]></dc:creator>
            <pubDate>Mon, 20 Mar 2017 00:36:57 GMT</pubDate>
            <atom:updated>2017-04-10T03:11:43.913Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*_uinFy_KftJaBgslNLKK3g.gif" /></figure><p>This is the <strong>2nd</strong> introduction for OpenCV. This is a <strong>code-along</strong> tutorial to learn OpenCV in <strong>Python</strong>.<strong> </strong>We will be looking at <strong>arithmetic operations, and filters (blurring</strong>, <strong>and sharpening).</strong></p><h4>Arithmetic Operations</h4><p>Addition and subtraction of color intensity in an image is done by simple OpenCV functions add() &amp; subtract(). This results in either brightening or darkening the image. Both functions take in two parameters:</p><p>1- The input image<br>2- Matrix of numbers to add to or subtract from</p><p>The main condition is to have the matrix has to be the same size as the input image. We can achieve that by using NumPy function ones().</p><p>The following example illustrates how it can be used:</p><pre># It is always a good habit to <br><strong>import cv2<br>import numpy as np</strong></pre><pre># Reading in the input image<br><strong>image = cv2.imread(&#39;images/input.jpg&#39;)</strong></pre><pre># Create a matrix of ones of type int in the same size as the image<br># then multiply it by a scaler of 75 <br># This gives a matrix with same dimesions of our image with all values being 75<br><strong>matrix = np.ones(image.shape, dtype = &quot;uint8&quot;) * 75</strong></pre><pre># We use the matrix to add to our image<br><strong>added = cv2.add(image, ones)<br>cv2.imshow(&quot;Added&quot;, matrix)</strong></pre><pre># Likewise we can also subtract<br><strong>subtracted = cv2.subtract(image, matrix)<br>cv2.imshow(&quot;Subtracted&quot;, subtracted)</strong></pre><pre># Wait &amp; terminate<strong><br>cv2.waitKey(0)<br>cv2.destroyAllWindows()</strong></pre><p>To demonstrate better what the matrix is in this case; it is an array of ones that are in the same size as the input image multiplied by 75.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*7KsXQo9f4a6TjqZvcZ7XVg.png" /><figcaption>Matrix of ones multiplied by 75</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*CejwbEOauhXgdEzo4z7wSQ.png" /><figcaption>Matrix size is the same as the image size</figcaption></figure><p>Results of running the code block above:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*yk9L086E16dCEdJEU0x8Pw.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*D0p28nqrRA5y0uFTqX-QzQ.png" /></figure><h3>Filters — Blurring and Sharpening</h3><p>Filters are essential for many OpenCV functions (hint: edge detections) because you can either reduce noise or increase it. Filters normalize and average out a block of pixels in an image to have low values difference. The result is</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ePtXqKRB5bFAWobdZclGnA.png" /><figcaption>Blurring effect on pixel values</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/500/1*gtwfybLlwRPHmQPVQVeSdw.jpeg" /><figcaption>A typical blurred image result</figcaption></figure><p>How blurring works? When processing an image, each pixel value changes according to its neighbouring pixels. To demonstrate, refer to the diagrams below; it shows an overly simplified algorithm to blurring.<br>Say you are processing pixel[1,1]. You decided to take into account the nearest neighbours only. You set the value of the pixel as the total average of all pixels.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*RqfoyBsjJTyLlm5hLJCS1g.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*HESPHt9hKtOHG6Yhq2eEFQ.png" /><figcaption>Left (1) Right (2)</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*3cOuNSuQnZUiEAlPbVtx7Q.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*0XcuoYFHGAQa1q-aYntcCg.png" /><figcaption>Left (3) Right (4)</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*TB8qXwJvjEkseuXCgMSALw.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*9kkuiSY8ZheHeGKRnMWeHg.png" /><figcaption>Left (5) Right (6)</figcaption></figure><p>The size of the pixels which we run our manipulation function on is called the <strong>kernel.</strong> In the example below, we used 3x3 kernel to process our image. The image below shows how an example of 3x3 kernel array to average out (blur) and image in Python. It is important that the number sums up to 1 so we maintain the same brightness of the image. Otherwise, the image will either brighten or darken.</p><p>Unlike blurring, sharpening does the opposite and tries to pop out the details in the image. Thus, making pixel value different than its neighbours.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/668/1*vbPnV1e0kUonPsSPH4ldbg.png" /><figcaption>Typical kernel for blurring (averaging)</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/456/1*GzPOEiW_C3KZfdt9F3Ns-w.png" /><figcaption>Typical kernel for sharpening</figcaption></figure><p>The function for filtering in OpenCV is filter2D(). It takes in three parameters:</p><p>1- Input image<br>2- Desired depth (more advanced topic)<br>3- Kernel</p><p>And would something like this:</p><pre># Putting a negative value for depth to indicate to use the <br># the same depth of the source image src.depth().</pre><pre><strong>cv2.filter2D(image, -1, kernel)</strong></pre><p>Now to apply a more comprehensive example of filters. Follow &amp; code along.</p><pre><strong>import cv2<br>import numpy as np</strong></pre><pre># Reading our image and displaying it<strong><br>image = cv2.imread(&#39;images/elephant.jpg&#39;)<br>cv2.imshow(&#39;Original Image&#39;, image)<br>cv2.waitKey(0)</strong></pre><pre># Creating our 3 x 3 kernel that would look like this:<br># [[ 0.11111111  0.11111111  0.11111111]<br>#  [ 0.11111111  0.11111111  0.11111111]<br>#  [ 0.11111111  0.11111111  0.11111111]]<br><strong>kernel_3x3 = np.ones((3, 3), np.float32) / 9</strong></pre><pre># We apply the filter and display the image<br><strong>blurred = cv2.filter2D(image, -1, kernel_3x3)<br>cv2.imshow(&#39;3x3 Kernel Blurring&#39;, blurred)<br>cv2.waitKey(0)</strong></pre><pre># Let&#39;s try with 7 x 7 kernel to get a more blurred image<br><strong>kernel_7x7 = np.ones((7, 7), np.float32) / 49</strong></pre><pre><strong>blurred2 = cv2.filter2D(image, -1, kernel_7x7)<br>cv2.imshow(&#39;7x7 Kernel Blurring&#39;, blurred2)<br>cv2.waitKey(0)</strong></pre><pre><strong>cv2.destroyAllWindows()</strong></pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*dahGVfRu5hXgro_a8IyJsA.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*FPIgplDA_Rtjr4kAwqOB0A.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*C43YzP0lOY752slAnPFSgQ.png" /><figcaption>Original (Left) — 3x3 Kernel (middle) — 7x7 Kernel (Right)</figcaption></figure><p>As for sharpening:</p><pre><strong>import cv2<br>import numpy as np</strong></pre><pre># Reading in and displaying our image<br><strong>image = cv2.imread(&#39;images/input.jpg&#39;)<br>cv2.imshow(&#39;Original&#39;, image)</strong></pre><pre># Create our shapening kernel, it must equal to one eventually<br><strong>kernel_sharpening = np.array([[-1,-1,-1], <br>                              [-1, 9,-1],<br>                              [-1,-1,-1]])</strong></pre><pre># applying the sharpening kernel to the input image &amp; displaying it.<br><strong>sharpened = cv2.filter2D(image, -1, kernel_sharpening)</strong></pre><pre><strong>cv2.imshow(&#39;Image Sharpening&#39;, sharpened)</strong></pre><pre><strong>cv2.waitKey(0)<br>cv2.destroyAllWindows()</strong></pre><p>Results:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*49vF0jVr00Mql1suB2nEdw.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*hbpJtoLS3eQ0b-VKOyFZuA.png" /><figcaption>Original (left) — Sharpened (right)</figcaption></figure><h4>What’s Next?</h4><p>On my next blog, we will be discussing edge detection functions by OpenCV. Stay tuned!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=2f4ff236d6aa" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Open CV: First Look! First App!]]></title>
            <link>https://medium.com/@almutawakel.ali/open-cv-first-look-first-app-c1ffb0e8025f?source=rss-a5c00d648667------2</link>
            <guid isPermaLink="false">https://medium.com/p/c1ffb0e8025f</guid>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[computer-vision]]></category>
            <category><![CDATA[opencv]]></category>
            <category><![CDATA[python]]></category>
            <dc:creator><![CDATA[Ali Almutawakel]]></dc:creator>
            <pubDate>Tue, 28 Feb 2017 02:45:15 GMT</pubDate>
            <atom:updated>2017-03-18T17:13:53.824Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/180/1*pa_tYg6eXULOv8K28O73kw.png" /></figure><p>This is the first introduction and <strong>code-along</strong> tutorial to learn OpenCV in <strong>Python</strong>. We will be looking at <strong>installation</strong> and super <strong>basic</strong> <strong>functions</strong> of OpenCV.</p><h3>OpenCV History &amp; Facts</h3><p>Short For Open Source Computer vision, is a programming library for Computer Vision functions. It was an Intel initiative that was officially launched in 1999. OpenCV was initiated to fulfill research needs for computer vision applications. A lot of the research was written in C++ which is what OpenCV is also written in. Later on, wrappers for all different languages like Java, Python, IOS, and Android were introduced with their individual documentation that can be found on <a href="http://docs.opencv.org/">OpenCV website</a>. However, if you are looking for speed, it will be slower if you use a language other than C++.</p><p>First major release v1.0 was in 2006, second v2.0 in 2009, and the third v3.0 was in 2015. The latest stable release for OpenCV 2.x is 2.4.13. OpenCV 3.x is very similar and has new functions, however, important algorithms such as SIFT and SURF were removed due to some patents.</p><p>In this blog, I will be using <strong>Python 2 </strong>and <strong>OpenCV 2.4.13 </strong>just because it is easier to grasp concepts using python and to also use NumPy library. NumPy gives supporting functions for large multidimensional arrays in python. In addition, we will be looking at Anaconda python package which comes with huge bundles of Python libraries that include Jupyter and NumPy.</p><h3>OpenCV Installation and Setup For Windows</h3><h4>STEP 1 — Download and Install Anaconda Python Package</h4><ul><li><strong>Visit</strong> to <a href="https://www.continuum.io/downloads">https://www.continuum.io/downloads</a>.</li><li><strong>Download</strong> Windows Python 2.7 version.</li><li><strong>Install</strong> with defaults.</li><li><strong>Test</strong> if installation went well by typing into a command line jupyter notebook. This should open your default browser to localhost.</li></ul><h4>STEP 2 — Download and Run OpenCV</h4><ul><li><strong>Visit</strong> <a href="http://opencv.org/downloads.html">http://opencv.org/downloads.html</a>.</li><li><strong>Download</strong> OpenCV 2.4.13</li><li><strong>Run </strong>.exe file</li><li><strong>Locate</strong> cv2.py file in the directory where OpenCV was installed. Path to the file is *\opencv\build\python\2.7\x64</li><li><strong>Copy</strong> cv2.py and place it in *\Anaconda2\Lib\site-packages where * is the directory where you installed Anaconda package.</li></ul><h4>STEP 3 — TEST</h4><ul><li><strong>Create</strong> a folder to store your code at.</li><li><strong>Open </strong>command line Window in that folder.</li><li><strong>Create</strong> a new notebook. By going to <em>file → new notebook → python 2</em></li><li><strong>Copy</strong> &amp; <strong>Run </strong>the following code. Shortcut to run is ctl + Enter</li></ul><pre>import cv2<br>import numpy<br>import matplotlib<br>print(“Packages successfully imported”)</pre><p>If everything went well, your screen should look like this:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*8CGfhhrciyGoO8pckUhCBQ.png" /></figure><h3>OpenCV Installation and Setup For <strong>Mac</strong></h3><h4>STEP 1 — Install Xcode</h4><ul><li><strong>Visit </strong><a href="https://developer.apple.com/xcode/">https://developer.apple.com/xcode/</a>.</li><li><strong>Follow</strong> the instructions there.</li></ul><h4>STEP 2 — Install Homebrew</h4><ul><li><strong>Open</strong> a terminal</li><li><strong>Copy </strong>&amp; <strong>Run</strong> the following cd ~ then ruby -e &quot;$(curl -fsSl https://raw.githubusercontent.com/Homebrew/install/master/install)&quot;</li><li><strong>Update</strong> Homebrew by running brew install python in the same terminal window</li></ul><h4>STEP 3 — Install the Anaconda Python Package</h4><ul><li><strong>Visit</strong> <a href="https://www.continuum.io/downloads">https://www.continuum.io/downloads</a></li><li><strong>Select</strong> the OSX installer</li><li><strong>Install </strong>with defaults</li><li><strong>Test</strong> if installation went well by typing into a command line jupyter notebook. This should open your default browser to localhost.</li></ul><h4>STEP 3 — Install OpenCV</h4><ul><li><strong>Open</strong> a terminal</li><li><strong>Copy</strong> &amp; <strong>run</strong> the following lines</li></ul><pre>brew install opencv<br>brew tap homebrew/science</pre><ul><li><strong>Create</strong> aliases of cv2.py &amp; cv2.so files into Anaconda site-packages folder by <strong>copying</strong> the following commands in order</li></ul><p>1.cd /Library/Python/2.7/site-packages/</p><p>2.ln -s /usr/local/Cellar/opencv/<strong>2.4.13.2</strong>/lib/python2.7/site-packages/cv.py cv.py</p><p>3.ln -s /usr/local/Cellar/opencv/<strong>2.4.13.2</strong>/lib/python2.7/site-packages/cv2.so cv2.so</p><p><strong>Note:</strong> Above bolded part of the directory can be different depending on the version of OpenCV 2.x version. Check the path in finder to be on the safe side.</p><h4>STEP 3 — TEST</h4><ul><li><strong>Create</strong> a folder to store your code at.</li><li><strong>Open </strong>command line Window in that folder.</li><li><strong>Create</strong> a new notebook. By going to <em>file → new notebook → python 2</em></li><li><strong>Copy</strong> &amp; <strong>Run </strong>the following code. Shortcut to run is ctl + Enter</li></ul><pre>import cv2<br>import numpy<br>import matplotlib<br>print(“Packages successfully imported”)</pre><p>If everything went well, your screen should look like this:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*8CGfhhrciyGoO8pckUhCBQ.png" /></figure><h3>Reading, Writing, and Displaying Images</h3><pre># First we have to make OpenCV availble<br>import cv2</pre><pre><strong># READING</strong><br># Load an image using &#39;imread&#39; into a variable<br># Make sure that &#39;input.jpg&#39; exists at declared path<br>input = cv2.imread(&#39;input.jpg&#39;)</pre><pre># <strong>DISPLAYING<br></strong># To display out image variable, we use &#39;imshow&#39;<br># 1st parameter: title on image window<br># 2nd parameter: image varible<br>cv2.imshow(&#39;Hello World&#39;, input)</pre><pre># waitKey acts as a blocking statement until you want<br># the application to move on to the next lines of code.<br># It waits until any key is pressed before continuing.<br># By placing numbers (except 0), we can specify a delay<br># in milliseconds instead of a key press.<br>cv2.waitKey()</pre><pre># <strong>DESTROYING WINDOWS</strong><br># destroyAllWindows closes all open windows<br>cv2.destroyAllWindows()</pre><pre># FOR MAC ONLY<br># You have to include waitKey() again after destroyAllWindows()<br># to avoid hanging window.<br>cv2.waitKey()</pre><pre><strong># WRITING<br></strong># Simply use &#39;imwrite&#39; to save image to disk<br># 1st parameter: Directory + file name<br># 2nd parameter: Image variable to be saved<br>cv2.imwrite(&#39;output.jpg&#39;, input)</pre><h3>How Images are Stored in OpenCV</h3><h4>Dimensions</h4><p>By printing input.shape where input is an image variable, you get an array of three elements.<br>At index [0] is the <strong>height.<br></strong>At index [1] is the <strong>width.<br></strong>At index [2] tells you how many components make up this image. In the example below, it 3 which corresponds to RGB color space<strong>.</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/944/1*dTyyoCjNcAVfsx0vkZVN0Q.png" /></figure><p>You could also get attributes by specifying the index.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/948/1*ps6M_6HlJ-zTOYegfV_ARw.png" /></figure><h4>Color Spaces</h4><p>Now we know that images are saved in <strong>RGB</strong> color spaces. Now, first thing to <strong>remember</strong> is in OpenCV, <strong>RGB</strong> are saved as <strong>BGR</strong> (reversed) sequence.<br>Next, you can target a specific pixel by providing the X &amp; Y axis of the pixel. <strong>input[0, 0]</strong> (where input is an image variable) <strong>returns </strong><strong>BGR</strong> values of x: 0 &amp; y:0 pixel.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*UHIL0QKS3_r4gnSC6HLX2w.png" /></figure><p>Or you could look at <strong>individual</strong> color <strong>levels</strong> like the following example.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*0JVxQ8WgKLiHhcgWzOKuXg.png" /></figure><p>Now, what if you want to <strong>convert</strong> an image to <strong>another</strong> <strong>color</strong> <strong>space</strong>. In the code below, we are converting an BGR image to gray scale.</p><pre># cvtColor is function used to convert an image&#39;s color space<br># 1st parameter: image variable<br># 2nd parameter: output image color space<br>gray_img = cv2.cvtColor(input, cv2.COLOR_BGR2GRAY)</pre><p><strong>Gray scaling</strong> changes how the image is stored. We can see in the example below that the dimensions are now only a <strong>two-dimensionsal</strong> array instead of <strong>3.</strong> In addition, now every cell only holds one value ranging (0 to 255) instead of three values (RGB). Gray scaling makes images smaller and faster to process.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*RqWllOHTzH-lXMM0FvMyVQ.png" /></figure><h3>Looking at RGB Channels Individually</h3><pre>#Get in the habit of always importing OpenCV and numPY<br>import cv2<br>import numpy as np</pre><pre>#Reading input image<br>input = cv2.imread(&#39;input.jpg&#39;)</pre><pre># OpenCV&#39;s &#39;split&#39; function splits the whole<br># image into each color index.<br>B, G, R = cv2.split(input)</pre><pre># Displaying every channel alone<br># Displays gray images because each pixel has only <strong>1</strong> value<br># while colorful images (BGR) have <strong>3</strong> values<br>cv2.imshow(&quot;Red Channel&quot;,   R)<br>cv2.imshow(&quot;Green Channel&quot;, G)<br>cv2.imshow(&quot;Blue Channel&quot;,  B)<br>cv2.waitKey()<br>cv2.destroyAllWindows()</pre><pre># Let&#39;s re-make the original image by merging all channels again<br>merged = cv2.merge([B, G, R]) <br>cv2.imshow(&quot;Merged&quot;, merged)</pre><pre># Let&#39;s amplify the blue color in the original picture<br>merged = cv2.merge([B+100, G, R])<br>cv2.imshow(&quot;Merged with Blue Amplified&quot;, merged)</pre><pre>cv2.waitKey()<br>cv2.destroyAllWindows()</pre><pre># ------------------------<br># Lastly, let&#39;s get each channel alone without having it turn gray.<br># We&#39;d have to eventually give <strong>each</strong> pixel a value of zero<br># in non-selected channel and leave only selected channel.<br># This is where numPy comes in play</pre><pre># Let&#39;s create a matrix of (int) zeros with dimensions of<br># the image h x w .<br>zeros = np.zeros(image.shape[:2], dtype = &quot;uint8&quot;)</pre><pre># Now we can show each channel in BGR instead of grayscale<br>cv2.imshow(&quot;Red&quot;, cv2.merge([zeros, zeros, R]))<br>cv2.imshow(&quot;Green&quot;, cv2.merge([zeros, G, zeros]))<br>cv2.imshow(&quot;Blue&quot;, cv2.merge([B, zeros, zeros]))</pre><pre>cv2.waitKey()<br>cv2.destroyAllWindows()</pre><h4>Results</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*V6G6vszTnPA4NXNzt0iHOQ.png" /><figcaption>RGB channels with each pixel having one value (R or G or B)</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*vwu7mjCT0AuUsLMggTTjyA.png" /><figcaption>Original with blue Amplified (left). Original photo after merge (right)</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ot-YqjqmderG0zvqjdUhug.png" /><figcaption>RGB Channels split with each pixel having three values with two levels set to zero.</figcaption></figure><h4>What’s Next?</h4><p>My third sequence of Computer Vision blogs will continue on exploring OpenCV functions. Stay tuned!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=c1ffb0e8025f" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Computer vision]]></title>
            <link>https://medium.com/@almutawakel.ali/computer-vision-ac2cb1cd2e68?source=rss-a5c00d648667------2</link>
            <guid isPermaLink="false">https://medium.com/p/ac2cb1cd2e68</guid>
            <category><![CDATA[machine-learning]]></category>
            <category><![CDATA[image-formation]]></category>
            <category><![CDATA[shape-from-x]]></category>
            <category><![CDATA[computer-vision]]></category>
            <dc:creator><![CDATA[Ali Almutawakel]]></dc:creator>
            <pubDate>Mon, 30 Jan 2017 04:20:41 GMT</pubDate>
            <atom:updated>2017-01-30T04:26:58.424Z</atom:updated>
            <content:encoded><![CDATA[<p>In its simplest definition, computer vision is the ability of computers to see. Computer vision is also known as image understanding, machine vision, robot vision, image analysis, or video understanding.</p><h3>An Image</h3><p>A 2D representational of the visible light spectrum. You might have a faint idea about what images are photographs or something you see on your laptop screen or TV screen and that is right. They are all 2D representations with an X and Y dimensions. Those X and Y dimensions correspond to pixels on the screen. You can visualize them as dots per inch as known as DPI. Now, each pixel reflects a different wavelength of the visible light spectrum. Each wavelength represent a different color all the way from red, green up to violet.</p><p>Images are formed by the reflection of light rays against an object on to a film, sensor or retina. Our eyes are exceptionally good at forming images. To understand how computers form images, first we have to understand how our eyes work. Below, is an example of how light rays might bounce from the top of the tree and from the middle to similar positions on the film resulting in an unfocused or blurred image. That is not how our eyes and cameras work.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/420/1*2rI8DfSDqTMsVmqRRg5Grw.png" /></figure><p>However, below shows a better example of how our eyes and cameras actually work. Using a small opening in a barrier called aperture, we block off most of the light rays reducing blurring effect on the film and producing a much more focused image. This is called the pinhole camera model.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/423/1*EYn7WCEJaRV49aA8sbEzaA.png" /></figure><p>However, there is a problem with the above model. The aperture is always fixed and the amount of light entered is always the same which at high light intensity can produce a white image. In addition, we can’t focus using only the aperture because we also need to move the film back and forth to focus the image. Which gets us to the diagram below. Controlling image formation is best done with a lens. Both our eyes and cameras use an adaptive lens that can control:</p><ol><li>Aperture size</li></ol><ul><li>Controls the amount of light allowed through f-stops in cameras.</li><li>Depth of field as known as Bokeh.</li></ul><p>2. Lens width</p><ul><li>Adjusts focus distance (close or far) instead of moving the film back and forth.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/420/1*JpWFMiI4qb-hM2gy8FZ6fw.png" /></figure><h3>How images are stored in computers</h3><p>We’ve concluded that images are made up of pixels at an X and Y coordinate. Now imagine the grid below are the pixels for an image. Each pixel coordinate (x, y) contains 3 values ranging from 0–255 (8-bit). These three values also known as RGB which are Red, Green, and Blue. Mixing different intensities of the three values gives us 16,581,375 colors which cover the full visible light spectrum colors.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/800/1*C2NSVSDaCPy1UTmnj_SvoQ.png" /></figure><p>Now that we have broken down an image into numbers, we can store those numbers in an array. Computers store images in a 3-dimensional array. For example, a cropped fragment of a sky photo shown below shows how the pixels of the photo. On its right, is a diagram that represents the 3-dimensional array that the sky photo is stored as. Each cell holds three values. For Example, the first pixel holds three arrays as {0,0,58} , {0,0,66}, {0,0,113}.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*btuBI8VZ1o7owXnQgzcIMQ.png" /></figure><p>Another type of images are grayscale images. They are stored in a two dimensional array where each pixel has a value ranging from 0–255 which represents the shades of grey.</p><p>Last type of images are black and white images which are also known as binary images. They are stored in a two-deminsional array where each pixel has a value of either 0 or 1.</p><h3>Shape from X</h3><p>A dimension is lost when we flatten a 3D object to 2D representation of it. Our eyes capture 2D images only but we still find a way to perceive the 3D world around us. Our mind uses other resources and cues to create that illusion. The goal in computer vision is to recover the dimension lost from a 2D image. This concept is called shape from X. Our brain uses the following methods:</p><ol><li>Stereo Vision — The shift between two eyes gives our brain an idea how far the object is. That explains why you get cross-eyed when looking at an object that is 10 cm away from your face.</li></ol><figure><img alt="" src="https://cdn-images-1.medium.com/max/232/1*9IGuC-j836z0sXPYaIJpTA.png" /></figure><p>2. Shape from Shading — The gradient variation gives you a cue of a 3D shape. That’s why the makeup industry exists. E.g. Makeup shading deceives the human vision system to show that the face is thinner or a sharper jaw line…etc. Software that creates 3D models can determine a light source and make up a synthetic object by creating the proper shading just the vase below. In computer vision, we try to recover the shape by determining where the light source is.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/153/1*ufliO9XF_4jYyL-NC9rI0Q.png" /></figure><p>3. Shape from texture — Gives an idea of the shape. If you had a paper with circles on it and folded it. Transformation of circles into ellipses and the density of circles can give us a cue of the shape.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/217/1*rT9OcQ23wO543oPqaai6OQ.png" /></figure><p>4. Shape from motion — If it is night and there is a biker with a reflector and lights on its wheels in front of you. If the biker isn’t moving, it would be very hard to spot what is the object is. Until the biker moves, you have a better cue that is it a biker from the lights on the wheel going round in circles and the object movement.</p><h3>Applications of computer vision</h3><p>Now why is it so important to recover the 3D shape from an image for computer vision? If you can recover the 3D shape, you can create an object recognition software. Once that’s possible the possibilities and uses are unlimited. For example:</p><p>1. In robotics: Have a robot follow a path. Even better, creating self-driving cars</p><p>2. Computer graphics: Creating 3D models from images.</p><p>3. Geo-Localization: Knowing where you are by taking a picture of the building next to you.</p><p>4. Character recognition: Process documents that are hand-written into a softcopy document.</p><p>5. Object tracking: Have a drone follow a race car around for live coverage.</p><p>6. Gesture recognition: Create interactive games like what Microsoft did with Kinect</p><p>7. Security: Facial recognition to allow access to authorized people into private areas.</p><p>8. Search: Smart image search like google search.</p><p>The possibilities are endless!</p><h3>Demo</h3><p>Using processing IDE. I implemented the simplest object recognition program. I extracted the rabbit from its background and creating a binary image. How could this be useful? This could be used as an input to compare with other animal photos to identify what this animal is.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/952/1*YFwi3P80Of2_i4XktExx1w.png" /></figure><p>Source code:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*dJ06U0LvU9-bnzZtIzo4sQ.png" /></figure><blockquote>An image is worth a thousand words.</blockquote><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=ac2cb1cd2e68" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>