使用 OpenCV 或 Skimage 填充图像中的孔

use*_*754 5 python opencv numpy scipy scikit-image

我正在尝试填补立体应用棋盘的漏洞。棋盘是微型的,因此避免灰尘很复杂......正如你所看到的:

在此输入图像描述

因此,角点检测是不可能的。我尝试使用 SciPy 的 binary_fill_holes 或类似方法,但我有一个全黑图像,我不明白。

Kas*_*mvd 4

这是一个函数,它将每个像素的颜色替换为其大多数相邻像素所具有的颜色。

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
Run Code Online (Sandbox Code Playgroud)

演示:

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

remove_noise(gray, 10)
Run Code Online (Sandbox Code Playgroud)

输入图像:

在此输入图像描述

输出:

在此输入图像描述

注意:由于该函数也替换了角点像素的颜色,因此您可以使用该cv2.goodFeaturesToTrack函数来查找角点并限制该像素的去噪

corners = cv2.goodFeaturesToTrack(gray, 100, 0.01, 30)
corners = np.squeeze(np.int0(corners))
Run Code Online (Sandbox Code Playgroud)