2017-05-03 78 views
2

我是新來的opencv。我有多個圖像。示例圖像之一,如左下角所示。基本上我想分離背景和前景,以便邊緣清晰,並且可以正確檢測輪廓。OpenCV python郵票過濾器photoshop

我已經嘗試了許多過濾器,當然使用各種參數的閾值。

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

+0

它看起來像一個二值化,並可能侵蝕和擴張。顯示原始的原始圖像也 –

+0

嗨@AnderBiguri剛剛添加了原始圖像。 – VK321

+0

@AnderBiguri ..有什麼幫助? – VK321

回答

0

它可以幫助自己添加幾個滑塊,高斯模糊,閾值濾波的,你可以得到相當不錯的結果:

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

隨意添加其他過濾器的混合和實驗與滑塊。

+0

哦@GeorgeProfenza你是配偶。非常感謝您花時間和這樣做。滑塊是非常酷的想法,我沒有想到。主要思想是保持上下骨的分離。我確定要在你的代碼上嘗試一些實驗,並會讓你知道如果我得到結果。 – VK321

+0

甜!如果答案有幫助,可以隨意投票/標記爲你認爲合適的;)享受探索過濾器的樂趣。期待看到什麼工作(可能希望通過[形態過濾器](http://docs.opencv.org/3.0-beta/doc/py_tutorials/py_imgproc/py_morphological_ops/py_morphological_ops.html)混合(你已經侵蝕了/擴張,也許開/關也可能有幫助) –