void ComputerVisionAlgo(IntPtr greyscale)
    {
        Utils.copyToMat(greyscale, imageMat);


        // Inverting Image pixel values
        inMat = (Mat.ones(imageMat.rows(), imageMat.cols(), CvType.CV_8UC1) * 255) - imageMat;

        // Creating Detector (Yellow Circle)
        // MatOfKeyPoint keyMat = new MatOfKeyPoint();
        // SimpleBlobDetector detector = SimpleBlobDetector.create();


        // Creating Detector (Red Circle)
        MatOfKeyPoint      keyMat   = new MatOfKeyPoint();
        SimpleBlobDetector detector = SimpleBlobDetector.create();

        inMat = imageMat;
        detector.read(circparam_path);

        // Finding circles
        detector.detect(inMat, keyMat);
        if (keyMat.size().height > 0)
        {
            blob_x = keyMat.get(0, 0)[0];
            blob_y = keyMat.get(0, 0)[1];
            blob_r = keyMat.get(0, 0)[2];
        }

        // Visualizing detected circles
        m_ImageInfo.text = string.Format("Circle Count: {0}\n Circle[0]: {1} x {2} -- {3}",
                                         keyMat.size().height, blob_x, blob_y, blob_r);

        Features2d.drawKeypoints(imageMat, keyMat, outMat);
    }
Exemplo n.º 2
0
    void getKeyPoints(Mat matImg)
    {
        keyPix.Clear();
        submats.Clear();
        MatOfKeyPoint keypts    = new MatOfKeyPoint();
        Mat           hierarchy = new Mat();

        blobber.detect(matImg, keypts);
        // Debug.Log("key points are " + keypts.size());
        Mat imgWithKeyPts = new Mat();

        Features2d.drawKeypoints(matImg, keypts, imgWithKeyPts, new Scalar(255, 255, 255), 4);
        Texture2D matToText = new Texture2D(imgWithKeyPts.cols(), imgWithKeyPts.rows(), TextureFormat.RGBA32, false);

        KeyPoint[] keyPtArray = keypts.toArray();
        for (int i = 0; i < keyPtArray.Length; i++)
        {
            //keyPix.Add(Camera.main.ScreenToWorldPoint(new Vector3((float)keyPtArray[i].pt.x, (float)keyPtArray[i].pt.y), Camera.MonoOrStereoscopicEye.Mono));
            keyPix.Add(new Vector3((float)keyPtArray[i].pt.x, (float)keyPtArray[i].pt.y, keyPtArray[i].size));
            //keyPix3.Add(Camera.main.ScreenToViewportPoint(new Vector3((int)keyPtArray[i].pt.x, (int)keyPtArray[i].pt.y, 0)));
        }

        // disposal
        Destroy(matToText);
        imgWithKeyPts.Dispose();
        keypts.Dispose();
        hierarchy.Dispose();
    }
        // Use this for initialization
        void Start()
        {
            Texture2D imgTexture = Resources.Load("detect_blob") as Texture2D;

            Mat imgMat = new Mat(imgTexture.height, imgTexture.width, CvType.CV_8UC1);

            Utils.texture2DToMat(imgTexture, imgMat);
            Debug.Log("imgMat dst ToString " + imgMat.ToString());

            Mat outImgMat = new Mat();

            FeatureDetector blobDetector = FeatureDetector.create(FeatureDetector.SIMPLEBLOB);

            blobDetector.read(Utils.getFilePath("blobparams.yml"));


            MatOfKeyPoint keypoints = new MatOfKeyPoint();

            blobDetector.detect(imgMat, keypoints);
            Features2d.drawKeypoints(imgMat, keypoints, outImgMat);


            Texture2D texture = new Texture2D(outImgMat.cols(), outImgMat.rows(), TextureFormat.RGBA32, false);

            Utils.matToTexture2D(outImgMat, texture);

            gameObject.GetComponent <Renderer> ().material.mainTexture = texture;
        }
        // Update is called once per frame
        void Update()
        {
            if (webCamTextureToMatHelper.IsPlaying() && webCamTextureToMatHelper.DidUpdateThisFrame())
            {
                Mat rgbaMat = webCamTextureToMatHelper.GetMat();

                Imgproc.cvtColor(rgbaMat, rgbMat, Imgproc.COLOR_RGBA2RGB);

                detector.detect(rgbaMat, keypoints);
//                Debug.Log ("keypoints.ToString() " + keypoints.ToString());
                Features2d.drawKeypoints(rgbMat, keypoints, rgbaMat, Scalar.all(-1), Features2d.NOT_DRAW_SINGLE_POINTS);

                Imgproc.rectangle(rgbaMat, patternRect.tl(), patternRect.br(), new Scalar(255, 0, 0, 255), 5);

                Utils.matToTexture2D(rgbaMat, texture, webCamTextureToMatHelper.GetBufferColors());
            }
        }
    // Detects Corners with Detector Framework and Draws Keypoints to outMat
    void CornerDetection()
    {
        // Creating Detector
        int   octaves        = 6;
        float corner_thresh  = 0.015f;
        float dog_thresh     = 0.015f;
        int   max_detections = 5;
        HarrisLaplaceFeatureDetector detector = HarrisLaplaceFeatureDetector.create(
            octaves, corner_thresh, dog_thresh, max_detections);

        // Finding corners
        // imageMat = cached_initMat;
        Core.flip(cached_initMat, imageMat, 0);
        keyMat = new MatOfKeyPoint();
        detector.detect(imageMat, keyMat);

        // Draw corners
        Features2d.drawKeypoints(imageMat, keyMat, outMat);
    }
Exemplo n.º 6
0
    // Detects Blobs with Detector Framework and stores Top-down view into cached_homoMat
    void BlobDetection()
    {
        SimpleBlobDetector detector = SimpleBlobDetector.create();

        Core.flip(cached_initMat, imageMat, 0);
        keyMat = new MatOfKeyPoint();
        detector.detect(imageMat, keyMat);

        Features2d.drawKeypoints(imageMat, keyMat, outMat);

        if (keyMat.rows() < 7)
        {
            return;
        }

        for (int i = 0; i < 7; i++)
        {
            srcPointArray[i] = new Point(keyMat.get(i, 0)[0], keyMat.get(i, 0)[1]);
        }

        SortBox();
    }
        private void Run()
        {
            //if true, The error log of the Native side OpenCV will be displayed on the Unity Editor Console.
            Utils.setDebugMode(true);


            Texture2D imgTexture = Resources.Load("detect_blob") as Texture2D;

            Mat imgMat = new Mat(imgTexture.height, imgTexture.width, CvType.CV_8UC1);

            Utils.texture2DToMat(imgTexture, imgMat);
            Debug.Log("imgMat.ToString() " + imgMat.ToString());

            Mat outImgMat = new Mat();

            SimpleBlobDetector blobDetector = SimpleBlobDetector.create();

            Debug.Log("blobDetector.getDefaultName() " + blobDetector.getDefaultName());

            blobDetector.read(blobparams_yml_filepath);



            MatOfKeyPoint keypoints = new MatOfKeyPoint();

            blobDetector.detect(imgMat, keypoints);
            Features2d.drawKeypoints(imgMat, keypoints, outImgMat);


            Texture2D texture = new Texture2D(outImgMat.cols(), outImgMat.rows(), TextureFormat.RGBA32, false);

            Utils.matToTexture2D(outImgMat, texture);

            gameObject.GetComponent <Renderer>().material.mainTexture = texture;


            Utils.setDebugMode(false);
        }
Exemplo n.º 8
0
    void ComputerVisionAlgo(IntPtr greyscale)
    {
        Utils.copyToMat(greyscale, imageMat);

        // Creating Detector (Red Circle)
        MatOfKeyPoint keyMat = new MatOfKeyPoint();
        HarrisLaplaceFeatureDetector detector = HarrisLaplaceFeatureDetector.create(6, 0.015f, 0.015f, 5);

        // Finding circles
        Debug.Log(keyMat.size());
        detector.detect(imageMat, keyMat);
        if (keyMat.size().height > 0)
        {
            double blob_x = keyMat.get(0, 0)[0];
            double blob_y = keyMat.get(0, 0)[1];
            double blob_r = keyMat.get(0, 0)[2];
        }

        // Visualizing detected circles
        // m_ImageInfo.text = string.Format("Circle Count: {0}\n Circle[0]: {1} x {2} -- {3}",
        // keyMat.size().height, blob_x, blob_y, blob_r);

        Features2d.drawKeypoints(imageMat, keyMat, outMat);
    }
Exemplo n.º 9
0
    public bool descriptorsORB_Old(Mat RGB, Mat cameraFeed, string targetName)//找出特徵的顏色方法三(可運行但效率不佳放棄)
    {
        if (RGB == null)
        {
            Debug.Log("RGB Mat is Null");
            return(false);
        }
        //將傳入的RGB存入Src
        Mat SrcMat = new Mat();

        RGB.copyTo(SrcMat);
        //比對樣本
        Texture2D imgTexture = Resources.Load(targetName) as Texture2D;
        //  Texture2D imgTexture2 = Resources.Load("lenaK") as Texture2D;

        //Texture2D轉Mat
        Mat img1Mat = new Mat(imgTexture.height, imgTexture.width, CvType.CV_8UC3);

        Utils.texture2DToMat(imgTexture, img1Mat);

        //創建 ORB的特徵點裝置
        FeatureDetector     detector  = FeatureDetector.create(FeatureDetector.ORB);
        DescriptorExtractor extractor = DescriptorExtractor.create(DescriptorExtractor.ORB);
        //產生存放特徵點Mat
        MatOfKeyPoint keypoints1     = new MatOfKeyPoint();
        Mat           descriptors1   = new Mat();
        MatOfKeyPoint keypointsSrc   = new MatOfKeyPoint();
        Mat           descriptorsSrc = new Mat();

        //找特徵點圖1
        detector.detect(img1Mat, keypoints1);
        extractor.compute(img1Mat, keypoints1, descriptors1);
        //找特徵點圖Src
        detector.detect(SrcMat, keypointsSrc);
        extractor.compute(SrcMat, keypointsSrc, descriptorsSrc);

        DescriptorMatcher matcher = DescriptorMatcher.create(DescriptorMatcher.BRUTEFORCE_HAMMINGLUT);
        MatOfDMatch       matches = new MatOfDMatch();

        matcher.match(descriptors1, descriptorsSrc, matches);
        DMatch[] arrayDmatch = matches.toArray();

        for (int i = arrayDmatch.Length - 1; i >= 0; i--)
        {
            //   Debug.Log("match " + i + ": " + arrayDmatch[i].distance);
        }
        //做篩選
        double max_dist = 0;
        double min_dist = 100;
        //-- Quick calculation of max and min distances between keypoints
        double dist = new double();

        for (int i = 0; i < matches.rows(); i++)
        {
            dist = arrayDmatch[i].distance;
            if (dist < min_dist)
            {
                min_dist = dist;
            }
            if (dist > max_dist)
            {
                max_dist = dist;
            }
        }
        Debug.Log("Max dist :" + max_dist);
        Debug.Log("Min dist :" + min_dist);
        //只畫好的點

        List <DMatch> matchesGoodList = new List <DMatch>();

        for (int i = 0; i < matches.rows(); i++)
        {
            //if (arrayDmatch[i].distance < RateDist.value * min_dist)
            //{
            //    //Debug.Log("match " + i + ": " + arrayDmatch[i].distance);
            //    matchesGoodList.Add(arrayDmatch[i]);
            //}
        }
        MatOfDMatch matchesGood = new MatOfDMatch();

        matchesGood.fromList(matchesGoodList);

        //Draw Keypoints
        Features2d.drawKeypoints(SrcMat, keypointsSrc, SrcMat);

        //做輸出的轉換予宣告

        Mat resultImg = new Mat();
        // Features2d.drawMatches(img1Mat, keypoints1, SrcMat, keypointsSrc, matchesGood, resultImg);

        List <Point> P1 = new List <Point>();
        // List<Point> P2 = new List<Point>();
        List <Point> pSrc = new List <Point>();

        Debug.Log("MatchCount" + matchesGoodList.Count);
        for (int i = 0; i < matchesGoodList.Count; i++)
        {
            P1.Add(new Point(keypoints1.toArray()[matchesGoodList[i].queryIdx].pt.x, keypoints1.toArray()[matchesGoodList[i].queryIdx].pt.y));
            pSrc.Add(new Point(keypointsSrc.toArray()[matchesGoodList[i].trainIdx].pt.x, keypointsSrc.toArray()[matchesGoodList[i].trainIdx].pt.y));
            //Debug.Log("ID = " + matchesGoodList[i].queryIdx );
            //Debug.Log("x,y =" + (int)keypoints1.toArray()[matchesGoodList[i].queryIdx].pt.x + "," + (int)keypoints1.toArray()[matchesGoodList[i].queryIdx].pt.y);
            //Debug.Log("x,y =" + (int)keypoints2.toArray()[matchesGoodList[i].trainIdx].pt.x + "," + (int)keypoints2.toArray()[matchesGoodList[i].trainIdx].pt.y);
        }

        MatOfPoint2f p2fTarget = new MatOfPoint2f(P1.ToArray());
        MatOfPoint2f p2fSrc    = new MatOfPoint2f(pSrc.ToArray());

        Mat          matrixH         = Calib3d.findHomography(p2fTarget, p2fSrc, Calib3d.RANSAC, 3);
        List <Point> srcPointCorners = new List <Point>();

        srcPointCorners.Add(new Point(0, 0));
        srcPointCorners.Add(new Point(img1Mat.width(), 0));
        srcPointCorners.Add(new Point(img1Mat.width(), img1Mat.height()));
        srcPointCorners.Add(new Point(0, img1Mat.height()));

        Mat          originalRect       = Converters.vector_Point2f_to_Mat(srcPointCorners);
        List <Point> srcPointCornersEnd = new List <Point>();

        srcPointCornersEnd.Add(new Point(0, img1Mat.height()));
        srcPointCornersEnd.Add(new Point(0, 0));
        srcPointCornersEnd.Add(new Point(img1Mat.width(), 0));
        srcPointCornersEnd.Add(new Point(img1Mat.width(), img1Mat.height()));

        Mat changeRect = Converters.vector_Point2f_to_Mat(srcPointCornersEnd);

        Core.perspectiveTransform(originalRect, changeRect, matrixH);
        List <Point> srcPointCornersSave = new List <Point>();

        Converters.Mat_to_vector_Point(changeRect, srcPointCornersSave);

        if ((srcPointCornersSave[2].x - srcPointCornersSave[0].x) < 5 || (srcPointCornersSave[2].y - srcPointCornersSave[0].y) < 5)
        {
            Debug.Log("Match Out Put image is to small");
            SrcMat.copyTo(cameraFeed);
            SrcMat.release();
            Imgproc.putText(cameraFeed, "X-S", new Point(10, 50), 0, 1, new Scalar(255, 255, 255), 2);
            return(false);
        }
        //    Features2d.drawMatches(img1Mat, keypoints1, SrcMat, keypointsSrc, matchesGood, resultImg);
        Imgproc.line(SrcMat, srcPointCornersSave[0], srcPointCornersSave[1], new Scalar(255, 0, 0), 3);
        Imgproc.line(SrcMat, srcPointCornersSave[1], srcPointCornersSave[2], new Scalar(255, 0, 0), 3);
        Imgproc.line(SrcMat, srcPointCornersSave[2], srcPointCornersSave[3], new Scalar(255, 0, 0), 3);
        Imgproc.line(SrcMat, srcPointCornersSave[3], srcPointCornersSave[0], new Scalar(255, 0, 0), 3);

        SrcMat.copyTo(cameraFeed);
        keypoints1.release();
        img1Mat.release();
        SrcMat.release();
        return(true);
    }
Exemplo n.º 10
0
//============================================================
//=================以下為沒有再使用的函式=====================
//============================================================

    //找出特徵的顏色方法三(ORB特徵點比對)
    public bool descriptorsORB(Mat RGB, Mat cameraFeed, string targetName)
    {
        if (RGB == null)
        {
            Debug.Log("RGB Mat is Null");
            return(false);
        }
        //將傳入的RGB存入Src
        Mat SrcMat = new Mat();

        RGB.copyTo(SrcMat);
        //比對樣本載入
        Texture2D imgTexture = Resources.Load(targetName) as Texture2D;

        //Texture2D轉Mat
        Mat targetMat = new Mat(imgTexture.height, imgTexture.width, CvType.CV_8UC3);

        Utils.texture2DToMat(imgTexture, targetMat);

        //創建 ORB的特徵點裝置
        FeatureDetector     detector  = FeatureDetector.create(FeatureDetector.ORB);
        DescriptorExtractor extractor = DescriptorExtractor.create(DescriptorExtractor.ORB);

        //產生存放特徵點Mat
        MatOfKeyPoint keypointsTarget   = new MatOfKeyPoint();
        Mat           descriptorsTarget = new Mat();
        MatOfKeyPoint keypointsSrc      = new MatOfKeyPoint();
        Mat           descriptorsSrc    = new Mat();

        //找特徵點圖Target
        detector.detect(targetMat, keypointsTarget);
        extractor.compute(targetMat, keypointsTarget, descriptorsTarget);

        //找特徵點圖Src
        detector.detect(SrcMat, keypointsSrc);
        extractor.compute(SrcMat, keypointsSrc, descriptorsSrc);

        //創建特徵點比對物件
        DescriptorMatcher matcher = DescriptorMatcher.create(DescriptorMatcher.BRUTEFORCE_HAMMINGLUT);
        MatOfDMatch       matches = new MatOfDMatch();

        //丟入兩影像的特徵點
        matcher.match(descriptorsTarget, descriptorsSrc, matches);
        DMatch[] arrayDmatch = matches.toArray();

        //做篩選
        double max_dist = 0;
        double min_dist = 100;
        //-- Quick calculation of max and min distances between keypoints
        double dist = new double();

        for (int i = 0; i < matches.rows(); i++)
        {
            dist = arrayDmatch[i].distance;
            if (dist < min_dist)
            {
                min_dist = dist;
            }
            if (dist > max_dist)
            {
                max_dist = dist;
            }
        }
        Debug.Log("Max dist :" + max_dist);
        Debug.Log("Min dist :" + min_dist);

        List <DMatch> matchesGoodList = new List <DMatch>();

        MatOfDMatch matchesGood = new MatOfDMatch();

        matchesGood.fromList(matchesGoodList);

        //Draw Keypoints
        Features2d.drawKeypoints(SrcMat, keypointsSrc, SrcMat);

        List <Point> pTarget = new List <Point>();
        List <Point> pSrc    = new List <Point>();

        Debug.Log("MatchCount" + matchesGoodList.Count);
        for (int i = 0; i < matchesGoodList.Count; i++)
        {
            pTarget.Add(new Point(keypointsTarget.toArray()[matchesGoodList[i].queryIdx].pt.x, keypointsTarget.toArray()[matchesGoodList[i].queryIdx].pt.y));
            pSrc.Add(new Point(keypointsSrc.toArray()[matchesGoodList[i].trainIdx].pt.x, keypointsSrc.toArray()[matchesGoodList[i].trainIdx].pt.y));
        }

        MatOfPoint2f p2fTarget = new MatOfPoint2f(pTarget.ToArray());
        MatOfPoint2f p2fSrc    = new MatOfPoint2f(pSrc.ToArray());

        Mat matrixH = Calib3d.findHomography(p2fTarget, p2fSrc, Calib3d.RANSAC, 3);

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

        srcPointCorners.Add(new Point(0, 0));
        srcPointCorners.Add(new Point(targetMat.width(), 0));
        srcPointCorners.Add(new Point(targetMat.width(), targetMat.height()));
        srcPointCorners.Add(new Point(0, targetMat.height()));
        Mat originalRect = Converters.vector_Point2f_to_Mat(srcPointCorners);

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

        srcPointCornersEnd.Add(new Point(0, targetMat.height()));
        srcPointCornersEnd.Add(new Point(0, 0));
        srcPointCornersEnd.Add(new Point(targetMat.width(), 0));
        srcPointCornersEnd.Add(new Point(targetMat.width(), targetMat.height()));
        Mat changeRect = Converters.vector_Point2f_to_Mat(srcPointCornersEnd);

        Core.perspectiveTransform(originalRect, changeRect, matrixH);
        List <Point> srcPointCornersSave = new List <Point>();

        Converters.Mat_to_vector_Point(changeRect, srcPointCornersSave);

        if ((srcPointCornersSave[2].x - srcPointCornersSave[0].x) < 5 || (srcPointCornersSave[2].y - srcPointCornersSave[0].y) < 5)
        {
            Debug.Log("Match Out Put image is to small");
            SrcMat.copyTo(cameraFeed);
            SrcMat.release();
            Imgproc.putText(cameraFeed, targetName, srcPointCornersSave[0], 0, 1, new Scalar(255, 255, 255), 2);
            return(false);
        }
        //畫出框框
        Imgproc.line(SrcMat, srcPointCornersSave[0], srcPointCornersSave[1], new Scalar(255, 0, 0), 3);
        Imgproc.line(SrcMat, srcPointCornersSave[1], srcPointCornersSave[2], new Scalar(255, 0, 0), 3);
        Imgproc.line(SrcMat, srcPointCornersSave[2], srcPointCornersSave[3], new Scalar(255, 0, 0), 3);
        Imgproc.line(SrcMat, srcPointCornersSave[3], srcPointCornersSave[0], new Scalar(255, 0, 0), 3);
        //畫中心
        Point middlePoint = new Point((srcPointCornersSave[0].x + srcPointCornersSave[2].x) / 2, (srcPointCornersSave[0].y + srcPointCornersSave[2].y) / 2);

        Imgproc.line(SrcMat, middlePoint, middlePoint, new Scalar(0, 0, 255), 10);


        SrcMat.copyTo(cameraFeed);
        keypointsTarget.release();
        targetMat.release();
        SrcMat.release();
        return(true);
    }
Exemplo n.º 11
0
        public void processFrame()
        {
            if (inversion)
            {
                //flip
                Core.bitwise_not(toneMat, toneMat);
            }
            if (resize)
            {
                Imgproc.resize(toneMat, toneMat, new Size((int)Math.Round(resizeRatio * toneMat.width()), (int)Math.Round(resizeRatio * toneMat.height())));
            }
            //
            if (toneThreshold)
            {
                Imgproc.threshold(toneMat, toneMat, thresholdValue, 255, Imgproc.THRESH_BINARY);
            }
            if (blobs)
            {
                blobDetector.detect(toneMat, keypoints);
                Features2d.drawKeypoints(toneMat, keypoints, toneMat);
            }
            if (blur)
            {
                Imgproc.blur(toneMat, toneMat, new Size(blurSize, blurSize));
            }
            if (centerPoint)
            {
                moments.Add(Imgproc.moments(toneMat, true));
                WeightedCentroid.Add(new Point((int)Math.Round(moments[0].m10 / moments[0].m00), (int)Math.Round(moments[0].m01 / moments[0].m00)));
                Debug.Log("center: " + WeightedCentroid[0].x + ", " + WeightedCentroid[0].y);
            }
            if (edge)
            {
                Imgproc.Canny(toneMat, toneMat, thresholdValue * 0.5, thresholdValue);
                //Imgproc.findContours (toneMat, contours, hierarchy, Imgproc.RETR_EXTERNAL, Imgproc.CHAIN_APPROX_SIMPLE );
                //
                //					foreach(MatOfPoint i in contours){
                //						Debug.Log ("contour " + i + ": " + i.ToString());
                //					}
                //Debug.Log ("contours count: " + contours.Count);
                moments.Add(Imgproc.moments(toneMat, true));
                if (WeightedCentroid.Count == 0)
                {
                    moments.Add(Imgproc.moments(toneMat, true));
                    WeightedCentroid.Add(new Point(0, 0));
                }
                WeightedCentroid.Add(new Point((int)Math.Round(moments[1].m10 / moments[1].m00), (int)Math.Round(moments[1].m01 / moments[1].m00)));

                if (thresholdValue >= thresholdValueCap && edgeCenterPoint == true)
                {
                    Imgproc.ellipse(toneMat, WeightedCentroid [1], new Size(20, 20), 1, 0.1, 360, new Scalar(180), 10);
                    Imgproc.putText(toneMat, " Edge center point", WeightedCentroid [1], 0, 1.5, new Scalar(180), 5);
                }
            }
            //draw center
            if (centerPoint)
            {
                Imgproc.ellipse(toneMat, WeightedCentroid [0], new Size(20, 20), 1, 0.1, 360, new Scalar(180), 10);
                Imgproc.putText(toneMat, " Tone center point", WeightedCentroid [0], 0, 1.5, new Scalar(180), 5);
            }
            if (resize)
            {
                Imgproc.resize(toneMat, toneMat, new Size((int)Math.Round((1 / resizeRatio) * toneMat.width()), (int)Math.Round((1 / resizeRatio) * toneMat.height())));
            }
            //assign to display
            if (showProcessing)
            {
                rgbaMat = toneMat;
            }
            else
            {
                rgbaMat = cloneMat;
            }

            WeightedCentroid.Clear();
            moments.Clear();
            contours.Clear();

            framesDropCount = 0;
        }
Exemplo n.º 12
0
    private void TryProcessImage(int index)
    {
        if (UseWebCam == false)
        {
            CurrentTexture = Sources[index];
        }
        else
        {
            CurrentTexture = webCamTexture;
        }

        using (Mat imgMat = new Mat(CurrentTexture.height, CurrentTexture.width, CvType.CV_8UC1))
            using (FeatureDetector blobDetector = FeatureDetector.create(FeatureDetector.SIMPLEBLOB))
                using (Mat outImgMat = new Mat())
                    using (MatOfKeyPoint keypoints = new MatOfKeyPoint())
                    {
                        if (CurrentTexture is Texture2D)
                        {
                            Utils.texture2DToMat(CurrentTexture as Texture2D, imgMat);
                        }
                        else if (CurrentTexture is WebCamTexture)
                        {
                            Utils.webCamTextureToMat(CurrentTexture as WebCamTexture, imgMat);
                        }
                        else
                        {
                            Utils.textureToMat(CurrentTexture, imgMat);
                        }

                        Debug.Log("imgMat dst ToString " + imgMat.ToString());

                        Imgproc.threshold(imgMat, imgMat, 0, 255, Imgproc.THRESH_BINARY | Imgproc.THRESH_OTSU);
                        Imgproc.erode(imgMat, imgMat, erodeMat, new Point(1, 1), 5);

                        blobDetector.read(Utils.getFilePath("blobparams.yml"));

                        blobDetector.detect(imgMat, keypoints);

                        Features2d.drawKeypoints(imgMat, keypoints, outImgMat);

                        KeyPoint[] points = keypoints.toArray();

                        ProcessKeyPoints(points, outImgMat);

                        Mat finalMat = outImgMat;

                        if (texture != null &&
                            (texture.width != finalMat.cols() || texture.height != finalMat.rows()))
                        {
                            DestroyImmediate(texture);

                            texture = null;
                        }

                        if (texture == null)
                        {
                            texture = new Texture2D(finalMat.cols(), finalMat.rows(), TextureFormat.RGBA32, false);
                        }

                        Utils.matToTexture2D(finalMat, texture);

                        gameObject.GetComponent <Renderer>().material.mainTexture = texture;
                    }
    }