gpt4 book ai didi

python - OpenCV python Stamp 过滤器 photoshop

转载 作者:太空宇宙 更新时间:2023-11-03 21:43:01 25 4
gpt4 key购买 nike

我是 opencv 的新手。我有多个图像。示例图像之一如下图左上角所示。基本上我想分开背景和前景,这样边缘就清晰了,我就可以正确地检测到轮廓。

我尝试了很多过滤器,当然还有使用各种参数的阈值。

enter image description here

最后,当我查看 photoshop 滤镜库时,我注意到一个名为 Stamp 的滤镜,它给我想要的结果(右上角)。它使边缘清晰,我想对软角使用一些模糊。

我不确定如何使用 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

最佳答案

为自己添加几个用于高斯模糊和阈值过滤的 slider 可能会有所帮助,您可以获得相当不错的结果:

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()

随意添加其他过滤器并尝试使用 slider 。

关于python - OpenCV python Stamp 过滤器 photoshop,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43758096/

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