Remove unwanted lines in captcha text - opencv - python

2024/10/11 20:23:28

I trying to get text from captcha image using opencv. Problem is text are masked with noise and it is complex to process with those horizontal line/noise.

Original image

enter image description here

My processed image :

enter image description here

not sure how to remove those horizontal lines and get text

Code :

import numpy as np
import cv2# Load an color image in grayscale
img = cv2.imread('captcha.jpg',0)#display image in window
#cv2.imshow('image',img) #@param - windowname, image to be displayedhorizontal_inv = cv2.bitwise_not(img)
#perform bitwise_and to mask the lines with provided mask
masked_img = cv2.bitwise_and(img, img, mask=horizontal_inv)
#reverse the image back to normal
masked_img_inv = cv2.bitwise_not(masked_img)
cv2.imshow("masked img", masked_img_inv)
cv2.imwrite("result2.jpg", masked_img_inv)cv2.waitKey(0) # time for window to show image in milliseconds - 0 is infinite wait
cv2.destroyAllWindows()

Edit : how to handle if text are in light color

enter image description here

enter image description here

Answer
import numpy as np
import cv2# Load an color image in grayscale
img = cv2.imread('captcha.jpg',0)#display image in window
#cv2.imshow('image',img) #@param - windowname, image to be displayedhorizontal_inv = cv2.bitwise_not(img)
#perform bitwise_and to mask the lines with provided mask
masked_img = cv2.bitwise_and(img, img, mask=horizontal_inv)
#reverse the image back to normal
masked_img_inv = cv2.bitwise_not(masked_img)kernel = np.ones((5,5),np.uint8)
dilation = cv2.dilate(masked_img_inv,kernel,iterations = 3) # to remove blackline noise
cv2.imwrite("result1.jpg", dilation)

enter image description here

ret,thresh2 = cv2.threshold(dilation,254,255,cv2.THRESH_BINARY_INV) 
thresh2=cv2.bitwise_not(thresh2)
# cv2.imshow("masked img", masked_img_inv)
cv2.imwrite("result2.jpg", thresh2)

enter image description here

cv2.waitKey(0) # time for window to show image in milliseconds - 0 is infinite wait
cv2.destroyAllWindows()

Let me know if you have future queries.

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

Related Q&A

Double Summation in Python

I am trying to write a code to conduct a double summation (see pic) in which; M is the subjects, N is the Trials, Yijt is the measured wave form data (3d array)so far I have; Given Y is the data arra…

psycopg, double and single quotes insert

I ran into problems, while trying to insert to database:ur_psql.execute("""insert into smth(data, filedate, filedby)"""""" values(%s, NOW(), %s)""…

How to debug Python 2.7 code with VS Code?

For work I have to use Python 2.7. But when I use the "debug my python file" function in VS Code, I get an error. Even with a simple program, like : print()

Python Sequence of Numbers

Ive decided not to waste my summer and start learning python. I figured Id start learning looping techniques so I wanted to start with a basic list of numbers, aka, write a for loop that will generate…

Object initializer syntax (c#) in python?

I was wondering if there is a quick way to initialise an object in python. For example in c# you can instantiate an object and set the fields/properties like...SomeClass myObject = new SomeClass() { va…

Plotly.io doesnt see the psutil package even though its installed

Im trying to execute the following code:import numpy as np import pandas as pd import matplotlib.pyplot as plt import matplotlib %matplotlib inline import seaborn as snsimport plotly.graph_objects as g…

How to get MultiCells in Pyfpdf Side by side?

I am making a table of about 10 cells with headings in them. They will not fit accross the page unless I use multi_cell option. However I cant figure out How to get a multi_cell side by side. When I ma…

Django: Require Checkbox to be ticked to Submit Form

Im creating a form in Django (using ModelForm). There are many checkboxes, and I want to make it so that one of these must be selected in order to submit the form. I dont mean any one checkbox, but on…

Filtering in django rest framework

In my project I use django rest framework. To filter the results I use django_filters backend. There is my code:models.pyfrom django.db import modelsclass Region(models.Model):name = models.CharField(m…

Efficiency difference between dict.has_key and key in dict in Python [duplicate]

This question already has answers here:Closed 11 years ago.Possible Duplicate:has_key() or in? In Python, therere two ways of deciding whether a key is in a dict:if dict.has_key(key) and if key in di…