Home > Software engineering >  How to erode this thresholded image using OpenCV
How to erode this thresholded image using OpenCV

Time:08-17

I am trying to first remove the captcha numbers by thresholding and then eroding it ,to get slim continuous lines to get better output.
Problem:the eroded image is not continuous as u can see
Original Image:
Threshold Image:(Here digits area is to thick,so i want it to be shrink,slim and continous)

my output:

Desired Output/Result wanted:

code:

import os
import os.path
import cv2
import glob
import imutils
import matplotlib.pyplot as plt
import numpy as np
CAPTCHA_IMAGE_FOLDER = "generated_captcha_images"
OUTPUT_FOLDER = "extracted_letter_images"


# Get a list of all the captcha images we need to process
captcha_image_files = glob.glob(os.path.join(CAPTCHA_IMAGE_FOLDER, "*"))
counts = {}

# loop over the image paths
for (i, captcha_image_file) in enumerate(captcha_image_files):
    print("[INFO] processing image {}/{}".format(i   1, len(captcha_image_files)))

    filename = os.path.basename(captcha_image_file)
    captcha_correct_text = os.path.splitext(filename)[0]

    image = cv2.imread(captcha_image_file)
    gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
    thresh = cv2.threshold(
        gray, 36, 255, cv2.THRESH_BINARY_INV)[1]
    erode = cv2.erode(thresh, np.ones((2, 2), np.uint8), iterations=1)
    plt.imshow(erode, cmap="gray")
    plt.show()

CodePudding user response:

In your case skeleton operation will work better

import cv2
import numpy as np

 
img = cv2.imread('/Users/alex/Downloads/fOrmgm.jpeg',0)

thinned = cv2.ximgproc.thinning(img)
element = cv2.getStructuringElement(cv2.MORPH_RECT, (3,3))
thinned = cv2.dilate(thinned, element) # this operation is optional

cv2.imshow("skeleton", thinned)
cv2.waitKey()

enter image description here


Update
You can enter image description here

Implementation of cv2.ximgproc.thinning() you can find here.

  • Related