2016-09-25 111 views
0

我試圖使用dialation和ertion腐蝕和膨脹返回白色

例如,像這樣:

dialated = cv2.dilate(edgesCopy, cv2.getStructuringElement(cv2.MORPH_CROSS,(3,3)), iterations = 1) 

的輸入是僅具有在0和255值的UINT8圖像,出來的

threshold, thresholdedEdges = cv2.threshold(edges, 220, 255, cv2.THRESH_BINARY_INV); 

然而,輸出只是一個白色的圖像。我不明白原因。

整個代碼是

imageSize = img.shape 
    if len(imageSize) != 2:#color 
     print "got a color image - quitting" 
     return 

    cv2.imshow("im1", img) 
    cv2.moveWindow("im1", 60, 50) 

    gaussianBlur = cv2.GaussianBlur(img, (5, 5), 0) 
    # cv2.imshow("gaussianBlur", gaussianBlur) 
    # cv2.moveWindow("gaussianBlur", 260, 50) 

    medianBlur = cv2.medianBlur(gaussianBlur, 5) 
    # cv2.imshow("medianBlur", medianBlur) 
    # cv2.moveWindow("medianBlur", 460, 50) 

    minGradientValueThreshold = 225 
    maxGradientValueThreshold = 150 
    edges = cv2.Canny(medianBlur, minGradientValueThreshold, maxGradientValueThreshold) 
    cv2.imshow("edges", edges) 
    cv2.moveWindow("edges", 660, 50) 

    # Threshold. 
    # Set values equal to or above 220 to 0. 
    # Set values below 220 to 255. 

    threshold, thresholdedEdges = cv2.threshold(edges, 220, 1, cv2.THRESH_BINARY_INV); 

    edgesCopy = thresholdedEdges.copy() 

    #close the edges before floodfilling, to avoid filing the background 
    # closing = cv2.morphologyEx(floodFilledImage, cv2.MORPH_CLOSE, cv2.getStructuringElement(cv2.MORPH_RECT,(5,5))) DOESN'T WORK 
    dialated = cv2.dilate(edgesCopy, cv2.getStructuringElement(cv2.MORPH_CROSS,(3,3)), iterations = 1) 

    cv2.imshow("dialated", dialated) 
    cv2.moveWindow("dialated", 60, 250) 

    eroded = cv2.erode(dialated, cv2.getStructuringElement(cv2.MORPH_CROSS,(3,3)), iterations = 1) 

    closing = eroded 

    cv2.imshow("closing", closing) 
    cv2.moveWindow("closing", 60, 250) 
+0

請問爲什麼用不同的濾鏡將圖像模糊兩次? – PSchn

回答

2

Canny邊緣檢測的結果與厚度1.您正在閾值化該邊緣(其中不需要的方式)與閾值設定cv2.THRESH_BINARY_INV的二進制邊緣圖象,這意味着閾值結果得到值1,其中像素在下面的閾值和0當上面。這種閾值處理的結果自然就是帶有黑線的幾乎是白色的圖像 - >您實際上只是將Canny邊緣檢測器的結果反轉。擴大這樣的圖像最終導致完全白色的圖像(無論輸入圖像實際是什麼)。

我建議你跳過閾值步驟!

如果你想要做的閾值然而,使用THRESH_BINARY並設置maxval到255我也認爲應該有cv2.waitKey()函數調用後各cv2.imshow()(至少在我的情況下,它不以其他方式顯示任何東西)。