首页 > 解决方案 > 从文本图像中删除分界 - 图像处理

问题描述

嗨,我需要编写一个程序,从灰度图像(带有文本的图像)中删除分界,我读到了阈值和模糊,但我仍然不知道我该怎么做。

我的图像是这样的希伯来文本图像:

输入 我需要删除分界线(假设分界线是图像中的最小元素)输出需要是这样的

输出 我想使用opencv在python中编写代码,我需要学习哪些主题才能做到这一点,以及如何做到这一点?

谢谢你。

编辑:我只能使用 cv2 函数

标签: pythonopencvimage-processing

解决方案


您要删除的符号明显小于所有其他形状,您可以使用它来确定要删除的女巫。

首先使用阈值将图像转换为二进制。接下来,您可以使用findContours来检测形状,然后使用 contourArea来确定形状是否大于阈值。

最后,您可以创建一个蒙版来删除不需要的形状,在新图像上绘制较大的符号或在原始图像中的原始符号上绘制白色的较小符号 - 使它们消失。我在下面的代码中使用了最后一种技术。

结果:

在此处输入图像描述

代码:

import cv2
# load image as grayscale
img = cv2.imread('1MioS.png',0)
# convert to binary. Inverted, so you get white symbols on black background
_ , thres = cv2.threshold(img, 200, 255, cv2.THRESH_BINARY_INV)
# find contours in the thresholded image (this gives all symbols)
contours, hierarchy = cv2.findContours(thres, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
# loop through the contours, if the size of the contour is below a threshold, 
# draw a white shape over it in the input image
for cnt in contours:
    if cv2.contourArea(cnt) < 250:
        cv2.drawContours(img,[cnt],0,(255),-1)
# display result
cv2.imshow('res', img)
cv2.waitKey(0)
cv2.destroyAllWindows()

更新

要找到最大的轮廓,您可以遍历它们并跟踪最大值:

maxArea = 0
for cnt in contours:
    currArea = cv2.contourArea(cnt)
    if currArea > maxArea:
        maxArea = currArea
print(maxArea)

我还制作了一个更复杂的版本,它创建了一个索引和轮廓大小的排序列表。然后它会寻找所有轮廓大小的最大相对差异,因此您知道哪些轮廓是“小”和“大”。我不知道这是否适用于所有字母/字体。

# create a list of the indexes of the contours and their sizes
contour_sizes = []
for index,cnt in enumerate(contours):
    contour_sizes.append([index,cv2.contourArea(cnt)])

# sort the list based on the contour size. 
# this changes the order of the elements in the list
contour_sizes.sort(key=lambda x:x[1])

# loop through the list and determine the largest relative distance
indexOfMaxDifference = 0
currentMaxDifference = 0
for i in range(1,len(contour_sizes)):
    sizeDifference = contour_sizes[i][1] / contour_sizes[i-1][1] 
    if sizeDifference > currentMaxDifference:
        currentMaxDifference = sizeDifference
        indexOfMaxDifference = i

# loop through the list again, ending (or starting) at the indexOfMaxDifference, to draw the contour
for i in range(0, indexOfMaxDifference):
    cv2.drawContours(img,contours,contour_sizes[i][0]     ,(255),-1)

要获得背景颜色,您可以使用minMaxLoc. 这将返回最低颜色值及其图像位置(也是最大值,但您不需要它)。如果将其应用于阈值图像(背景为黑色),它将返回背景像素的位置(很有可能是 (0,0) )。然后,您可以在原始彩色图像中查找该像素。

# get the location of a pixel with background color
min_val, _, min_loc, _ = cv2.minMaxLoc(thres)
# load color image
img_color = cv2.imread('1MioS.png')
# get bgr values of background
b,g,r = img_color[min_loc]
# convert from numpy object
background_color = (int(b),int(g),int(r))

然后绘制轮廓

cv2.drawContours(img_color,contours,contour_sizes[i][0],background_color,-1)

而且当然

cv2.imshow('res', img_color)

推荐阅读