2017-02-25 126 views
1

我想刪除我的圖像水平和垂直線條看起來像這樣:enter image description here轉換C++ OpenCV的Python的

雖然google搜索,我發現,我認爲可能的工作的解決方案:Extract horizontal and vertical lines by using morphological operations,但是,它是在C++ 。

我已經嘗試將解決方案轉換爲Python,但我沒有得到相同的結果。爲了保持圖像一樣,我已經盡我的Python版本在該解決方案中使用的相同的圖像:

下面是我的Python版本有關C++版本中的註釋:

img = cv2.imread(path) 
    img=cv2.cvtColor(img,cv2.COLOR_BGR2GRAY) 

    #// Apply adaptiveThreshold at the bitwise_not of gray, notice the ~ symbol 
    #Mat bw; 
    #adaptiveThreshold(~gray, bw, 255, CV_ADAPTIVE_THRESH_MEAN_C, THRESH_BINARY, 15, -2); 
    th2 = cv2.adaptiveThreshold(img,255, cv2.ADAPTIVE_THRESH_MEAN_C,cv2.THRESH_BINARY,15,-2) 
    cv2.imwrite("th2.jpg", th2) 

    #Mat horizontal = bw.clone(); 
    #Mat vertical = bw.clone(); 
    horizontal = th2 
    vertical = th2 

    #int horizontalsize = horizontal.cols/30; 
    rows,cols = horizontal.shape 
    horizontalsize = cols/30 

    #// Create structure element for extracting horizontal lines through morphology operations 
    #Mat horizontalStructure = getStructuringElement(MORPH_RECT, Size(horizontalsize,1)); 
    horizontalStructure = cv2.getStructuringElement(cv2.MORPH_RECT, (horizontalsize,1)) 

    #// Apply morphology operations 
    #erode(horizontal, horizontal, horizontalStructure, Point(-1, -1)); 
    #dilate(horizontal, horizontal, horizontalStructure, Point(-1, -1)); 
    #// Show extracted horizontal lines 
    #imshow("horizontal", horizontal); 
    horizontal = cv2.erode(horizontal, horizontalStructure, (-1, -1)) 
    horizontal = cv2.dilate(horizontal, horizontalStructure, (-1, -1)) 
    cv2.imwrite("horizontal.jpg", horizontal) 

    #// Specify size on vertical axis 
    #int verticalsize = vertical.rows/30; 
    verticalsize = rows/30 

    #// Create structure element for extracting vertical lines through morphology operations 
    #Mat verticalStructure = getStructuringElement(MORPH_RECT, Size(1,verticalsize)); 
    verticalStructure = cv2.getStructuringElement(cv2.MORPH_RECT, (1, verticalsize)) 

    #// Apply morphology operations 
    #erode(vertical, vertical, verticalStructure, Point(-1, -1)); 
    #dilate(vertical, vertical, verticalStructure, Point(-1, -1)); 
    #// Show extracted vertical lines 
    #imshow("vertical", vertical); 

    vertical = cv2.erode(vertical, verticalStructure, (-1, -1)) 
    vertical = cv2.dilate(vertical, verticalStructure, (-1, -1)) 
    cv2.imwrite("vertical.jpg", vertical) 

    #// Inverse vertical image 
    #bitwise_not(vertical, vertical); 
    #imshow("vertical_bit", vertical); 

    vertical = cv2.bitwise_not(vertical) 
    cv2.imwrite("vertical_bit.jpg", vertical) 

    #// Extract edges and smooth image according to the logic 
    #// 1. extract edges 
    #// 2. dilate(edges) 
    #// 3. src.copyTo(smooth) 
    #// 4. blur smooth img 
    #// 5. smooth.copyTo(src, edges) 


    #step1 
    #Mat edges; 
    #adaptiveThreshold(vertical, edges, 255, CV_ADAPTIVE_THRESH_MEAN_C, THRESH_BINARY, 3, -2); 
    #imshow("edges", edges); 
    edges = cv2.adaptiveThreshold(vertical,255, cv2.ADAPTIVE_THRESH_MEAN_C,cv2.THRESH_BINARY,3,-2) 
    cv2.imwrite("edges.jpg", edges) 

    #step2 
    #Mat kernel = Mat::ones(2, 2, CV_8UC1); 
    #dilate(edges, edges, kernel); 
    #imshow("dilate", edges); 
    kernel = np.ones((2, 2), dtype = "uint8") 
    dilated = cv2.dilate(edges, kernel) 
    cv2.imwrite("dialted.jpg", dilated) 

    # step3 
    #Mat smooth; 
    #vertical.copyTo(smooth); 
    smooth = vertical.copy() 

    #step 4 
    #blur(smooth, smooth, Size(2, 2)); 
    smooth = cv2.blur(smooth, (2,2)) 

    #step 5 
    #smooth.copyTo(vertical, edges); 
    (rows, cols) = np.where(edges != 0) 
    vertical[rows, cols] = smooth[rows, cols] 

    // Show final result 
    #imshow("smooth", vertical); 
    cv2.imwrite("smooth.jpg", vertical) 

當我在enter image description here

運行此我回來

enter image description here

這是沒有結果的搜索解決方案上面鏈接得到。

我相信這個問題可能是在我的轉換本的C++行:

// Apply adaptiveThreshold at the bitwise_not of gray, notice the ~ symbol 
    Mat bw; 
    adaptiveThreshold(~gray, bw, 255, CV_ADAPTIVE_THRESH_MEAN_C, THRESH_BINARY, 15, -2); 

到這條巨蟒線

th2 = cv2.adaptiveThreshold(img,255, cv2.ADAPTIVE_THRESH_MEAN_C,cv2.THRESH_BINARY,15,-2) 

問題

我怎樣才能最好地轉換成C++代碼鏈接到Python的解決方案?

+0

這是唯一不同的行......當您調用自適應閾值方法時。嘗試實際反轉輸入圖像。您可以使用與C++中相同的操作符。使用'〜'。 – rayryeng

+0

@rayryeng謝謝。這適用於解決方案中提到的圖像,但是,它不適合我的圖像。我根本沒有得到好的結果。我想知道你能不能看看?我一直在一個簡單的要點保持一切https://gist.github.com/anonymous/058999a32bde41aba5ccce754fd3cc05 – Anthony

+0

這是最好問一個單獨的問題http://stackoverflow.com/questions/42461211/how-to-remove-horizo​​ntal - 從圖像垂直線 – Anthony

回答

1

看起來像你有這個問題,因爲波形符號運算符應用按位非操作的圖像中的所有像素。看看這三條線的C++代碼:

cv::Mat img = imread("smiley.png", IMREAD_GRAYSCALE); 
imshow("Image0", img); 
imshow("Image1", ~img); // tilde 

這些圖像你:

enter image description hereenter image description here

快速的解決方案:如果你要正確地應用閾值那麼無論

  • 向輸入數組應用按位求反,或使用'THRESH_BINARY_IN'或'
  • ' V'而不是'THRESH_BINARY'
+0

謝謝。這適用於解決方案中提到的圖像,但是,它不適合我的圖像。我根本沒有得到好的結果。我想知道你能不能看看?我將所有內容都保存在一個簡單的要點gist.github.com/anonymous/058999a32bde41aba5ccce754fd3cc05 – Anthony

+0

最好問一個單獨的問題http://stackoverflow.com/questions/42461211/how-to-remove-horizo​​ntal-and-vertical -lines從 - 一個圖像 – Anthony