Opencv:如何在不增加图像大小的情况下增加字母大小?

时间:2018-06-22 09:50:24

标签: python opencv tesseract

我想增加包含多行分布的字母的图像中字母的大小,并保持它们的坐标不变,也要删除字母之间的线。 例如

enter image description here

我已应用了形态转换。使用 tesseract OCR 识别图像中的问题时,它会有所帮助,但用字母 4

不足以识别字符

我已申请erodecv2.MORPH_CLOSE

kernel = np.ones((2,2),np.uint8)
erosion = cv2.erode(img,kernel,iterations = 1)

kernel = np.ones((3,3),np.uint8)
closing = cv2.morphologyEx(erosion, cv2.MORPH_CLOSE, kernel)

我得到以下输出
enter image description here

已编辑
输入图像

我使用的完整代码

import cv2
import numpy as np



img = cv2.imread('total_2.png',0)



edges = cv2.Canny(img,50,150,apertureSize = 3)
minLineLength=100
lines = cv2.HoughLinesP(image=edges,rho=1,theta=np.pi/180, threshold=100,lines=np.array([]), minLineLength=minLineLength,maxLineGap=80)

a,b,c = lines.shape
for i in range(a):
    x = lines[i][0][0] - lines [i][0][2]
    y = lines[i][0][1] - lines [i][0][3]
    if x!= 0 and abs(y/x) <1:
        cv2.line(img, (lines[i][0][0], lines[i][0][1]), (lines[i][0][2], lines[i][0][3]), (255, 255, 255), 1, cv2.LINE_AA)

se = cv2.getStructuringElement(cv2.MORPH_ELLIPSE , (4,3))
gray = cv2.morphologyEx(img, cv2.MORPH_CLOSE, se)
img = cv2.fastNlMeansDenoising(gray, None, 65, 5, 21)
img = cv2.threshold(img, 128, 255, cv2.THRESH_BINARY_INV)[1]
img = cv2.bitwise_not(img)
k1 = np.zeros((3, 3), np.uint8)

img = cv2.erode(img, k1, iterations = 1)

ret,img = cv2.threshold(img,0,255,0)
element = cv2.getStructuringElement(cv2.cv2.MORPH_RECT,(3,3))
kernel = np.ones((1,1),np.uint8)


#dilation = cv2.dilate(img,element,iterations = 1)
erosion = cv2.erode(img,element,iterations = 1)



kernel = np.ones((3,3),np.uint8)
#opening = cv2.morphologyEx(erosion, cv2.MORPH_OPEN, element)
closing = cv2.morphologyEx(erosion, cv2.MORPH_CLOSE, element)

使用cv2.findContours

的另一种方法
# threshold the gray image to binarize, and negate it

_,binary = cv2.threshold(image, 150, 255, cv2.THRESH_BINARY)

if flag :

    binary = cv2.bitwise_not(binary)



# find external contours of all shapes

_,contours,_ = cv2.findContours(binary, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)



# create a mask for floodfill function, see documentation

h,w= image.shape

mask = np.zeros((h+2,w+2), np.uint8)



# determine which contour belongs to a square or rectangle

for cnt in contours:

    poly = cv2.approxPolyDP(cnt, 0.05*cv2.arcLength(cnt,True),True)

    if len(poly) == 4:

        # if the contour has 4 vertices then floodfill that contour with black color

        cnt = np.vstack(cnt).squeeze()

        _,binary,_,_ = cv2.floodFill(binary, mask, tuple(cnt[0]), 0)

# convert image back to original color

if flag:

    binary = cv2.bitwise_not(binary)

cv2.findContours的问题在于我还有其他图像,我对它们应用了相同的预处理技术,但是这些图像未装箱,这种方法会破坏一些字母。

例如此图像

enter image description here

我理想的解决方案是增加字母大小,并使字母更清晰

0 个答案:

没有答案