Answers to Filling holes in image with OpenCV or Skimage ( 2 )

  1. 2016-09-24 16:09

    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:

    import numpy as np
    import cv2
    def remove_noise(gray, num):
        Y, X = gray.shape
        nearest_neigbours = [[
                    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


    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))
  2. 2016-09-25 14:09

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

Leave a reply to - Filling holes in image with OpenCV or Skimage

◀ Go back