{"id":1537,"date":"2020-12-02T16:00:27","date_gmt":"2020-12-02T16:00:27","guid":{"rendered":"https:\/\/thenextweb.com\/?p=1330176"},"modified":"2020-12-02T16:00:27","modified_gmt":"2020-12-02T16:00:27","slug":"a-foolproof-guide-to-image-manipulation-in-python-with-opencv","status":"publish","type":"post","link":"https:\/\/www.londonchiropracter.com\/?p=1537","title":{"rendered":"A foolproof guide to image manipulation in Python with OpenCV"},"content":{"rendered":"\n<p>Whether you want to build a complex deep learning model for a self-driving car, a live face recognition program, or making your image processing software for your graduate project, you will have to learn OpenCV along the way.<\/p>\n<p>OpenCV is a huge image and video processing library designed to work with many languages such as python, C\/C++, Java, and more. It is the foundation&nbsp;for many of the applications you know that deal with image processing.<\/p>\n<p>Getting started with OpenCV can be challenging, primarily if you rely on its official documentation, which is known for being cumbersome and hard to understand.<\/p>\n<p>Today we will learn how to work with OpenCV, and I\u2019ll do my best to keep it simple.<\/p>\n<h2 id=\"install-opencv\">Install OpenCV<\/h2>\n<p>Now it\u2019s the time to install OpenCV using only one simple command:<\/p>\n<p><span><\/p>\n<figure class=\"post-image post-mediaBleed alignnone\"><img decoding=\"async\" loading=\"lazy\" class=\"alignnone wp-image-1330177 lazy\" src=\"https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2020\/12\/Screenshot-2020-12-02-at-10.47.44.png\" alt width=\"751\" height=\"109\" sizes=\"(max-width: 751px) 100vw, 751px\" data-lazy=\"true\" data-srcset=\"https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2020\/12\/Screenshot-2020-12-02-at-10.47.44.png 1050w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2020\/12\/Screenshot-2020-12-02-at-10.47.44-280x41.png 280w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2020\/12\/Screenshot-2020-12-02-at-10.47.44-540x78.png 540w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2020\/12\/Screenshot-2020-12-02-at-10.47.44-270x39.png 270w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2020\/12\/Screenshot-2020-12-02-at-10.47.44-796x115.png 796w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2020\/12\/Screenshot-2020-12-02-at-10.47.44-1044x152.png 1044w\"><\/figure>\n<p><\/span><\/p>\n<h2 id=\"importing-a-simple-image\">Importing a simple image<\/h2>\n<p>The first thing you will need to learn is importing a simple image and displaying it using OpenCV.<\/p>\n<p>The code is straightforward:<\/p>\n<p><span><\/p>\n<figure class=\"post-image post-mediaBleed alignnone\"><img decoding=\"async\" loading=\"lazy\" class=\"alignnone wp-image-1330178 lazy\" src=\"https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2020\/12\/Screenshot-2020-12-02-at-10.47.48.png\" alt width=\"786\" height=\"560\" sizes=\"(max-width: 786px) 100vw, 786px\" data-lazy=\"true\" data-srcset=\"https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2020\/12\/Screenshot-2020-12-02-at-10.47.48.png 1050w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2020\/12\/Screenshot-2020-12-02-at-10.47.48-280x199.png 280w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2020\/12\/Screenshot-2020-12-02-at-10.47.48-379x270.png 379w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2020\/12\/Screenshot-2020-12-02-at-10.47.48-190x135.png 190w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2020\/12\/Screenshot-2020-12-02-at-10.47.48-796x567.png 796w\"><\/figure>\n<p><\/span><\/p>\n<figure class data-src=\"\/post\/2020-11-29-first-steps-with-opencv-for-python\/opencv-1_hu0296ab829625d9dd355a154d6ff275de_80974_700x0_resize_q75_box.jpg\">\n<p><figure class=\"post-image post-mediaBleed aligncenter\"><img decoding=\"async\" loading=\"lazy\" class=\"lazy loaded lazy\" src=\"https:\/\/livecodestream.dev\/post\/2020-11-29-first-steps-with-opencv-for-python\/opencv-1_hu0296ab829625d9dd355a154d6ff275de_80974_700x0_resize_q75_box.jpg\" alt width=\"700\" height=\"466\" data-lazy=\"true\"><figcaption><a href=\"https:\/\/thenextweb.com\/syndication\/2020\/12\/02\/a-foolproof-guide-to-image-manipulation-in-python-with-opencv\/#\" data-url=\"https:\/\/twitter.com\/intent\/tweet?url=https%3A%2F%2Fthenextweb.com%2Fsyndication%2F2020%2F12%2F02%2Fa-foolproof-guide-to-image-manipulation-in-python-with-opencv%2F&amp;via=thenextweb&amp;related=thenextweb&amp;text=Check out this picture on: Reading our first image\" data-title=\"Share Reading our first image on Twitter\" data-width=\"685\" data-height=\"500\" class=\"post-image-share popitup\" title=\"Share Reading our first image on Twitter\"><i class=\"icon icon--inline icon--twitter--dark\"><\/i><\/a>Reading our first image<span><\/span><\/figcaption><\/figure>\n<\/p>\n<\/figure>\n<p>After reading the code, if you think that we are doing more than just loading the image, you are right. After all, loading an image with OpenCV goes down to only one line of python code:<\/p>\n<div class=\"highlight\" readability=\"7\">\n<pre><code class=\"language-python\" data-lang=\"python\">img <span>=<\/span> cv2<span>.<\/span>imread(<span>\"image.jpg\"<\/span>) <\/code><\/pre>\n<\/div>\n<p>So, what about the rest? Well\u2026 the first thing we have to do is to import the library. Only then we can read the image using the<span>&nbsp;<\/span><code>imread<\/code><span>&nbsp;<\/span>method and pass the image\u2019s path as the only parameter.<\/p>\n<p>If we stop the program now, we would have loaded the image but done nothing with it, so instead let\u2019s at least present the image into a new window so the user can see the result. For that, we will use<span>&nbsp;<\/span><code>cv2.imshow<\/code><span>&nbsp;<\/span>and passing the window name and the image as arguments.<\/p>\n<p>Lastly, we tell Python not to exit the program until we press a key or close the window. Then we clean everything up by destroying all windows we opened.<\/p>\n<p><em>[Read:&nbsp;<a class=\"c-link c-message_attachment__title_link\" href=\"https:\/\/thenextweb.com\/readme\/2020\/11\/26\/how-to-build-a-search-engine-for-criminal-data\/\" target=\"_blank\" rel=\"noreferrer noopener\" data-qa=\"message_attachment_title_link\"><span dir=\"auto\">How to build a search engine for criminal data<\/span><\/a>]<\/em><\/p>\n<h2 id=\"loading-videos\">Loading videos<\/h2>\n<p>OpenCV is great at dealing not only with images but also with videos. The video streams can be loaded from a video file or directly from a video source such as a webcam.<\/p>\n<p>In the next example, we will load a video from the webcam and present it on a new window:<\/p>\n<p><span><\/p>\n<figure class=\"post-image post-mediaBleed alignnone\"><img decoding=\"async\" loading=\"lazy\" class=\"alignnone wp-image-1330179 lazy\" src=\"https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2020\/12\/Screenshot-2020-12-02-at-10.49.08.png\" alt width=\"795\" height=\"454\" sizes=\"(max-width: 795px) 100vw, 795px\" data-lazy=\"true\" data-srcset=\"https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2020\/12\/Screenshot-2020-12-02-at-10.49.08.png 1058w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2020\/12\/Screenshot-2020-12-02-at-10.49.08-280x160.png 280w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2020\/12\/Screenshot-2020-12-02-at-10.49.08-473x270.png 473w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2020\/12\/Screenshot-2020-12-02-at-10.49.08-236x135.png 236w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2020\/12\/Screenshot-2020-12-02-at-10.49.08-796x454.png 796w\"><\/figure>\n<p><\/span><\/p>\n<p>The code is self-explanatory, but let\u2019s review it in detail. We use the method<span>&nbsp;<\/span><code>VideoCapture<\/code><span>&nbsp;<\/span>to load the video resource. The first argument defines what input we are reading. Passing a<span>&nbsp;<\/span><code>0<\/code>, we are referring to the main webcam (if existent). In case you have multiple webcams connect, you can use<span>&nbsp;<\/span><code>1<\/code>,<span>&nbsp;<\/span><code>2<\/code>, etc. If your video is captured and saved in a file, you can pass a string with the path to the file. Next, we start a loop that will only end on user command, but more on that later. What\u2019s important here is what happens inside the loop. The first thing we are doing is asking our<span>&nbsp;<\/span><code>VideoCapture<\/code><span>&nbsp;<\/span>to read a frame of the video. In the case of the camera, it will be a snapshot of the camera at that time, and in the case of a video file, it will be the current video frame.<\/p>\n<p>Each frame we read from a video load in the same way as an image is crucial because it means that we have the entire arsenal of OpenCV functions at our disposal when dealing with videos.<\/p>\n<p>For example, the frame\u2019s output captured with<span>&nbsp;<\/span><code>read<\/code><span>&nbsp;<\/span>can be passed to the method<span>&nbsp;<\/span><code>imshow<\/code><span>&nbsp;<\/span>exactly as we did in the previous example of working with images.<\/p>\n<p>Now the video is playing, but there\u2019s no way out of the<span>&nbsp;<\/span><code>while<\/code><span>&nbsp;<\/span>loop, so let\u2019s build an exit strategy by detecting if the<span>&nbsp;<\/span><code>q<\/code><span>&nbsp;<\/span>key has been pressed. If it is, then we exit the loop for the cleanup activities.<\/p>\n<p>We have an additional step for clean up, which releases the camera or video file because even if we are not reading any more frames, we still have the objects open in memory. We can do that by using the method<span>&nbsp;<\/span><code>release<\/code><span>&nbsp;<\/span>from the<span>&nbsp;<\/span><code>VideoCapture<\/code><span>&nbsp;<\/span>object.<\/p>\n<h2 id=\"resizing-images\">Resizing images<\/h2>\n<p>Changing image sizes has a wide range of applications, from optimizing for sizes, zooming, or even feeding a neural network to perform some magic. If resizing an image is what you want, OpenCV got you covered.<\/p>\n<p>Let\u2019s now see an example of how to resize an image:<\/p>\n<p><span><\/p>\n<figure class=\"post-image post-mediaBleed alignnone\"><img decoding=\"async\" loading=\"lazy\" class=\"alignnone wp-image-1330181 lazy\" src=\"https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2020\/12\/Screenshot-2020-12-02-at-10.49.13.png\" alt width=\"820\" height=\"306\" sizes=\"(max-width: 820px) 100vw, 820px\" data-lazy=\"true\" data-srcset=\"https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2020\/12\/Screenshot-2020-12-02-at-10.49.13.png 1050w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2020\/12\/Screenshot-2020-12-02-at-10.49.13-280x105.png 280w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2020\/12\/Screenshot-2020-12-02-at-10.49.13-540x202.png 540w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2020\/12\/Screenshot-2020-12-02-at-10.49.13-270x101.png 270w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2020\/12\/Screenshot-2020-12-02-at-10.49.13-796x297.png 796w\"><\/figure>\n<p><\/span><\/p>\n<figure class data-src=\"\/post\/2020-11-29-first-steps-with-opencv-for-python\/opencv-2_hu0bb0a4b86ccaa4857f480d2631a1f04d_54539_700x0_resize_q75_box.jpg\">\n<p><figure class=\"post-image post-mediaBleed aligncenter\"><img decoding=\"async\" loading=\"lazy\" class=\"lazy loaded lazy\" src=\"https:\/\/livecodestream.dev\/post\/2020-11-29-first-steps-with-opencv-for-python\/opencv-2_hu0bb0a4b86ccaa4857f480d2631a1f04d_54539_700x0_resize_q75_box.jpg\" alt width=\"700\" height=\"340\" data-lazy=\"true\"><figcaption><a href=\"https:\/\/thenextweb.com\/syndication\/2020\/12\/02\/a-foolproof-guide-to-image-manipulation-in-python-with-opencv\/#\" data-url=\"https:\/\/twitter.com\/intent\/tweet?url=https%3A%2F%2Fthenextweb.com%2Fsyndication%2F2020%2F12%2F02%2Fa-foolproof-guide-to-image-manipulation-in-python-with-opencv%2F&amp;via=thenextweb&amp;related=thenextweb&amp;text=Check out this picture on: Resizing images\" data-title=\"Share Resizing images on Twitter\" data-width=\"685\" data-height=\"500\" class=\"post-image-share popitup\" title=\"Share Resizing images on Twitter\"><i class=\"icon icon--inline icon--twitter--dark\"><\/i><\/a>Resizing images<span><\/span><\/figcaption><\/figure>\n<\/p>\n<\/figure>\n<p>It\u2019s pretty simple, so we added some flavor to it, and instead of simply resizing the image to a specific size, we are scaling down the image by a factor of X (60% for the example). Note that the code will be simpler if we are targeting specific dimensions.<\/p>\n<p>The method<span>&nbsp;<\/span><code>resize<\/code><span>&nbsp;<\/span>expects at least two arguments, the image to be resized and the new dimensions (in x and y as a tuple). Optionally we can pass the third argument to define the interpolation as described on the<span>&nbsp;<\/span><a href=\"https:\/\/docs.opencv.org\/4.3.0\/da\/d54\/group__imgproc__transform.html#ga47a974309e9102f5f08231edc7e7529d\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">resize function docs<\/a><span>&nbsp;<\/span>.<\/p>\n<h2 id=\"switching-color-spaces\">Switching color spaces<\/h2>\n<p>When we read an image with OpenCV we think of colors as channels or the depth of the image array where each channel or dimension corresponds to a color. The most common color space and the one you probably already know is<span>&nbsp;<\/span><strong>RGB<\/strong>, consisting of 3 channels: red, green, and blue. But other systems can represent a color on an image, like<span>&nbsp;<\/span><strong>LAB<\/strong>,<span>&nbsp;<\/span><strong>YCrCb<\/strong>,<span>&nbsp;<\/span><strong>HLS<\/strong>, and<span>&nbsp;<\/span><strong>HSV<\/strong>, among others. Each of them with different characteristics worth studying and learning.<\/p>\n<p>A more popular option for setting colors on an image is grayscale, where only one channel defines each pixel. Let\u2019s see an example of how we can transform a color image into a greyscale one.<\/p>\n<p><span><\/p>\n<figure class=\"post-image post-mediaBleed alignnone\"><img decoding=\"async\" loading=\"lazy\" class=\"alignnone wp-image-1330183 lazy\" src=\"https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2020\/12\/Screenshot-2020-12-02-at-10.51.07.png\" alt width=\"794\" height=\"293\" sizes=\"(max-width: 794px) 100vw, 794px\" data-lazy=\"true\" data-srcset=\"https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2020\/12\/Screenshot-2020-12-02-at-10.51.07.png 1058w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2020\/12\/Screenshot-2020-12-02-at-10.51.07-280x103.png 280w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2020\/12\/Screenshot-2020-12-02-at-10.51.07-540x199.png 540w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2020\/12\/Screenshot-2020-12-02-at-10.51.07-270x100.png 270w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2020\/12\/Screenshot-2020-12-02-at-10.51.07-796x293.png 796w\"><\/figure>\n<p><\/span><\/p>\n<figure class data-src=\"\/post\/2020-11-29-first-steps-with-opencv-for-python\/opencv-3_hu726b2782d0d6ab54633c95fd2ee92c8b_69360_700x0_resize_q75_box.jpg\">\n<p><figure class=\"post-image post-mediaBleed aligncenter\"><img decoding=\"async\" loading=\"lazy\" class=\"lazy loaded lazy\" src=\"https:\/\/livecodestream.dev\/post\/2020-11-29-first-steps-with-opencv-for-python\/opencv-3_hu726b2782d0d6ab54633c95fd2ee92c8b_69360_700x0_resize_q75_box.jpg\" alt width=\"700\" height=\"466\" data-lazy=\"true\"><figcaption><a href=\"https:\/\/thenextweb.com\/syndication\/2020\/12\/02\/a-foolproof-guide-to-image-manipulation-in-python-with-opencv\/#\" data-url=\"https:\/\/twitter.com\/intent\/tweet?url=https%3A%2F%2Fthenextweb.com%2Fsyndication%2F2020%2F12%2F02%2Fa-foolproof-guide-to-image-manipulation-in-python-with-opencv%2F&amp;via=thenextweb&amp;related=thenextweb&amp;text=Check out this picture on: Grayscale image\" data-title=\"Share Grayscale image on Twitter\" data-width=\"685\" data-height=\"500\" class=\"post-image-share popitup\" title=\"Share Grayscale image on Twitter\"><i class=\"icon icon--inline icon--twitter--dark\"><\/i><\/a>Grayscale image<\/figcaption><\/figure>\n<\/p>\n<\/figure>\n<p>The function where all the magic happens is<span>&nbsp;<\/span><code>cvtColor<\/code>, which expects two arguments, the image and the color space, and returns the new image without altering the original. Fortunately, OpenCV has defined values for each known color space transformation. In our case, we use<span>&nbsp;<\/span><code>COLOR_BGR2GRAY<\/code>, which transforms BGR to GRAY.<\/p>\n<p>So what\u2019s BGR? It\u2019s the default way that OpenCV loads images.<\/p>\n<h2 id=\"saving-images\">Saving images<\/h2>\n<p>We often need to save the image result after processing it, maybe after changing its color space, making image transformation, or whatever operation we do on that image.<\/p>\n<p>The following code shows you how to save an image after changing its color to grayscale:<\/p>\n<p><span><\/p>\n<figure class=\"post-image post-mediaBleed alignnone\"><img decoding=\"async\" loading=\"lazy\" class=\"alignnone wp-image-1330184 lazy\" src=\"https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2020\/12\/Screenshot-2020-12-02-at-10.51.49.png\" alt width=\"828\" height=\"347\" sizes=\"(max-width: 828px) 100vw, 828px\" data-lazy=\"true\" data-srcset=\"https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2020\/12\/Screenshot-2020-12-02-at-10.51.49.png 1058w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2020\/12\/Screenshot-2020-12-02-at-10.51.49-280x118.png 280w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2020\/12\/Screenshot-2020-12-02-at-10.51.49-540x227.png 540w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2020\/12\/Screenshot-2020-12-02-at-10.51.49-270x113.png 270w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2020\/12\/Screenshot-2020-12-02-at-10.51.49-796x334.png 796w\"><\/figure>\n<p><\/span><\/p>\n<p>You can see that we\u2019ve used a function called<span>&nbsp;<\/span><code>imwrite<\/code><span>&nbsp;<\/span>responsible for saving the gray image in our computer after specifying the path for the newly saved image. The actual image that we need to save is the variable gray in this case.<\/p>\n<h2 id=\"image-smoothing\">Image smoothing<\/h2>\n<p>OpenCV offers tools to smooth an image and help reduce the noise in it. The process is fully automated, and all the complexity of how it works is encapsulated for us on a single, simple to use function.<\/p>\n<p>Smoothing an image intends to improve its quality, though not perfect, in some scenarios, it can be a substantial change and key for using the image on further processes.<\/p>\n<p>Here is an example of how to smooth an image:<\/p>\n<p><span><\/p>\n<figure class=\"post-image post-mediaBleed alignnone\"><img decoding=\"async\" loading=\"lazy\" class=\"alignnone wp-image-1330185 lazy\" src=\"https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2020\/12\/Screenshot-2020-12-02-at-10.53.26.png\" alt width=\"811\" height=\"310\" sizes=\"(max-width: 811px) 100vw, 811px\" data-lazy=\"true\" data-srcset=\"https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2020\/12\/Screenshot-2020-12-02-at-10.53.26.png 1046w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2020\/12\/Screenshot-2020-12-02-at-10.53.26-280x107.png 280w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2020\/12\/Screenshot-2020-12-02-at-10.53.26-540x207.png 540w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2020\/12\/Screenshot-2020-12-02-at-10.53.26-270x103.png 270w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2020\/12\/Screenshot-2020-12-02-at-10.53.26-796x304.png 796w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2020\/12\/Screenshot-2020-12-02-at-10.53.26-1044x400.png 1044w\"><\/figure>\n<p><\/span><\/p>\n<figure class data-src=\"\/post\/2020-11-29-first-steps-with-opencv-for-python\/opencv-4_hu3bdf41378a17a6420a984135f80fa876_436188_700x0_resize_q75_box.jpg\">\n<p><figure class=\"post-image post-mediaBleed aligncenter\"><img decoding=\"async\" loading=\"lazy\" class=\"lazy loaded lazy\" src=\"https:\/\/livecodestream.dev\/post\/2020-11-29-first-steps-with-opencv-for-python\/opencv-4_hu3bdf41378a17a6420a984135f80fa876_436188_700x0_resize_q75_box.jpg\" alt width=\"700\" height=\"261\" data-lazy=\"true\"><figcaption><a href=\"https:\/\/thenextweb.com\/syndication\/2020\/12\/02\/a-foolproof-guide-to-image-manipulation-in-python-with-opencv\/#\" data-url=\"https:\/\/twitter.com\/intent\/tweet?url=https%3A%2F%2Fthenextweb.com%2Fsyndication%2F2020%2F12%2F02%2Fa-foolproof-guide-to-image-manipulation-in-python-with-opencv%2F&amp;via=thenextweb&amp;related=thenextweb&amp;text=Check out this picture on: Original Image\" data-title=\"Share Original Image on Twitter\" data-width=\"685\" data-height=\"500\" class=\"post-image-share popitup\" title=\"Share Original Image on Twitter\"><i class=\"icon icon--inline icon--twitter--dark\"><\/i><\/a>Original Image<\/figcaption><\/figure>\n<\/p>\n<\/figure>\n<figure class data-src=\"\/post\/2020-11-29-first-steps-with-opencv-for-python\/opencv-5_hu49098c221b6b5d45585db7a0fa2da69e_209602_700x0_resize_q75_box.jpg\">\n<p><figure class=\"post-image post-mediaBleed aligncenter\"><img decoding=\"async\" loading=\"lazy\" class=\"lazy loaded lazy\" src=\"https:\/\/livecodestream.dev\/post\/2020-11-29-first-steps-with-opencv-for-python\/opencv-5_hu49098c221b6b5d45585db7a0fa2da69e_209602_700x0_resize_q75_box.jpg\" alt width=\"700\" height=\"261\" data-lazy=\"true\"><figcaption><a href=\"https:\/\/thenextweb.com\/syndication\/2020\/12\/02\/a-foolproof-guide-to-image-manipulation-in-python-with-opencv\/#\" data-url=\"https:\/\/twitter.com\/intent\/tweet?url=https%3A%2F%2Fthenextweb.com%2Fsyndication%2F2020%2F12%2F02%2Fa-foolproof-guide-to-image-manipulation-in-python-with-opencv%2F&amp;via=thenextweb&amp;related=thenextweb&amp;text=Check out this picture on: Smoothed Image\" data-title=\"Share Smoothed Image on Twitter\" data-width=\"685\" data-height=\"500\" class=\"post-image-share popitup\" title=\"Share Smoothed Image on Twitter\"><i class=\"icon icon--inline icon--twitter--dark\"><\/i><\/a>Smoothed Image<\/figcaption><\/figure>\n<\/p>\n<\/figure>\n<p>The image looks much better, but how does it work? Through the method<span>&nbsp;<\/span><code>blur<\/code><span>&nbsp;<\/span>on the OpenCV library, which expects the image and the kernel size as arguments. The kernel size being a tuple to reflect the x and y axis. Note that different values of x and y will result in different outputs, so you will have to play around with those values for your images.<\/p>\n<p>The kernel size works by taking a small pixel area (5\u00d75 in our case), taking the average value of those pixels, and replacing the real one (pixel) to get the new little noisy image.<\/p>\n<p>There are other ways to smooth an image using for example,<span>&nbsp;<\/span><code>gaussianBlur<\/code><span>&nbsp;<\/span>or<span>&nbsp;<\/span><code>medianBlur<\/code><span>&nbsp;<\/span>that works similarly.<\/p>\n<h2 id=\"drawing-on-images\">Drawing on images<\/h2>\n<p>So far, we have been playing with the images without adding anything new to them. It\u2019s time we change that. OpenCV allows us not only to perform transformations and effects to the images but to change them or draw on them.<\/p>\n<p>Drawing on images can be useful if, for example, you are trying to make an object tracking program or face recognition program where you want to draw a square or shape to highlight the identified objects.<\/p>\n<p>Let\u2019s draw a few geometric shapes on images to show how it works.<\/p>\n<p id=\"drawing-a-line\"><strong>Drawing a line<\/strong><\/p>\n<p>We will try to draw a line on an image using the<span>&nbsp;<\/span><code>line<\/code><span>&nbsp;<\/span>function:<\/p>\n<p><span><\/p>\n<figure class=\"post-image post-mediaBleed alignnone\"><img decoding=\"async\" loading=\"lazy\" class=\"alignnone wp-image-1330187 lazy\" src=\"https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2020\/12\/Screenshot-2020-12-02-at-10.54.00.png\" alt width=\"822\" height=\"307\" sizes=\"(max-width: 822px) 100vw, 822px\" data-lazy=\"true\" data-srcset=\"https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2020\/12\/Screenshot-2020-12-02-at-10.54.00.png 1046w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2020\/12\/Screenshot-2020-12-02-at-10.54.00-280x104.png 280w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2020\/12\/Screenshot-2020-12-02-at-10.54.00-540x201.png 540w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2020\/12\/Screenshot-2020-12-02-at-10.54.00-270x101.png 270w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2020\/12\/Screenshot-2020-12-02-at-10.54.00-796x297.png 796w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2020\/12\/Screenshot-2020-12-02-at-10.54.00-1044x390.png 1044w\"><\/figure>\n<p><\/span><\/p>\n<figure class data-src=\"\/post\/2020-11-29-first-steps-with-opencv-for-python\/opencv-6_hucdaed7f85ccf3861208ed144fda3e796_82130_700x0_resize_q75_box.jpg\">\n<p><figure class=\"post-image post-mediaBleed aligncenter\"><img decoding=\"async\" loading=\"lazy\" class=\"lazy loaded lazy\" src=\"https:\/\/livecodestream.dev\/post\/2020-11-29-first-steps-with-opencv-for-python\/opencv-6_hucdaed7f85ccf3861208ed144fda3e796_82130_700x0_resize_q75_box.jpg\" alt width=\"700\" height=\"466\" data-lazy=\"true\"><figcaption><a href=\"https:\/\/thenextweb.com\/syndication\/2020\/12\/02\/a-foolproof-guide-to-image-manipulation-in-python-with-opencv\/#\" data-url=\"https:\/\/twitter.com\/intent\/tweet?url=https%3A%2F%2Fthenextweb.com%2Fsyndication%2F2020%2F12%2F02%2Fa-foolproof-guide-to-image-manipulation-in-python-with-opencv%2F&amp;via=thenextweb&amp;related=thenextweb&amp;text=Check out this picture on: Drawing a line\" data-title=\"Share Drawing a line on Twitter\" data-width=\"685\" data-height=\"500\" class=\"post-image-share popitup\" title=\"Share Drawing a line on Twitter\"><i class=\"icon icon--inline icon--twitter--dark\"><\/i><\/a>Drawing a line<\/figcaption><\/figure>\n<\/p>\n<\/figure>\n<p>The<span>&nbsp;<\/span><code>line<\/code><span>&nbsp;<\/span>function expects the image and four more arguments: the start of the line in (x1, y1), the end of the line in (x2, y2), the line\u2019s color (in BGR for our image), and its thickness in pixels.<\/p>\n<p id=\"drawing-a-rectangle\"><strong>Drawing a rectangle<\/strong><\/p>\n<p>I think rectangles are the most used shape, at least in the AI world, as they are commonly used to track objects like faces, cars, or traffic signs on the images. They are also super easy to use. Here is an example:<\/p>\n<p><span><\/p>\n<figure class=\"post-image post-mediaBleed alignnone\"><img decoding=\"async\" loading=\"lazy\" class=\"alignnone wp-image-1330188 lazy\" src=\"https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2020\/12\/Screenshot-2020-12-02-at-10.54.30.png\" alt width=\"819\" height=\"304\" sizes=\"(max-width: 819px) 100vw, 819px\" data-lazy=\"true\" data-srcset=\"https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2020\/12\/Screenshot-2020-12-02-at-10.54.30.png 1034w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2020\/12\/Screenshot-2020-12-02-at-10.54.30-280x104.png 280w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2020\/12\/Screenshot-2020-12-02-at-10.54.30-540x201.png 540w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2020\/12\/Screenshot-2020-12-02-at-10.54.30-270x100.png 270w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2020\/12\/Screenshot-2020-12-02-at-10.54.30-796x296.png 796w\"><\/figure>\n<p><\/span><\/p>\n<figure class data-src=\"\/post\/2020-11-29-first-steps-with-opencv-for-python\/opencv-7_hu3f51a92120f0c120a4547774d1a1f87c_82541_700x0_resize_q75_box.jpg\">\n<p><figure class=\"post-image post-mediaBleed aligncenter\"><img decoding=\"async\" loading=\"lazy\" class=\"lazy loaded lazy\" src=\"https:\/\/livecodestream.dev\/post\/2020-11-29-first-steps-with-opencv-for-python\/opencv-7_hu3f51a92120f0c120a4547774d1a1f87c_82541_700x0_resize_q75_box.jpg\" alt width=\"700\" height=\"466\" data-lazy=\"true\"><figcaption><a href=\"https:\/\/thenextweb.com\/syndication\/2020\/12\/02\/a-foolproof-guide-to-image-manipulation-in-python-with-opencv\/#\" data-url=\"https:\/\/twitter.com\/intent\/tweet?url=https%3A%2F%2Fthenextweb.com%2Fsyndication%2F2020%2F12%2F02%2Fa-foolproof-guide-to-image-manipulation-in-python-with-opencv%2F&amp;via=thenextweb&amp;related=thenextweb&amp;text=Check out this picture on: Drawing a rectangle\" data-title=\"Share Drawing a rectangle on Twitter\" data-width=\"685\" data-height=\"500\" class=\"post-image-share popitup\" title=\"Share Drawing a rectangle on Twitter\"><i class=\"icon icon--inline icon--twitter--dark\"><\/i><\/a>Drawing a rectangle<\/figcaption><\/figure>\n<\/p>\n<\/figure>\n<p>The<span>&nbsp;<\/span><code>rectangle<\/code><span>&nbsp;<\/span>function is very similar to the<span>&nbsp;<\/span><code>line<\/code><span>&nbsp;<\/span>function. It expects the image and four more arguments: the top-left corner of the rectangle in (x1, y1), the bottom-right of it in (x2, y2), the line\u2019s color (in BGR for our image), and its thickness in pixels. ###. Drawing a Circle The last thing we will draw is a small circle on the image which is sometimes useful when you are tracking a circle object like a ball.<\/p>\n<p><span><\/p>\n<figure class=\"post-image post-mediaBleed alignnone\"><img decoding=\"async\" loading=\"lazy\" class=\"alignnone wp-image-1330189 lazy\" src=\"https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2020\/12\/Screenshot-2020-12-02-at-10.55.01.png\" alt width=\"827\" height=\"315\" sizes=\"(max-width: 827px) 100vw, 827px\" data-lazy=\"true\" data-srcset=\"https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2020\/12\/Screenshot-2020-12-02-at-10.55.01.png 1044w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2020\/12\/Screenshot-2020-12-02-at-10.55.01-280x107.png 280w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2020\/12\/Screenshot-2020-12-02-at-10.55.01-540x206.png 540w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2020\/12\/Screenshot-2020-12-02-at-10.55.01-270x103.png 270w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2020\/12\/Screenshot-2020-12-02-at-10.55.01-796x303.png 796w\"><\/figure>\n<p><\/span><\/p>\n<figure class data-src=\"\/post\/2020-11-29-first-steps-with-opencv-for-python\/opencv-8_hu3f51a92120f0c120a4547774d1a1f87c_82210_700x0_resize_q75_box.jpg\">\n<p><figure class=\"post-image post-mediaBleed aligncenter\"><img decoding=\"async\" loading=\"lazy\" class=\"lazy loaded lazy\" src=\"https:\/\/livecodestream.dev\/post\/2020-11-29-first-steps-with-opencv-for-python\/opencv-8_hu3f51a92120f0c120a4547774d1a1f87c_82210_700x0_resize_q75_box.jpg\" alt width=\"700\" height=\"466\" data-lazy=\"true\"><figcaption><a href=\"https:\/\/thenextweb.com\/syndication\/2020\/12\/02\/a-foolproof-guide-to-image-manipulation-in-python-with-opencv\/#\" data-url=\"https:\/\/twitter.com\/intent\/tweet?url=https%3A%2F%2Fthenextweb.com%2Fsyndication%2F2020%2F12%2F02%2Fa-foolproof-guide-to-image-manipulation-in-python-with-opencv%2F&amp;via=thenextweb&amp;related=thenextweb&amp;text=Check out this picture on: Drawing a circle\" data-title=\"Share Drawing a circle on Twitter\" data-width=\"685\" data-height=\"500\" class=\"post-image-share popitup\" title=\"Share Drawing a circle on Twitter\"><i class=\"icon icon--inline icon--twitter--dark\"><\/i><\/a>Drawing a circle<\/figcaption><\/figure>\n<\/p>\n<\/figure>\n<p>Again all these functions are pretty similar. To render a circle on an image, we use the<span>&nbsp;<\/span><code>circle<\/code><span>&nbsp;<\/span>function that expects the image and four more arguments: the center point of the circle in (x, y), the radius in pixels, the color of it, and the line thickness.<\/p>\n<h2 id=\"conclusion\">Conclusion<\/h2>\n<p>OpenCV is an exciting and powerful library for dealing with images and videos. Its wide range of uses goes from a simple helper library to perform image manipulations to implement state-of-the-art computer vision algorithms.<\/p>\n<p>Today we covered just a small percentage of what this library is capable of. If you enjoy this reading, I recommend checking my article on<span>&nbsp;<\/span><a href=\"https:\/\/livecodestream.dev\/post\/2020-06-11-essential-opencv-functions-to-get-you-started-into-computer-vision\/\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">essential OpenCV functions<\/a>, which will extend your knowledge of the library a bit more.<\/p>\n<p>Computer vision is a topic that fascinates me, and I\u2019ll write more about OpenCV in the future, so stay tuned.<\/p>\n<p><i><span>This <\/span><\/i><a href=\"https:\/\/livecodestream.dev\/post\/2020-11-29-first-steps-with-opencv-for-python\/\" target=\"_blank\" rel=\"nofollow noopener noreferrer\"><i><span>article<\/span><\/i><\/a><i><span> was originally published on <\/span><\/i><a href=\"https:\/\/livecodestream.dev\/\" target=\"_blank\" rel=\"nofollow noopener noreferrer\"><i><span>Live Code Stream<\/span><\/i><\/a><i><span> by <\/span><\/i><a href=\"https:\/\/www.linkedin.com\/in\/bajcmartinez\/\" target=\"_blank\" rel=\"nofollow noopener noreferrer\"><i><span>Juan Cruz Martinez<\/span><\/i><\/a><i><span> (twitter: <\/span><\/i><a href=\"https:\/\/twitter.com\/bajcmartinez\" target=\"_blank\" rel=\"nofollow noopener noreferrer\"><i><span>@bajcmartinez<\/span><\/i><\/a><i><span>), founder and publisher of Live Code Stream, entrepreneur, developer, author, speaker, and doer of things.<\/span><\/i><\/p>\n<p><a href=\"https:\/\/livecodestream.dev\/subscribe\" target=\"_blank\" rel=\"nofollow noopener noreferrer\"><i><span>Live Code Stream<\/span><\/i><\/a><i><span> is also available as a free weekly newsletter. Sign up for updates on everything related to programming, AI, and computer science in general.<\/span><\/i><\/p>\n<p> <a href=\"https:\/\/thenextweb.com\/syndication\/2020\/12\/02\/a-foolproof-guide-to-image-manipulation-in-python-with-opencv\/\">Source<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Whether you want to build a complex deep learning model for a self-driving car, a live face recognition program, or making your image processing software for your graduate project, you will have&#8230;<\/p>\n","protected":false},"author":1,"featured_media":1538,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[1],"tags":[],"_links":{"self":[{"href":"https:\/\/www.londonchiropracter.com\/index.php?rest_route=\/wp\/v2\/posts\/1537"}],"collection":[{"href":"https:\/\/www.londonchiropracter.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.londonchiropracter.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.londonchiropracter.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.londonchiropracter.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1537"}],"version-history":[{"count":0,"href":"https:\/\/www.londonchiropracter.com\/index.php?rest_route=\/wp\/v2\/posts\/1537\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.londonchiropracter.com\/index.php?rest_route=\/wp\/v2\/media\/1538"}],"wp:attachment":[{"href":"https:\/\/www.londonchiropracter.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1537"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.londonchiropracter.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1537"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.londonchiropracter.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1537"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}