Filling holes in image with OpenCV or Skimage

Question:

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 :

enter image description here

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.

Asked By: user3601754

||

Answers:

Here is a function that replaces the color of each pixel with the color that majority of its neighbor pixels have.

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

Demo:

img = cv2.imread('mCOFl.png')
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

remove_noise(gray, 10)

Input image:

enter image description here

Out put:

enter image description here

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

corners = cv2.goodFeaturesToTrack(gray, 100, 0.01, 30)
corners = np.squeeze(np.int0(corners))
Answered By: Mazdak

You can use morphology: dilate, and then erode with same kernel size.

Answered By: Andrey Smorodov

A faster, more accurate way is to use skimage.morphology.remove_small_objects docs

im = imread('a.png',cv2.IMREAD_GRAYSCALE)
im = im ==255
from skimage import morphology
cleaned = morphology.remove_small_objects(im, 200)

enter image description here

Answered By: yazan sayed