コード例 #1
0
        public IActionResult FileUpload(UploadFile uploadFile)
        {
            UploadFile updata = new UploadFile();

            using (MemoryStream ms = new MemoryStream())
            {
                uploadFile.FormFile.CopyTo(ms);
                byte[]             fileBytes       = ms.ToArray();
                ProcessCaptured    processCaptured = new ProcessCaptured();
                var                image           = Image.FromStream(ms);
                Image <Bgra, Byte> imgg            = processCaptured.GetImageFromStream(ms);
                updata = processCaptured.ProcessImage(imgg);
            }
            return(View("Index", updata));
        }
コード例 #2
0
        public IActionResult Gauss(GaussData GaussData)
        {
            Image <Bgra, Byte> img = null;
            Mat       GaussImg     = new Mat();
            Mat       GrayImg      = new Mat();
            Mat       SobalImg     = new Mat();
            Mat       ThresholdImg = new Mat();
            Mat       MorpImg      = new Mat();
            GaussData gd           = new GaussData();

            using (MemoryStream ms = new MemoryStream())
            {
                if (ms != null)
                {
                    ProcessCaptured capture = new ProcessCaptured();
                    GaussData.FormFile.CopyTo(ms);
                    byte[] fileBytes = ms.ToArray();
                    img = capture.GetImageFromStream(ms);
                    Size size = new Size(3, 3);
                    CvInvoke.GaussianBlur(img, GaussImg, size, 0);
                    CvInvoke.CvtColor(GaussImg, GrayImg, Emgu.CV.CvEnum.ColorConversion.Bgr2Gray);
                    CvInvoke.Sobel(GrayImg, SobalImg, Emgu.CV.CvEnum.DepthType.Cv8U, 1, 0, 3);
                    CvInvoke.Threshold(SobalImg, ThresholdImg, 0, 255, Emgu.CV.CvEnum.ThresholdType.Binary);
                    var       gi      = ThresholdImg.ToImage <Bgr, Byte>();
                    Size      Ksize   = new Size(17, 3);
                    Point     points  = new Point(-1, -1);
                    var       element = CvInvoke.GetStructuringElement(Emgu.CV.CvEnum.ElementShape.Rectangle, Ksize, points);
                    MCvScalar scalar  = new MCvScalar(1);
                    CvInvoke.MorphologyEx(ThresholdImg, MorpImg, Emgu.CV.CvEnum.MorphOp.Close, element, points, 3, Emgu.CV.CvEnum.BorderType.Default, scalar);
                    var    tempImg = MorpImg.ToImage <Bgr, Byte>();
                    var    temp    = tempImg.ToJpegData();
                    string data    = "data:image/jpg;base64," + Convert.ToBase64String(temp, 0, temp.Length);
                    gd.Images.Add(new Images {
                        Data = data, ImageName = "Gauss"
                    });
                }
            }
            return(View("Index", gd));
        }
コード例 #3
0
        public IActionResult Steps(StepData uploadFile)
        {
            StepData           stepData        = new StepData();
            ProcessCaptured    processCaptured = new ProcessCaptured();
            Image <Bgra, Byte> imgg            = null;
            Mat finalImg = new Mat();

            using (MemoryStream ms = new MemoryStream())
            {
                if (ms != null)
                {
                    CvInvoke.UseOpenCL = true;
                    uploadFile.FormFile.CopyTo(ms);
                    byte[] fileBytes = ms.ToArray();
                    imgg     = processCaptured.GetImageFromStream(ms);
                    finalImg = imgg.Mat.Clone();
                    stepData = AddData(stepData, finalImg, "Orginal");
                    using (Mat im = imgg.Mat)
                    {
                        using (Mat threshold = new Mat())
                        {
                            using (Mat gray = new Mat())
                            {
                                using (Mat canny = new Mat())
                                {
                                    using (Mat rectImg = new Mat())
                                    {
                                        CvInvoke.Threshold(im, threshold, 100, 255, ThresholdType.BinaryInv);
                                        stepData = AddData(stepData, threshold, "Threshold");

                                        CvInvoke.CvtColor(threshold, gray, ColorConversion.Bgr2Gray);
                                        stepData = AddGrayData(stepData, gray, "Gray");

                                        CvInvoke.Canny(gray, canny, 100, 50, 7);
                                        stepData = AddData(stepData, canny, "Canny");

                                        List <RotatedRect> rect = new List <RotatedRect>();
                                        rect = Contours(canny);

                                        if (rect != null && rect.Count > 0)
                                        {
                                            foreach (RotatedRect boxr in rect)
                                            {
                                                CvInvoke.Polylines(finalImg, Array.ConvertAll(boxr.GetVertices(), Point.Round), true,
                                                                   new Bgr(Color.DarkOrange).MCvScalar, 2);
                                            }
                                        }
                                        stepData = AddData(stepData, finalImg, "With Rectangle");

                                        List <Mat> mat = RoI(rect, gray);
                                        int        i   = 0;
                                        //OCR
                                        string    path = AppContext.BaseDirectory;
                                        Tesseract _ocr = new Tesseract(path, "eng", OcrEngineMode.TesseractLstmCombined);
                                        _ocr.SetVariable("tessedit_char_whitelist", "ABCDEFGHIJKLMNOPQRSTUVWXYZ-1234567890");
                                        foreach (Mat m in mat)
                                        {
                                            i += 1;
                                            _ocr.SetImage(m);
                                            _ocr.Recognize();
                                            Tesseract.Character[] words = _ocr.GetCharacters();
                                            //string wor=words.
                                            StringBuilder sb = new StringBuilder();
                                            foreach (var c in words)
                                            {
                                                sb.Append(c.Text);
                                            }
                                            stepData = AddData(stepData, m, i.ToString(), sb.ToString());
                                        }
                                    }
                                }
                            }
                        }
                    }

                    //Mat blur = new Mat();
                    //blur = Blur(im);
                    //stepData = AddData(stepData, blur, "Blur");

                    //Mat gray = new Mat();
                    //gray = Gray(blur);
                    //stepData = AddData(stepData, gray, "Gray");

                    //Mat threshold = new Mat();
                    //threshold = Threshold(im);
                    //stepData = AddData(stepData, threshold, "Threshold");

                    //Mat gray = new Mat();
                    //gray = Gray(threshold);
                    //stepData = AddGrayData(stepData, gray, "Gray");

                    //Mat canny = new Mat();
                    //canny = Canny(gray);
                    //stepData = AddData(stepData, canny, "Canny");


                    //List<RotatedRect> rect = new List<RotatedRect>();
                    //rect = Contours(canny);

                    //Mat rectImg = new Mat();

                    //rectImg = DrawRect(imgg.Mat, rect);
                    //stepData = AddData(stepData, rectImg, "With Rectangle");

                    //List<Mat> mat = RoI(rect, threshold.Clone());

                    //int i = 0;
                    //foreach (Mat m in mat)
                    //{
                    //    i += 1;
                    //    stepData = AddData(stepData, m, i.ToString());
                    //}
                }
            }
            #region Old
            //Image<Bgr, Byte> imgg = null;
            //Image<Bgr, byte> tempImg = null;
            //Image<Gray, Byte> GrayImg = null;
            //Image<Gray, Byte> CannyImgTemp = null;
            //Image<Bgr, Byte> CannyImg = null;
            //Image<Bgr, byte> temp = null;
            //int[,] hierachy = null;
            //List<VectorOfPoint> box = new List<VectorOfPoint>();
            //List<RotatedRect> boxList = new List<RotatedRect>();
            //CvInvoke.UseOpenCL = true;
            //using (MemoryStream ms = new MemoryStream())
            //{
            //    if (ms != null)
            //    {
            //        uploadFile.FormFile.CopyTo(ms);
            //        byte[] fileBytes = ms.ToArray();
            //        imgg = processCaptured.GetImageFromStream(ms);
            //        Mat im = imgg.Mat;
            //        stepData = AddData(stepData, im, "Orginal");
            //        tempImg = imgg.Copy();
            //    }
            //}
            //using (Mat blur = new Mat())
            //{
            //    using (Mat gray = new Mat())
            //    {
            //        using (Mat canny = new Mat())
            //        {
            //            Size kSize = new Size(3, 3);
            //           // CvInvoke.GaussianBlur(imgg, blur, kSize, 0);
            //            CvInvoke.Threshold(imgg, blur, 50, 255, ThresholdType.Binary);
            //            //CvInvoke.BilateralFilter(imgg, blur, -1, -1, 100);
            //            stepData = AddData(stepData, blur, "Gray");

            //            CvInvoke.Threshold(imgg, gray,50, 255, ThresholdType.Binary);
            //            //CvInvoke.CvtColor(blur, gray, ColorConversion.Bgr2Gray);

            //            stepData = AddData(stepData, gray, "Threshold");



            //            CvInvoke.Canny(imgg, canny, 100, 50, 3, false);
            //            CannyImgTemp = canny.ToImage<Gray, byte>();
            //            stepData = AddData(stepData, gray, "canny");

            //            //Find the Rectangle

            //            using (VectorOfVectorOfPoint contours = new VectorOfVectorOfPoint())
            //            {
            //                using (VectorOfPoint approxContour = new VectorOfPoint())
            //                {
            //                    //hierachy = CvInvoke.FindContourTree(canny, contours, ChainApproxMethod.ChainApproxSimple);
            //                    CvInvoke.FindContours(canny, contours, null, RetrType.List, ChainApproxMethod.ChainApproxSimple);
            //                    //for (int i = 0; i < hierachy.GetLength(0); i++)
            //                    int count = contours.Size;
            //                    for (int i = 0; i < count; i++)
            //                    {
            //                        CvInvoke.ApproxPolyDP(contours[i], approxContour, CvInvoke.ArcLength(contours[i], true) * 0.05, true);
            //                        if (CvInvoke.ContourArea(approxContour, false) > 250)
            //                        {
            //                        //    if (approxContour.Size <=6) //The contour has 4 vertices.
            //                        //    {
            //                                //#region determine if all the angles in the contour are within [80, 100] degree
            //                                //bool isRectangle = true;
            //                                //Point[] pts = approxContour.ToArray();
            //                                //LineSegment2D[] edges = PointCollection.PolyLine(pts, true);

            //                                //for (int j = 0; j < edges.Length; j++)
            //                                //{
            //                                //    double angle = Math.Abs(
            //                                //        edges[(j + 1) % edges.Length].GetExteriorAngleDegree(edges[j]));
            //                                //    if (angle < 80 || angle > 100)
            //                                //    {
            //                                //        isRectangle = false;
            //                                //        break;
            //                                //    }
            //                                //}

            //                                //#endregion
            //                                //if (isRectangle)
            //                                boxList.Add(CvInvoke.MinAreaRect(approxContour));
            //                           // }
            //                        }
            //                    }

            //                }
            //            }
            //        }
            //    }
            //}
            //foreach (RotatedRect boxr in boxList)
            //{
            //    CvInvoke.Polylines(tempImg, Array.ConvertAll(boxr.GetVertices(), Point.Round), true,
            //        new Bgr(Color.DarkOrange).MCvScalar, 2);
            //}
            //Mat pI = tempImg.Mat;
            //stepData = AddData(stepData, pI, "Poly");
            #endregion
            return(View("Index", stepData));
        }
コード例 #4
0
        public StepData StepsVideo(string uploadFile)
        {
            StepData           stepData        = new StepData();
            ProcessCaptured    processCaptured = new ProcessCaptured();
            Image <Bgra, Byte> imgg            = null;
            Mat finalImg = new Mat();

            if (!string.IsNullOrWhiteSpace(uploadFile))
            {
                byte[] fileBytes = Convert.FromBase64String(uploadFile);
                using (MemoryStream ms = new MemoryStream(fileBytes))
                {
                    imgg     = processCaptured.GetImageFromStream(ms);
                    finalImg = imgg.Mat.Clone();
                    //stepData = AddData(stepData, finalImg.Clone(), "Orginal");
                    using (Mat im = imgg.Mat.Clone())
                    {
                        using (Mat threshold = new Mat())
                        {
                            using (Mat gray = new Mat())
                            {
                                using (Mat canny = new Mat())
                                {
                                    using (Mat rectImg = new Mat())
                                    {
                                        CvInvoke.Threshold(im, threshold, 100, 255, ThresholdType.BinaryInv);
                                        //stepData = AddData(stepData, threshold, "Threshold");

                                        CvInvoke.CvtColor(threshold, gray, ColorConversion.Bgr2Gray);
                                        //stepData = AddGrayData(stepData, gray, "Gray");

                                        CvInvoke.Canny(gray, canny, 100, 50, 7);
                                        //stepData = AddData(stepData, canny, "Canny");

                                        List <RotatedRect> rect = new List <RotatedRect>();
                                        rect = Contours(canny);

                                        if (rect != null && rect.Count > 0)
                                        {
                                            foreach (RotatedRect boxr in rect)
                                            {
                                                CvInvoke.Polylines(finalImg, Array.ConvertAll(boxr.GetVertices(), Point.Round), true,
                                                                   new Bgr(Color.DarkOrange).MCvScalar, 2);
                                            }
                                        }
                                        //stepData = AddData(stepData, finalImg, "With Rectangle");

                                        List <Mat> mat = RoI(rect, gray);
                                        int        i   = 0;
                                        //OCR
                                        string    path = AppContext.BaseDirectory;
                                        Tesseract _ocr = new Tesseract(path, "eng", OcrEngineMode.TesseractLstmCombined);
                                        _ocr.SetVariable("tessedit_char_whitelist", "ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890");
                                        foreach (Mat m in mat)
                                        {
                                            i += 1;
                                            _ocr.SetImage(m);
                                            _ocr.Recognize();
                                            Tesseract.Character[] words = _ocr.GetCharacters();
                                            //string wor=words.
                                            StringBuilder sb = new StringBuilder();
                                            foreach (var c in words)
                                            {
                                                sb.Append(c.Text);
                                            }
                                            if (sb.Length > 3 && sb.Length <= 10)
                                            {
                                                stepData = AddData(stepData, m, i.ToString(), sb.ToString());
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
            //return View("Index", stepData);
            finalImg = null;
            return(stepData);
        }
コード例 #5
0
        public IActionResult Steps(StepData uploadFile)
        {
            StepData           stepData        = new StepData();
            ProcessCaptured    processCaptured = new ProcessCaptured();
            Image <Bgra, Byte> imgg            = null;
            Mat finalImg = new Mat();

            using (MemoryStream ms = new MemoryStream())
            {
                if (ms != null)
                {
                    CvInvoke.UseOpenCL = true;
                    uploadFile.FormFile.CopyTo(ms);
                    byte[] fileBytes = ms.ToArray();
                    imgg     = processCaptured.GetImageFromStream(ms);
                    finalImg = imgg.Mat.Clone();
                    stepData = AddData(stepData, finalImg, "Orginal");
                    using (Mat im = imgg.Mat)
                    {
                        using (Mat threshold = new Mat())
                        {
                            using (Mat gray = new Mat())
                            {
                                using (Mat canny = new Mat())
                                {
                                    using (Mat rectImg = new Mat())
                                    {
                                        CvInvoke.Threshold(im, threshold, 100, 255, ThresholdType.BinaryInv);
                                        stepData = AddData(stepData, threshold, "Threshold");

                                        CvInvoke.CvtColor(threshold, gray, ColorConversion.Bgr2Gray);
                                        stepData = AddGrayData(stepData, gray, "Gray");

                                        CvInvoke.Canny(gray, canny, 100, 50, 7);
                                        stepData = AddData(stepData, canny, "Canny");

                                        List <RotatedRect> rect = new List <RotatedRect>();
                                        rect = Contours(canny);

                                        if (rect != null && rect.Count > 0)
                                        {
                                            foreach (RotatedRect boxr in rect)
                                            {
                                                CvInvoke.Polylines(finalImg, Array.ConvertAll(boxr.GetVertices(), Point.Round), true,
                                                                   new Bgr(Color.DarkOrange).MCvScalar, 2);
                                            }
                                        }
                                        stepData = AddData(stepData, finalImg, "With Rectangle");

                                        List <Mat> mat = RoI(rect, gray);
                                        int        i   = 0;
                                        //OCR
                                        string    path = AppContext.BaseDirectory;
                                        Tesseract _ocr = new Tesseract(path, "eng", OcrEngineMode.TesseractLstmCombined);
                                        _ocr.SetVariable("tessedit_char_whitelist", "ABCDEFGHIJKLMNOPQRSTUVWXYZ-1234567890");
                                        foreach (Mat m in mat)
                                        {
                                            i += 1;
                                            _ocr.SetImage(m);
                                            _ocr.Recognize();
                                            Tesseract.Character[] words = _ocr.GetCharacters();
                                            //string wor=words.
                                            StringBuilder sb = new StringBuilder();
                                            foreach (var c in words)
                                            {
                                                sb.Append(c.Text);
                                            }
                                            stepData = AddData(stepData, m, i.ToString(), sb.ToString());
                                        }
                                    }
                                }
                            }
                        }
                    }

                    //Mat blur = new Mat();
                    //blur = Blur(im);
                    //stepData = AddData(stepData, blur, "Blur");

                    //Mat gray = new Mat();
                    //gray = Gray(blur);
                    //stepData = AddData(stepData, gray, "Gray");

                    //Mat threshold = new Mat();
                    //threshold = Threshold(im);
                    //stepData = AddData(stepData, threshold, "Threshold");

                    //Mat gray = new Mat();
                    //gray = Gray(threshold);
                    //stepData = AddGrayData(stepData, gray, "Gray");

                    //Mat canny = new Mat();
                    //canny = Canny(gray);
                    //stepData = AddData(stepData, canny, "Canny");


                    //List<RotatedRect> rect = new List<RotatedRect>();
                    //rect = Contours(canny);

                    //Mat rectImg = new Mat();

                    //rectImg = DrawRect(imgg.Mat, rect);
                    //stepData = AddData(stepData, rectImg, "With Rectangle");

                    //List<Mat> mat = RoI(rect, threshold.Clone());

                    //int i = 0;
                    //foreach (Mat m in mat)
                    //{
                    //    i += 1;
                    //    stepData = AddData(stepData, m, i.ToString());
                    //}
                }
            }
            return(View("Index", stepData));
        }
コード例 #6
0
        public StepData StepsVideo(string uploadFile)
        {
            StepData           stepData        = new StepData();
            ProcessCaptured    processCaptured = new ProcessCaptured();
            Image <Bgra, Byte> imgg            = null;

            if (!string.IsNullOrWhiteSpace(uploadFile))
            {
                var    element   = CvInvoke.GetStructuringElement(ElementShape.Rectangle, new Size(22, 3), new Point(1, 1));
                byte[] fileBytes = Convert.FromBase64String(uploadFile);
                using (MemoryStream ms = new MemoryStream(fileBytes))
                {
                    imgg = processCaptured.GetImageFromStream(ms);
                    Mat finalImg = new Mat();
                    using (Mat im = imgg.Mat.Clone())
                    {
                        using (Mat blur = new Mat())
                        {
                            using (Mat gray = new Mat())
                            {
                                using (Mat sobal = new Mat())
                                {
                                    using (Mat threshold = new Mat())
                                    {
                                        using (Mat morpology = new Mat())
                                        {
                                            using (Mat canny = new Mat())
                                            {
                                                Size size = new Size(7, 7);
                                                CvInvoke.Threshold(im, threshold, 100, 255, ThresholdType.Binary);
                                                //CvInvoke.GaussianBlur(im, blur, size, 0);
                                                CvInvoke.CvtColor(im, gray, ColorConversion.Bgr2Gray);
                                                //CvInvoke.Sobel(gray, sobal, DepthType.Cv8U, 1, 0, 3);
                                                //CvInvoke.Threshold(sobal, threshold, 0, 255, ThresholdType.Otsu);
                                                //CvInvoke.MorphologyEx(gray, morpology, MorphOp.Close, element, new Point(0, 0), 1, BorderType.Default, new MCvScalar());
                                                CvInvoke.Canny(gray, canny, 100, 50, 7);
                                                finalImg = im.Clone();
                                                var box = Contours(canny);
                                                //Mat mat=DrawRect(finalImg, box);
                                                List <Mat> roi  = RoI(box, finalImg);
                                                string     path = AppContext.BaseDirectory;
                                                using (Tesseract _ocr = new Tesseract(path, "eng", OcrEngineMode.TesseractLstmCombined))
                                                {
                                                    _ocr.SetVariable("tessedit_char_whitelist", "ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890");
                                                    foreach (Mat m in roi)
                                                    {
                                                        _ocr.SetImage(m);
                                                        _ocr.Recognize();
                                                        Tesseract.Character[] words = _ocr.GetCharacters();
                                                        //string wor=words.
                                                        StringBuilder sb = new StringBuilder();
                                                        foreach (var c in words)
                                                        {
                                                            sb.Append(c.Text);
                                                        }
                                                        string strTemp = sb.ToString().Replace(" ", "");
                                                        if (strTemp.ToString().Length > 3 && strTemp.Length <= 10)
                                                        {
                                                            stepData = AddData(stepData, m, "", strTemp);
                                                        }
                                                    }
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
            imgg = null;
            return(stepData);
        }