Example #1
0
        /// <summary>
        /// Generates a binary image where only the lane markings wil be high.
        /// Filters based on color (allows white & yellow markings).
        /// </summary>
        /// <param name="src"> BGR image to filter </param>
        /// <returns> Binary lane markings image </returns>
        public Mat FilterMarkings(Mat src)
        {
            // Filter (pass) white & yellow
            Mat white  = FilterHSV(src, HsvWhiteMin, HsvWhiteMax);
            Mat yellow = FilterHSV(src, HsvYellowMin, HsvYellowMax);

            // Equalize histogram and thresh
            Mat whiteFromEq = GetWhiteFromHistogramEq(src, 250, 255);
            Mat bin         = new Mat(src.Rows(), src.Cols(), CvType.Cv8uc1, Scalar.All(0));

            Core.Core.Bitwise_or(bin, white, bin);
            Core.Core.Bitwise_or(bin, yellow, bin);
            Core.Core.Bitwise_or(bin, whiteFromEq, bin);

            //kernel.SetTo(new Scalar(1));
            Imgproc.MorphologyEx(bin, bin, Imgproc.MorphClose, kernel, new Point(-1, -1), 1);

            white.Release();
            yellow.Release();
            whiteFromEq.Release();

            return(bin);
        }
Example #2
0
        private IList <MatOfPoint> ProcessImage()
        {
            Mat grayMat  = new Mat();
            Mat blurMat  = new Mat();
            Mat edgesMat = new Mat();
            Mat final    = new Mat();
            Mat h        = new Mat();

            IList <MatOfPoint> contours = new JavaList <MatOfPoint>();

            OpenCV.Android.Utils.BitmapToMat(originalImage, originalMat);
            originalImage.Dispose();
            Imgproc.CvtColor(originalMat, grayMat, Imgproc.ColorBgr2gray);
            Imgproc.GaussianBlur(grayMat, blurMat, new OpenCV.Core.Size(3, 3), 0);
            Imgproc.Canny(blurMat, edgesMat, 10, 250);


            Mat kernel = Imgproc.GetStructuringElement(Imgproc.MorphRect, new Size(3, 3));

            Imgproc.MorphologyEx(edgesMat, final, Imgproc.MorphClose, kernel);

            Imgproc.FindContours(final, contours, h, Imgproc.RetrExternal, Imgproc.ChainApproxSimple);
            return(contours);
        }
Example #3
0
        public static async Task <string> detectAndExtractText(Bitmap img)
        {
            //Matrix für die Bilder
            Mat large = new Mat();
            Mat small = new Mat();
            Mat rgb   = new Mat();

            //Bild zu Matrix umwandeln
            Utils.BitmapToMat(img, large);

            // downsample and use it for processing
            Imgproc.PyrDown(large, rgb);

            //Grey
            Imgproc.CvtColor(rgb, small, Imgproc.ColorBgr2gray);

            //Gradiant
            Mat  grad        = new Mat();
            Size morphsize   = new Size(3.0, 3.0);
            Mat  morphKernel = Imgproc.GetStructuringElement(Imgproc.MorphEllipse, morphsize);

            Imgproc.MorphologyEx(small, grad, Imgproc.MorphGradient, morphKernel);

            //Binarize
            Mat bw = new Mat();

            Imgproc.Threshold(grad, bw, 0.0, 255.0, Imgproc.ThreshBinary | Imgproc.ThreshOtsu);

            // connect horizontally oriented regions
            Mat  connected   = new Mat();
            Size connectsize = new Size(9.0, 1.0);

            morphKernel = Imgproc.GetStructuringElement(Imgproc.MorphRect, connectsize);
            Imgproc.MorphologyEx(bw, connected, Imgproc.MorphClose, morphKernel);

            // find contours
            Mat mask = Mat.Zeros(bw.Size(), CvType.Cv8uc1);

            JavaList <MatOfPoint> contours = new JavaList <MatOfPoint>();
            Mat hierarchy = new Mat();

            OpenCV.Core.Point contourPoint = new OpenCV.Core.Point(0, 0);

            Imgproc.FindContours(connected, contours, hierarchy, Imgproc.RetrCcomp, Imgproc.ChainApproxSimple, contourPoint);

            Scalar zero        = new Scalar(0, 0, 0);
            Scalar contourscal = new Scalar(255, 255, 255);

            Scalar rectScalar = new Scalar(0, 255, 0);


            OpenCV.Core.Rect rect;
            Mat    maskROI;
            double r;

            double[] contourInfo;

            string resulttext = "";
            string part;

            Bitmap bmpOcr;
            Mat    croppedPart;


            for (int i = 0; i >= 0;)
            {
                rect = Imgproc.BoundingRect(contours[i]);

                maskROI = new Mat(mask, rect);
                maskROI.SetTo(zero);

                //fill the contour
                Imgproc.DrawContours(mask, contours, i, contourscal, Core.Filled);

                // ratio of non-zero pixels in the filled region
                r = (double)Core.CountNonZero(maskROI) / (rect.Width * rect.Height);

                /* assume at least 45% of the area is filled if it contains text */
                /* constraints on region size */

                /* these two conditions alone are not very robust. better to use something
                 * like the number of significant peaks in a horizontal projection as a third condition */
                if (r > .45 && (rect.Height > 8 && rect.Width > 8))
                {
                    //Imgproc.Rectangle(rgb, rect.Br(), rect.Tl(), rectScalar, 2);
                    try
                    {
                        croppedPart = rgb.Submat(rect);

                        bmpOcr = Bitmap.CreateBitmap(croppedPart.Width(), croppedPart.Height(), Bitmap.Config.Argb8888);
                        Utils.MatToBitmap(croppedPart, bmpOcr);

                        part = await OCR.getText(bmpOcr);

                        resulttext = resulttext + part;
                        Console.WriteLine("------------------Durchlauf-------------");
                    }
                    catch (Exception e)
                    {
                        Android.Util.Log.Debug("Fehler", "cropped part data error " + e.Message);
                    }
                }


                //Nächste Element bestimmen
                contourInfo = hierarchy.Get(0, i);
                i           = (int)contourInfo[0];
            }


            return(resulttext);
        }