Finding lines from text image OpenCV C++ - c++

I have images that are noised with some random lines like the following one:
I want to apply on them some preprocessing in order to find the lines (the lines that distort the writing).
I was seen some ways, but it is in Python, not C++:
Remove noisy lines from an image
Remove non straight lines from text image
In C++, I was try but result images:
the result which I want (I do it with Photoshop):
How to find lines in that images in C++ with OpenCV? thanks

I am not sure about this. Like #Chistoph Racwit said, you might need to use some sort of OCR.
But just to try it out, I think you can apply a horizontal filter that highlights any horizontal line in the image. It might not give the best-looking result but with some clean-up, you could end up with where the lines are in the image.
You can use this image to detect lines' locations and draw them in the original image with red color.

Related

XTK lesson 13 (ami example) - Removing the boundary lines in slices obtained from 2D volume rendering

I've implemented XTK lesson 13 from the AMI examples (this - https://fnndsc.github.io/ami/#xtk_lesson13) by passing a set of DICOM images.
In the slices obtained, there are these colored dotted lines that I'm assuming is a boundary of some sort. I need to remove them but I'm not sure what exactly they are. Can someone tell me how they are created or what they represent?
Edit:
I'm not at liberty to post a screenshot from my implementation but this is a screenshot from the example url. Sorry the image isn't very clear, the example loads super small images. The slice is the image on the bottom.
yellow dashed lines
When you move the image (slice) around in the actual example, the yellow dashed lines appear on the image. That's what I need to get rid of. I've also been referring to them as dotted lines, I suppose they are actually dashed lines. Sorry about that.
You should set stackHelper.slice.canvasWidth and stackHelper.slice.canvasHeight to 0 for the dotted lines to disappear.
Those dotted lines are meant to inform that a part of the slice if partially off-canvas.

How extract lines from a messy background

Given the next canny edge result image:
I'm trying to extract the selected lines:
I did try several methods without success. For ex. I tried morphological operations but didn't work well because some times the lines are in angle or they are not completely vertical or horizontal...
I wonder if there's a method or if it is possible to extract them properly.
Thanks.
If you want to identify the longest lines try with finding contour and filter by its length.
You will get only the long connected lines irrespective of angle.
if you want that to match with line
then try houghlines
Hope it helps.

OpenCV: Ignore text-like contours

Background
I want to detect all contours in an image that contains 2D geometric shapes, but strip away anything that looks like text.
Example input:
I tried to detect text areas via Tesseract and remove those text areas subsequently. For some images where the OCR recognition is good this works fine, thus text areas are recognized with quite good rate and contours of recognized text can then be removed. But for most images the text is not recognized well and thus I cannot remove irrelevant text contours from the image.
Therefore my question is: How can I distinguish text-like contours from my 2D "geometric" contours?
If you don't care about the text and just want to get rid of it, then you can just detect outer contours by passing RETR_EXTERNAL as the mode parameter to the findCountours() function. That will give you the outermost contours and ignore anything contained inside of the geometric shapes.
Or if you want more control, you can pass the mode parameter as RETR_TREE and then walk the tree, keeping only the top-level contours and ignoring anything below that level in the hierarchy. That way you'll get everything and you can decide later what you want to keep and what you want to ignore.
Read this page of the OpenCV documentation for information on how findCountours() represents the hierarchy (that page is from a python tutorial, but it's generic enough to follow along).
Of course that will only work if the images always look similar to the example you gave in your question - i.e. the text is always inside of the geometric shapes. If you have text outside of the shapes, then maybe you could look at the size of the contours (bounding rectangles) and ignore anything that falls below a certain threshhold (assuming text contours will be much smaller than your geometric shapes).
Contours that belong to text, also represent a region according to your example. So that you can try to use the properties of regions to eliminate some unneeded regions (text contours!) I can suggest that you can use some properties like eccentricity, solidity or compactness (you can find code example here: https://github.com/mribrahim/Blob-Detection
)
For ex: Regular shapes and the others can be distinguished by using compactness value, or you can combine any other properties

Improve Tesseract detection quality

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.

Taking parts of one image to create another image

I'm working with images from which I would like to take parts out and make one new image. I can make use of ImageMagick or OpenCV. Here is a sample image:
From this image I would like to take out the title, two annotated texts (one in circle one in rectangle), and the text from bottom.
So, the final image would have: Image Title, Annotated Text1, Annotated TExt, and This is some test. These parts of the image don't have to be in any particular order in the new image.
Questions
What kind of strategy can I use to do this?
Will hough or canny help?
I'm thinking that since the parts of the image I want back are all text, maybe hough line can detect the straight lines and then I crop out those parts of the images...
My main goal is to extract text so I can send it to an OCR
I've tried to erode the image and came up with this:
My Strategy
Following is my strategy to only keep parts of the image with white background and text. However, I'm not sure if this is doable with OpenCV...
There will be different ROI's in the image
there will always be white background on top of the image, lets call this space title. So I crop out the rectangle part on top of the image and save it as a separate image
there will always be white background at bottom of the image, lets call this body. So I crop out the rectangle part at bottom of the image and save it as a separate image
there will be some text on top of the image, lets call this annotated text. This will be in squares or circles. I can use technique mentioned in this answer to crop out those parts of the image and save them as a separate image.
If you are dealing with only similar looking fonts, and you are not looking for something super efficient, you can simply perform correlation with each letter of the alphabet (26 upper and 26 lower). Threshold out the peaks and add them together. You can then just define you bounding boxes around the peaks.