Remove Captcha background - python

I entered a captcha-ed website I would like to get rid of. Here is some sample images
Since the background is static and the word is so computer-generated non distorted character, I believe it is very do-able. Since passing the image directly to Tesseract (OCR engine) doesn't come a positive result. I would like to remove the captcha background before OCR.
I tried multiple background removal methods using Python-PIL
Remove all non-black pixels, which remove the lines but it wouldn't remove the small solid black box.
Apply filter mentioned another StackOverflow post, which would not remove the small solid black box. Also it is less effective than method 1.
Method 1 and 2 would give me a image like this
It seems close but Tesseract couldn't recognize the character, even after the top and bottom dot row is removed.
Create a background mask, and apply the background mask to the image.
Here is the mask image
And this is the image with the mask applied and grey lines removed
However blindly applying this mask would generate some "white holes" in the captcha character. And still Tesseract failed to find out the words.
Are there any better methods removing the static background?
Lastly how could I split the filtered image into 6 image with single character? Thanks very much.

I can give you a few ideas to have a try.
After you have applied step 3, you may thicken the black edges in the images using PIL so as the fill the white holes. And I guess you are using python-tesseract. If so, please refer to Example 4 in https://code.google.com/p/python-tesseract/wiki/CodeSnippets
In order to extract the characters, you may refer to Numpy PIL Python : crop image on whitespace or crop text with histogram Thresholds. There are methods about analysing the histogram of the image so as to locate the position of the whitespaces from which you can infer the boundary.

Related

How to crop character from image using openCV from given Image Python?

Cropped Only character from Image and remove spaces
I am trying to remove extra spaces from left and right of character image through opencv .Every image has different dimension .Is there any way to crop or remove extra spaces from left and right only or fetch only character from image.
Under the assumption that all your images look like the examples provided you can go with HansHirse's solution.
Segment the image into fore- and background using a global threshold. Then find the extreme coordinates of foreground pixels to define the boundaries of your sub image.
Of course you can do both steps on the fly instead of doing two iterations over your image.
Another solution: Use a blob detector to find the character components and merge their bounding boxes.

Prepare Image for OCR

The images that I have gives me inconsistent results. My thought process is: my text is always in white font; if I can switch the pixel of my text to black and turned everything else to white or transparent, I will have better success.
My question is, what library or language is best for this? Do I have to turn my white pixel into some unique RGB, turn everything else to white or transparent, then find the unique RGB and make that black? Any help is appreciated.
Yes, if you could make the text pixels black and all the rest of the documents white you would have better success, although this is not always possible, there are processes that can help.
The median filter (and other low pass filters) can be used to remove noise present in the image.
erosion can also help to remove things that are not characters, like thin lines and also noise.
align the text is also a good idea, the OCR accuracy can drop considerably if the text is not aligned. To do this you could try the Hough transform followed by a rotation. Use the Hough transform to find a line in your text and then rotate the image in the same angle as the line.
All processing steps mentioned can be done with opencv or scikit-image.
Is also good to point out that there are many other ways to process text, too many to mention.

Extract person from an image or make the background transparent

I want to extract the person in a image without any background.I want to do this for multiple images of the same kind.Please help me do so using python which can automate this process.
https://depositphotos.com/148319285/stock-video-man-run-on-green-screen.html
I,ve tried using canny edge detector could only find edges but couldnot crop it.Is there any alternative way to detect background and remove completely.
Background subtraction using opencv. Check the link for examples and details: https://opencv-python-tutroals.readthedocs.io/en/latest/py_tutorials/py_video/py_bg_subtraction/py_bg_subtraction.html
In case of such prepared images, you migth find useful simple thresholding: https://docs.opencv.org/3.4.0/d7/d4d/tutorial_py_thresholding.html in your case you would need to get grayscale image by extracting green channel from image. It would produce binary (black and white ONLY) image with white for background and black for object (or reverse depending on your choice).

OpenCV get subimages by mask

Have you any ideas how can I with python and OpenCV get subimages on original image by that mask? I need separated subimages of every white area.
Because it's not rects it's hard to get them separated.
I think you are looking for connectedComponentsWithStats(), which will give you connected components (i.e., one label per white area). The result will be a labeled image with a separate label for each component.
From this, it is easy to extract the part of the image with a specific label.

crop unwanted black space of image

I have a set of grayscale images, like this:
This is an example image as I cannot post the original image. Each image has an area with a texture, a pure white watermark (pos), and lots of unwanted black space.
Ideally this image should be cropped to:
The watermark can be slightly different in each image, but is always very thin pure white text.
The pictures can look very different, here is another example
this one only needs cropping on the left
another one:
this one needs to be cropped on top and bottom:
and another one
this one needs to be cropped at the top and right. Note that I left the watermark in this picture. Ideally the watermark would be removed as well, but I guess it is easier without.
here is a picture of the watermark how it looks in reality.
The images vary in size, but are usually large (over 2000x2000).
I am looking for a solution in python (cv2 maybe).
my first idea was to use something like this:
Python & OpenCV: Second largest object
but this solution code fails for me
I work in C# and C++ and don't work in python but can suggest you the logic.
You need to run two scan of the image, one row wise and other columns wise.
Since you said the unwanted part of image is always black, just read the pixel values in both scan. If the color of all the pixels in a certain row is black then you can elemminate or delete that row. Similar steps can be followed for column wise scanning.
Now we cannot just eleminate the rows and columns so easily, so just note down the redundant rows and columns and then you can crop your image using following code:( I will code in C# with emgucv library but it is easy to understand for python)
Mat original_image = new Mat();
Rect ROI = new Rect(x,y,width,height);
Mat image_needed_to_crop = new Mat(original_image,ROI);
This code just extracts only the region of interest from the original image.

Categories