Пример #1
0
    public virtual int ProcessTexture(T texture, OpenCvSharp.Unity.TextureConversionParams texParams, bool detect = true)
    {
        // convert Unity texture to OpenCv::Mat
        ImportTexture(texture, texParams);

        // detect
        if (detect)
        {
            double invF = 1.0 / appliedFactor;
            DataStabilizer.ThresholdFactor = invF;

            // convert to grayscale and normalize
            Mat gray = new Mat();
            Cv2.CvtColor(processingImage, gray, ColorConversionCodes.BGR2GRAY);

            // fix shadows
            Cv2.EqualizeHist(gray, gray);

            /*Mat normalized = new Mat();
             * CLAHE clahe = CLAHE.Create();
             * clahe.TilesGridSize = new Size(8, 8);
             * clahe.Apply(gray, normalized);
             * gray = normalized;*/

            // detect matching regions (meats bounding)
            Rect[] rawMeats = cascadeMeats.DetectMultiScale(gray, 1.2, 6);
            if (Meats.Count != rawMeats.Length)
            {
                Meats.Clear();
            }

            for (int i = 0; i < rawMeats.Length; ++i)
            {
                Rect meatRect       = rawMeats[i];
                Rect meatRectScaled = meatRect * invF;
                using (Mat grayMeat = new Mat(gray, meatRect))
                {
                    // get meat object
                    DetectedMeat meat = null;
                    if (Meats.Count < i + 1)
                    {
                        meat = new DetectedMeat(DataStabilizer, meatRectScaled);
                        Meats.Add(meat);
                    }
                    else
                    {
                        meat = Meats[i];
                        meat.SetRegion(meatRectScaled);
                    }
                }
            }
            // log
            //UnityEngine.Debug.Log(String.Format("Found {0} meats", Meats.Count));
        }

        return(Meats.Count);
    }
Пример #2
0
    protected override bool ProcessTexture(WebCamTexture input, ref Texture2D output)
    {
        Mat    image      = OpenCvSharp.Unity.TextureToMat(input, TextureParameters);
        Mat    downscaled = image.Resize(Size.Zero, downScale, downScale);
        Rect2d obj        = Rect2d.Empty;
        var    areaRect   = new OpenCvSharp.Rect();

        int tempCount = processor.ProcessTexture(input, TextureParameters);

        if (meatlist.Count < tempCount)
        {
            Debug.Log(tempCount + " meat change");

            for (int i = 0; i < meatlist.Count; i++)
            {
                DropTracking(i);
            }

            meatlist = new List <DetectedMeat>(processor.Meats);
        }

        //Debug.Log("meat count : " + meatlist.Count);

        // If not dragged - show the tracking data
        if (meatlist.Count > 0)
        {
            for (int i = 0; i < meatlist.Count; i++)
            {
                DetectedMeat meat = meatlist[meatlist.Count - i - 1];

                // we have to tracker - let's initialize one
                if (tracker.Count <= i || tracker[i] == null)
                {
                    // but only if we have big enough "area of interest", this one is added to avoid "tracking" some 1x2 pixels areas
                    if (new Vector2(meat.Region.X, meat.Region.Y).magnitude >= minimumAreaDiagonal)
                    {
                        obj = new Rect2d(meat.Region.X * downScale, meat.Region.Y * downScale, meat.Region.Width * downScale, meat.Region.Height * downScale);

                        // initial tracker with current image and the given rect, one can play with tracker types here
                        if (tracker.Count <= i)
                        {
                            tracker.Add(Tracker.Create(TrackerTypes.MedianFlow));
                            meatText.Add(InstantiateText.Instxt(gameObject.GetComponent <RectTransform>(), meat.Region));
                            Debug.Log("add tracker");
                        }
                        else if (tracker[i] == null)
                        {
                            Debug.Log("replace tracker");
                            tracker[i]  = Tracker.Create(TrackerTypes.MedianFlow);
                            meatText[i] = InstantiateText.Instxt(gameObject.GetComponent <RectTransform>(), meat.Region);
                        }
                        tracker[i].Init(downscaled, obj);

                        //GameObject text = InstantiateText.Instxt(gameObject.GetComponent<RectTransform>(), meat.Region);

                        //frameSize.Add(downscaled.Size());

                        //var newVec = new Vector2((float)obj.X, -(float)obj.Y) - new Vector2(image.Width / 2.0f, -image.Height / 2.0f);

                        //GameObject a = GameObject.Instantiate(UIText, gameObject.transform.parent);
                        //a.transform.localPosition = (newVec + new Vector2((float)obj.Width / 2.0f, 0)) * gameObject.transform.localScale.x;
                    }
                }

                // if we already have an active tracker - just to to update with the new frame and check whether it still tracks object
                else
                {
                    // drop tracker if the frame's size has changed, this one is necessary as tracker doesn't hold it well
                    //if (frameSize.Count > i && frameSize[i].Height != 0 && frameSize[i].Width != 0)
                    //DropTracking(i);

                    if (!tracker[i].Update(downscaled, ref obj))
                    {
                        Debug.Log("404 not found");

                        obj = Rect2d.Empty;

                        DropTracking(i);
                    }
                }

                // save tracked object location
                if (0 != obj.Width && 0 != obj.Height)
                {
                    areaRect = new OpenCvSharp.Rect((int)obj.X, (int)obj.Y, (int)obj.Width, (int)obj.Height);
                }

                // render rect we've tracker or one is being drawn by the user
                if (null != tracker[i] && obj.Width != 0)
                {
                    Cv2.Rectangle((InputOutputArray)(image), areaRect * (1.0 / downScale), Scalar.LightGreen, 4);

                    TransformText.Trnstxt(meatText[i], gameObject.GetComponent <RectTransform>(), areaRect);

                    //Cv2.PutText((InputOutputArray)(image), i.ToString("000"), new Point(areaRect.X, areaRect.Y), HersheyFonts.HersheySimplex, 2, Scalar.Yellow, 3);
                }
            }
        }

        // mark detected objects
        //processor.MarkDetected(image);

        // processor.Image now holds data we'd like to visualize
        output = OpenCvSharp.Unity.MatToTexture(image, output);   // if output is valid texture it's buffer will be re-used, otherwise it will be re-created

        return(true);
    }