示例#1
0
        static void FindLaneInTheVideo(string path)
        {
            VideoCapture capture      = new VideoCapture(path);
            Mat          workAreaMask = CreateMask();

            using (Window win1 = new Window("test1"))
            {
                Mat image = new Mat();
                //  We will save previous results here
                List <List <Sensor> > oldResultGroups = null;
                int[] countTaked = new int[2] {
                    0, 0
                };
                while (true)
                {
                    DateTime dt1 = DateTime.Now;
                    capture.Read(image);
                    if (image.Empty())
                    {
                        break;
                    }

                    if (capture.PosFrames % 2 != 0)
                    {
                        continue;
                    }

                    //  Get the work area
                    Mat image_s = image.SubMat(Camera.vert_frame[0], Camera.vert_frame[1],
                                               Camera.hor_frame[0], Camera.hor_frame[1]);
                    Mat workArea = new Mat();
                    image_s.CopyTo(workArea, workAreaMask);

                    //  Get HSV, grat and canny
                    Mat hsv_image = workArea.CvtColor(ColorConversionCodes.RGB2HSV);
                    Mat canny1    = hsv_image.Canny(40, 60);
                    Mat gray      = workArea.CvtColor(ColorConversionCodes.BGR2GRAY);
                    Mat canny2    = gray.Canny(40, 60);
                    Mat canny     = new Mat();
                    Cv2.BitwiseAnd(canny1, canny2, canny);

                    //  Get, filter and draw contours
                    Mat hsv_contoures = new Mat();
                    hsv_image.CopyTo(hsv_contoures);
                    var contoures = FindContoures(canny);
                    hsv_contoures.DrawContours(contoures, -1, Scalar.Red);

                    //  Get indexers
                    MatOfByte3 hsv_cont_ind     = new MatOfByte3(hsv_contoures);
                    MatOfByte3 hsv_ind          = new MatOfByte3(hsv_image);
                    var        hsv_cont_indexer = hsv_cont_ind.GetIndexer();
                    var        hsv_indexer      = hsv_ind.GetIndexer();

                    //  Make steps of the algorithm
                    List <Sensor>         sensors                = GetSensors(hsv_contoures, hsv_cont_indexer);
                    List <Sensor>         filteredByContours     = FilterByContours(sensors, hsv_cont_indexer);
                    List <Sensor>         filteredByColors       = FilterByColorAndChangeColor(filteredByContours, hsv_indexer);
                    List <Sensor>         filteredByNearSensors  = FilterByNearSensors(filteredByColors);
                    List <List <Sensor> > groupedByAngle         = GroupByAngle(filteredByNearSensors).Where(g => g.Count > 2).ToList();
                    List <List <Sensor> > groupedByDistance      = GroupByDistance(groupedByAngle).Where(g => g.Count > 2).ToList();
                    List <List <Sensor> > groupedWithoudCovering = DeleteCovering(groupedByDistance);
                    List <List <Sensor> > unionGroups            = UnionGroups(groupedWithoudCovering).Where(g => g.Count > 2).ToList();
                    List <List <Sensor> > resultGroups           = SelectGroups(unionGroups, oldResultGroups, ref countTaked);
                    image.SaveImage("image.png");
                    //  Draw the result
                    foreach (var group in resultGroups)
                    {
                        if (group != null)
                        {
                            foreach (var line in GetLinesForGroup(group))
                            {
                                image.Line(line.x1 + Camera.hor_frame[0], line.y1 + Camera.vert_frame[0],
                                           line.x2 + Camera.hor_frame[0], line.y2 + Camera.vert_frame[0], Scalar.Blue, 5);
                            }
                        }
                    }
                    image.SaveImage("res.png");
                    Mat imageForDisplay = image.Resize(new Size(0, 0), 0.5, 0.5);
                    win1.ShowImage(imageForDisplay);
                    oldResultGroups = resultGroups;

                    DateTime dt2 = DateTime.Now;
                    Console.WriteLine("{0}\tms", (dt2 - dt1).TotalMilliseconds);

                    int key = Cv2.WaitKey(0);
                    if (key == 27)
                    {
                        break;            //escape
                    }
                    //  Free resourses
                    image_s.Release();
                    workArea.Release();
                    hsv_ind.Release();
                    hsv_cont_ind.Release();
                    gray.Release();
                    canny1.Release();
                    canny2.Release();
                    canny.Release();
                    hsv_image.Release();
                    hsv_contoures.Release();
                }
            }
        }
        static void FindLaneInTheImage(string path)
        {
            Mat workAreaMask = CreateMask();

            using (Window win1 = new Window("test1"))
            {
                Mat image = new Mat(path);
                //  Get the work area
                Mat imageS = image.SubMat(Camera.vert_frame[0], Camera.vert_frame[1],
                                          Camera.hor_frame[0], Camera.hor_frame[1]);
                Mat workArea = new Mat();
                imageS.CopyTo(workArea, workAreaMask);

                //  Get HSV, gray and canny
                Mat hsvImage = workArea.CvtColor(ColorConversionCodes.RGB2HSV);
                Mat canny1   = hsvImage.Canny(40, 60);
                Mat gray     = workArea.CvtColor(ColorConversionCodes.BGR2GRAY);
                Mat canny2   = gray.Canny(40, 60);
                Mat canny    = new Mat();
                Cv2.BitwiseAnd(canny1, canny2, canny);

                //  Get, filter and draw contours
                Mat hsvContoures = new Mat();
                hsvImage.CopyTo(hsvContoures);
                var contoures = FindContoures(canny);
                hsvContoures.DrawContours(contoures, -1, Scalar.Red);

                //  Get indexers
                MatOfByte3 hsvContInd     = new MatOfByte3(hsvContoures);
                MatOfByte3 hsvInd         = new MatOfByte3(hsvImage);
                var        hsvContIndexer = hsvContInd.GetIndexer();
                var        hsvIndexer     = hsvInd.GetIndexer();

                //  Make steps of the algorithm
                List <Sensor>         sensors                = GetSensors(hsvContoures, hsvContIndexer);
                List <Sensor>         filteredByContours     = FilterByContours(sensors, hsvContIndexer);
                List <Sensor>         filteredByColors       = FilterByColorAndChangeColor(filteredByContours, hsvIndexer);
                List <Sensor>         filteredByNearSensors  = FilterByNearSensors(filteredByColors);
                List <List <Sensor> > groupedByAngle         = GroupByAngle(filteredByNearSensors).Where(g => g.Count > 2).ToList();
                List <List <Sensor> > groupedByDistance      = GroupByDistance(groupedByAngle).Where(g => g.Count > 2).ToList();
                List <List <Sensor> > groupedWithoudCovering = DeleteCovering(groupedByDistance);
                List <List <Sensor> > unionGroups            = UnionGroups(groupedWithoudCovering).Where(g => g.Count > 2).ToList();
                List <List <Sensor> > resultGroups           = SelectGroups(unionGroups);

                //  Draw the result
                foreach (var group in resultGroups)
                {
                    if (group != null)
                    {
                        foreach (var line in GetLinesForGroup(group))
                        {
                            image.Line(line.x1 + Camera.hor_frame[0], line.y1 + Camera.vert_frame[0],
                                       line.x2 + Camera.hor_frame[0], line.y2 + Camera.vert_frame[0], Scalar.Blue, 5);
                        }
                    }
                }

                Mat imageForDisplay = image.Resize(new Size(0, 0), 0.5, 0.5);
                win1.ShowImage(imageForDisplay);
                Cv2.WaitKey(0);

                //  Free resourses
                image.Release();
                imageS.Release();
                workArea.Release();
                hsvInd.Release();
                hsvContInd.Release();
                gray.Release();
                canny1.Release();
                canny2.Release();
                canny.Release();
                hsvImage.Release();
                hsvContoures.Release();
            }
        }