使用opencv python从二进制图像中删除小白点

时间:2019-07-31 05:28:27

标签: python opencv image-preprocessing

我有一个二进制图像,我想使用opencv python从图像中去除小的白点。您可以在此处enter link description here

参阅我的问题

我的原始图片是

enter image description here

我希望输出图像为:

enter image description here

3 个答案:

答案 0 :(得分:2)

这似乎可以在Python Opencv中使用连接的组件来工作。

enter image description here

#!/bin/python3.7

import cv2
import numpy as np

src = cv2.imread('img.png', cv2.IMREAD_GRAYSCALE)

# convert to binary by thresholding
ret, binary_map = cv2.threshold(src,127,255,0)

# do connected components processing
nlabels, labels, stats, centroids = cv2.connectedComponentsWithStats(binary_map, None, None, None, 8, cv2.CV_32S)

#get CC_STAT_AREA component as stats[label, COLUMN] 
areas = stats[1:,cv2.CC_STAT_AREA]

result = np.zeros((labels.shape), np.uint8)

for i in range(0, nlabels - 1):
    if areas[i] >= 100:   #keep
        result[labels == i + 1] = 255

cv2.imshow("Binary", binary_map)
cv2.imshow("Result", result)
cv2.waitKey(0)
cv2.destroyAllWindows()

cv2.imwrite("Filterd_result.png, result)


enter image description here

请参见here

答案 1 :(得分:1)

您可以简单地使用诸如高斯模糊之类的图像平滑技术从图像中去除噪点,然后进行如下所示的二进制阈值处理:

img = cv2.imread("your-image.png",0)
blur = cv2.GaussianBlur(img,(13,13),0)
thresh = cv2.threshold(blur, 100, 255, cv2.THRESH_BINARY)[1]

cv2.imshow('original', img)
cv2.imshow('output', thresh)
cv2.waitKey(0)
cv2.destroyAllWinsdows()

输出:

enter image description here

here中了解不同的图像平滑/模糊技术。

答案 2 :(得分:0)

您可以使用“关闭”功能-腐蚀后再膨胀。不需要模糊功能。

import cv2 as cv
import numpy as np

img = cv.imread('original',0)
kernel = np.ones((5,5),np.uint8)

opening = cv2.morphologyEx(img, cv2.MORPH_OPEN, kernel)

cv2.imshow('original', img)
cv2.imshow('output', opening)
cv2.waitKey(0)
cv2.destroyAllWindows()