【发布时间】:2018-11-26 03:33:14
【问题描述】:
【问题讨论】:
标签: python opencv image-processing imagemagick pillow
【问题讨论】:
标签: python opencv image-processing imagemagick pillow
您可以使用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()
【讨论】:
cv2.MORPH_CLOSE。