Filling holes in image with OpenCV or Skimage
<p>I m trying to fill holes for a chessboard for stereo application. The chessboard is at micro scale thus it is complicated to avoid dust... as you can see :</p>

<p><a href="http://i.stack.imgur.com/mCOFl.png" rel="nofollow"><img src="http://i.stack.imgur.com/mCOFl.png" alt="enter image description here"></a></p>

<p>Thus, the corners detection is impossible. I tried with SciPy's binary_fill_holes or similar approaches but i have a full black image, i dont understand.</p>

<p>You can use following function in order to remove the holes, by replacing the color of each pixel with a maximum colors of it's environment pixels:  </p>

<pre><code>import numpy as np
import cv2

def remove_noise(gray, num):
    Y, X = gray.shape
    nearest_neigbours = [[
        np.argmax(
            np.bincount(
                gray[max(i - num, 0):min(i + num, Y), max(j - num, 0):min(j + num, X)].ravel()))
        for j in range(X)] for i in range(Y)]
    result = np.array(nearest_neigbours, dtype=np.uint8)
    cv2.imwrite('result2.jpg', result)
    return result
</code></pre>

<p>Demo:</p>

<pre><code>img = cv2.imread('mCOFl.png')
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

remove_noise(gray, 10)
</code></pre>

<p>Input image:</p>

<p><a href="http://i.stack.imgur.com/AAypM.png" rel="nofollow"><img src="http://i.stack.imgur.com/AAypM.png" alt="enter image description here"></a></p>

<p>Out put:</p>

<p><a href="http://i.stack.imgur.com/PYttM.jpg" rel="nofollow"><img src="http://i.stack.imgur.com/PYttM.jpg" alt="enter image description here"></a></p>

<p>Note: Since this function replace the color of corner pixels too, you can sue <code>cv2.goodFeaturesToTrack</code> function to find the corners and restrict the denoising for that pixels </p>

<pre><code>corners = cv2.goodFeaturesToTrack(gray, 100, 0.01, 30)
corners = np.squeeze(np.int0(corners))
</code></pre>

python opencv numpy scipy skimage