Ejemplo n.º 1
0
        public static void test_detector(string datacfg, string cfgfile, string weightfile, string filename, float thresh)
        {
            var    options  = OptionList.read_data_cfg(datacfg);
            string nameList = OptionList.option_find_str(options, "names", "Data.Data/names.list");

            string[] names = Data.Data.get_labels(nameList);

            Image[][] alphabet = LoadArgs.load_alphabet();
            Network   net      = Parser.parse_network_cfg(cfgfile);

            if (string.IsNullOrEmpty(weightfile))
            {
                Parser.load_weights(net, weightfile);
            }
            Network.set_batch_network(net, 1);
            Utils.Rand = new Random(2222222);
            var sw = new Stopwatch();

            string input = "";
            int    j;
            float  nms = .4f;

            while (true)
            {
                if (!string.IsNullOrEmpty(filename))
                {
                    input = filename;
                }
                else
                {
                    Console.Write($"Enter Image Path: ");

                    input = Console.ReadLine();
                    if (string.IsNullOrEmpty(input))
                    {
                        return;
                    }
                    input = input.TrimEnd();
                }
                Image im    = LoadArgs.load_image_color(input, 0, 0);
                Image sized = LoadArgs.resize_image(im, net.W, net.H);
                Layer l     = net.Layers[net.N - 1];

                Box[]     boxes = new Box[l.W * l.H * l.N];
                float[][] probs = new float[l.W * l.H * l.N][];
                for (j = 0; j < l.W * l.H * l.N; ++j)
                {
                    probs[j] = new float[l.Classes];
                }

                float[] x = sized.Data;
                sw.Start();
                Network.network_predict(net, x);
                sw.Stop();
                Console.Write($"%s: Predicted ini %f seconds.\n", input, sw.Elapsed.Seconds);
                Layer.get_region_boxes(l, 1, 1, thresh, probs, boxes, false, new int[0]);
                if (nms != 0)
                {
                    Box.do_nms_sort(boxes, probs, l.W * l.H * l.N, l.Classes, nms);
                }
                LoadArgs.draw_detections(im, l.W * l.H * l.N, thresh, boxes, probs, names, alphabet, l.Classes);
                LoadArgs.save_image(im, "predictions");
                LoadArgs.show_image(im, "predictions");

                CvInvoke.WaitKey();
                CvInvoke.DestroyAllWindows();
                if (!string.IsNullOrEmpty(filename))
                {
                    break;
                }
            }
        }
Ejemplo n.º 2
0
        private void Button_Click(object sender, RoutedEventArgs e)
        {
            // decode();
            // buttonClick();

            //以锐化效果显示图像
            try
            {
                Bitmap oldBitmap = new Bitmap(@"D:\Desktop\DIYcode\微孔-识别\RT5911A9OMZ3000973.jpg");
                oldBitmap.RotateFlip(RotateFlipType.Rotate270FlipNone);
                int    Height    = oldBitmap.Height;
                int    Width     = oldBitmap.Width;
                Bitmap newBitmap = new Bitmap(Width, Height);

                Color pixel;
                //拉普拉斯模板
                //int[] Laplacian = { -1, -1, -1, -1, 9, -1, -1, -1, -1 };
                //for (int x = 1; x < Width - 1; x++)
                //    for (int y = 1; y < Height - 1; y++)
                //    {
                //        int r = 0, g = 0, b = 0;
                //        int Index = 0;
                //        for (int col = -1; col <= 1; col++)
                //            for (int row = -1; row <= 1; row++)
                //            {
                //                pixel = oldBitmap.GetPixel(x + row, y + col); r += pixel.R * Laplacian[Index];
                //                g += pixel.G * Laplacian[Index];
                //                b += pixel.B * Laplacian[Index];
                //                Index++;
                //            }
                //        //处理颜色值溢出
                //        r = r > 255 ? 255 : r;
                //        r = r < 0 ? 0 : r;
                //        g = g > 255 ? 255 : g;
                //        g = g < 0 ? 0 : g;
                //        b = b > 255 ? 255 : b;
                //        b = b < 0 ? 0 : b;
                //        newBitmap.SetPixel(x - 1, y - 1, Color.FromArgb(r, g, b));
                //    }

                // new GrayBitmapData(newBitmap).NewFilter(9);
                // newBitmap.Save(@"D:\Desktop\DIYcode\newBitmap.jpg");


                Image <Gray, Byte> matimg = OtsuThreshold(oldBitmap);

                matimg.Save(@"D:\Desktop\DIYcode\matimg.jpg");
                matimg = matimg.SmoothMedian(11);
                matimg.Save(@"D:\Desktop\DIYcode\SmoothMedian.jpg");
                Image <Gray, Byte> edges = new Image <Gray, byte>(newBitmap.Width, newBitmap.Height);

                Image <Bgr, Byte> contoursimg = new Image <Bgr, byte>(oldBitmap);

                Mat hierarchy = new Mat();
                // Image<Gray, Byte> hierarchy = new Image<Gray, byte>(matimg.Width, matimg.Height);

                VectorOfVectorOfPoint contours = new VectorOfVectorOfPoint();

                VectorOfVectorOfPointF VectorOfPointF = new VectorOfVectorOfPointF();

                CvInvoke.Canny(matimg, edges, 0, 200);

                CvInvoke.FindContours(edges,
                                      contours,
                                      hierarchy,
                                      RetrType.Tree,
                                      ChainApproxMethod.ChainApproxSimple);
                List <int> fater_contours = new List <int>();

                int[,,] arraylist = ((int[, , ])(hierarchy.GetData()));

                for (int k = 0; k < contours.Size; k++)
                {
                    int i = k;
                    int c = 0;
                    while (arraylist[0, i, 2] != -1)
                    {
                        i = arraylist[0, i, 2];
                        c = c + 1;
                        if (c >= 5)
                        {
                            fater_contours.Add(k);
                        }
                    }
                }
                for (int j = 0; j < fater_contours.Count; j++)
                {
                    RotatedRect rect = CvInvoke.MinAreaRect(contours[fater_contours[j]]); //minAreaRect
                    PointF[]    pf   = CvInvoke.BoxPoints(rect);                          //BoxPoints();
                    // VectorOfPointF.Push(new VectorOfPointF(pf));
                    contoursimg.Draw(rect, new Bgr(255, 0, 0), 2);
                    // CvInvoke.DrawContours(new Image<Bgr, Byte>(newBitmap), VectorOfPointF, -1, new MCvScalar(0, 255, 0), 1, LineType.AntiAlias);
                }

                var str = decode(matimg.ToBitmap());
                ImgContorl.Source = ToBitmapsource(contoursimg.ToBitmap());
                //CvInvoke.DrawContours(new Image<Bgr, Byte>(newBitmap), VectorOfPointF, -1 , new MCvScalar(0, 255, 0), 1, LineType.AntiAlias);
                //double area = CvInvoke.ContourArea(contours[k]);
                //if (area > 20000 && area < 30000)
                //{
                //    CvInvoke.DrawContours(contoursimg, contours, k, new MCvScalar(0, 255, 0), 1, Emgu.CV.CvEnum.LineType.AntiAlias);
                //    contoursimg.Save(@"D:\Desktop\DIYcode\contoursimg" + k.ToString() + ".jpg");
                //    vector.Push(ofPoint);
                //}

                //if (area > 3300 && area < 3800)
                //{
                //    CvInvoke.DrawContours(contoursimg, contours, k, new MCvScalar(255, 0, 0), 1, Emgu.CV.CvEnum.LineType.AntiAlias);
                //    contoursimg.Save(@"D:\Desktop\DIYcode\werwer" + k.ToString() + ".jpg");
                //    vector.Push(ofPoint);
                //}

                //if (area>500)
                //{
                //    CvInvoke.DrawContours(contoursimg, contours, k, new MCvScalar(255, 0, 0), 1, Emgu.CV.CvEnum.LineType.AntiAlias);
                //}
                //  CvInvoke.DrawContours(disp, contours, k, new MCvScalar(0, 255, 0), 1, Emgu.CV.CvEnum.LineType.AntiAlias, null, 1);
                //    }
                //}
            }
            catch (Exception ex)
            {
                //  return null;
                //  MessageBox.Show(ex.Message, "信息提示");
            }
        }
Ejemplo n.º 3
0
 /// <summary>
 /// Release the blob trakcer
 /// </summary>
 protected override void DisposeObject()
 {
     CvInvoke.CvBlobTrackerRealease(ref _ptr);
 }
Ejemplo n.º 4
0
        public static void Run(FaceParams MainFace, int SizeBufNum = 3, int BinaryValue = 143)
        {
            //Read the files as an 8-bit Bgr image


            long             detectionTime;
            List <Rectangle> faces = new List <Rectangle>();
            List <Rectangle> eyes  = new List <Rectangle>();

            Detect(MainFace.CurrentFrame, @"Haar\haarcascade_frontalface_default.xml", @"Haar\haarcascade_eye.xml",
                   faces, eyes,
                   out detectionTime);

            if (faces.Count == 1)
            {
                if (Math.Abs(MainFace.PrevFaceSize.X - faces[0].X) < SizeBufNum && !MainFace.PrevFaceSize.IsEmpty)
                {
                    faces[0] = MainFace.PrevFaceSize;
                }
                CvInvoke.Rectangle(MainFace.CurrentFrame, faces[0], new Bgr(Color.Red).MCvScalar, 2);
                MainFace.FaceImg      = new Mat((Mat)MainFace.CurrentFrame, faces[0]);
                MainFace.PrevFaceSize = faces[0];
            }

            if (eyes.Count == 2)

            {
                if (eyes[0].X < eyes[1].X) //0 - правый, 1- левый
                {
                    eyes.Reverse();
                }
                if (Math.Abs(MainFace.PrevRightEyeSize.X - eyes[0].X) < SizeBufNum && !MainFace.PrevRightEyeSize.IsEmpty)
                {
                    eyes[0] = MainFace.PrevRightEyeSize;
                }
                if (Math.Abs(MainFace.PrevLeftEyeSize.X - eyes[1].X) < SizeBufNum && !MainFace.PrevLeftEyeSize.IsEmpty)
                {
                    eyes[1] = MainFace.PrevLeftEyeSize;
                }

                MainFace.PrevRightEyeSize = eyes[0];
                MainFace.PrevLeftEyeSize  = eyes[1];

                MainFace.RightEyeImg = new Mat((Mat)MainFace.CurrentFrame, eyes[0]);
                MainFace.LeftEyeImg  = new Mat((Mat)MainFace.CurrentFrame, eyes[1]);

                CvInvoke.Rectangle(MainFace.CurrentFrame, eyes[0], new Bgr(Color.Blue).MCvScalar, 2);
                CvInvoke.Rectangle(MainFace.CurrentFrame, eyes[1], new Bgr(Color.Green).MCvScalar, 2);


                VectorOfVectorOfPoint RightPupilAreas = PupilDetection.Detect(MainFace.RightEyeImg, BinaryValue, MainFace);
                VectorOfVectorOfPoint LeftPupilAreas  = PupilDetection.Detect(MainFace.LeftEyeImg);

                for (int i = 0; i < RightPupilAreas.Size; i++)
                {
                    CvInvoke.DrawContours(MainFace.RightEyeImg, RightPupilAreas, i, new MCvScalar(255, 0, 0));
                }

                for (int i = 0; i < LeftPupilAreas.Size; i++)
                {
                    CvInvoke.DrawContours(MainFace.LeftEyeImg, LeftPupilAreas, i, new MCvScalar(255, 0, 0));
                }
            }
        }
Ejemplo n.º 5
0
        internal unsafe static void Main(string[] args)
        {
            try
            {
                int    pixelsCount = 1 << 18;
                uint[] pixelsArray = new uint[65536];

                using (DisposableHandle handle = DisposableHandle.Alloc(pixelsArray))
                {
                    NTInvoke.SetUnmanagedMemory(handle, 255, pixelsCount);
                }

                byte[] outputData = null;

                MCvScalar redColor   = MCvScalarExtensions.FromColor(Colors.Red);
                MCvScalar greenColor = MCvScalarExtensions.FromColor(Colors.Green);

                using (PresentationImage prImage = new PresentationImage(256, 256))
                {
                    prImage.WritePixels(pixelsArray);

                    FontFace[] faces = new FontFace[]
                    {
                        FontFace.HersheyComplex,
                        FontFace.HersheyComplexSmall,
                        FontFace.HersheyDuplex,
                        FontFace.HersheyPlain,
                        FontFace.HersheyScriptComplex,
                        FontFace.HersheyScriptSimplex,
                        FontFace.HersheySimplex,
                        FontFace.HersheyTriplex
                    };

                    Parallel.For(0, 8, (int index) =>
                    {
                        Point drawPoint   = new Point(10, 30 + 30 * index);
                        FontFace drawFace = faces[index];

                        string outputText = Enum.GetName(typeof(FontFace), drawFace);

                        CvInvoke.DrawText(prImage, outputText, drawPoint, drawFace, 1.0D, redColor, 1);
                    });

                    outputData = CvInvoke.Imencode(prImage, ImageEncoding.Jpeg, new int[] { 95 });
                }

                if (outputData != null && outputData.Length > 0)
                {
                    using (MemoryStream dataStream = new MemoryStream(outputData))
                    {
                        JpegBitmapDecoder decoder = new JpegBitmapDecoder(dataStream, BitmapCreateOptions.DelayCreation, BitmapCacheOption.OnLoad);
                        BitmapFrame       frame   = decoder.Frames[0];

                        Window imageWindow = new Window()
                        {
                            Height                = 300.0D,
                            Width                 = 500.0D,
                            Title                 = "Image window",
                            ResizeMode            = ResizeMode.CanResize,
                            WindowStartupLocation = WindowStartupLocation.CenterScreen
                        };
                        imageWindow.Content = new Image()
                        {
                            Source = frame
                        };
                        new Application().Run(imageWindow);
                    }
                }
            }
            catch (Exception exc)
            {
                Debug.WriteLine("\tCatched exception: {0}\r\n{1}", exc.Message, exc);
            }
            finally
            {
                Debug.WriteLine("\tData released succesfully.");
            }
        }
Ejemplo n.º 6
0
        //Thanks to Carl Vondrick
        //http://www.juergenbrauer.org/old_wiki/doku.php?id=public:hog_descriptor_computation_and_visualization
        public Image <Bgr, Byte> hogVisualization(Image <Bgr, Byte> image, float[] descriptorValues, int cellSize)
        {
            int   DIMX    = image.Width;
            int   DIMY    = image.Height;
            float zoomFac = 3;

            Image <Bgr, Byte> result = image.Copy();

            result = result.Resize((int)(result.Cols * zoomFac), (int)(result.Rows * zoomFac), Inter.Linear);

            int   gradientBinSize   = 9;
            float radRangeForOneBin = (float)(Math.PI / (float)gradientBinSize); // dividing 180 into 9 bins, how large (in rad) is one bin?

            int cells_in_x_dir = DIMX / cellSize;
            int cells_in_y_dir = DIMY / cellSize;

            // prepare data structure
            float[][][] gradientStrengths = new float[cells_in_y_dir][][];
            int[][]     cellUpdateCounter = new int[cells_in_y_dir][];
            for (int y = 0; y < cells_in_y_dir; y++)
            {
                gradientStrengths[y] = new float[cells_in_x_dir][];
                cellUpdateCounter[y] = new int[cells_in_x_dir];
                for (int x = 0; x < cells_in_x_dir; x++)
                {
                    gradientStrengths[y][x] = new float[gradientBinSize];
                    cellUpdateCounter[y][x] = 0;

                    for (int bin = 0; bin < gradientBinSize; bin++)
                    {
                        gradientStrengths[y][x][bin] = 0.0f;
                    }
                }
            }

            // nr of blocks = nr of cells - 1
            // since there is a new block on each cell (overlapping blocks!) but the last one
            int blocks_in_x_dir = cells_in_x_dir - 1;
            int blocks_in_y_dir = cells_in_y_dir - 1;

            // compute gradient strengths per cell
            int descriptorDataIdx = 0;
            int cellx             = 0;
            int celly             = 0;

            for (int blockx = 0; blockx < blocks_in_x_dir; blockx++)
            {
                for (int blocky = 0; blocky < blocks_in_y_dir; blocky++)
                {
                    // 4 cells per block ...
                    for (int cellNr = 0; cellNr < 4; cellNr++)
                    {
                        // compute corresponding cell nr
                        cellx = blockx;
                        celly = blocky;
                        if (cellNr == 1)
                        {
                            celly++;
                        }
                        if (cellNr == 2)
                        {
                            cellx++;
                        }
                        if (cellNr == 3)
                        {
                            cellx++;
                            celly++;
                        }

                        for (int bin = 0; bin < gradientBinSize; bin++)
                        {
                            float gradientStrength = descriptorValues[descriptorDataIdx];
                            descriptorDataIdx++;

                            gradientStrengths[celly][cellx][bin] += gradientStrength;
                        } // for (all bins)


                        // note: overlapping blocks lead to multiple updates of this sum!
                        // we therefore keep track how often a cell was updated,
                        // to compute average gradient strengths
                        cellUpdateCounter[celly][cellx]++;
                    } // for (all cells)
                }     // for (all block x pos)
            }         // for (all block y pos)


            // compute average gradient strengths
            for (celly = 0; celly < cells_in_y_dir; celly++)
            {
                for (cellx = 0; cellx < cells_in_x_dir; cellx++)
                {
                    float NrUpdatesForThisCell = (float)cellUpdateCounter[celly][cellx];

                    // compute average gradient strenghts for each gradient bin direction
                    for (int bin = 0; bin < gradientBinSize; bin++)
                    {
                        gradientStrengths[celly][cellx][bin] /= NrUpdatesForThisCell;
                    }
                }
            }

            // draw cells
            for (celly = 0; celly < cells_in_y_dir; celly++)
            {
                for (cellx = 0; cellx < cells_in_x_dir; cellx++)
                {
                    int drawX = cellx * cellSize;
                    int drawY = celly * cellSize;

                    int mx = drawX + cellSize / 2;
                    int my = drawY + cellSize / 2;

                    result.Draw(
                        new Rectangle(
                            new Point((int)(drawX * zoomFac), (int)(drawY * zoomFac)),
                            new Size((int)(cellSize * zoomFac), (int)(cellSize * zoomFac))),
                        new Bgr(Color.Gray), 3);

                    // draw in each cell all 9 gradient strengths
                    for (int bin = 0; bin < gradientBinSize; bin++)
                    {
                        float currentGradStrength = gradientStrengths[celly][cellx][bin];

                        // no line to draw?
                        if (currentGradStrength == 0)
                        {
                            continue;
                        }

                        float currRad = bin * radRangeForOneBin + radRangeForOneBin / 2;

                        float dirVecX   = (float)Math.Cos(currRad);
                        float dirVecY   = (float)Math.Sin(currRad);
                        float maxVecLen = (float)(cellSize / 2.0f);
                        float scale     = 2.5f; // just a visualization scale, to see the lines better

                        // compute line coordinates
                        float x1 = mx - dirVecX * currentGradStrength * maxVecLen * scale;
                        float y1 = my - dirVecY * currentGradStrength * maxVecLen * scale;
                        float x2 = mx + dirVecX * currentGradStrength * maxVecLen * scale;
                        float y2 = my + dirVecY * currentGradStrength * maxVecLen * scale;

                        CvInvoke.Line(result,
                                      new Point((int)(x1 * zoomFac), (int)(y1 * zoomFac)),
                                      new Point((int)(x2 * zoomFac), (int)(y2 * zoomFac)),
                                      new MCvScalar(0, 255, 0), 1);
                    } // for (all bins)
                }     // for (cellx)
            }         // for (celly)

            return(result);
        }
Ejemplo n.º 7
0
        public static IEnumerable <Rectangle> DetectSquares(Mat sourceImage)
        {
            Mat destinationImage = new Mat();

            destinationImage.Create(sourceImage.Rows, sourceImage.Cols, sourceImage.Depth, 1);
            Mat greyscaleImage = new Mat();

            CvInvoke.CvtColor(sourceImage, greyscaleImage, ColorConversion.Bgr2Gray);

            Mat detectedEdges = new Mat();

            CvInvoke.GaussianBlur(greyscaleImage, detectedEdges, new Size(1, 1), 1);
            CvInvoke.Canny(detectedEdges, detectedEdges, Treshold, Treshold * 3);
            CvInvoke.Dilate(detectedEdges, detectedEdges, new Mat(), new Point(-1, -1), 3, BorderType.Default, new MCvScalar(255, 255, 255));

            //ImageViewer.Show(detectedEdges);

            List <Rectangle> boxList = new List <Rectangle>();

            //List<LineSegment2D> lines = new List<LineSegment2D>();

            using (VectorOfVectorOfPoint contours = new VectorOfVectorOfPoint())
            {
                CvInvoke.FindContours(detectedEdges, contours, null, RetrType.List, ChainApproxMethod.ChainApproxSimple);
                int count = contours.Size;
                for (int i = 0; i < count; i++)
                {
                    using (VectorOfPoint approxContour = new VectorOfPoint())
                        using (VectorOfPoint approx = contours[i])
                        {
                            CvInvoke.ApproxPolyDP(approx, approxContour, CvInvoke.ArcLength(approx, true) * 0.035, true);
                            Point[]         pts   = approxContour.ToArray();
                            LineSegment2D[] edges = PointCollection.PolyLine(pts, true);
                            //lines.AddRange(edges);

                            double contourArea = CvInvoke.ContourArea(approxContour, true);
                            if (contourArea >= 500 && contourArea <= detectedEdges.Width * detectedEdges.Height / 5)
                            {
                                if (approxContour.Size >= 2)
                                {
                                    bool isRectangle = true;

                                    for (int j = 0; j < edges.Length; j++)
                                    {
                                        double angle = Math.Abs(edges[(j + 1) % edges.Length]
                                                                .GetExteriorAngleDegree(edges[j]));

                                        if (angle < 85 || angle > 95)
                                        {
                                            isRectangle = false;
                                            break;
                                        }
                                    }

                                    if (isRectangle)
                                    {
                                        RotatedRect currentRectangle = CvInvoke.MinAreaRect(approxContour);
                                        Rectangle   minRectangle     = currentRectangle.MinAreaRect();
                                        //int ninetyPercentWidth = minRectangle.Width - (int)(minRectangle.Width * 0.05);
                                        //int ninetyPercentHeight = minRectangle.Height - (int)(minRectangle.Height * 0.05);
                                        //minRectangle.Size = new Size(ninetyPercentWidth, ninetyPercentHeight);
                                        //minRectangle.Offset(5, 5);
                                        boxList.Add(minRectangle);
                                    }
                                }
                            }
                        }
                }
            }

            return(boxList);
        }
Ejemplo n.º 8
0
 /// <summary>
 /// Clears the train descriptor collections.
 /// </summary>
 public void Clear()
 {
     CvInvoke.cveDescriptorMatcherClear(_descriptorMatcherPtr);
 }
Ejemplo n.º 9
0
 /// <summary>
 /// Trains a descriptor matcher (for example, the flann index). In all methods to match, the method
 /// train() is run every time before matching.Some descriptor matchers(for example, BruteForceMatcher)
 /// have an empty implementation of this method.Other matchers really train their inner structures (for
 /// example, FlannBasedMatcher trains flann::Index ).
 /// </summary>
 public void Train()
 {
     CvInvoke.cveDescriptorMatcherTrain(_descriptorMatcherPtr);
 }
Ejemplo n.º 10
0
 /// <summary>
 /// Release the unmanaged resource associated with the BruteForceMatcher
 /// </summary>
 protected override void DisposeObject()
 {
     CvInvoke.CvBruteForceMatcherRelease(ref _ptr);
 }
Ejemplo n.º 11
0
 /// <summary>
 /// Find the k-nearest match
 /// </summary>
 /// <param name="queryDescriptor">An n x m matrix of descriptors to be query for nearest neighbours. n is the number of descriptor and m is the size of the descriptor</param>
 /// <param name="trainIdx">The resulting n x <paramref name="k"/> matrix of descriptor index from the training descriptors</param>
 /// <param name="distance">The resulting n x <paramref name="k"/> matrix of distance value from the training descriptors</param>
 /// <param name="k">Number of nearest neighbors to search for</param>
 /// <param name="mask">Can be null if not needed. An n x 1 matrix. If 0, the query descriptor in the corresponding row will be ignored.</param>
 public void KnnMatch(Matrix <T> queryDescriptor, Matrix <int> trainIdx, Matrix <float> distance, int k, Matrix <Byte> mask)
 {
     CvInvoke.CvDescriptorMatcherKnnMatch(Ptr, queryDescriptor, trainIdx, distance, k, mask);
 }
Ejemplo n.º 12
0
 /// <summary>
 /// Add the model descriptors
 /// </summary>
 /// <param name="modelDescriptors">The model discriptors</param>
 public void Add(Matrix <T> modelDescriptors)
 {
     CvInvoke.CvDescriptorMatcherAdd(_ptr, modelDescriptors);
 }
Ejemplo n.º 13
0
        private static String GetText(Tesseract ocr, Mat image, OCRMode mode, Mat imageColor)
        {
            Bgr drawCharColor = new Bgr(Color.Red);

            if (image.NumberOfChannels == 1)
            {
                CvInvoke.CvtColor(image, imageColor, ColorConversion.Gray2Bgr);
            }
            else
            {
                image.CopyTo(imageColor);
            }


            if (mode == OCRMode.FullPage)
            {
                ocr.SetImage(imageColor);

                if (ocr.Recognize() != 0)
                {
                    throw new Exception("Failed to recognizer image");
                }
                Tesseract.Character[] characters = ocr.GetCharacters();
                //if (characters.Length == 0)
                //{
                //    Mat imgGrey = new Mat();
                //    CvInvoke.CvtColor(image, imgGrey, ColorConversion.Bgr2Gray);
                //    Mat imgThresholded = new Mat();
                //    CvInvoke.Threshold(imgGrey, imgThresholded, 65, 255, ThresholdType.Binary);
                //    ocr.SetImage(imgThresholded);
                //    characters = ocr.GetCharacters();
                //    imageColor = imgThresholded;
                //    if (characters.Length == 0)
                //    {
                //        CvInvoke.Threshold(image, imgThresholded, 190, 255, ThresholdType.Binary);
                //        ocr.SetImage(imgThresholded);
                //        characters = ocr.GetCharacters();
                //        imageColor = imgThresholded;
                //    }
                //}
                foreach (Tesseract.Character c in characters)
                {
                    CvInvoke.Rectangle(imageColor, c.Region, drawCharColor.MCvScalar);
                }

                return(ocr.GetUTF8Text());
            }
            else
            {
                bool checkInvert = true;

                Rectangle[] regions;

                using (
                    ERFilterNM1 er1 = new ERFilterNM1("trained_classifierNM1.xml", 8, 0.00025f, 0.13f, 0.4f, true, 0.1f))
                    using (ERFilterNM2 er2 = new ERFilterNM2("trained_classifierNM2.xml", 0.3f))
                    {
                        int    channelCount = image.NumberOfChannels;
                        UMat[] channels     = new UMat[checkInvert ? channelCount * 2 : channelCount];

                        for (int i = 0; i < channelCount; i++)
                        {
                            UMat c = new UMat();
                            CvInvoke.ExtractChannel(image, c, i);
                            channels[i] = c;
                        }

                        if (checkInvert)
                        {
                            for (int i = 0; i < channelCount; i++)
                            {
                                UMat c = new UMat();
                                CvInvoke.BitwiseNot(channels[i], c);
                                channels[i + channelCount] = c;
                            }
                        }

                        VectorOfERStat[] regionVecs = new VectorOfERStat[channels.Length];
                        for (int i = 0; i < regionVecs.Length; i++)
                        {
                            regionVecs[i] = new VectorOfERStat();
                        }

                        try
                        {
                            for (int i = 0; i < channels.Length; i++)
                            {
                                er1.Run(channels[i], regionVecs[i]);
                                er2.Run(channels[i], regionVecs[i]);
                            }
                            using (VectorOfUMat vm = new VectorOfUMat(channels))
                            {
                                regions = ERFilter.ERGrouping(image, vm, regionVecs, ERFilter.GroupingMethod.OrientationHoriz,
                                                              "trained_classifier_erGrouping.xml", 0.5f);
                            }
                        }
                        finally
                        {
                            foreach (UMat tmp in channels)
                            {
                                if (tmp != null)
                                {
                                    tmp.Dispose();
                                }
                            }
                            foreach (VectorOfERStat tmp in regionVecs)
                            {
                                if (tmp != null)
                                {
                                    tmp.Dispose();
                                }
                            }
                        }

                        Rectangle imageRegion = new Rectangle(Point.Empty, imageColor.Size);
                        for (int i = 0; i < regions.Length; i++)
                        {
                            Rectangle r = ScaleRectangle(regions[i], 1.1);

                            r.Intersect(imageRegion);
                            regions[i] = r;
                        }
                    }


                List <Tesseract.Character> allChars = new List <Tesseract.Character>();
                String allText = String.Empty;
                foreach (Rectangle rect in regions)
                {
                    using (Mat region = new Mat(image, rect))
                    {
                        ocr.SetImage(region);
                        if (ocr.Recognize() != 0)
                        {
                            throw new Exception("Failed to recognize image");
                        }
                        Tesseract.Character[] characters = ocr.GetCharacters();

                        //convert the coordinates from the local region to global
                        for (int i = 0; i < characters.Length; i++)
                        {
                            Rectangle charRegion = characters[i].Region;
                            charRegion.Offset(rect.Location);
                            characters[i].Region = charRegion;
                        }
                        allChars.AddRange(characters);

                        allText += ocr.GetUTF8Text() + Environment.NewLine;
                    }
                }

                Bgr drawRegionColor = new Bgr(Color.Red);
                foreach (Rectangle rect in regions)
                {
                    CvInvoke.Rectangle(imageColor, rect, drawRegionColor.MCvScalar);
                }
                foreach (Tesseract.Character c in allChars)
                {
                    CvInvoke.Rectangle(imageColor, c.Region, drawCharColor.MCvScalar);
                }

                return(allText);
            }
        }
Ejemplo n.º 14
0
 /// <summary>
 /// Create KAZE using the specific values
 /// </summary>
 public KAZE(bool extended = false, bool upright = false, float threshold = 0.001f, int octaves = 4, int sublevels = 4, Diffusivity diffusivity = Diffusivity.PmG2)
 {
     _ptr = CvInvoke.cveKAZEDetectorCreate(
         extended, upright, threshold, octaves, sublevels, diffusivity,
         ref _feature2D, ref _sharedPtr);
 }
Ejemplo n.º 15
0
 static DpmInvoke()
 {
     CvInvoke.Init();
 }
Ejemplo n.º 16
0
 /// <summary>
 /// Add the model descriptors
 /// </summary>
 /// <param name="modelDescriptors">The model descriptors</param>
 public void Add(IInputArray modelDescriptors)
 {
     using (InputArray iaModelDescriptors = modelDescriptors.GetInputArray())
         CvInvoke.cveDescriptorMatcherAdd(_descriptorMatcherPtr, iaModelDescriptors);
 }
Ejemplo n.º 17
0
 static VectorOfOclPlatformInfo()
 {
     CvInvoke.CheckLibraryLoaded();
 }
Ejemplo n.º 18
0
        private async void ReadAllFrames()
        {
            Mat m = new Mat();

            while (IsReadingFrame == true && FrameNo < TotalFrame)
            {
                FrameNo += Convert.ToInt16(numericUpDown1.Value);
                capture.SetCaptureProperty(Emgu.CV.CvEnum.CapProp.PosFrames, FrameNo);
                capture.Read(m);
                CvInvoke.CvtColor(m, _grayFrame, ColorConversion.Bgr2Gray);
                //GrayScale(m.ToImage<Bgr, byte>(), out Image<Gray, byte> grayImage);
                // var bm = Emgu.CV.BitmapExtension.ToBitmap(m);
                CurrentImg = _grayFrame.ToImage <Gray, byte>();
                // var bm = grayImage.ToBitmap();
                //byte* ptr = (byte*)grayImg.MIplImage.ImageData;


                TransformFromImgCVToGrayImage(CurrentImg);


                m_Drawimg = System.Drawing.Image.FromHbitmap(iImage.GetBitmapAddress(GrayImg));

                if (m_bRTMatch)
                {
                    m_Drawbmp = new Bitmap(CurrentImg.Width, CurrentImg.Height, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
                    m_Drawg   = Graphics.FromImage(m_Drawbmp);
                    m_Drawg.DrawImage(m_Drawimg, 0, 0, CurrentImg.Width, CurrentImg.Height);

                    E_iVision_ERRORS err;
                    int       objnum  = 0;
                    iNCCFound objdata = new iNCCFound();
                    ResultPoint = new iDPoint();
                    iDPoint[] RegPoint = new iDPoint[4];
                    double[]  Fang     = new double[4];
                    int       i;

                    if (m_bColorSensor)
                    {
                        err = iMatch.MatchNCCModel(ColorImg, Matchmodel);
                    }
                    else
                    {
                        err = iMatch.MatchNCCModel(GrayImg, Matchmodel);
                    }

                    if (err != E_iVision_ERRORS.E_OK)
                    {
                        m_bRTMatch = false;
                    }

                    iMatch.iGetNCCMatchNum(Matchmodel, ref objnum);

                    for (i = 0; i < objnum; i++)
                    {
                        iMatch.iGetNCCMatchResults(Matchmodel, i, ref objdata);

                        ResultPoint.x = objdata.cp.x;
                        ResultPoint.y = objdata.cp.y;

                        SetTextCoordinates(textBox1, objdata.cp.x.ToString("0.00"));
                        SetTextCoordinates(textBox2, objdata.cp.y.ToString("0.00"));
                        SetTextCoordinates(textBox3, objdata.angle.ToString("0.00"));

                        m_Drawg.DrawString("score:" + objdata.score.ToString("0.00"), drawfont, drawbrush, Convert.ToInt32(ResultPoint.x - 50), Convert.ToInt32(ResultPoint.y - 60));
                        m_Drawg.DrawString("angle:" + objdata.angle.ToString("0.00"), drawfont, drawbrush, Convert.ToInt32(ResultPoint.x - 50), Convert.ToInt32(ResultPoint.y - 40));
                        m_Drawg.DrawString("scale:" + objdata.scale.ToString("0.00"), drawfont, drawbrush, Convert.ToInt32(ResultPoint.x - 50), Convert.ToInt32(ResultPoint.y - 20));
                    }


                    iMatch.iDrawiMatchResults(Matchmodel, m_Drawg.GetHdc(), 1);
                    RefreshPictureImage(m_Drawbmp);
                    m_Drawg.Dispose();
                }
                else
                {
                    RefreshPicturePtr(iImage.GetBitmapAddress(GrayImg));
                    await Task.Delay(1000 / Convert.ToInt16(30));

                    label1.Text = FrameNo.ToString() + "/" + TotalFrame.ToString();
                }

                await Task.Delay(1000 / Convert.ToInt16(30));

                label1.Text = FrameNo.ToString() + "/" + TotalFrame.ToString();

                pixelSpeedX    = ResultPoint.x - lastPositionX;
                pixelSpeedY    = ResultPoint.y - lastPositionY;
                TBXSPEEDX.Text = pixelSpeedX.ToString("F2");
                TBSSPEEDY.Text = pixelSpeedY.ToString("F2");
                lastPositionX  = (int)ResultPoint.x;
                lastPositionY  = (int)ResultPoint.y;
                //DrawScale = (double)Convert.ToDouble(txb.Text);
                ///DrawScaledImage(bm, (float)DrawScale, out Bitmap l_Bitmap);

                //pictureBox1.Image = l_Bitmap;

                /*
                 * pictureBox1.Image = _grayFrame.ToBitmap();
                 * await Task.Delay(1000 / Convert.ToInt16(30));
                 * label1.Text = FrameNo.ToString() + "/" + TotalFrame.ToString();
                 */
                // bm.Dispose();
                //l_Bitmap.Dispose();
            }
        }
Ejemplo n.º 19
0
        public static void Detect(
            Mat image, String faceFileName, String eyeFileName,
            List <Rectangle> faces, List <Rectangle> eyes,
            bool tryUseCuda, bool tryUseOpenCL,
            out long detectionTime)
        {
            Stopwatch watch;

#if !(IOS || NETFX_CORE)
            if (tryUseCuda && CudaInvoke.HasCuda)
            {
                using (CudaCascadeClassifier face = new CudaCascadeClassifier(faceFileName))
                    using (CudaCascadeClassifier eye = new CudaCascadeClassifier(eyeFileName))
                    {
                        face.ScaleFactor   = 1.1;
                        face.MinNeighbors  = 10;
                        face.MinObjectSize = Size.Empty;
                        eye.ScaleFactor    = 1.1;
                        eye.MinNeighbors   = 10;
                        eye.MinObjectSize  = Size.Empty;
                        watch = Stopwatch.StartNew();
                        using (CudaImage <Bgr, Byte> gpuImage = new CudaImage <Bgr, byte>(image))
                            using (CudaImage <Gray, Byte> gpuGray = gpuImage.Convert <Gray, Byte>())
                                using (GpuMat region = new GpuMat())
                                {
                                    face.DetectMultiScale(gpuGray, region);
                                    Rectangle[] faceRegion = face.Convert(region);
                                    faces.AddRange(faceRegion);
                                    foreach (Rectangle f in faceRegion)
                                    {
                                        using (CudaImage <Gray, Byte> faceImg = gpuGray.GetSubRect(f))
                                        {
                                            //For some reason a clone is required.
                                            //Might be a bug of CudaCascadeClassifier in opencv
                                            using (CudaImage <Gray, Byte> clone = faceImg.Clone(null))
                                                using (GpuMat eyeRegionMat = new GpuMat())
                                                {
                                                    eye.DetectMultiScale(clone, eyeRegionMat);
                                                    Rectangle[] eyeRegion = eye.Convert(eyeRegionMat);
                                                    foreach (Rectangle e in eyeRegion)
                                                    {
                                                        Rectangle eyeRect = e;
                                                        eyeRect.Offset(f.X, f.Y);
                                                        eyes.Add(eyeRect);
                                                    }
                                                }
                                        }
                                    }
                                }
                        watch.Stop();
                    }
            }
            else
#endif
            {
                //Many opencl functions require opencl compatible gpu devices.
                //As of opencv 3.0-alpha, opencv will crash if opencl is enable and only opencv compatible cpu device is presented
                //So we need to call CvInvoke.HaveOpenCLCompatibleGpuDevice instead of CvInvoke.HaveOpenCL (which also returns true on a system that only have cpu opencl devices).
                CvInvoke.UseOpenCL = tryUseOpenCL && CvInvoke.HaveOpenCLCompatibleGpuDevice;


                //Read the HaarCascade objects
                using (CascadeClassifier face = new CascadeClassifier(faceFileName))
                    using (CascadeClassifier eye = new CascadeClassifier(eyeFileName))
                    {
                        watch = Stopwatch.StartNew();
                        using (UMat ugray = new UMat())
                        {
                            CvInvoke.CvtColor(image, ugray, Emgu.CV.CvEnum.ColorConversion.Bgr2Gray);

                            //normalizes brightness and increases contrast of the image
                            CvInvoke.EqualizeHist(ugray, ugray);

                            //Detect the faces  from the gray scale image and store the locations as rectangle
                            //The first dimensional is the channel
                            //The second dimension is the index of the rectangle in the specific channel
                            Rectangle[] facesDetected = face.DetectMultiScale(
                                ugray,
                                1.1,
                                10,
                                new Size(20, 20));

                            faces.AddRange(facesDetected);

                            foreach (Rectangle f in facesDetected)
                            {
                                //Get the region of interest on the faces
                                using (UMat faceRegion = new UMat(ugray, f))
                                {
                                    Rectangle[] eyesDetected = eye.DetectMultiScale(
                                        faceRegion,
                                        1.1,
                                        10,
                                        new Size(20, 20));

                                    foreach (Rectangle e in eyesDetected)
                                    {
                                        Rectangle eyeRect = e;
                                        eyeRect.Offset(f.X, f.Y);
                                        eyes.Add(eyeRect);
                                    }
                                }
                            }
                        }
                        watch.Stop();
                    }
            }
            detectionTime = watch.ElapsedMilliseconds;
        }
Ejemplo n.º 20
0
    public void OnTrackablesUpdated()
    {
        if (!m_RegisteredFormat)
        {
            if (CameraDevice.Instance.SetFrameFormat(m_PixelFormat, true))
            {
                m_RegisteredFormat = true;
            }
        }

        CameraDevice cam   = CameraDevice.Instance;
        Image        image = cam.GetCameraImage(m_PixelFormat);

        if (image == null)
        {
            Debug.Log("Image is not available yet");
        }
        else
        {
            byte[]   pixels      = image.Pixels;
            GCHandle pixelHandle = GCHandle.Alloc(pixels, GCHandleType.Pinned);
            try
            {
                if (m_PixelFormat == Image.PIXEL_FORMAT.RGBA8888)
                {
                    using (
                        Mat m = new Mat(new Size(image.Width, image.Height), DepthType.Cv8U, 4,
                                        pixelHandle.AddrOfPinnedObject(), image.Stride))
                        using (Mat alphaChannel = new Mat())
                        {
                            //process the image (RGBA) here, replace the following with your code
                            CvInvoke.ExtractChannel(m, alphaChannel, 3); //extract the alphaChannel
                            CvInvoke.BitwiseNot(m, m);                   //simple inversion, invert all channels including alpha
                            CvInvoke.InsertChannel(alphaChannel, m, 3);  //put the alphaChannel back
                        }
                }
                else if (m_PixelFormat == Image.PIXEL_FORMAT.RGB888)
                {
                    using (
                        Mat m = new Mat(new Size(image.Width, image.Height), DepthType.Cv8U, 3,
                                        pixelHandle.AddrOfPinnedObject(), image.Stride))
                    {
                        //process the image (RGB) here, replace the following with your code.
                        CvInvoke.BitwiseNot(m, m);
                    }
                }
                else
                {
                    string s = String.Format("Image type {0} is not supported\n", m_PixelFormat);
                    s += "  size: " + image.Width + "x" + image.Height + "\n";
                    s += "  bufferSize: " + image.BufferWidth + "x" + image.BufferHeight + "\n";
                    s += "  stride: " + image.Stride;
                    Debug.Log(s);
                }
            }
            finally
            {
                pixelHandle.Free();
            }
        }
    }
Ejemplo n.º 21
0
        private void FindPlate(
            VectorOfVectorOfPoint cevreler, int[,] hiyerarsi, int idx, IInputArray gri, IInputArray cannykenar,
            List <IInputOutputArray> PlateGoruntuLisesi, List <IInputOutputArray> filtrelenmisPlateGoruntuListesi, List <RotatedRect> tespitEdilenPlateBolgesiListesi,
            List <String> licenses)
        {
            for (; idx >= 0; idx = hiyerarsi[idx, 0])
            {
                int karaktersayisi = CharacterCount(hiyerarsi, idx);

                if (karaktersayisi == 0)
                {
                    continue;
                }

                using (VectorOfPoint cevre = cevreler[idx])
                {
                    if (CvInvoke.ContourArea(cevre) > 400)
                    {
                        if (karaktersayisi < 3)
                        {
                            FindPlate(cevreler, hiyerarsi, hiyerarsi[idx, 2], gri, cannykenar, PlateGoruntuLisesi,
                                      filtrelenmisPlateGoruntuListesi, tespitEdilenPlateBolgesiListesi, licenses);
                            continue;
                        }

                        RotatedRect kutu = CvInvoke.MinAreaRect(cevre);

                        if (kutu.Angle < -45.0)
                        {
                            float tmp = kutu.Size.Width;
                            kutu.Size.Width  = kutu.Size.Height;
                            kutu.Size.Height = tmp;
                            kutu.Angle      += 90.0f;
                        }
                        else if (kutu.Angle > 45.0)
                        {
                            float tmp = kutu.Size.Width;
                            kutu.Size.Width  = kutu.Size.Height;
                            kutu.Size.Height = tmp;
                            kutu.Angle      -= 90.0f;
                        }

                        double enboyoran = (double)kutu.Size.Width / kutu.Size.Height;
                        if (!(3.0 < enboyoran && enboyoran < 10.0))

                        {
                            if (hiyerarsi[idx, 2] > 0)
                            {
                                FindPlate(cevreler, hiyerarsi, hiyerarsi[idx, 2], gri, cannykenar, PlateGoruntuLisesi,
                                          filtrelenmisPlateGoruntuListesi, tespitEdilenPlateBolgesiListesi, licenses);
                            }
                            continue;
                        }

                        using (UMat tmp1 = new UMat())
                            using (UMat tmp2 = new UMat())
                            {
                                PointF[] kutuKoseNokta = kutu.GetVertices(); // kutunun köşe noktalarını alır

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

                                using (Mat rot = CvInvoke.GetAffineTransform(kutuKoseNokta, destCorners))
                                {
                                    CvInvoke.WarpAffine(gri, tmp1, rot, Size.Round(kutu.Size));
                                }

                                Size   yaklasikBoyut = new Size(240, 180); // yaklaşık boyut
                                double olcek         = Math.Min(yaklasikBoyut.Width / kutu.Size.Width, yaklasikBoyut.Height / kutu.Size.Height);
                                Size   newSize       = new Size((int)Math.Round(kutu.Size.Width * olcek), (int)Math.Round(kutu.Size.Height * olcek));
                                CvInvoke.Resize(tmp1, tmp2, newSize, 0, 0, Inter.Cubic);


                                int       edgePixelSize = 2;
                                Rectangle newRoi        = new Rectangle(new Point(edgePixelSize, edgePixelSize),
                                                                        tmp2.Size - new Size(2 * edgePixelSize, 2 * edgePixelSize));
                                UMat Plate = new UMat(tmp2, newRoi);

                                UMat filtrelenmisPlate = FilterPlate(Plate);

                                Tesseract.Character[] words;
                                StringBuilder         strBuilder = new StringBuilder();
                                using (UMat tmp = filtrelenmisPlate.Clone())
                                {
                                    OCR.Recognize(tmp);
                                    words = OCR.GetCharacters();

                                    if (words.Length == 0)
                                    {
                                        continue;
                                    }

                                    for (int i = 0; i < words.Length; i++)
                                    {
                                        strBuilder.Append(words[i].Text);
                                    }
                                }

                                int sayac  = 0;
                                int bosluk = 0;
                                for (int i = 0; i < strBuilder.Length; i++)
                                {
                                    if (strBuilder[i].ToString() == "." || strBuilder[i].ToString() == "?" || strBuilder[i].ToString() == "*" || strBuilder[i].ToString() == "-")
                                    {
                                        sayac++;
                                    }

                                    if (strBuilder[i].ToString() == " ")
                                    {
                                        bosluk++;
                                    }
                                }

                                if (strBuilder[0].ToString() == "E" || strBuilder[0].ToString() == "F" || strBuilder[0].ToString() == "I" || strBuilder[0].ToString() == "T")
                                {
                                    strBuilder.Remove(0, 1);
                                }

                                if (sayac == 0 && strBuilder.Length >= 9 && bosluk <= 2)
                                {
                                    string[] parcalar;

                                    String asd = strBuilder.ToString();
                                    parcalar = asd.Split(' ');
                                    if (parcalar.Length == 3)
                                    {
                                        CityCode    = parcalar[0];
                                        LetterGruop = parcalar[1];
                                        NumberGroup = parcalar[2];
                                        if (CityCode.Length == 3)
                                        {
                                            CityCode = CityCode.Remove(0, 1);
                                        }

                                        CityCode = CityCode.Replace('B', '8');
                                        CityCode = CityCode.Replace('D', '0');
                                        CityCode = CityCode.Replace('G', '6');
                                        CityCode = CityCode.Replace('I', '1');
                                        CityCode = CityCode.Replace('O', '0');
                                        CityCode = CityCode.Replace('S', '5');
                                        CityCode = CityCode.Replace('Z', '2');
                                        CityCode = CityCode.Replace('L', '4');

                                        LetterGruop = LetterGruop.Replace('2', 'Z');
                                        LetterGruop = LetterGruop.Replace('0', 'D');
                                        LetterGruop = LetterGruop.Replace('5', 'S');
                                        LetterGruop = LetterGruop.Replace('6', 'G');
                                        LetterGruop = LetterGruop.Replace('8', 'B');
                                        LetterGruop = LetterGruop.Replace('1', 'I');

                                        NumberGroup = NumberGroup.Replace('B', '8');
                                        NumberGroup = NumberGroup.Replace('D', '0');
                                        NumberGroup = NumberGroup.Replace('G', '6');
                                        NumberGroup = NumberGroup.Replace('I', '1');
                                        NumberGroup = NumberGroup.Replace('O', '0');
                                        NumberGroup = NumberGroup.Replace('S', '5');
                                        NumberGroup = NumberGroup.Replace('Z', '2');
                                        NumberGroup = NumberGroup.Replace('A', '4');

                                        if (NumberGroup.Length == 5)
                                        {
                                            NumberGroup = NumberGroup.Remove(4, 1);
                                        }

                                        strBuilder.Remove(0, strBuilder.Length);
                                        strBuilder.Append(CityCode);
                                        strBuilder.Append(" ");
                                        strBuilder.Append(LetterGruop);
                                        strBuilder.Append(" ");
                                        strBuilder.Append(NumberGroup);
                                    }

                                    if (strBuilder.Length >= 7 && strBuilder.Length <= 10)
                                    {
                                        licenses.Add(strBuilder.ToString());
                                        PlateGoruntuLisesi.Add(Plate);
                                        filtrelenmisPlateGoruntuListesi.Add(filtrelenmisPlate);
                                        tespitEdilenPlateBolgesiListesi.Add(kutu);
                                    }
                                }
                            }
                    }
                }
            }
        }
Ejemplo n.º 22
0
        private void FindStopSign(Mat img, List <Mat> stopSignList, List <Rectangle> boxList, VectorOfVectorOfPoint contours, int[,] hierachy, int idx)
        {
            for (; idx >= 0; idx = hierachy[idx, 0])
            {
                using (VectorOfPoint c = contours[idx])
                    using (VectorOfPoint approx = new VectorOfPoint())
                    {
                        CvInvoke.ApproxPolyDP(c, approx, CvInvoke.ArcLength(c, true) * 0.02, true);
                        double area = CvInvoke.ContourArea(approx);
                        if (area > 200)
                        {
                            double ratio = CvInvoke.MatchShapes(_octagon, approx, Emgu.CV.CvEnum.ContoursMatchType.I3);

                            if (ratio > 0.1) //not a good match of contour shape
                            {
                                //check children
                                if (hierachy[idx, 2] >= 0)
                                {
                                    FindStopSign(img, stopSignList, boxList, contours, hierachy, hierachy[idx, 2]);
                                }
                                continue;
                            }

                            Rectangle box = CvInvoke.BoundingRectangle(c);

                            Mat candidate = new Mat();
                            using (Mat tmp = new Mat(img, box))
                                CvInvoke.CvtColor(tmp, candidate, ColorConversion.Bgr2Gray);

                            //set the value of pixels not in the contour region to zero
                            using (Mat mask = new Mat(candidate.Size.Height, candidate.Width, DepthType.Cv8U, 1))
                            {
                                mask.SetTo(new MCvScalar(0));
                                CvInvoke.DrawContours(mask, contours, idx, new MCvScalar(255), -1, LineType.EightConnected, null, int.MaxValue, new Point(-box.X, -box.Y));

                                double mean = CvInvoke.Mean(candidate, mask).V0;
                                CvInvoke.Threshold(candidate, candidate, mean, 255, ThresholdType.Binary);
                                CvInvoke.BitwiseNot(candidate, candidate);
                                CvInvoke.BitwiseNot(mask, mask);

                                candidate.SetTo(new MCvScalar(0), mask);
                            }

                            int              minMatchCount         = 8;
                            double           uniquenessThreshold   = 0.8;
                            VectorOfKeyPoint _observeredKeypoint   = new VectorOfKeyPoint();
                            Mat              _observeredDescriptor = new Mat();
                            _detector.DetectAndCompute(candidate, null, _observeredKeypoint, _observeredDescriptor, false);

                            if (_observeredKeypoint.Size >= minMatchCount)
                            {
                                int k = 2;

                                Mat mask;

                                using (VectorOfVectorOfDMatch matches = new VectorOfVectorOfDMatch())
                                {
                                    _modelDescriptorMatcher.KnnMatch(_observeredDescriptor, matches, k, null);
                                    mask = new Mat(matches.Size, 1, DepthType.Cv8U, 1);
                                    mask.SetTo(new MCvScalar(255));
                                    Features2DToolbox.VoteForUniqueness(matches, uniquenessThreshold, mask);
                                }

                                int nonZeroCount = CvInvoke.CountNonZero(mask);
                                if (nonZeroCount >= minMatchCount)
                                {
                                    boxList.Add(box);
                                    stopSignList.Add(candidate);
                                }
                            }
                        }
                    }
            }
        }
Ejemplo n.º 23
0
        public static void Detect(
            IInputArray image,
            String faceFileName,
            String eyeFileName,
            List <Rectangle> faces,
            List <Rectangle> eyes,
            out long detectionTime)
        {
            Stopwatch watch;

            using (InputArray iaImage = image.GetInputArray())
            {
            #if !(__IOS__ || NETFX_CORE)
                if (iaImage.Kind == InputArray.Type.CudaGpuMat && CudaInvoke.HasCuda)
                {
                    using (CudaCascadeClassifier face = new CudaCascadeClassifier(faceFileName))
                        using (CudaCascadeClassifier eye = new CudaCascadeClassifier(eyeFileName))
                        {
                            face.ScaleFactor   = 1.1;
                            face.MinNeighbors  = 10;
                            face.MinObjectSize = Size.Empty;
                            eye.ScaleFactor    = 1.1;
                            eye.MinNeighbors   = 10;
                            eye.MinObjectSize  = Size.Empty;
                            watch = Stopwatch.StartNew();
                            using (CudaImage <Bgr, Byte> gpuImage = new CudaImage <Bgr, byte>(image))
                                using (CudaImage <Gray, Byte> gpuGray = gpuImage.Convert <Gray, Byte>())
                                    using (GpuMat region = new GpuMat())
                                    {
                                        face.DetectMultiScale(gpuGray, region);
                                        Rectangle[] faceRegion = face.Convert(region);
                                        faces.AddRange(faceRegion);
                                        foreach (Rectangle f in faceRegion)
                                        {
                                            using (CudaImage <Gray, Byte> faceImg = gpuGray.GetSubRect(f))
                                            {
                                                //For some reason a clone is required.
                                                //Might be a bug of CudaCascadeClassifier in opencv
                                                using (CudaImage <Gray, Byte> clone = faceImg.Clone(null))
                                                    using (GpuMat eyeRegionMat = new GpuMat())
                                                    {
                                                        eye.DetectMultiScale(clone, eyeRegionMat);
                                                        Rectangle[] eyeRegion = eye.Convert(eyeRegionMat);
                                                        foreach (Rectangle e in eyeRegion)
                                                        {
                                                            Rectangle eyeRect = e;
                                                            eyeRect.Offset(f.X, f.Y);
                                                            eyes.Add(eyeRect);
                                                        }
                                                    }
                                            }
                                        }
                                    }
                            watch.Stop();
                        }
                }
                else
                #endif
                {
                    //Read the HaarCascade objects
                    using (CascadeClassifier face = new CascadeClassifier(faceFileName))
                        using (CascadeClassifier eye = new CascadeClassifier(eyeFileName))
                        {
                            watch = Stopwatch.StartNew();

                            using (UMat ugray = new UMat())
                            {
                                CvInvoke.CvtColor(image, ugray, Emgu.CV.CvEnum.ColorConversion.Bgr2Gray);

                                //normalizes brightness and increases contrast of the image
                                CvInvoke.EqualizeHist(ugray, ugray);

                                //Detect the faces  from the gray scale image and store the locations as rectangle
                                //The first dimensional is the channel
                                //The second dimension is the index of the rectangle in the specific channel
                                Rectangle[] facesDetected = face.DetectMultiScale(
                                    ugray,
                                    1.1,
                                    10,
                                    new Size(200, 200));

                                faces.AddRange(facesDetected);

                                foreach (Rectangle f in facesDetected)
                                {
                                    //Get the region of interest on the faces
                                    using (UMat faceRegion = new UMat(ugray, f))
                                    {
                                        Rectangle[] eyesDetected = eye.DetectMultiScale(
                                            faceRegion,
                                            1.1,
                                            10,
                                            new Size(20, 20));

                                        foreach (Rectangle e in eyesDetected)
                                        {
                                            Rectangle eyeRect = e;
                                            eyeRect.Offset(f.X, f.Y);
                                            eyes.Add(eyeRect);
                                        }
                                    }
                                }
                            }
                            watch.Stop();
                        }
                }
                detectionTime = watch.ElapsedMilliseconds;
            }
        }
Ejemplo n.º 24
0
        public static (ShapeType, List <Point>) Check(List <Point> polygonPoints, int canvasWidth, int canvasHeigth)
        {
            if (!ClosedContour(polygonPoints))
            {
                return(ShapeType.None, null);
            }

            CreateBitmap(canvasWidth, canvasHeigth, polygonPoints);

            Image <Bgr, byte>  img       = new Image <Bgr, byte>(@"IMAGE.bmp");
            Image <Gray, byte> processed = img
                                           .Convert <Gray, byte>()
                                           .SmoothGaussian(5)
                                           .Canny(240, 200);

            VectorOfVectorOfPoint contours = new VectorOfVectorOfPoint();
            Mat m = new Mat();

            CvInvoke.FindContours(
                processed, contours, m, Emgu.CV.CvEnum.RetrType.External,
                Emgu.CV.CvEnum.ChainApproxMethod.ChainApproxSimple);

            if (contours.Size == 0)
            {
                return(ShapeType.None, null);
            }

            double perimeter = CvInvoke.ArcLength(contours[0], true);

            var polygon = new VectorOfPoint();

            CvInvoke.ApproxPolyDP(contours[0], polygon, 0.02 * perimeter, true);

            if (!CvInvoke.IsContourConvex(polygon))
            {
                return(ShapeType.None, null);
            }

            var   moments = CvInvoke.Moments(contours[0]);
            int   x       = (int)(moments.M10 / moments.M00);
            int   y       = (int)(moments.M01 / moments.M00);
            Point center  = new Point(x, y);

            List <Point> points = new List <Point>();

            for (int i = 0; i < polygon.Size; i++)
            {
                points.Add(new Point(polygon[i].X, polygon[i].Y));
            }

            if (points.Count == 3)
            {
                return(ShapeType.Triangle, points);
            }
            else if (points.Count == 4)
            {
                return(RectangleOrSquare(center, perimeter, points));
            }
            else if (points.Count > 4)
            {
                return(CircleOrEllipse(center, perimeter, points));
            }

            return(ShapeType.None, null);
        }
Ejemplo n.º 25
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, bool ocr_mode)
        {
            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 < 3)
                        {
                            //If the contour has less than 3 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);
                            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);
                            }
                            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))
                                {
                                    CvInvoke.WarpAffine(gray, tmp1, rot, Size.Round(box.Size));
                                }

                                //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);

                                //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);

                                UMat          filteredPlate = FilterPlate(plate);
                                StringBuilder strBuilder    = new StringBuilder();
                                if (ocr_mode)
                                {
                                    strBuilder = TesseractOCR(filteredPlate);
                                }
                                else
                                {
                                    strBuilder = GoogleApiOCR(filteredPlate);
                                }

                                licenses.Add(strBuilder.ToString());
                                licensePlateImagesList.Add(plate);
                                filteredLicensePlateImagesList.Add(filteredPlate);
                                detectedLicensePlateRegionList.Add(box);
                            }
                    }
                }
            }
        }
Ejemplo n.º 26
0
 private void OnDestroy()
 {
     CvInvoke.DestroyAllWindows();
 }
Ejemplo n.º 27
0
            /// <summary>

            /// Determina si un contorno es un circulo, verificando que tenga al menos 7 puntos y todos estén al mismo radio del centro

            /// </summary>

            /// <param name="MaxVariation"> Máxima desviacion permitida (0 a 1) </param>

            /// <returns></returns>

            public static bool isCircle(this VectorOfPoint contour, float MaxVariation = 0.2f)
            {
                bool result = false;



                if (contour.Size >= 7)
                {
                    Moments Moments = CvInvoke.Moments(contour);

                    Vector2 Center = Vector2.zero;

                    Center.x = (float)(Moments.M10 / Moments.M00);

                    Center.y = (float)(Moments.M01 / Moments.M00);



                    result = true;

                    Vector2[] pts = contour.ToVector2();

                    float MaxRadius = (pts[0] - Center).magnitude;

                    float MinRadius = MaxRadius;

                    float MinLimit = 1 - MaxVariation;

                    float MaxLimit = 1 + MaxVariation;



                    //Verifica que el radio sea consistente

                    for (int j = 1; j < pts.Length; j++)
                    {
                        float r = (pts[j] - Center).magnitude;

                        if (r > MaxRadius)
                        {
                            MaxRadius = r;
                        }

                        if (r < MinRadius)
                        {
                            MinRadius = r;
                        }
                    }



                    if (MinRadius / MaxRadius < MinLimit)
                    {
                        result = false;
                    }



                    //Verifica que el perímetro sea consistente

                    if (result)
                    {
                        double perimetroReal = CvInvoke.ArcLength(contour, true);

                        float Radio = (MaxRadius + MinRadius) * 0.5f;

                        float perimetroIdeal = 6.28f * Radio;

                        float factor = (float)(perimetroIdeal / perimetroReal);

                        if ((factor < MinLimit) || (factor > MaxLimit))
                        {
                            result = false;
                        }
                    }



                    pts = null;
                }



                return(result);
            }
Ejemplo n.º 28
0
    private void HandleUnitSelection()
    {
        Vector2   temp = endBoxPos - orgBoxPos;
        Rectangle rect = new Rectangle((int)orgBoxPos.x, 480 - (int)(orgBoxPos.y),
                                       (int)(Mathf.Abs(temp.x)),
                                       (int)(Mathf.Abs(temp.y)));


        test = new Mat(frame, rect);

        Image <Bgr, Byte> imgBgr = test.ToImage <Bgr, byte>();

        var moments = CvInvoke.Moments(imgBgr[0]);
        int cx      = (int)(moments.M10 / moments.M00);
        int cy      = (int)(moments.M01 / moments.M00);

        int b = 0, g = 0, r = 0;

        for (int i = 0; i < 5; i++)
        {
            Point pts1 = new Point(cx + i, cy + i);
            Point pts2 = new Point(cx - i, cy - i);
            Point pts3 = new Point(cx - i, cy + i);
            Point pts4 = new Point(cx + i, cy - i);

            r += (int)imgBgr[pts1].Red;
            r += (int)imgBgr[pts2].Red;
            r += (int)imgBgr[pts3].Red;
            r += (int)imgBgr[pts4].Red;

            g += (int)imgBgr[pts1].Green;
            g += (int)imgBgr[pts2].Green;
            g += (int)imgBgr[pts3].Green;
            g += (int)imgBgr[pts4].Green;

            b += (int)imgBgr[pts1].Blue;
            b += (int)imgBgr[pts2].Blue;
            b += (int)imgBgr[pts3].Blue;
            b += (int)imgBgr[pts4].Blue;
        }

        r = r / 20;
        g = g / 20;
        b = b / 20;

        System.Drawing.Color c = System.Drawing.Color.FromArgb(r, g, b);
        Hsv hsv = new Hsv(c.GetHue(), c.GetSaturation(), c.GetBrightness());

        hTarget = hsv.Hue / 2;
        sTarget = hsv.Satuation;
        vTarget = hsv.Value;

        Mat tata = test.Clone();

        CvInvoke.CvtColor(test, tata, ColorConversion.Bgr2Hsv);
        Image <Hsv, byte> ImgHSV = tata.ToImage <Hsv, byte>();

        Image <Gray, byte> tarace = ImgHSV.InRange(new Hsv(hTarget - intensity, 0, 0), new Hsv(hTarget + intensity, 255, 255));

        Mat hierarchy = new Mat();
        VectorOfVectorOfPoint contours = new VectorOfVectorOfPoint();

        CvInvoke.FindContours(tarace, contours, hierarchy, RetrType.List, ChainApproxMethod.ChainApproxNone);

        double        biggestContourArea  = 0;
        VectorOfPoint biggestContour      = new VectorOfPoint();
        int           biggestContourIndex = 0;

        for (int i = 0; i < contours.Size; i++)
        {
            if (CvInvoke.ContourArea(contours[i]) > biggestContourArea)
            {
                biggestContour      = contours[i];
                biggestContourIndex = i;
                biggestContourArea  = CvInvoke.ContourArea(contours[i]);
            }
        }


        Mat biggestContourMat = new Mat(test, CvInvoke.BoundingRectangle(contours[biggestContourIndex]));

        CvInvoke.Mean()



        // colorDetect = true;
    }
Ejemplo n.º 29
0
 /// <summary>
 /// Delete blob by its index
 /// </summary>
 /// <param name="blobIndex">The index of the blob</param>
 public void RemoveAt(int blobIndex)
 {
     CvInvoke.CvBlobTrackerDelBlob(_ptr, blobIndex);
 }
Ejemplo n.º 30
0
        public ImageProcess(Mat image, bool mode)
        {
            //split 0 b 1 g 2 r
            mImage    = new Image <Rgb, byte>(image.Size);
            rImage    = new Image <Rgb, byte>(image.Size);
            gImage    = new Image <Rgb, byte>(image.Size);
            bImage    = new Image <Rgb, byte>(image.Size);
            grayImage = new Image <Gray, byte>(image.Size);

            redMat   = new Mat();
            greenMat = new Mat();
            blueMat  = new Mat();
            grayMat  = new Mat();

            Debug.WriteLine(mode);
            processMode = mode;


            mImage = image.ToImage <Rgb, byte>();

            VectorOfMat vectorOfMat = new VectorOfMat();

            CvInvoke.Split(mImage, vectorOfMat);

            vectorOfMat[0].CopyTo(blueMat);
            vectorOfMat[1].CopyTo(greenMat);
            vectorOfMat[2].CopyTo(redMat);


            Stopwatch stopwatch = new Stopwatch();

            stopwatch.Start();
            lsbR = new LsbProcess(redMat);
            lsbG = new LsbProcess(greenMat);
            lsbB = new LsbProcess(blueMat);
            stopwatch.Stop();
            Debug.WriteLine("lsb time is : " + stopwatch.Elapsed);

            stopwatch.Start();
            dftR = new DftProcess(redMat);
            dftG = new DftProcess(greenMat);
            dftB = new DftProcess(blueMat);
            stopwatch.Stop();
            Debug.WriteLine("dft time is : " + stopwatch.Elapsed);

            stopwatch.Start();
            dctR = new DctProcess(redMat);
            dctG = new DctProcess(greenMat);
            dctB = new DctProcess(blueMat);
            stopwatch.Stop();
            Debug.WriteLine("dct time is : " + stopwatch.Elapsed);


            Mat fillMat = new Mat(image.Size, DepthType.Cv8U, 1);

            fillMat.SetTo(new MCvScalar(0));
            Mat tmpMat = new Mat();

            CvInvoke.Merge(new VectorOfMat(redMat.Clone(), fillMat.Clone(), fillMat.Clone()), tmpMat);
            rImage = tmpMat.ToImage <Rgb, byte>();
            CvInvoke.Merge(new VectorOfMat(fillMat.Clone(), greenMat.Clone(), fillMat.Clone()), tmpMat);
            gImage = tmpMat.ToImage <Rgb, byte>();
            CvInvoke.Merge(new VectorOfMat(fillMat.Clone(), fillMat.Clone(), blueMat.Clone()), tmpMat);
            bImage = tmpMat.ToImage <Rgb, byte>();


            grayImage = image.ToImage <Gray, byte>();
            grayImage.Mat.CopyTo(grayMat);

            lsbGray = new LsbProcess(grayMat);

            dftGray = new DftProcess(grayMat);

            dctGray = new DctProcess(grayMat);
        }