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

2024-09-27 09:24:16 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个二进制图像,我想用opencv从图像中删除小白点Python。你这里可以参考我的问题enter link description here

我最初的形象是

enter image description here

我希望输出图像为:

enter image description here


Tags: 图像here二进制linkdescriptionopencventer形象
3条回答

你可以使用“关闭”功能-腐蚀后扩张。它不需要模糊功能。在

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()

您可以简单地使用图像平滑技术(如高斯模糊等)来去除图像中的噪声,然后进行如下二值化阈值处理:

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中的不同图像平滑/模糊技术。在

使用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

相关问题 更多 >

    热门问题