Ejemplo n.º 1
0
        ///Improve the quality of the image.
        ///Process the image.

        public async Task <string> GetTextFromImage(Bitmap image)
        {
            var ocr           = new TesseractOCR();
            var improvedImage = ImproveImageQuality(image);
            var result        = await ocr.GetText(improvedImage);

            return(result);
        }
Ejemplo n.º 2
0
        private void FindLicensePlate(
            VectorOfVectorOfPoint contours, int[,] hierachy, int idx, IInputArray gray, IInputArray canny,
            List <IInputOutputArray> licensePlateImagesList, List <IInputOutputArray> filteredLicensePlateImagesList, List <RotatedRect> detectedLicensePlateRegionList,
            List <String> licenses, int ocr_mode, int threshold_parameter)
        {
            for (; idx >= 0; idx = hierachy[idx, 0])
            {
                int numberOfChildren = GetNumberOfChildren(hierachy, idx);
                //if it does not contains any children (charactor), it is not a license plate region
                if (numberOfChildren == 0)
                {
                    continue;
                }

                using (VectorOfPoint contour = contours[idx])
                {
                    if (CvInvoke.ContourArea(contour) > 400)
                    {
                        if (numberOfChildren < 6)
                        {
                            //If the contour has less than 6 children, it is not a license plate (assuming license plate has at least 3 charactor)
                            //However we should search the children of this contour to see if any of them is a license plate
                            FindLicensePlate(contours, hierachy, hierachy[idx, 2], gray, canny, licensePlateImagesList,
                                             filteredLicensePlateImagesList, detectedLicensePlateRegionList, licenses, ocr_mode, threshold_parameter);
                            continue;
                        }

                        RotatedRect box = CvInvoke.MinAreaRect(contour);
                        if (box.Angle < -45.0)
                        {
                            float tmp = box.Size.Width;
                            box.Size.Width  = box.Size.Height;
                            box.Size.Height = tmp;
                            box.Angle      += 90.0f;
                        }
                        else if (box.Angle > 45.0)
                        {
                            float tmp = box.Size.Width;
                            box.Size.Width  = box.Size.Height;
                            box.Size.Height = tmp;
                            box.Angle      -= 90.0f;
                        }

                        double whRatio = (double)box.Size.Width / box.Size.Height;
                        if (!(3.0 < whRatio && whRatio < 10.0))
                        //if (!(1.0 < whRatio && whRatio < 2.0))
                        {
                            //if the width height ratio is not in the specific range,it is not a license plate
                            //However we should search the children of this contour to see if any of them is a license plate
                            //Contour<Point> child = contours.VNext;
                            if (hierachy[idx, 2] > 0)
                            {
                                FindLicensePlate(contours, hierachy, hierachy[idx, 2], gray, canny, licensePlateImagesList,
                                                 filteredLicensePlateImagesList, detectedLicensePlateRegionList, licenses, ocr_mode, threshold_parameter);
                            }
                            continue;
                        }

                        using (UMat tmp1 = new UMat())
                            using (UMat tmp2 = new UMat())
                            {
                                PointF[] srcCorners = box.GetVertices();

                                PointF[] destCorners = new PointF[] {
                                    new PointF(0, box.Size.Height - 1),
                                    new PointF(0, 0),
                                    new PointF(box.Size.Width - 1, 0),
                                    new PointF(box.Size.Width - 1, box.Size.Height - 1)
                                };

                                using (Mat rot = CvInvoke.GetAffineTransform(srcCorners, destCorners))
                                {
                                    //box.Center.X -= 20;
                                    //box.Size.Height += 20;
                                    //box.Size.Width += 20;
                                    //box.Center.Y -= 20;

                                    CvInvoke.WarpAffine(gray, tmp1, rot, Size.Round(box.Size));
                                }
                                SaveImageClass.SaveImage(gray, "gray2.jpg");
                                SaveImageClass.SaveImage(tmp1, "tmp1.jpg");
                                //resize the license plate such that the front is ~ 10-12. This size of front results in better accuracy from tesseract
                                Size   approxSize = new Size(240, 180);
                                double scale      = Math.Min(approxSize.Width / box.Size.Width, approxSize.Height / box.Size.Height);
                                Size   newSize    = new Size((int)Math.Round(box.Size.Width * scale), (int)Math.Round(box.Size.Height * scale));
                                CvInvoke.Resize(tmp1, tmp2, newSize, 0, 0, Inter.Cubic);

                                SaveImageClass.SaveImage(tmp1, "tmp1after.jpg");
                                SaveImageClass.SaveImage(tmp2, "tmp2.jpg");

                                //removes some pixels from the edge
                                int       edgePixelSize = 3;
                                Rectangle newRoi        = new Rectangle(new Point(edgePixelSize, edgePixelSize),
                                                                        tmp2.Size - new Size(2 * edgePixelSize, 2 * edgePixelSize));

                                UMat plate = new UMat(tmp2, newRoi);

                                SaveImageClass.SaveImage(plate, "plate.jpg");
                                UMat filteredPlate = new UMat();

                                filteredPlate = FilterPlate(plate, threshold_parameter);

                                SaveImageClass.SaveImage(filteredPlate, "filtered.jpg");
                                StringBuilder strBuilder = new StringBuilder();
                                switch (ocr_mode)
                                {
                                case 1:
                                {
                                    strBuilder = TesseractOCR.GetText(filteredPlate, _ocr);
                                    break;
                                }

                                case 2:
                                {
                                    strBuilder = GoogleApiOCR.GetText(filteredPlate);
                                    break;
                                }

                                case 3:
                                {
                                    strBuilder = ComputerVisionOCR.GetText(filteredPlate);
                                    break;
                                }

                                default:
                                    break;
                                }
                                if (strBuilder != null)
                                {
                                    licenses.Add(strBuilder.ToString());
                                    licensePlateImagesList.Add(plate);
                                    filteredLicensePlateImagesList.Add(filteredPlate);
                                    detectedLicensePlateRegionList.Add(box);
                                }
                            }
                    }
                }
            }
        }