gpt4 book ai didi

python - 使用opencv python从二值图像中去除小白点

转载 作者:行者123 更新时间:2023-12-02 17:50:46 26 4
gpt4 key购买 nike

我有一个二值图像,我想使用opencv python从图像中删除小白点。您可以在这里引用我的问题enter link description here

我的原图是

enter image description here

我希望输出图像为:

enter image description 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

关于python - 使用opencv python从二值图像中去除小白点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57283802/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com