'How to use vertical dilation in OpenCV?

I cordially need help with a procedure.

I'm trying to make the image sharper but my biggest challenge is that many times the letters end up being too close together (as is the case with number 4 in the image), I tried a dilatation but it damages the image a lot periodically depending on its intensity .

Is there a possibility (in python) that I managed to leave it in the way of the final example?

Original Image. My version has the gray backgroung taken care of.

This is a version of the image that I come up with using the opening version:

This is a version of the image that I come up with using the opening version:

Change the initial gray for black_white.

mrz = cv2.resize(initial_img, (800, 140))
    black_white = cv2.threshold(mrz, 0, 255, 
    cv2.THRESH_BINARY_INV | cv2.THRESH_OTSU)[1]

My code:

img = cv2.resize(img, (400, 70))
    black_white = cv2.threshold(mrz, 0, 255, cv2.THRESH_BINARY_INV | cv2.THRESH_OTSU)     
    cv2.imshow("Black-White", black_white)

    dist = cv2.distanceTransform(black_white, cv2.DIST_L2, 5)
    dist = cv2.normalize(dist, dist, 0, 1.0, cv2.NORM_MINMAX)
    dist = (dist * 255).astype("uint8")
    cv2.imshow("Dist", dist)

    black_white = cv2.threshold(dist, 0, 255, cv2.THRESH_BINARY | cv2.THRESH_OTSU)[1]
    cv2.imshow("black_white", black_white)

    kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (3,3))
    opening = cv2.morphologyEx(black_white, cv2.MORPH_OPEN, kernel)
    img_erosion = cv2.erode(opening, kernel, iterations=1)

    final = cv2.bitwise_and(opening.copy(), opening)

    


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source