× {{alert.msg}} Never ask again
Get notified about new tutorials RECEIVE NEW TUTORIALS

Remove spurious small islands of noise in an image - Python OpenCV

Ray Phan
May 27, 2015
<p>A lot of your questions stem from the fact that you're not sure how morphological image processing works, but we can put your doubts to rest. You can interpret the structuring element as the "base shape" to compare to. 1 in the structuring element corresponds to a pixel that you want to look at in this shape and 0 is one you want to ignore. There are different shapes, such as rectangular (as you have figured out with <code>MORPH_RECT</code>), ellipse, circular, etc.</p> <p>As such, <code>cv2.getStructuringElement</code> returns a structuring element for you. The first parameter specifies the type you want and the second parameter specifies the size you want. In your case, you want a 2 x 2 "rectangle"... which is really a square, but that's fine.</p> <p>In a more bastardized sense, you use the structuring element and scan from left to right and top to bottom of your image and you grab pixel neighbourhoods. Each pixel neighbourhood has its centre exactly at the pixel of interest that you're looking at. The size of each pixel neighbourhood is the same size as the structuring element.</p> <h1>Erosion</h1> <p>For an erosion, you examine all of the pixels in a pixel neighbourhood that are touching the structuring element. If <strong>every non-zero pixel</strong> is touching a structuring element pixel that is 1, then the output pixel in the corresponding centre position with respect to the input is 1. If there is at least one non-zero pixel that <strong>does not</strong> touch a structuring pixel that is 1, then the output is 0. </p> <p>In terms of the rectangular structuring element, you need to make sure that every pixel in the structuring element is touching a non-zero pixel in your image for a pixel neighbourhood. If it isn't, then the output is 0, else 1. This effectively eliminates small spurious areas of noise and also decreases the area of objects slightly.</p> <p>The size factors in where the larger the rectangle, the more shrinking is performed. The size of the structuring element is a baseline where any objects that are smaller than this rectangular structuring element, you can consider them as being filtered and not appearing in the output. Basically, choosing a 1 x 1 rectangular structuring element is the same as the input image itself because that structuring element fits all pixels inside it as the pixel is the smallest representation of information possible in an image.</p> <h1>Dilation</h1> <p>Dilation is the opposite of erosion. If there is at least one non-zero pixel that touches a pixel in the structuring element that is 1, then the output is 1, else the output is 0. You can think of this as slightly enlarging object areas and making small islands bigger.</p> <p>The implications with size here is that the larger the structuring element, the larger the areas of the objects will be and the larger the isolated islands become.</p> <hr> <p>What you're doing is an erosion first followed by a dilation. This is what is known as an <strong>opening</strong> operation. The purpose of this operation is to remove small islands of noise while (trying to) maintain the areas of the larger objects in your image. The erosion removes those islands while the dilation grows back the larger objects to their original sizes.</p> <p>You follow this with an erosion again for some reason, which I can't quite understand, but that's ok.</p> <hr> <p>What I would personally do is perform a <strong>closing</strong> operation first which is a dilation followed by an erosion. Closing helps group areas that are close together into a single object. As such, you see that there are some larger areas that are close to each other that should probably be joined before we do anything else. As such, I would do a closing first, then do an <strong>opening</strong> after so that we can remove the isolated noisy areas. Take note that I'm going to make the closing structuring element size <strong>larger</strong> as I want to make sure I get nearby pixels and the opening structuring element size <strong>smaller</strong> so that I don't want to mistakenly remove any of the larger areas.</p> <p>Once you do this, I would mask out any extra information with the original image so that you leave the larger areas intact while the small islands go away.</p> <p>Instead of chaining an erosion followed by a dilation, or a dilation followed by an erosion, use <a href="http://docs.opencv.org/modules/imgproc/doc/filtering.html#morphologyex" rel="nofollow"><code>cv2.morphologyEx</code></a>, where you can specify <code>MORPH_OPEN</code> and <code>MORPH_CLOSE</code> as the flags.</p> <p>As such, I would personally do this, assuming your image is called <code>spots.png</code>:</p> <pre><code>import cv2 import numpy as np img = cv2.imread('spots.png') img_bw = 255*(cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) &gt; 5).astype('uint8') se1 = cv2.getStructuringElement(cv2.MORPH_RECT, (5,5)) se2 = cv2.getStructuringElement(cv2.MORPH_RECT, (2,2)) mask = cv2.morphologyEx(img_bw, cv2.MORPH_CLOSE, se1) mask = cv2.morphologyEx(mask, cv2.MORPH_OPEN, se2) mask = np.dstack([mask, mask, mask]) / 255 out = img * mask cv2.imshow('Output', out) cv2.waitKey(0) cv2.destroyAllWindows() cv2.imwrite('output.png', out) </code></pre> <p>The above code is pretty self-explanatory. First, I read in the image and then I convert the image to grayscale and threshold with an intensity of 5 to create a mask of what is considered object pixels. This is a rather clean image and so anything larger than 5 seems to have worked. For the morphology routines, I need to convert the image to <code>uint8</code> and scale the mask to 255. Next, we create two structuring elements - one that is a 5 x 5 rectangle for the closing operation and another that is 2 x 2 for the opening operation. I run <code>cv2.morphologyEx</code> twice for the opening and closing operations respectively on the thresholded image.</p> <p>Once I do that, I stack the mask so that it becomes a 3D matrix and divide by 255 so that it becomes a mask of <code>[0,1]</code> and then we multiply this mask with the original image so that we can grab the original pixels of the image back and maintaining what is considered a true object from the mask output.</p> <p>The rest is just for illustration. I show the image in a window, and I also save the image to a file called <code>output.png</code>, and its purpose is to show you what the image looks like in this post.</p> <p>I get this:</p> <p><img src="http://i.stack.imgur.com/1JXvU.png" alt="enter image description here"></p> <p>Bear in mind that it isn't perfect, but it's much better than how you had it before. You'll have to play around with the structuring element sizes to get something that you consider as a good output, but this is certainly enough to get you started. Good luck!</p> <p>This tip was originally posted on <a href="http://stackoverflow.com/questions/30369031/Remove%20spurious%20small%20islands%20of%20noise%20in%20an%20image%20-%20Python%20OpenCV/30380543">Stack Overflow</a>.</p>

Get New Tutorials Delivered to Your Inbox

New tutorials will be sent to your Inbox once a week.

comments powered by Disqus