Python 使用OpenCV从图像中删除水平/垂直线
我想从图像中删除垂直线和水平线。Python 使用OpenCV从图像中删除水平/垂直线,python,opencv,image-processing,ocr,Python,Opencv,Image Processing,Ocr,我想从图像中删除垂直线和水平线。 这是我的方法 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
这是我的方法
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
def拆卸(自):
#转换为THRESH_二进制
self.retval,self.im=cv2.threshold(self.im,128255,cv2.THRESH_BINARY | cv2.THRESH_OTSU)
如果行长度大于chop,则chop=6。它将设置为白色
阈值=200像素的颜色
lineColor=150#如果是line。设置颜色255:白色0:黑色
(高度、宽度)=自我识别形状
#循环每个像素
对于X范围内的i(高度):
对于X范围内的j(宽度):
#如果是黑点
如果self.im[i][j]<阈值:
countWidth=0
#如果=切掉,则删除垂直线:
对于范围内的c(计数宽度):
尝试:
#检查此像素是否在像素周围
如果self.im[i+1,j+c]>阈值和self.im[i-1,j+c]>阈值:
self.im[i,j+c]=线条颜色
除索引器外:
通过
j+=计数宽度
删除水平线与删除垂直线相同
这是我的结果。我首先将图像转换为黑白(黄色背景和紫色数字)
然后将所有线绘制为灰色(matplotlib中的青色)
如果两条线重叠,此方法无法检测。
如果一行是数字,它将错误地删除这一行。
如何改进此算法请在问题中添加原始图像。如果两行重叠,此方法无法检测。->我想这是因为你的切碎值被设置为6。你尝试过形态运算来过滤水平线和垂直线吗