简体   繁体   中英

How to erode this thresholded image using OpenCV

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()

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()

在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM