{"id":17665,"date":"2021-06-08T05:08:50","date_gmt":"2021-06-08T05:08:50","guid":{"rendered":"https:\/\/www.askpython.com\/?p=17665"},"modified":"2021-06-08T05:08:51","modified_gmt":"2021-06-08T05:08:51","slug":"image-segmentation","status":"publish","type":"post","link":"https:\/\/www.askpython.com\/python\/examples\/image-segmentation","title":{"rendered":"Python: Image Segmentation"},"content":{"rendered":"\n<p>Hello there fellow coder! Today in this tutorial we will understand what Image Segmentation is and in the later sections implement the same using OpenCV in the Python programming language.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">What is Image Segmentation?<\/h2>\n\n\n\n<p>Image Segmentation implies grouping a similar set of pixels and parts of an image together for easy classification and categorization of objects in the images.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Why is Image Segmentation Needed?<\/h2>\n\n\n\n<p>Image Segmentation&nbsp;is an important stage in Image processing systems as it helps in extracting the objects of our interest and makes the future modeling easy. It helps to separate the desired objects from the unnecessary objects.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Applications of Image Segmentation<\/h2>\n\n\n\n<p>Image Segmentation has various applications in the real life. Some of them are as follows:<\/p>\n\n\n\n<ol class=\"wp-block-list\"><li>Traffic Management System<\/li><li>Cancer and other medical issue detection<\/li><li>Satellite Image Analysis<\/li><\/ol>\n\n\n\n<h2 class=\"wp-block-heading\">Image Segmentation Implementation<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">1. Importing Modules<\/h3>\n\n\n\n<p>All the necessary modules required for Image Segmentation implementation and Image plotting are imported into the program.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; gutter: true; title: ; notranslate\" title=\"\">\nimport numpy as np\nimport cv2\nfrom matplotlib import pyplot as plt\n<\/pre><\/div>\n\n\n<h3 class=\"wp-block-heading\">2. Loading Original Image<\/h3>\n\n\n\n<p>The next step is to load the original image ( stored in the same directory as the code file ) using the code below. The output is also displayed right below the code.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; gutter: true; title: ; notranslate\" title=\"\">\nimg = cv2.imread(&#039;image1.jpg&#039;)\nimg=cv2.cvtColor(img,cv2.COLOR_BGR2RGB)\nplt.figure(figsize=(8,8))\nplt.imshow(img,cmap=&quot;gray&quot;)\nplt.axis(&#039;off&#039;)\nplt.title(&quot;Original Image&quot;)\nplt.show()\n<\/pre><\/div>\n\n\n<div class=\"wp-block-image is-style-default\"><figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"449\" height=\"465\" src=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2021\/06\/Original_img_segmentation.png\" alt=\"Original Image Segmentation\" class=\"wp-image-17690\" srcset=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2021\/06\/Original_img_segmentation.png 449w, https:\/\/www.askpython.com\/wp-content\/uploads\/2021\/06\/Original_img_segmentation-290x300.png 290w\" sizes=\"auto, (max-width: 449px) 100vw, 449px\" \/><figcaption>Original Img Segmentation<\/figcaption><\/figure><\/div>\n\n\n\n<h3 class=\"wp-block-heading\">3. Converting to Grayscale<\/h3>\n\n\n\n<p>To make future image processing less complex and simple we will be converting the image loaded in the previous step to grayscale image using the code mentioned below. The output image is also displayed below the code.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; gutter: true; title: ; notranslate\" title=\"\">\ngray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)\nplt.figure(figsize=(8,8))\nplt.imshow(gray,cmap=&quot;gray&quot;)\nplt.axis(&#039;off&#039;)\nplt.title(&quot;GrayScale Image&quot;)\nplt.show()\n<\/pre><\/div>\n\n\n<div class=\"wp-block-image is-style-default\"><figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"449\" height=\"465\" src=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2021\/06\/Grayscale_img_segmentation.png\" alt=\"Grayscale Image Segmentation\" class=\"wp-image-17691\" srcset=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2021\/06\/Grayscale_img_segmentation.png 449w, https:\/\/www.askpython.com\/wp-content\/uploads\/2021\/06\/Grayscale_img_segmentation-290x300.png 290w\" sizes=\"auto, (max-width: 449px) 100vw, 449px\" \/><figcaption>Grayscale Img Segmentation<\/figcaption><\/figure><\/div>\n\n\n\n<h3 class=\"wp-block-heading\">4. Converting to a Binary Inverted Image<\/h3>\n\n\n\n<p>To study the image in more detail and have a very precise study of the image we will be converting the image into a binary inverted image using the code mentioned below. The output is also displayed along with the code.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; gutter: true; title: ; notranslate\" title=\"\">\nret, thresh = cv2.threshold(gray, 0, 255,cv2.THRESH_BINARY_INV +cv2.THRESH_OTSU)\nplt.figure(figsize=(8,8))\nplt.imshow(thresh,cmap=&quot;gray&quot;)\nplt.axis(&#039;off&#039;)\nplt.title(&quot;Threshold Image&quot;)\nplt.show()\n<\/pre><\/div>\n\n\n<div class=\"wp-block-image is-style-default\"><figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"449\" height=\"465\" src=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2021\/06\/Threshold_img_segmentation.png\" alt=\"Threshold Img Segmentation\" class=\"wp-image-17692\" srcset=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2021\/06\/Threshold_img_segmentation.png 449w, https:\/\/www.askpython.com\/wp-content\/uploads\/2021\/06\/Threshold_img_segmentation-290x300.png 290w\" sizes=\"auto, (max-width: 449px) 100vw, 449px\" \/><figcaption>Threshold Img Segmentation<\/figcaption><\/figure><\/div>\n\n\n\n<h3 class=\"wp-block-heading\">5. Segmenting the Image<\/h3>\n\n\n\n<p>Now the last step is to get the segmented image with the help of the code mentioned below. We will be making use of all the previous images somewhere or the other to try to get the most accurate segmented image we can.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; gutter: true; title: ; notranslate\" title=\"\">\nkernel = np.ones((3, 3), np.uint8)\nclosing = cv2.morphologyEx(thresh, cv2.MORPH_CLOSE,kernel, iterations = 15)\nbg = cv2.dilate(closing, kernel, iterations = 1)\ndist_transform = cv2.distanceTransform(closing, cv2.DIST_L2, 0)\nret, fg = cv2.threshold(dist_transform, 0.02*dist_transform.max(), 255, 0)\ncv2.imshow(&#039;image&#039;, fg)\nplt.figure(figsize=(8,8))\nplt.imshow(fg,cmap=&quot;gray&quot;)\nplt.axis(&#039;off&#039;)\nplt.title(&quot;Segmented Image&quot;)\nplt.show()\n<\/pre><\/div>\n\n\n<div class=\"wp-block-image is-style-default\"><figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"449\" height=\"465\" src=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2021\/06\/Segmented_img_segmentation.png\" alt=\"Segmented Img Segmentation\" class=\"wp-image-17693\" srcset=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2021\/06\/Segmented_img_segmentation.png 449w, https:\/\/www.askpython.com\/wp-content\/uploads\/2021\/06\/Segmented_img_segmentation-290x300.png 290w\" sizes=\"auto, (max-width: 449px) 100vw, 449px\" \/><figcaption>Segmented Img Segmentation<\/figcaption><\/figure><\/div>\n\n\n\n<h2 class=\"wp-block-heading\">The Final Output<\/h2>\n\n\n\n<p>After all the processing is done and the image is segmented, let&#8217;s plot all the results in one frame with the help of subplots. The code for the same is mentioned below.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; gutter: true; title: ; notranslate\" title=\"\">\nplt.figure(figsize=(10,10))\n\nplt.subplot(2,2,1)\nplt.axis(&#039;off&#039;)\nplt.title(&quot;Original Image&quot;)\nplt.imshow(img,cmap=&quot;gray&quot;)\n\nplt.subplot(2,2,2)\nplt.imshow(gray,cmap=&quot;gray&quot;)\nplt.axis(&#039;off&#039;)\nplt.title(&quot;GrayScale Image&quot;)\n\nplt.subplot(2,2,3)\nplt.imshow(thresh,cmap=&quot;gray&quot;)\nplt.axis(&#039;off&#039;)\nplt.title(&quot;Threshold Image&quot;)\n\nplt.subplot(2,2,4)\nplt.imshow(fg,cmap=&quot;gray&quot;)\nplt.axis(&#039;off&#039;)\nplt.title(&quot;Segmented Image&quot;)\n\nplt.show()\n<\/pre><\/div>\n\n\n<p>The final results are as follows.<\/p>\n\n\n\n<div class=\"wp-block-image is-style-default\"><figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"565\" height=\"573\" src=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2021\/06\/Segmentation_output1.png\" alt=\"Segmentation Output1\" class=\"wp-image-17695\" srcset=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2021\/06\/Segmentation_output1.png 565w, https:\/\/www.askpython.com\/wp-content\/uploads\/2021\/06\/Segmentation_output1-296x300.png 296w\" sizes=\"auto, (max-width: 565px) 100vw, 565px\" \/><figcaption>Segmentation Output1<\/figcaption><\/figure><\/div>\n\n\n\n<p>The same algorithm was tested for another image and the results are as follows. You can see the results are pretty satisfying.<\/p>\n\n\n\n<div class=\"wp-block-image is-style-default\"><figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"519\" height=\"573\" src=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2021\/06\/Segmentation_output2.png\" alt=\"Segmentation Output2\" class=\"wp-image-17696\" srcset=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2021\/06\/Segmentation_output2.png 519w, https:\/\/www.askpython.com\/wp-content\/uploads\/2021\/06\/Segmentation_output2-272x300.png 272w\" sizes=\"auto, (max-width: 519px) 100vw, 519px\" \/><figcaption>Segmentation Output2<\/figcaption><\/figure><\/div>\n\n\n\n<h2 class=\"wp-block-heading\">Conclusion<\/h2>\n\n\n\n<p>So today we learned about Image Segmentation and now you know how to implement the same on your own. Try out things by yourself for various images. Happy coding!<\/p>\n\n\n\n<p>Thank you for reading!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hello there fellow coder! Today in this tutorial we will understand what Image Segmentation is and in the later sections implement the same using OpenCV in the Python programming language. What is Image Segmentation? Image Segmentation implies grouping a similar set of pixels and parts of an image together for easy classification and categorization of [&hellip;]<\/p>\n","protected":false},"author":28,"featured_media":17667,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[9],"tags":[],"class_list":["post-17665","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-examples"],"blocksy_meta":[],"_links":{"self":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts\/17665","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/users\/28"}],"replies":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/comments?post=17665"}],"version-history":[{"count":0,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts\/17665\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/media\/17667"}],"wp:attachment":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/media?parent=17665"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/categories?post=17665"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/tags?post=17665"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}