【发布时间】:2018-02-26 17:38:29
【问题描述】:
使用以下代码,我可以删除图像中的水平线。见下面的结果。
import cv2
from matplotlib import pyplot as plt
img = cv2.imread('image.png',0)
laplacian = cv2.Laplacian(img,cv2.CV_64F)
sobelx = cv2.Sobel(img,cv2.CV_64F,1,0,ksize=5)
plt.subplot(2,2,1),plt.imshow(img,cmap = 'gray')
plt.title('Original'), plt.xticks([]), plt.yticks([])
plt.subplot(2,2,2),plt.imshow(laplacian,cmap = 'gray')
plt.title('Laplacian'), plt.xticks([]), plt.yticks([])
plt.subplot(2,2,3),plt.imshow(sobelx,cmap = 'gray')
plt.title('Sobel X'), plt.xticks([]), plt.yticks([])
plt.show()
结果还不错,不是很完美,但是很好。我想要实现的是showed here。 我正在使用this code。
我的一个问题是:如何在不应用灰色效果的情况下保存Sobel X?作为原始但经过处理..
另外,有没有更好的方法呢?
编辑
对源图像使用以下代码很好。效果很好。
import cv2
import numpy as np
img = cv2.imread("image.png")
img=cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
img = cv2.bitwise_not(img)
th2 = cv2.adaptiveThreshold(img,255, cv2.ADAPTIVE_THRESH_MEAN_C,cv2.THRESH_BINARY,15,-2)
cv2.imshow("th2", th2)
cv2.imwrite("th2.jpg", th2)
cv2.waitKey(0)
cv2.destroyAllWindows()
horizontal = th2
vertical = th2
rows,cols = horizontal.shape
#inverse the image, so that lines are black for masking
horizontal_inv = cv2.bitwise_not(horizontal)
#perform bitwise_and to mask the lines with provided mask
masked_img = cv2.bitwise_and(img, img, mask=horizontal_inv)
#reverse the image back to normal
masked_img_inv = cv2.bitwise_not(masked_img)
cv2.imshow("masked img", masked_img_inv)
cv2.imwrite("result2.jpg", masked_img_inv)
cv2.waitKey(0)
cv2.destroyAllWindows()
horizontalsize = int(cols / 30)
horizontalStructure = cv2.getStructuringElement(cv2.MORPH_RECT, (horizontalsize,1))
horizontal = cv2.erode(horizontal, horizontalStructure, (-1, -1))
horizontal = cv2.dilate(horizontal, horizontalStructure, (-1, -1))
cv2.imshow("horizontal", horizontal)
cv2.imwrite("horizontal.jpg", horizontal)
cv2.waitKey(0)
cv2.destroyAllWindows()
verticalsize = int(rows / 30)
verticalStructure = cv2.getStructuringElement(cv2.MORPH_RECT, (1, verticalsize))
vertical = cv2.erode(vertical, verticalStructure, (-1, -1))
vertical = cv2.dilate(vertical, verticalStructure, (-1, -1))
cv2.imshow("vertical", vertical)
cv2.imwrite("vertical.jpg", vertical)
cv2.waitKey(0)
cv2.destroyAllWindows()
vertical = cv2.bitwise_not(vertical)
cv2.imshow("vertical_bitwise_not", vertical)
cv2.imwrite("vertical_bitwise_not.jpg", vertical)
cv2.waitKey(0)
cv2.destroyAllWindows()
#step1
edges = cv2.adaptiveThreshold(vertical,255, cv2.ADAPTIVE_THRESH_MEAN_C,cv2.THRESH_BINARY,3,-2)
cv2.imshow("edges", edges)
cv2.imwrite("edges.jpg", edges)
cv2.waitKey(0)
cv2.destroyAllWindows()
#step2
kernel = np.ones((2, 2), dtype = "uint8")
dilated = cv2.dilate(edges, kernel)
cv2.imshow("dilated", dilated)
cv2.imwrite("dilated.jpg", dilated)
cv2.waitKey(0)
cv2.destroyAllWindows()
# step3
smooth = vertical.copy()
#step 4
smooth = cv2.blur(smooth, (4,4))
cv2.imshow("smooth", smooth)
cv2.imwrite("smooth.jpg", smooth)
cv2.waitKey(0)
cv2.destroyAllWindows()
#step 5
(rows, cols) = np.where(img == 0)
vertical[rows, cols] = smooth[rows, cols]
cv2.imshow("vertical_final", vertical)
cv2.imwrite("vertical_final.jpg", vertical)
cv2.waitKey(0)
cv2.destroyAllWindows()
但是如果我有这张图片呢?
我尝试执行上面的代码,结果真的很差……
我正在处理的其他图像是这些......
【问题讨论】:
-
你为什么不使用该示例所示的形态学运算?这是形态学运算的完美运用。请参阅我的回答 here 以了解来自
Sobel的值。 -
我知道,但是使用 C++ 代码(事件转换为 Python)给了我一些错误。如果我上面发布的那个不能按我想要的那样工作,我会尝试形态学操作。我看到你擅长 OpenCV,你能给我一个提示吗?除了变形,现在..
-
形态学运算绝对是最好的选择,而且更容易使用。渐变将捕获音符的边缘,这些边缘将与线条一起被删除。此外,Sobel 和相关函数是适用于任何矩阵的通用函数,因此它们并未严格按照图像数据类型进行缩放。您可以移动、获取 Sobel 的绝对值、比例和阈值以对其进行二值化,然后使用它。由于您正在尝试删除水平线,因此您应该使用
Y方向的渐变。请注意,线路上没有XSobel 的回复。 -
那么关注this link应该是个好办法吧?
-
由于您的线条出现在整个图像中,使用 HoughLines 可能会更好,这样您就不会切断文本片段(变形操作可能会发生这种情况)。
标签: python opencv matplotlib image-processing computer-vision