gpt4 book ai didi

python - 将 C++ OpenCV 转换为 Python

转载 作者:太空宇宙 更新时间:2023-11-03 22:49:05 24 4
gpt4 key购买 nike

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

在谷歌搜索时,我找到了一个我认为可能有效的解决方案: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);

到这条 python 行

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

问题

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

最佳答案

看起来您遇到问题是因为波形符运算符对图像中的所有像素应用按位非 运算。看看这三行 C++ 代码:

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

这些是你得到的图像:

enter image description here enter image description here

快速解决方案:如果您想正确应用阈值,则要么

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

关于python - 将 C++ OpenCV 转换为 Python,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42453892/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com