将C ++ OpenCV转换为Python [英] Converting C++ OpenCV to Python

查看:226
本文介绍了将C ++ OpenCV转换为Python的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从我的图片中删除看起来像这样的水平线和垂直线:



谷歌搜索时我找到了我认为可行的解决方案:



我回来了





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



我相信问题可能在于我转换此c ++行:

  //以bitwise_not为灰色应用adaptiveThreshold,注意〜符号
Mat bw;
adaptiveThreshold(~gray,bw,255,CV_ADAPTIVE_THRESH_MEAN_C,THRESH_BINARY,15,-2);

到此python线

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

问题



如何才能最好地将链接解决方案中的C ++代码转换为Python?

解决方案

看起来你有问题,因为代字号运算符将按位NOT 操作应用于所有像素图片。看看这三行C ++代码:

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

以下是您获得的图片:





快速解决方案:如果你想要正确应用阈值然后




  • 将按位否定应用于输入数组,或

  • 使用'THRESH_BINARY_INV '而不是'THRESH_BINARY'


I am trying to remove horizontal and vertical lines from my images that look like this:

While googling I found a solution that I believe might work: Extract horizontal and vertical lines by using morphological operations, however, it is in C++.

I have tried converting the solution to Python, but I don't get the same results. In order to keep the image same, I've tried my python version on the same image used in that solution:

Below is my python version with relevant c++ version in the comments:

    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)

When I run this on

I get back

which is not the result that the solution linked above gets.

I belive the problem might be in my conversion of this c++ line:

// 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);

to this python line

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

Question

How can I best convert the C++ code in the linked solution to Python?

解决方案

Looks like that you have the problem because of tilde operator that applies bitwise NOT operation to all pixels in the image. Look at this three lines of C++ code:

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

These are the images you get:

Quick solution: if you want to apply thresholding correctly then either

  • apply bitwise negation to the input array, or
  • use 'THRESH_BINARY_INV' instead of 'THRESH_BINARY'

这篇关于将C ++ OpenCV转换为Python的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆