Example #1
0
        public static string CutSchnitzelImage(string inputData, IEnumerable <Tuple <string, double> > extraIinput = null)
        {
            var name = inputData;

            if (inputData.Contains(';'))
            {
                var inputDataSplitted = inputData.Split(';');
                name = inputDataSplitted[0];
                var percent = inputDataSplitted[1];
                if (percent != "0" && percent != "100")
                {
                    var percentInDouble = Double.Parse(inputDataSplitted[1]) / 100;
                    extraIinput = new List <Tuple <string, double> >()
                    {
                        new Tuple <string, double>(string.Empty, percentInDouble),
                        new Tuple <string, double>(string.Empty, 1 - percentInDouble),
                    };
                }
            }

            if (extraIinput == null)
            {
                extraIinput = new List <Tuple <string, double> >()
                {
                    new Tuple <string, double>(string.Empty, 0.5),
                    new Tuple <string, double>(string.Empty, 0.5),
                };
            }


            Bitmap bitMap  = DownloadFromStorage(name);
            var    newName = "modified-" + name;

            try
            {
                var imageCV = new Image <Bgr, byte>(bitMap);
                Mat mat     = imageCV.Mat;

                CvInvoke.CvtColor(mat, mat, ColorConversion.Bgr2Rgb);
                CvInvoke.CvtColor(mat, mat, ColorConversion.Rgb2Hsv);
                Image <Hsv, Byte> image = mat.ToImage <Hsv, Byte>();
                using (Image <Hsv, Byte> hsv = image.Convert <Hsv, Byte>())
                {
                    Image <Gray, Byte>[] channels = hsv.Split();
                    try
                    {
                        var botLimit = new ScalarArray(new MCvScalar(10, 125, 75));
                        var uprLimit = new ScalarArray(new MCvScalar(15, 255, 255));

                        var contours = new VectorOfVectorOfPoint();
                        Image <Hsv, byte> imageHsvDest = new Image <Hsv, byte>(image.Width, image.Width);
                        CvInvoke.InRange(mat, botLimit, uprLimit, imageHsvDest);
                        CvInvoke.FindContours(imageHsvDest, contours, null, RetrType.List, ChainApproxMethod.ChainApproxSimple);
                        var arrayList = new List <ContourArea>(contours.Size);
                        for (int i = 0; i < contours.Size; i++)
                        {
                            var contour = contours[i];
                            arrayList.Add(new ContourArea(CvInvoke.ContourArea(contour), contour));
                        }

                        var color           = new MCvScalar(255, 0, 0);
                        var biggestContours = arrayList.OrderByDescending(x => x.Area)?.Take(2);
                        var biggestContour  = biggestContours.FirstOrDefault();

                        if (biggestContour == null || biggestContour.Contour == null || biggestContour.Area < 100000)
                        {
                            try
                            {
                                StringBuilder imageComments         = new StringBuilder();
                                var           imageCV2              = new Image <Bgr, byte>(bitMap);
                                var           thisIsNotSchnitzelMat = imageCV2.Mat;
                                CvInvoke.CvtColor(thisIsNotSchnitzelMat, thisIsNotSchnitzelMat, ColorConversion.Bgr2Rgb);
                                CvInvoke.PutText(
                                    thisIsNotSchnitzelMat,
                                    "This is not a Schnitzel",
                                    new System.Drawing.Point(thisIsNotSchnitzelMat.Cols / 2, thisIsNotSchnitzelMat.Rows / 2),
                                    FontFace.HersheyPlain,
                                    2.0,
                                    new Rgb(0, 0, 255).MCvScalar, 3, LineType.Filled);

                                var newImage = thisIsNotSchnitzelMat.ToImage <Bgr, Byte>();
                                //var fileName = @"c:\temp\thisIsNotSchnizel.png";
                                //newImage.ToBitmap().Save(fileName);
                                UploadToStorage(newImage.ToJpegData(), newName);
                                return(newName);
                            }
                            catch (Exception e)
                            {
                                return("error3: " + e.Message + "\n" + e.StackTrace);
                            }
                        }

                        var mask = Mat.Zeros(imageHsvDest.Rows, imageHsvDest.Cols, DepthType.Cv8U, 3);

                        // Eliraz feature to write percent on schnitzel
                        if (biggestContours.Count() == 2 && biggestContours.Last().Area * 10 > biggestContour.Area)
                        {
                            var secondContour = biggestContours.Last();
                            var imageDivided  = new Image <Bgr, byte>(bitMap);

                            CvInvoke.CvtColor(imageDivided, imageDivided, ColorConversion.Bgr2Rgb);
                            var biggestContourArea = (int)(biggestContour.Area / (biggestContour.Area + secondContour.Area) * 100);
                            var secondContourArea  = 100 - biggestContourArea;
                            var biggestContourM    = CvInvoke.Moments(biggestContour.Contour);
                            var biggestContourX    = (int)(biggestContourM.M10 / biggestContourM.M00);
                            var biggestContourY    = (int)(biggestContourM.M01 / biggestContourM.M00);
                            var secondContourM     = CvInvoke.Moments(secondContour.Contour);
                            var secondContourX     = (int)(secondContourM.M10 / secondContourM.M00);
                            var secondContourY     = (int)(secondContourM.M01 / secondContourM.M00);
                            CvInvoke.PutText(
                                imageDivided,
                                biggestContourArea.ToString() + "%",
                                new System.Drawing.Point(biggestContourX, biggestContourY),
                                FontFace.HersheyPlain,
                                2.0,
                                new Rgb(0, 0, 255).MCvScalar, 3, LineType.Filled);
                            CvInvoke.PutText(
                                imageDivided,
                                secondContourArea.ToString() + "%",
                                new System.Drawing.Point(secondContourX, secondContourY),
                                FontFace.HersheyPlain,
                                2.0,
                                new Rgb(0, 0, 255).MCvScalar, 3, LineType.Filled);

                            var newImageToSave = mat.ToImage <Bgr, Byte>();
                            UploadToStorage(newImageToSave.ToJpegData(), newName);
                            return(newName);
                        }
                        else
                        {
                            CvInvoke.DrawContours(mask, biggestContour.Contour, 0, color, -1);
                            // CvInvoke.DrawContours(mask, new VectorOfVectorOfPoint(biggestContour.Contour), 0, color, -1);
                            var newByteImage = SchnitzelCutter.DividePicture(bitMap, mask, biggestContour.Area, extraIinput.ToList());
                            UploadToStorage(newByteImage, newName);
                            return(newName);
                        }
                    }
                    catch (Exception e)
                    {
                        Console.WriteLine(e.Message);
                        return("error1: " + e.Message + "\n" + e.StackTrace);
                    }
                    finally
                    {
                        channels[0].Dispose();
                        channels[1].Dispose();
                        channels[2].Dispose();
                    }
                }
            }
            catch (Exception e)
            {
                return("error2: " + e.Message);
                // do nothing
            }
        }
Example #2
0
        ///storage/emulated/0/Android/data/Camera2Basic.Camera2Basic/files/
        //[DllImport("cvextern.dll", CharSet = CharSet.Unicode)]
        //public static extern Mat Imread(string path, ImreadModes mode);
        public static void CutSchnitzelImage(string folderPath, IEnumerable <Tuple <string, double> > input = null)
        {
            var path = folderPath + "pic.jpg";

            try
            {
                var mat = CvInvoke.Imread(path, ImreadModes.AnyColor);
                CvInvoke.CvtColor(mat, mat, ColorConversion.Bgr2Rgb);
                CvInvoke.CvtColor(mat, mat, ColorConversion.Rgb2Hsv);
                Image <Hsv, Byte> image = mat.ToImage <Hsv, Byte>();
                using (Image <Hsv, Byte> hsv = image.Convert <Hsv, Byte>())
                {
                    Image <Gray, Byte>[] channels = hsv.Split();
                    try
                    {
                        var botLimit = new ScalarArray(new MCvScalar(10, 125, 75));
                        var uprLimit = new ScalarArray(new MCvScalar(15, 255, 255));

                        var contours = new VectorOfVectorOfPoint();
                        Image <Hsv, byte> imageHsvDest = new Image <Hsv, byte>(image.Width, image.Width);
                        CvInvoke.InRange(mat, botLimit, uprLimit, imageHsvDest);
                        CvInvoke.FindContours(imageHsvDest, contours, null, RetrType.List, ChainApproxMethod.ChainApproxSimple);
                        var arrayList = new List <ContourArea>(contours.Size);
                        for (int i = 0; i < contours.Size; i++)
                        {
                            var contour = contours[i];
                            arrayList.Add(new ContourArea(CvInvoke.ContourArea(contour), contour));
                        }

                        var color           = new MCvScalar(255, 0, 0);
                        var biggestContours = arrayList.OrderByDescending(x => x.Area)?.Take(2);
                        var biggestContour  = biggestContours.FirstOrDefault();

                        if (biggestContour == null || biggestContour.Area < 100000)
                        {
                            StringBuilder imageComments         = new StringBuilder();
                            var           thisIsNotSchnitzelMat = CvInvoke.Imread(path, ImreadModes.AnyColor);
                            CvInvoke.CvtColor(thisIsNotSchnitzelMat, thisIsNotSchnitzelMat, ColorConversion.Bgr2Rgb);
                            CvInvoke.PutText(
                                thisIsNotSchnitzelMat,
                                "This is not a schnitzel",
                                new System.Drawing.Point(thisIsNotSchnitzelMat.Cols / 2, thisIsNotSchnitzelMat.Rows / 2),
                                FontFace.HersheyPlain,
                                2.0,
                                new Rgb(0, 0, 255).MCvScalar, 3, LineType.Filled);

                            var newImage = thisIsNotSchnitzelMat.ToImage <Rgb, Byte>();
                            var fileName = @"c:\temp\thisIsNotSchnizel.png";
                            newImage.ToBitmap().Save(fileName);
                            return;
                        }


                        var mask = Mat.Zeros(imageHsvDest.Rows, imageHsvDest.Cols, DepthType.Cv8U, 3);
                        if (biggestContours.Count() == 2 && biggestContours.Last().Area * 10 > biggestContour.Area)
                        {
                            var secondContour = biggestContours.Last();
                            var imageDivided  = CvInvoke.Imread(path, ImreadModes.AnyColor);
                            CvInvoke.CvtColor(imageDivided, imageDivided, ColorConversion.Bgr2Rgb);
                            var biggestContourArea = (int)(biggestContour.Area / (biggestContour.Area + secondContour.Area) * 100);
                            var secondContourArea  = 100 - biggestContourArea;
                            var biggestContourM    = CvInvoke.Moments(biggestContour.Contour);
                            var biggestContourX    = (int)(biggestContourM.M10 / biggestContourM.M00);
                            var biggestContourY    = (int)(biggestContourM.M01 / biggestContourM.M00);
                            var secondContourM     = CvInvoke.Moments(secondContour.Contour);
                            var secondContourX     = (int)(secondContourM.M10 / secondContourM.M00);
                            var secondContourY     = (int)(secondContourM.M01 / secondContourM.M00);
                            CvInvoke.PutText(
                                imageDivided,
                                biggestContourArea.ToString() + "%",
                                new System.Drawing.Point(biggestContourX, biggestContourY),
                                FontFace.HersheyPlain,
                                2.0,
                                new Rgb(0, 0, 255).MCvScalar, 3, LineType.Filled);
                            CvInvoke.PutText(
                                imageDivided,
                                secondContourArea.ToString() + "%",
                                new System.Drawing.Point(secondContourX, secondContourY),
                                FontFace.HersheyPlain,
                                2.0,
                                new Rgb(0, 0, 255).MCvScalar, 3, LineType.Filled);
                            var newImage = imageDivided.ToImage <Rgb, Byte>();
                            var fileName = @"c:\temp\imageDivided.png";
                            newImage.ToBitmap().Save(fileName);
                            return;
                        }
                        else
                        {
                            CvInvoke.DrawContours(mask, biggestContour.Contour, 0, color, -1);
                        }

                        var newPicPath = SchnitzelCutter.DividePicture(path, mask, biggestContour.Area, input.ToList());
                        //return newPicPath;
                    }
                    catch (Exception e)
                    {
                        Console.WriteLine(e.Message);
                    }
                    finally
                    {
                        channels[0].Dispose();
                        channels[1].Dispose();
                        channels[2].Dispose();
                    }
                }
            }
            catch (Exception)
            {
                // do nothing
            }
        }