我想从图像中删除垂直和水平线 那么这是我的方法
def removeLines(self):
# Convert to THRESH_BINARY
self.retval, self.im = cv2.threshold(self.im, 128, 255, cv2.THRESH_BINARY | cv2.THRESH_OTSU)
chop = 6 # If line length is more than chop . It will set to white
threshold = 200 # pixel's color
lineColor = 150 # if is line. set color 255:white 0:black
(height, width) = self.im.shape
# loop each pixel
for i in xrange(height):
for j in xrange(width):
# if is black point
if self.im[i][j] < threshold:
countWidth = 0
# remove vertical line if <threshold then count+1
for c in range(j, width):
if self.im[i][c] < threshold:
countWidth += 1
else:
break
# if length is more than chop. then remove line
if countWidth >= chop:
for c in range(countWidth):
try:
# check this pixel's around pixel
if self.im[i+1, j+c] > threshold and self.im[i-1, j+c] > threshold:
self.im[i, j+c] = lineColor
except IndexError:
pass
j += countWidth
删除水平线与删除垂直线相同
这是我的结果。
我首先将图像转换为黑白(黄色背景和紫色数字)
然后将所有线条绘制成灰色(matplotlib中的青色)
如果两条线重叠,则此方法无法检测 如果一行是数字,则会错误地删除该行 如何改进此算法