I'm trying to develop simple PC application for license plate recognition (Java + OpenCV + Tess4j). Images aren't really good (in further they will be good). I want to preprocess image for tesseract, and I'm stuck on detection of license plate (rectangle detection).
1) Source Image
Mat img = new Mat();
img = Imgcodecs.imread("sample_photo.jpg");
Mat imgGray = new Mat();
Imgproc.cvtColor(img, imgGray, Imgproc.COLOR_BGR2GRAY);
Mat imgGaussianBlur = new Mat();
Imgproc.GaussianBlur(imgGray,imgGaussianBlur,new Size(3, 3),0);
Mat imgAdaptiveThreshold = new Mat();
Imgproc.adaptiveThreshold(imgGaussianBlur, imgAdaptiveThreshold, 255, CV_ADAPTIVE_THRESH_MEAN_C ,CV_THRESH_BINARY, 99, 4);
File imageFile = new File("preprocess/adaptive_threshold_AFTER_PAINT.png");
ITesseract instance = new Tesseract();
String result = instance.doOCR(imageFile);
Here's how I suggest you should do this task.
Apply Sobel Filter to find vertical edges.
Sobel(gray, dst, -1, 1, 0)
minAreaRectof each contour. Select rectangles based on aspect ratio and minimum and maximum area.
adaptiveThresholdand apply OCR.
a) Result after Step 5
b) Result after Step 7. Green ones are all the
minAreaRects and the Red ones are those which satisfy the following criteria: Aspect Ratio range (2,12) & Area range (300,10000)
c) Result after Step 9. Selected rectangle. Criteria: Edge Density > 0.5
For edge-density, what I did in the above examples is the following.
Edge Density = No. of White Pixels in the Rectangle/Total no. of Pixels in the rectangle
NOTE: Instead of going through steps 1 to 3, you can also use the binary image from step 5 for calculating edge density.