填充对象 - openCV / Python

时间:2017-01-24 20:42:21

标签: python opencv

在基于remove-noise-in-bw-image的脚本中,我尝试填充对象(相当于matlab中的imfill),但没有成功。有人可以解释一下如何这样做。

脚本:

import cv2
import numpy as np

#image can be found in https://i.stack.imgur.com/afCDL.jpg
filename = 'E:/afCDL.jpg'

img = cv2.imread(filename)
cv2.imwrite('E:/img.jpg',img)
cv2.imshow("Original Image", img)
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
cv2.imwrite('E:/gray.jpg',gray)

bi = cv2.bilateralFilter(gray,5,25,25)

cv2.imwrite('E:/bi.jpg',bi)
blur = cv2.GaussianBlur(bi,(25,25),0)
cv2.imwrite('E:/blur.jpg',blur)
dog = blur - bi
cv2.imwrite('E:/DoG.jpg',dog)
cv2.imshow("DOG Image", dog)

kernel = np.ones((3,3),np.uint8)
fill = cv2.morphologyEx(dog, cv2.MORPH_CLOSE, kernel) #Fill holes- DOES NOT WORK
cv2.imshow("Fill", fill)
removeNoise = cv2.morphologyEx(fill, cv2.MORPH_OPEN, kernel) #Remove noise
cv2.imshow("remove Noise ", removeNoise)

cv2.waitKey(0)
cv2.destroyAllWindows()

1 个答案:

答案 0 :(得分:1)

尝试阅读此link。我认为它解决了这个问题。

这篇文章python-equivalent-to-matlab-funciton-imfill-for-grayscale也有一些指示。