Cropping license plate based on blue sides - python

I'm currently working on license plate recognition with OpenCV and Tesseract.
The process is: search for rectangles in the image and search for the license plate. Get the contours, crop it, preprocess and after, analyze the characters.
The code is actually giving accurate results with traineddata but I have a problem with the detection of the license plate if something (even so little) is on the contour like in this picture:
So I thought about another way to do that: every plate in my country has two blue side bars. If I can detect them (two rectangles placed at a specific distance between each other), I can just crop and work on that. Less work for all the preprocessing and a more accurate result.
With the inrange method i obtained this:
But now I'm blocked and i don't actually know what to do.

Related

find camera/image obstructed by an object with opencv python

I have a FOV camera that has approximately 195*130 degree. So this 'lens' will put in a circle holder and the lens should not see the holder. Here's the image of not I want.
I draw 4 rectangle in Paint. There are 4 black spots which is the holder. The full red one is for censorship not there actually
If the camera image streams like that, that's a no. I need to detect that black spots and if it is like this it should gives me a error message or simply 'false'. I searched google and couldn't found this. I'm a noob of this subject but if you explain me how to do this I can connect the dots.
Thank you for your helps.
And I get the stream via USB-Capture Card. It acts like webcam.
#UPDATE1: I cropped the four corners of image then get the threshold. Made a basic if else logic and get what I want. Thank you anyway.
Try detection by generating custom HAAR filters.
Or make it simply by applying a threshold (nearly black) and look if some tiny squares in the corneres are completely black.

Coordinates of framed text on an image

I would like to get the coordinates of framed text on an image. The paragraphs have thin black borders. The rest of the image contains usual paragraphs and sketchs.
Here is an example:
Do you have any idea of what kind of algorithms should I use in Python with an image library to achieve this ? Thanks.
A few ideas to detect a framed text which largely comes down to searching boxes/rectangles of substantial size:
find contours with OpenCV, analyze shapes using cv2.approxPolyDP() polygon approximation algorithm (also known as Ramer–Douglas–Peucker algorithm). You could additionally check the aspect ratio of the bounding box to make sure the shape is a rectangle as well as check the page width as this seems to be a known metric in your case. PyImageSearch did this amazing article:
OpenCV shape detection
in a related question, there is also a suggestion to look into Hough Lines to detect a horizontal line, taking a turn a detecting vertical lines the same way. Not 100% sure how reliable this approach would be.
Once you find the box frames, the next step would be to check if there is any text inside them. Detecting text is a broader problem in general and there are many ways of doing it, here are a few examples:
apply EAST text detector
PixelLink
tesseract (e.g. via pytesseract) but not sure if this would not have too many false positives
if it is a simpler case of boxes being empty or not, you could check for average pixel values inside - e.g. with cv2.countNonZero(). Examples:
How to identify empty rectangle using OpenCV
Count the black pixels using OpenCV
Additional references:
ideas on quadrangle/rectangle detection using convolutional neural networks

How to find center coordinates of numbers in an image

I'm currently working on my first assignment in image processing (using OpenCV in Python). My assignment is to calculate a precise score (to tenths of a point) of one to several shooting holes in an image uploaded by a user. One of the requirements is to transform the uploaded shooting target image to be from "birds-eye view" for further processing. For that I have decided that I need to find center coordinates of numbers (7 & 8) to select them as my 4 quadrilateral.
Unfortunately, there are several limitations that need to be taken into account.
Limitations:
resolution of the processed shooting target image can vary
the image can be taken in different lighting conditions
the image processed by this part of my algorithm will always be taken under an angle (extreme angles will be automatically rejected)
the image can be slightly rotated (+/- 10 degrees)
the shooting target can be just a part of the image
the image can be only of the center black part of the target, meaning the user doesn't have to take a photo of the whole shooting target (but there always has to be the center black part on it)
this algorithm can take a maximum of 2000ms runtime
What I have tried so far:
Template matching
here I quickly realized that it was unusable since the numbers could be slightly rotated and a different scale
Feature matching
I have tried all of the different feature matching types (SIFT, SURF, ORB...)
unfortunately, the numbers do not have that specific set of features so they matched a quite lot of false positives, but I could possibly filter them by adding shape matching, etc..
the biggest blocker was runtime, the runtime of only a single number feature matching took around 5000ms (even after optimizations) (on MacBook PRO 2017)
Optical character recognition
I mostly tried using pytesseract library
even after thresholding the image to inverted binary (so the text of numbers 7 and 8 is black and the background white) it failed to recognize them
I also tried several ways of preprocessing the image and I played a lot with the tesseract config parameter but it didn't seem to help whatsoever
Contour detection
I have easily detected all of the wanted numbers (7 & 8) as single contours but failed to filter out all of the false positives (since the image can be in different resolutions and also there are two types of targets with different sizes of the numbers I couldn't simply threshold the contour by its width, height or area)
After I would detect the numbers as contours I wanted to extract them as some ROI and then I would use OCR on them (but since there were so many false positives this would take a lot of time)
I also tried filtering them by using cv2.matchShapes function on both contours and cropped template / ROI but it seemed really unreliable
Example processed images:
high resolution version here
high resolution version here
high resolution version here
high resolution version here
high resolution version here
high resolution version here
As of right now, I'm lost on how to progress about this. I have tried everything I could think of. I would be immensely happy if any of you image recognition experts gave me any kind of advice or even better a usable code example to help me solve my problem.
Thank you all in advance.
Find the black disk by adaptive binarization and contour (possibly blur to erase the inner features);
Fit an ellipse to the outline, as accurate as possible;
Find at least one edge of the square (Hough lines);
Classify the edge as one of NWSE (according to angle);
Use the ellipse and the line information to reconstruct the perspective transformation (it is an homography);
Apply the inverse homography to straighten the image and obtain the exact target center and axis;
Again by adaptive binarization, find the bullet holes (center/radius);
Rate the holes after their distance to the center, relative to the back disk radius.
If the marking scheme is variable, detect the circles (Hough circles, using the known center, or detect peaks in an oblique profile starting from the center).
If necessary, you could OCR the digits, but it seems that the score is implicitly starting at one in the outer ring.

OCR on images using python and opencv

I am a newbie to computer vision, image processing and OCR. As a part of task, i need to perform the OCR on attached emails. However, the problem is ROI is not constant in all images. For example, in all images we need to extract the information related with patient Yadav.
This needs to be achieved in Python and OpenCV. Please help. I have already tried the approach as given below:
Change DPI
Grayscale
Binarize
However, facing the problem with layout or zone analysis.
Thanks for help.
Welcome to the computer vision world.
I think you're not understanding your problem well enough. Just trying something and check whether it work will never work.
At first read how tesseract (OCR engine) do to improve their accuracy at https://github.com/tesseract-ocr/tesseract/wiki/ImproveQuality
Ok, then what make OCR do not work on your data. As you can see your data is clean, and there is nearly none of noise. So it is the border of printed email (as "ROI" in your question) does not correctly line up.
So have can we make the ROI line up correctly . we could use some perspective transformation. i took an example from https://www.pyimagesearch.com/2014/08/25/4-point-opencv-getperspective-transform-example/
So how can we perform perspective transformation. We have to find the 4 corners of the ROI and move it back to corners of the image. To find 4 corner of the ROI you could find some contours .
So here the summary, here is the steps.
1. Find the ROI (white color) using color segmentation
2. Find the contour which cover the ROI
3. Find 4 corners of the ROI's contour
4. Apply perspective transform
5. Run tesseract on transformed image
Hope that help

detect card symbol using opencv python

I'm trying to detect the difference between a spade, club, diamond and hart. the number on the card is irrelevant, just the suit matters.
i've tried color detection by looking at just the red or black colors, but that still leaves me with two results per color. how could i make sure i can detect each symbol individually?
for instance: i have a picture of a red hart, a red diamond, a black spade and a black club. i want to draw the contours of each symbol in a different color.
I'm using my webcam as a camera.
If the suit of your cards is always identical (only one single kind of card) you could just store images of spade, club, diamond and hart and then check the cross-correlation of the given image to your references and pick the highest value (see this question). If you have a fairly large sample set of different kinds of cards that have slight differences between the suit-representations you can pretty much follow this tutorial almost exactly to do a four-way classification of your training set and use a svm, clustering or other approaches, which are available in open-cv.
hope this helps and all the best
I don't see any code, so can't really tweak what you have, but more generally you ought to be able to train it just the same except for the symbol instead of color:
Here is a sample code for doing a specific card. You can either train them to be card specific, thus capturing all of the symbols by specific card, or back it up and more generally train for the symbol (heart / diamond). You should also look at template matching on opencv.
There are a couple guides and code already completed for this on github.
Here is a tutorial worth reading
As the card symbol is at fixed positions, you may try below (e.g. in OpenCV 3.2 Python):
Crop the symbol at top left corner, image = image[h1:h2,w1:w2]
Threshold the symbol colors to black, the rest to white, thresh = mask = cv2.inRange(image,(0,0,0),(100,100,100))
Perform a find contour detection, _, contours, hierarchy = cv2.findContours()
Get the area size of the contour. area = cv2.contourArea(contour)
Compare the area to determine which one of the 4 symbols it belongs to.
In which you have to build the area thresholds of each symbol for step 4 to compare. All the cv2 functions above are just for reference.
Hope this help.
Unless you have major perspective distortion (when it may work anyway), template matching is very effective. You can run this in red and black channels separately (obviously don't look for red spades etc.).
Take a look at e.g. (not mine)
http://www.pyimagesearch.com/2015/01/26/multi-scale-template-matching-using-python-opencv
For playing cards, see also https://github.com/arnabdotorg/Playing-Card-Recognition

Categories