使用OpenCV从图像中删除水平/垂直线

2024-05-29 11:47:06 发布

您现在位置:Python中文网/ 问答频道 /正文

我要从图像中删除垂直线和水平线。
那么这就是我的方法

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中为青色)

如果两行重叠,则此方法无法检测。
如果一行是以数字表示的,则会错误地删除该行。
如何改进这个算法


Tags: in图像selfforthresholdifisline

热门问题