OpenCV Python:印章滤镜Photoshop

3
我是opencv的新手。我有多张图片。其中一张样例图片如下所示,位于左上角。基本上我想要分离背景和前景,以便边缘清晰,并且可以正确检测轮廓。
我已经尝试了许多滤波器和当然使用了各种参数的阈值。

enter image description here

最后,当我在Photoshop滤镜库中查找时,我注意到一个名为“印章”的滤镜可以给我所需的结果(右上角)。它使边缘清晰,我猜使用了一定程度的模糊来软化角落。

我不确定如何使用Python CV2获得与Photoshop的印章滤镜相同的操作?

任何帮助或建议都将不胜感激。

原始未经处理的图像

enter image description here

尝试 1:-- 代码

import cv2
import numpy as np
from matplotlib import pyplot as plt

input_img = cv2.imread('images/Tas/t3.bmp')
desired_img = cv2.imread('images/stamp.jpg')

# gray scale
gray = cv2.cvtColor(input_img, cv2.COLOR_BGR2GRAY)

kernel = np.ones((3,3),np.uint8)

thresh1 = cv2.threshold(input_img,80,255,cv2.THRESH_BINARY)[1]
erosion1 = cv2.erode(thresh1,kernel,iterations = 1)
dilation1 = cv2.dilate(erosion1,kernel,iterations = 1)

thresh2 = cv2.threshold(input_img,120,255,cv2.THRESH_BINARY)[1]
erosion2 = cv2.erode(thresh2,kernel,iterations = 1)
dilation2 = cv2.dilate(erosion2,kernel,iterations = 1)

titles = ['Original', 'Desired','thresh1', 'erosion1','dilation1','thresh2','erosion2','dilation2']
images = [input_img, desired_img, thresh1, erosion1,dilation1, thresh2,erosion2, dilation2]
for i in xrange(8):
  plt.subplot(2,4,i+1),plt.imshow(images[i])
  plt.title(titles[i])
  plt.xticks([]),plt.yticks([])

plt.show()

输出:

enter image description here


嗨@AnderBiguri,我刚刚添加了原始图像。 - VK321
@AnderBiguri .. 你能帮忙吗? - VK321
嗨@AnderBiguri。是的,我尝试过了,但没有得到所需的输出。如果您想看,我可以附上图片。 - VK321
是的,在stackoverflow上展示你尝试过的内容是最基本的要求。 - Ander Biguri
看起来你只需要调整阈值、腐蚀和膨胀核。我建议你坚持使用Otsus阈值水平,然后只需尝试不同的腐蚀和膨胀尺寸即可。 - Ander Biguri
显示剩余4条评论
1个回答

3

如果你在高斯模糊和阈值过滤方面添加了一些滑块,你就可以得到相当不错的效果:

fake "photoshop stamp" filter with gaussian blur + threshold

这里是我用来生成它的基本代码片段:

import numpy as np
import cv2
import cv2.cv as cv
from matplotlib import pyplot as plt

# slider callbacks
def printThreshold(x):
    print "threshold",x
def printGaussianBlur(x):
    print "gaussian blur kernel size",x
# make a window to add sliders/preview to
cv2.namedWindow('processed')
#make some sliders
cv2.createTrackbar('threshold','processed',60,255,printThreshold)
cv2.createTrackbar('gaussian blur','processed',3,10,printGaussianBlur)
# load image
img = cv2.imread('cQMgT.png',0)
# continously process for quick feedback
while 1:
    # exit on ESC key
    k = cv2.waitKey(1) & 0xFF
    if k == 27:
        break

    # Gaussian Blur ( x2 +1 = odd number for kernel size)
    kernelSize = ((cv2.getTrackbarPos('gaussian blur','processed') * 2) + 1)
    blur = cv2.GaussianBlur(img,(kernelSize,kernelSize),0)
    # Threshold
    ret,thresh = cv2.threshold(blur,cv2.getTrackbarPos('threshold','processed',),255,0)
    # show result
    cv2.imshow('processed ',thresh)

# exit
cv2.destroyAllWindows()

随意添加其他过滤器并尝试使用滑块进行实验。


1
哦,@GeorgeProfenza,你真是个好伙伴。非常感谢你抽出时间来做这件事。滑块是一个很酷的想法,我没有想到过。主要思路是保持上下骨骼的分离。我一定会在你的代码上尝试一些实验,并在有结果时告诉你。 - VK321
太好了!如果这个答案有帮助,请随意投票/标记,如你所愿;)玩得开心,探索过滤器。期待看到哪些方法有效(也许可以加入形态学滤波器(你已经使用了腐蚀/膨胀,也许开/闭操作也会有所帮助)。 - George Profenza

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接