【问题标题】:Converting C++ OpenCV to Python将 C++ OpenCV 转换为 Python
【发布时间】:2017-07-16 04:27:37
【问题描述】:

我正在尝试从我的图像中删除水平和垂直线条,如下所示:

在谷歌搜索时,我发现了一个我认为可能有效的解决方案:Extract horizontal and vertical lines by using morphological operations,但是,它是用 C++ 编写的。

我尝试将解决方案转换为 Python,但没有得到相同的结果。为了保持图像相同,我在该解决方案中使用的同一图像上尝试了我的 python 版本:

下面是我的python版本和cmets中的相关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)

当我在 上运行它时

我回来了

这不是上面链接的解决方案得到的结果。

我相信问题可能出在我对这条 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?

【问题讨论】:

  • 这是唯一不同的线......当您调用自适应阈值方法时。尝试实际反转输入图像。您可以使用与 C++ 中相同的运算符。使用~
  • @rayryeng 谢谢。这适用于解决方案中提到的图像,但是,它不适用于我的图像。我根本没有得到好的结果。我想知道你能不能看一下?我将所有内容都保存在一个简单的要点中gist.github.com/anonymous/058999a32bde41aba5ccce754fd3cc05

标签: python c++ opencv image-processing


【解决方案1】:

看起来您遇到了问题,因为波浪号运算符将 bitwise NOT 运算应用于图像中的所有像素。看看这三行 C++ 代码:

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

这些是你得到的图像:

快速解决方案:如果您想正确应用阈值处理,那么

  • 对输入数组应用按位求反,或
  • 使用“THRESH_BINARY_INV”而不是“THRESH_BINARY”

【讨论】:

  • 谢谢。这适用于解决方案中提到的图像,但是,它不适用于我的图像。我根本没有得到好的结果。我想知道你能不能看一下?我将所有内容都保存在一个简单的要点中 gist.github.com/anonymous/058999a32bde41aba5ccce754fd3cc05
猜你喜欢
  • 2012-11-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-04-22
  • 2012-08-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多