I'm building a pre-processing project to enhance the OCR results that will happen in stage 2, but from two images.
For example I have image1, and image2, and we need to check which one is better to do the OCR on it.
The performance and process time is so important (real-time application).
Here are some cases I need to discuss about:
Case1:
Both are "F" letter, but the first one is readable "F" in the OCR that will happen next, where the second one is not readable at all, so for case 1 I need to choose the first "F" as an input for the OCR and ignore the second image.
Case2:
Both are "R" letter, and both are readable in the OCR, but the first one is better from the second one as we see, so I need to choose the first "R" here.
Case3:
It's similar to first case, where the "n" here is not readable in the OCR so I need to choose the first "na"
Case4:
In the first "na" the "n" and "a" are not merged together, where the second one they are one "contour" so the first "na" is much better to be the input to the OCR.
I need to build a generic fast algorithm to check if this part of the image is better for the OCR or not.
I tried the following:
1- Method1: Check if the image is blurry or not, and choose the one which is better.
2- Method2: called canny method (or sobel) and choose the image that's better.
3- Method2: check count of contours on both the image and choose the one that's seems better according to contours area, and counts.
Any better suggestions?
The question is how do you get these chunks?
How do you decide that these are the things to compare.
If you know the sizes of expected characters - so you cut the chunks at that size - then you can use that as well in the comparison.
I think this question is a bit too broad. For different types of defects I would suggest different approaches.
For the first 2 cases and possibly the third, I would suggest doing a small morphological close and comparing the result with the original. The better letter would change less using close since it doesn't have small holes and dots. The comparison metric can be as simple as a sum over a pixelwise absolute difference.
You can try morphological operations.Have a look on this.
http://docs.opencv.org/master/d9/d61/tutorial_py_morphological_ops.html#gsc.tab=0
Related
I am currently dealing with text recognition. Here is a part of binarized image with edge detection (using Canny):
EDIT: I am posting a link to an image. I don't have 10 rep points so I cannot post an image.
EDIT 2: And here's the same piece after thresholding. Honestly, I don't know which approach would be better.
[2
The questions remain the same:
How should I detect certain letters? I need to determine location of every letter and then every word.
Is it a problem that some letters are "opened"? I mean that they are not closed areas.
If I use cv::matchtemplate, does it mean that I need to have 24 templates for every letter + 10 for every digit? And then loop over my image to determine the best correlation?
If both the letters and squares they are in, are 1-pixel wide, what filters / operations should I do to close the opened letters? I tried various combinations of dilate and erode - with no effect.
The question is kind of "how do I do OCR with Open CV?" and the answer is that it's an involved process and quite difficult.
But some pointers. Firstly, its hard to detect letters which are outlined. Most of the tools are designed for filled letters. But that image looks as if there will only be one non-letter distractor if you fill all loops using a certain size threshold. You can get rid of the non-letter lines because they are a huge connected object.
Once you've filled the letters, they can be skeletonised.
You can't use morphological operations like open and close very sensibly on images where the details are one pixel wide. You can put the image through the operation, but essentially there is no distinction between detail and noise if all features are one pixel. However once you fill the letters, that problem goes away.
This isn't in any way telling you how to do it, just giving some pointers.
As mentioned in the previous answer by malcolm OCR will work better on filled letters so you can do the following
1 use your second approach but take the inverse result and not the one you are showing.
2 run connected component labeling
3 for each component you can run the OCR algorithm
In order to discard outliers I will try to use the spatial relation between detected letters. They sold have other letter horizontally or vertically next to them.
Good luck
I'm taking a course in image recognition. We've just been taught about corner detection, e.g Harris.
I'm wondering what would be the next step in computation after it?
Let's take for example an image from wiki - https://en.wikipedia.org/wiki/Corner_detection#/media/File:Corner.png
Lowercase n, denoting a power on t, resulted in tree points being detected.
If I were to use this data, what do I do with it to know it might be a 'n' letter? Is there a way to compute similarity of this line that n got transformed into against an alphabet of choice and pick a best match?
What if I'm not looking for something as simple as a black letter on white background?
Well, Just for the records, I will state the Machine Learning method, here's a good paper for that matter : http://www.cs.stanford.edu/~acoates/papers/coatesetal_icdar_2011.pdf
Also, OpenCV has it's own implementation of it and it might work for your case, as long as it's somehow simple to do. More than that, you'll need a more sophisticated one or create your own (I don't recommend that !).
I'd like to write OCR in OpenCV. I need to recognize single letters. I'd like to use K-Nearest Neighbors. I'd like to recognize letters with different size and font and handwritten.
So, I'll prepare images to train. The first question is. Should I use letters in the (1) same size of images or (2) fit image?
1)
2)
How about found letters? Should I pass it as 1 (with the same size as train images) or 2 (just fit rectangle to letter)???
The "benchmark" MNIST dataset normalizes and centers the characters as in scenario (1) you described. If you're just interested in classification, it might make any difference how you do it.
If I understand you correctly, your second question has to do with what's called "preprocessing" in ML jargon. If you apply a transformation to convert each raw image into one of type either (1) or (2), it's called a preprocessing step -- which ever one you choose. Whatever preprocessing you do to the training set, the exact same preprocessing has to be done to the data before applying the model.
To make it simple, if you have a giant data set that you want to split into "training" and "testing" examples, first transform this into a "preprocessed data" set, and split this one. That way you're sure the exact same transformation parameters are used for both training and testing.
I am trying to extract alphanumeric characters (a-z0-9) which do not form sensefull words from an image which is taken with a consumer camera (including mobile phones). The characters have equal size and font type and are not formated. The actual processing is done under Windows.
The following image shows the raw input:
After perspective processing I apply the following with OpenCV:
Convert from RGB to gray
Apply cv::medianBlur to remove noise
Convert the image to binary using adaptive thresholding cv::adaptiveThreshold
I know the number of rows and columns of the grid. Thus I simply extract each grid cell using this information.
After all these steps I get images which look similar to these:
Then I run tesseract (latest SVN version with latest training data) on each extracted cell image individually (I tried different -psm and -l values):
tesseract.exe -l eng -psm 11 sample.png outtext
The results produced by tesseract are not very good:
Most characters are not recognized.
The grid lines are sometimes interpreted as "l" or "i" characters.
I already experimented with morphologic operations (open, close, erode, dilate) and replaced adaptive thresholding with OTSU thresholding (THRESH_OTSU) but the results got worse.
What else could I try to improve the recognition quality? Or is there even a better method to extract the characters besides using tesseract (for instance template matching?)?
Edit (21-12-2014):
I tested simple template matching (using normalized cross correlation and LMS but with even worse results). But I have made a huge step forward by extracting each character using findCountours and then running tesseract with only one character and the -psm 10 option which interprets each input image as a single character. Additonaly I remove non-alphanumeric characters in a post processing step. The first results are encouraging with detection rates of 90% and better. The main problem are misdetections of "9" and "g" and "q" characters.
Regards,
As I say here, you can tell tesseract to pay attention on "almost same" characters.
Also, there is some option in tesseract that don't help you in your example.
For instance, a "Pocahonta5S" will become, most of the time, a "PocahontaSS" because the number is in a letter word. You can see in this way so.
Concerning pre-processing, you better have to use a sharpen filter.
Don't forget that tesseract will always apply an Otsu's filter before reading anything.
If you want good result, sharpening + Adaptive Threshold with some other filters are good ideas.
I recommend to use OpenCV in Combination with tesseract.
The problem in your input images for tesseract are the non-character regions in your image.
An approach myself
To get rid of these I would use the openCV findContour function to receive all contours in your binary image. Afterwards define some criteria to illiminate the non-character regions. For example only take the regions, which are inside the image and doesn't touch the border, or to only take the regions with a specific region-area or a specific ratio of heigth to width. Find some kind of features, that let you distinguish between character an non-character contours.
Afterwards eliminate these non-character regions and handle the images forward to tesseract.
Just as idea for general testing this approach:
Eliminate the non-character regions manual (gimp or paint,...) and give the image to tesseract. If the result fits your expactations you can try to eliminate the the non-character regions with proposed method of above.
I suggest a similar approach I'm using in my case.
(I only have the problem of speed, which you should not have if its only some characters to compare)
First: Get the form to have default size and transform it:
https://www.youtube.com/watch?v=W9oRTI6mLnU
Second: Use matchTemplate
Improve template matching with many templates for one Image/ find characters on image
I also played around with OCR but I didn't like it because of 2 reasons:
Some kind of blackbox and hard to debug why its not recognized
In my case it was never 100% accurate no matter what i did even for screenshots with "perfect" characters.
i want to detect text area from image as a preprocessing step for tesseract OCR engine, the engine works well when the input is text only but when the input image contains Nontext content it falls, so i want to detect only text content in image,any idea of how to do that will be helpful,thanks.
Take a look at this bounding box technique demonstrated with OpenCV code:
Input:
Eroded:
Result:
Well, I'm not well-experienced in image processing, but I hope I could help you with my theoretical approach.
In most cases, text is forming parallel, horisontal rows, where the space between rows will contail lots of background pixels. This could be utilized to solve this problem.
So... if you compose every pixel columns in the image, you'll get a 1 pixel wide image as output. When the input image contains text, the output will be very likely to a periodic pattern, where dark areas are followed by brighter areas repeatedly. These "groups" of darker pixels will indicate the position of the text content, while the brighter "groups" will indicate the gaps between the individual rows.
You'll probably find that the brighter areas will be much smaller that the others. Text is much more generic than any other picture element, so it should be easy to separate.
You have to implement a procedure to detect these periodic recurrences. Once the script can determine that the input picture has these characteristics, there's a high chance that it contains text. (However, this approach can't distinguish between actual text and simple horisontal stripes...)
For the next step, you must find a way to determine the borderies of the paragraphs, using the above mentioned method. I'm thinking about a pretty dummy algorithm, witch would divide the input image into smaller, narrow stripes (50-100 px), and it'd check these areas separately. Then, it would compare these results to build a map of the possible areas filled with text. This method wouldn't be so accurate, but it probably doesn't bother the OCR system.
And finally, you need to use the text-map to run the OCR on the desired locations only.
On the other side, this method would fail if the input text is rotated more than ~3-5 degrees. There's another backdraw, beacuse if you have only a few rows, then your pattern-search will be very unreliable. More rows, more accuracy...
Regards, G.
I am new to stackoverflow.com, but I wrote an answer to a question similar to this one which may be useful to any readers who share this question. Whether or not the question is actually a duplicate, since this one was first, I'll leave up to others. If I should copy and paste that answer here, let me know. I also found this question first on google rather than the one i answered so this may benefit more people with a link. Especially since it provides different ways of going about getting text areas. For me, when I looked up this question, it did not fit my problem case.
Detect text area in an image using python and opencv
In the Current time, the best way to detect the text is by using EAST (An Efficient and Accurate Scene Text Detector)
The EAST pipeline is capable of predicting words and lines of text at arbitrary orientations on 720p images, and furthermore, can run at 13 FPS, according to the authors.
EAST quick start tutorial can be found here
EAST paper can be found here