Advertisement
LdDl

License Plate Recognition

May 20th, 2016
4,187
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.22 KB | None | 0 0
  1. //http://stackoverflow.com/questions/37302098/image-preprocessing-with-opencv-before-doing-character-recognition-tesseract/
  2.  
  3. import java.util.List;
  4. import java.util.ArrayList;
  5. import org.opencv.core.*;
  6. import org.opencv.imgcodecs.Imgcodecs;
  7. import org.opencv.imgproc.Imgproc;
  8. import static org.opencv.imgproc.Imgproc.MORPH_RECT;
  9. import static org.opencv.imgproc.Imgproc.getStructuringElement;
  10. import java.io.File;
  11. import net.sourceforge.tess4j.*;
  12.  
  13. public class ShowImage {
  14.    
  15.     private static final int
  16.             CV_MOP_CLOSE = 3,
  17.             CV_THRESH_OTSU = 8,
  18.             CV_THRESH_BINARY = 0,
  19.             CV_ADAPTIVE_THRESH_GAUSSIAN_C  = 1,
  20.             CV_ADAPTIVE_THRESH_MEAN_C = 0,
  21.             CV_THRESH_BINARY_INV  = 1;
  22.    
  23.    
  24.     public static boolean checkRatio(RotatedRect candidate) {
  25.         double error = 0.3;
  26.         //Spain car plate size: 52x11 aspect 4,7272
  27.         //Russian 12x2 (52cm / 11.5)
  28.         //double aspect = 52/11.5;
  29.         double aspect = 6;
  30.         int min = 15 * (int)aspect * 15;
  31.         int max = 125 * (int)aspect * 125;
  32.         //Get only patchs that match to a respect ratio.
  33.         double rmin= aspect - aspect*error;
  34.         double rmax= aspect + aspect*error;
  35.         double area= candidate.size.height * candidate.size.width;
  36.         float r= (float)candidate.size.width / (float)candidate.size.height;
  37.         if(r<1)
  38.             r= 1/r;
  39.         if(( area < min || area > max ) || ( r < rmin || r > rmax )){
  40.             return false;
  41.         }else{
  42.             return true;
  43.         }
  44.     }
  45.    
  46.     public static boolean checkDensity(Mat candidate) {
  47.         float whitePx = 0;
  48.         float allPx = 0;
  49.         whitePx = Core.countNonZero(candidate);
  50.         allPx = candidate.cols() * candidate.rows();
  51.         //System.out.println(whitePx/allPx);
  52.         if (0.62 <= whitePx/allPx)
  53.             return true;
  54.         else
  55.             return false;
  56.     }
  57.    
  58.     public static void main(String[] args) throws InterruptedException {
  59.                
  60.         System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
  61.        
  62.         Mat img = new Mat();
  63.         Mat imgGray = new Mat();
  64.         Mat imgGaussianBlur = new Mat();  
  65.         Mat imgSobel = new Mat();
  66.         Mat imgThreshold = new Mat();
  67.         Mat imgAdaptiveThreshold = new Mat();
  68.         Mat imgAdaptiveThreshold_forCrop = new Mat();
  69.         Mat imgMoprhological = new Mat();  
  70.         Mat imgContours = new Mat();
  71.         Mat imgMinAreaRect = new Mat();
  72.         Mat imgDetectedPlateCandidate = new Mat();
  73.         Mat imgDetectedPlateTrue = new Mat();
  74.        
  75.         List<MatOfPoint> contours = new ArrayList<MatOfPoint>();
  76.        
  77.         img = Imgcodecs.imread("T341OX177.jpg");
  78.         //Imgcodecs.imwrite("changes/st/1_True_Image.png", img);
  79.        
  80.         Imgproc.cvtColor(img, imgGray, Imgproc.COLOR_BGR2GRAY);
  81.         //Imgcodecs.imwrite("changes/st/2_imgGray.png", imgGray);
  82.        
  83.         Imgproc.GaussianBlur(imgGray,imgGaussianBlur, new Size(3, 3),0);
  84.         //Imgcodecs.imwrite("changes/st/3_imgGaussianBlur.png", imgGray);
  85.        
  86.         Imgproc.Sobel(imgGaussianBlur, imgSobel, -1, 1, 0);
  87.         //Imgcodecs.imwrite("changes/st/4_imgSobel.png", imgSobel);
  88.        
  89.         Imgproc.threshold(imgSobel, imgThreshold, 0, 255,  CV_THRESH_OTSU + CV_THRESH_BINARY);
  90.         //Imgcodecs.imwrite("changes/st/5_imgThreshold.png", imgThreshold);
  91.        
  92.         Imgproc.adaptiveThreshold(imgSobel, imgAdaptiveThreshold, 255, CV_ADAPTIVE_THRESH_GAUSSIAN_C, CV_THRESH_BINARY_INV, 75, 35);
  93.         //Imgcodecs.imwrite("changes/st/5_imgAdaptiveThreshold.png", imgAdaptiveThreshold);
  94.        
  95.         Imgproc.adaptiveThreshold(imgGaussianBlur, imgAdaptiveThreshold_forCrop, 255, CV_ADAPTIVE_THRESH_MEAN_C, CV_THRESH_BINARY, 99, 4);
  96.         //Imgcodecs.imwrite("changes/st/5_imgAdaptiveThreshold_forCrop.png", imgAdaptiveThreshold_forCrop);
  97.        
  98.         Mat element = getStructuringElement(MORPH_RECT, new Size(17, 3));
  99.         Imgproc.morphologyEx(imgAdaptiveThreshold, imgMoprhological, CV_MOP_CLOSE, element); //или imgThreshold
  100.         //Imgcodecs.imwrite("changes/st/6_imgMoprhologicald.png", imgMoprhological);
  101.        
  102.         imgContours = imgMoprhological.clone();
  103.         Imgproc.findContours(imgContours,
  104.                 contours,
  105.                 new Mat(),
  106.                 Imgproc.RETR_LIST,
  107.                 Imgproc.CHAIN_APPROX_SIMPLE);
  108.         Imgproc.drawContours(imgContours, contours, -1, new Scalar(255,0,0));
  109.         //Imgcodecs.imwrite("changes/st/7_imgContours.png", imgContours);  
  110.        
  111.         imgMinAreaRect = img.clone();
  112.         if (contours.size() > 0) {
  113.             for (MatOfPoint matOfPoint : contours) {
  114.                     MatOfPoint2f points = new MatOfPoint2f(matOfPoint.toArray());
  115.  
  116.                     RotatedRect box = Imgproc.minAreaRect(points);
  117.                     if(checkRatio(box)){
  118.                         Imgproc.rectangle(imgMinAreaRect, box.boundingRect().tl(), box.boundingRect().br(), new Scalar(0, 0, 255));
  119.                         imgDetectedPlateCandidate = new Mat(imgAdaptiveThreshold_forCrop, box.boundingRect());
  120.                         if(checkDensity(imgDetectedPlateCandidate))
  121.                             imgDetectedPlateTrue = imgDetectedPlateCandidate.clone();
  122.                     }
  123.                     else
  124.                         Imgproc.rectangle(imgMinAreaRect, box.boundingRect().tl(), box.boundingRect().br(), new Scalar(0, 255, 0));
  125.             }
  126.         }
  127.        
  128.         //Imgcodecs.imwrite("changes/st/8_imgMinAreaRect.png", imgMinAreaRect);
  129.         //Imgcodecs.imwrite("changes/st/9_imgDetectedPlateCandidate.png", imgDetectedPlateCandidate);
  130.         Imgcodecs.imwrite("changes/st/10_imgDetectedPlateTrue.png", imgDetectedPlateTrue);
  131.        
  132.         File imageFile = new File("changes/st/10_imgDetectedPlateTrue.png");
  133.         ITesseract instance = new Tesseract();
  134.         instance.setLanguage("eng");
  135.         instance.setTessVariable("tessedit_char_whitelist", "acekopxyABCEHKMOPTXY0123456789");
  136.         try {
  137.             String result = instance.doOCR(imageFile);
  138.            System.out.println(result);
  139.         } catch (TesseractException e) {
  140.             System.err.println(e.getMessage());
  141.         }  
  142.     }
  143. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement