OpenCV Python戳过滤器Photoshop [英] OpenCV python Stamp filter photoshop

查看:118
本文介绍了OpenCV Python戳过滤器Photoshop的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是opencv的新手.我有多张图片.样本图像之一,如下左上角所示.基本上,我想将背景和前景分开,以便边缘清晰,并且可以正确检测轮廓.

I am new to opencv. I have multiple images. One of sample image as shown below at top left corner. Basically I want to separate background and foreground so that edges are clear and I can detect contours properly.

我尝试了许多过滤器,当然也使用各种参数设置了阈值.

I have tried many filter and of course thresholds using various parameters.

最后,当我在photoshop滤镜库上查看时,我注意到一个名为Stamp的滤镜,它给了我想要的结果(右上角).它使边缘更清晰,我想对柔和的角使用一定程度的模糊.

Finally when I was looking on photoshop filters gallery I noticed a filter called Stamp which is giving me desired result(top-right corner). It makes edges clear and I guess use some amount of blur to soft corners.

我不确定如何使用python CV2获得与photoshop的图章过滤器相同的操作?

I am not sure how I can obtain same operation as photoshop's stamp filter using python CV2?

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

Any help or suggestions will be grateful.

原始原始图像

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

输出:

推荐答案

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

It might help to add yourself a couple of sliders for Gaussian Blur and Threshold filtering and you can get pretty decent results:

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

and here's the basic snippet I used to generate it:

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

随时向混合添加其他过滤器,并尝试使用滑块.

Feel free to add other filters to the mix and experiment with sliders.

这篇关于OpenCV Python戳过滤器Photoshop的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆