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

2024-05-20 12:28:48 发布

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

我想增加图像中字母的大小包含字母分布在多行中,并保持其坐标不变,同时删除字母之间的线条。 例如

enter image description here

我已经应用了形态变换。在这一问题中,使用OCR并不足以解决这一问题

我已经申请了erode和{}

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

已编辑
输入图像

我使用的完整代码

^{pr2}$

使用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

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


Tags: to图像imageifnp字母maskcv2