消除图像中的杂物

时间:2019-05-03 22:06:46

标签: python opencv

我正在寻找一个功能/过滤器/算法来消除对象轮廓内的噪声。

例如:

输入图片:

enter image description here

我想得到:

enter image description here

P.S。该对象可能具有其他形式。

这个想法是:

img_file = 'third.png'
img = cv2.imread(img_file, cv2.IMREAD_COLOR)
img = cv2.blur(img, (5, 5))

hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
h, s, v = cv2.split(hsv)
thresh0 = cv2.adaptiveThreshold(s, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY_INV, 11, 2)

img_2d = color.rgb2gray(img)
print(img_2d.shape)
print(thresh0.shape)
diff = cv2.absdiff(img_2d, thresh0)
mask = cv2.cvtColor(diff, cv2.COLOR_BGR2GRAY)
i_mask = mask > 10
canvas = np.zeros_like(thresh0, np.uint8)
canvas[i_mask] = thresh0[i_mask]

cv2.imshow('Result', canvas)
cv2.imwrite("result.jpg", canvas)
cv2.waitKey(0)

但是出现以下错误:

cv2.error: OpenCV(3.4.2) C:\projects\opencv-python\opencv\modules\core\src\arithm.cpp:683: 
    error: (-5:Bad argument) When the input arrays in 
    add/subtract/multiply/divide functions have different types, the 
    output array type must be explicitly specified in function 
    'cv::arithm_op'

thresh0具有以下视图:

enter image description here

也许如果我将原始图像与thresh0进行比较,我会得到干净的图像。顺便说一句,它还会破坏底部。而且不能清除所有噪音。

0 个答案:

没有答案
相关问题