Esempio n. 1
0
        }//end loadKNNData

        public static List <PossiblePlate> detectCharsInPlates(List <PossiblePlate> listOfPossiblePlates)
        {
            //frmMain frm = new frmMain();

            int    intPlateCounter = 0;
            Mat    imgContours;
            Random random = new Random();

            if (listOfPossiblePlates == null)
            {
                return(listOfPossiblePlates);
            }
            else if (listOfPossiblePlates.Count == 0)
            {
                return(listOfPossiblePlates);
            }

            foreach (PossiblePlate possiblePlate in listOfPossiblePlates)
            {
                Preprocess.preprocess(possiblePlate.imgPlate, possiblePlate.imgGrayscale, possiblePlate.imgThresh);

                //if (frm.IsCheckedSteps() == true)
                //{
                //    CvInvoke.Imshow("5a", possiblePlate.imgPlate);
                //    CvInvoke.Imshow("5b", possiblePlate.imgGrayscale);
                //    CvInvoke.Imshow("5c", possiblePlate.imgThresh);
                //}

                CvInvoke.Resize(possiblePlate.imgThresh, possiblePlate.imgThresh, new Size(), 1.6, 1.6);

                CvInvoke.Threshold(possiblePlate.imgThresh, possiblePlate.imgThresh, 0.0, 255.0, ThresholdType.Binary | ThresholdType.Otsu);//??????????????????  brakuje Or'a

                //if (frm.IsCheckedSteps() == true)
                //{
                //    CvInvoke.Imshow("5d", possiblePlate.imgThresh);
                //}

                List <PossibleChar> listOfPossibleCharsInPlate = findPossibleCharsInPlate(possiblePlate.imgGrayscale, possiblePlate.imgThresh);

                //if (frm.IsCheckedSteps() == true)
                //{
                //    imgContours = new Mat(possiblePlate.imgThresh.Size, DepthType.Cv8U, 3);

                //    VectorOfVectorOfPoint contours = new VectorOfVectorOfPoint();

                //    foreach (PossibleChar possibleChar in listOfPossibleCharsInPlate)
                //    {
                //        contours.Push(possibleChar.contour);
                //    }

                //    CvInvoke.DrawContours(imgContours, contours, -1, SCALAR_WHITE);

                //    CvInvoke.Imshow("6", imgContours);
                //}

                List <List <PossibleChar> > listOfListsOfMatchingCharsInPlate = findListOfListsOfMatchingChars(listOfPossibleCharsInPlate);

                //if (frm.IsCheckedSteps() == true)
                //{
                //    imgContours = new Mat(possiblePlate.imgThresh.Size, DepthType.Cv8U, 3);

                //    VectorOfVectorOfPoint contours = new VectorOfVectorOfPoint();

                //    foreach (List<PossibleChar> listOfMatchingChars in listOfListsOfMatchingCharsInPlate)
                //    {
                //        int intRandomBlue = random.Next(0, 256);
                //        int intRandomGreen = random.Next(0, 256);
                //        int intRandomRed = random.Next(0, 256);

                //        foreach (PossibleChar matchingChar in listOfMatchingChars)
                //        {
                //            contours.Push(matchingChar.contour);
                //        }
                //        CvInvoke.DrawContours(imgContours, contours, -1, new MCvScalar((double)(intRandomBlue), (double)(intRandomGreen), (double)(intRandomRed)));
                //    }
                //    CvInvoke.Imshow("7", imgContours);
                //}

                if (listOfListsOfMatchingCharsInPlate == null)
                {
                    //if (frm.IsCheckedSteps() == true)
                    //{
                    //    intPlateCounter = intPlateCounter + 1;
                    //    CvInvoke.DestroyWindow("8");
                    //    CvInvoke.DestroyWindow("9");
                    //    CvInvoke.DestroyWindow("10");
                    //    CvInvoke.WaitKey(0);
                    //}
                    possiblePlate.strChars = "";
                }
                else if (listOfListsOfMatchingCharsInPlate.Count == 0)
                {
                    //if (frm.IsCheckedSteps() == true)
                    //{
                    //    intPlateCounter = intPlateCounter + 1;
                    //    CvInvoke.DestroyWindow("8");
                    //    CvInvoke.DestroyWindow("9");
                    //    CvInvoke.DestroyWindow("10");
                    //    CvInvoke.WaitKey(0);
                    //}
                    possiblePlate.strChars = "";
                }

                for (int i = 0; i <= listOfListsOfMatchingCharsInPlate.Count - 1; i++)
                {
                    listOfListsOfMatchingCharsInPlate[i].Sort((oneChar, otherChar) => oneChar.boundingRect.X.CompareTo(otherChar.boundingRect.X));

                    listOfListsOfMatchingCharsInPlate[i] = removeInnerOverlappingChars(listOfListsOfMatchingCharsInPlate[i]);
                }

                //if (frm.IsCheckedSteps() == true)
                //{
                //    imgContours = new Mat(possiblePlate.imgThresh.Size, DepthType.Cv8U, 3);

                //    foreach (var listOfMatchingChars in listOfListsOfMatchingCharsInPlate)
                //    {
                //        int intRandomBlue = random.Next(0, 256);
                //        int intRandomGreen = random.Next(0, 256);
                //        int intRandomRed = random.Next(0, 256);

                //        VectorOfVectorOfPoint contours = new VectorOfVectorOfPoint();//? posprawdzac czy maja byc te same zmienne

                //        foreach (var matchingChar in listOfMatchingChars)
                //        {
                //            contours.Push(matchingChar.contour);
                //        }
                //        CvInvoke.DrawContours(imgContours, contours, -1, new MCvScalar((double)(intRandomBlue), (double)(intRandomGreen), (double)(intRandomRed)));
                //        CvInvoke.Imshow("8", imgContours);
                //    }
                //}

                int intLenOfLongestListOfChars   = 0;
                int intIndexOfLongestListOfChars = 0;

                for (int i = 0; i <= listOfListsOfMatchingCharsInPlate.Count - 1; i++)
                {
                    if (listOfListsOfMatchingCharsInPlate[i].Count > intLenOfLongestListOfChars)
                    {
                        intLenOfLongestListOfChars   = listOfListsOfMatchingCharsInPlate[i].Count;
                        intIndexOfLongestListOfChars = i;
                    }
                }

                if (intIndexOfLongestListOfChars > 0)
                {
                    List <PossibleChar> longestListOfMatchingCharsInPlate = listOfListsOfMatchingCharsInPlate[intIndexOfLongestListOfChars - 1];

                    //if (frm.IsCheckedSteps() == true)
                    //{
                    //    imgContours = new Mat(possiblePlate.imgThresh.Size, DepthType.Cv8U, 3);
                    //    VectorOfVectorOfPoint contours = new VectorOfVectorOfPoint();//? posprawdzac czy maja byc te same zmienne

                    //    foreach (var matchingChar in longestListOfMatchingCharsInPlate)
                    //    {
                    //        contours.Push(matchingChar.contour);
                    //    }

                    //    CvInvoke.DrawContours(imgContours, contours, -1, SCALAR_WHITE);
                    //    CvInvoke.Imshow("9", imgContours);
                    //}

                    possiblePlate.strChars = recognizeCharsInPlate(possiblePlate.imgThresh, longestListOfMatchingCharsInPlate);
                }
                //if (frm.IsCheckedSteps() == true)
                //{
                //    intPlateCounter = intPlateCounter + 1;
                //    CvInvoke.WaitKey(0);
                //}
            }//end foreach

            //jakis tekst do tekstboxa   ewentulnie dorobic

            return(listOfPossiblePlates);
        }//end detectcharsinplate
        public static List <PossiblePlate> detectPlatesInScene(Mat imgOriginalScene)
        {
            List <PossiblePlate> listOfPossiblePlates = new List <PossiblePlate>();

            Mat imgGrayscaleScene = new Mat();
            Mat imgThreshScene    = new Mat();
            Mat imgContours       = new Mat(imgOriginalScene.Size, DepthType.Cv8U, 3);

            Random random = new Random();

            CvInvoke.DestroyAllWindows();

            //if(frm.IsCheckedSteps()==true)
            //{
            //    CvInvoke.Imshow("0", imgOriginalScene);
            //}

            Preprocess.preprocess(imgOriginalScene, imgGrayscaleScene, imgThreshScene);

            //if (frm.IsCheckedSteps() == true)
            //{
            //     CvInvoke.Imshow("1a", imgGrayscaleScene);
            //     CvInvoke.Imshow("1b", imgThreshScene);
            //}

            List <PossibleChar> listOfPossibleCharsInScene = findPossibleCharsInScene(imgThreshScene);

            //if (frm.IsCheckedSteps() == true)
            //{
            //    VectorOfVectorOfPoint contours = new VectorOfVectorOfPoint();

            //    foreach (PossibleChar possibleChar in listOfPossibleCharsInScene)
            //    {
            //        contours.Push(possibleChar.contour);
            //    }

            //    CvInvoke.DrawContours(imgContours, contours, -1, SCALAR_WHITE);
            //    CvInvoke.Imshow("2b", imgContours);
            //}

            List <List <PossibleChar> > listOfListsOfMatchingCharsInScene = DetectChars.findListOfListsOfMatchingChars(listOfPossibleCharsInScene);



            //if (frm.IsCheckedSteps() == true)
            //{
            //    imgContours = new Mat(imgOriginalScene.Size, DepthType.Cv8U, 3);

            //    foreach (var listOfMatchingChars in listOfListsOfMatchingCharsInScene)
            //    {
            //        int intRandomBlue = random.Next(0, 256);
            //        int intRandomGreen = random.Next(0, 256);
            //        int intRandomRed = random.Next(0, 256);

            //        VectorOfVectorOfPoint contours = new VectorOfVectorOfPoint();

            //        foreach (PossibleChar matchingChar in listOfMatchingChars)
            //        {
            //            contours.Push(matchingChar.contour);
            //        }

            //        CvInvoke.DrawContours(imgContours, contours, -1, new MCvScalar((double)(intRandomBlue), (double)(intRandomGreen), (double)(intRandomRed)));

            //    }
            //    CvInvoke.Imshow("3", imgContours);
            //}

            PossiblePlate possiblePlate;//?????????????

            foreach (var listOfMatchingChars in listOfListsOfMatchingCharsInScene)
            {
                possiblePlate = extractPlate(imgOriginalScene, listOfMatchingChars);//??????????????????

                if (possiblePlate.imgPlate != null)
                {
                    listOfPossiblePlates.Add(possiblePlate);
                }
            }

            tmptxt = $"possible plates: {listOfPossiblePlates.Count}";
            // dorobic wpis do txtboxa ile jest tablic mozliwych

            //if (frm.IsCheckedSteps() == true)
            //{
            //    CvInvoke.Imshow("4a", imgContours);

            //    for (int i = 0; i <= listOfPossiblePlates.Count - 1; i++)
            //    {
            //        PointF[] ptfRectPoints = new PointF[4];

            //        ptfRectPoints = listOfPossiblePlates[i].rrLocationOfPlateInScene.GetVertices();

            //        Point pt0 = new Point((int)(ptfRectPoints[0].X), (int)(ptfRectPoints[0].Y));
            //        Point pt1 = new Point((int)(ptfRectPoints[1].X), (int)(ptfRectPoints[1].Y));
            //        Point pt2 = new Point((int)(ptfRectPoints[2].X), (int)(ptfRectPoints[2].Y));
            //        Point pt3 = new Point((int)(ptfRectPoints[3].X), (int)(ptfRectPoints[3].Y));

            //        CvInvoke.Line(imgContours, pt0, pt1, SCALAR_RED, 2);
            //        CvInvoke.Line(imgContours, pt1, pt2, SCALAR_RED, 2);
            //        CvInvoke.Line(imgContours, pt2, pt3, SCALAR_RED, 2);
            //        CvInvoke.Line(imgContours, pt3, pt0, SCALAR_RED, 2);

            //        CvInvoke.Imshow("4a", imgContours);
            //        CvInvoke.Imshow("4b", listOfPossiblePlates[i].imgPlate);
            //        CvInvoke.WaitKey(0);
            //    }
            //}
            return(listOfPossiblePlates);
        }//end detectplatesinscene