我有带有“穿孔”的BW图像。穿孔水平可以不同
是否有任何“标准”方法可以完全用黑色填充形状以使其更相似?
首选枕头和opencv,但imagemagick也可以。
最佳答案
您可以使用image morphology(i.e: closing)来实现。
import cv2
import numpy as np
if __name__ == '__main__':
# read image
image = cv2.imread('image.png',cv2.IMREAD_UNCHANGED)
# convert image to gray
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
# ensure only black and white pixels exist
ret,binary = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY)
# morphology works with white forground
binary = cv2.bitwise_not(binary)
# get kernel for morphology
kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (3,3))
# number of iterations depends on the type of image you're providing
binary = cv2.morphologyEx(binary, cv2.MORPH_CLOSE, kernel, iterations=3)
# get black foreground
binary = cv2.bitwise_not(binary)
cv2.imshow('image', binary)
cv2.waitKey(0)
cv2.destroyAllWindows()
关于python - 使用python库在图像中填充穿孔的形状?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/50889958/