How to erode this thresholded image using OpenCV

2024/11/16 4:19:08

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

In your case skeleton operation will work better

import cv2
import numpy as npimg = 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 optionalcv2.imshow("skeleton", thinned)
cv2.waitKey()

enter image description here


Update
You can find implementations of skeleton in python using morph operations. Result for me is not good. Probably you can filter this antennas.
enter image description here

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

https://en.xdnf.cn/q/119571.html

Related Q&A

Searching for only the first value in an array in a csv file

So i am creating a account login system which searches a database for a username (and its relevant password) and, if found, will log the user on.This is what the csv file currently looks like[dom, ente…

how to write a single row cell by cell and fill it in csv file

I have a CSV file that only has column headers:cat mycsv.csvcol_1@@@col_2@@@col_3@@@col_3I have to fill a single row with None values in each cell of the CSV file. Can someone suggest me the best-optim…

Greedy String Tiling in Python

I am trying to learn greedy string tiling in algorithmI have two lists as follows:a=[a,b,c,d,e,f] b=[d,e,a,b,c,f]i would like to retrieve c=[a,b,c,d,e]Another example would be a = [1,2,3,4,5,6,7,8,9,1,…

Python - efficient way to create 20 variables?

I need to create 20 variables in Python. That variables are all needed, they should initially be empty strings and the empty strings will later be replaced with other strings. I cann not create the var…

Whatsapp asking for updating chrome version

I am trying to open whatsapp with selenium and python, it was working fine until today. In headless or non, whatsapp is now asking to update chrome, when I try to do so, Chrome throws this error: An er…

how to find the longest N words from a list, using python?

I am now studying Python, and I am trying to solve the following exercise:Assuming there is a list of words in a text file, My goal is to print the longest N words in this list.Where there are several …

([False, True] and [True, True]) evaluates to [True, True]

I have observed the following behavior in python 3: >>> ([False, True] and [True, True]) [True, True]>>> ([False, True] or [True, True]) [False, True]I was expecting exactly the oppos…

Uploading an image to Flask server

I am struggling bit with Flask and uploading a file, here is my Flask code so far:@app.route(/api/user/update/, methods=[PUT]) @auth.login_required def update_user():# check if the post request has the…

Enemy Projectiles Arent Appending On Screen

I have here my script that targets the player what ever position he is at but the projectiles arent showing on my screen VIDEO. He isnt attacking at all, I dont know why. I am in my main loop I draw t…

Python+Selenium. Cant locate element

Ive implemented the script using Python and selenium to click on the ads. But now this script is not working.Unable to find element on the page.Please help me to correct the script. Thank you!from sele…