Python – OpenCV python stamp filter photoshop

OpenCV python stamp filter photoshop… here is a solution to the problem.

OpenCV python stamp filter photoshop

I’m new to OpenCV. I have multiple images. One example image is shown in the upper-left corner of the following figure. Basically I want to separate the background and foreground so that the edges are clear and I can detect the outline correctly.

I tried a lot of filters and of course thresholds that used various parameters.

enter image description here

Finally, when I looked at the Photoshop filter gallery, I noticed a filter called Stamp, which gave me the result I wanted (top right corner). It makes the edges clear and I want to use some blur for soft corners.

I’m not sure how to use python CV2 to get the same operation as Photoshop’s stamp filter?

Any help or advice would be appreciated.

Original unmodified image

enter image description here

Try the 1:– code

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

Output:

enter image description here

Solution

It might help to add a couple of sliders for Gaussian blur and threshold filtering to yourself, and you can get pretty good results :

fake "photoshop stamp" filter with gaussian blur + threshold

Here is 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 and experiment with sliders.

Related Problems and Solutions