我想删除我的图像水平和垂直线条看起来像这样:转换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)
运行此我回来
这是没有结果的搜索解决方案上面链接得到。
我相信这个问题可能是在我的转换本的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
@rayryeng谢谢。这适用于解决方案中提到的图像,但是,它不适合我的图像。我根本没有得到好的结果。我想知道你能不能看看?我一直在一个简单的要点保持一切https://gist.github.com/anonymous/058999a32bde41aba5ccce754fd3cc05 – Anthony
这是最好问一个单独的问题http://stackoverflow.com/questions/42461211/how-to-remove-horizontal - 从图像垂直线 – Anthony