Example #1
0
        /// <summary>
        /// Tracks the filtered object.
        /// </summary>
        /// <param name="theColorObject">The color object.</param>
        /// <param name="threshold">Threshold.</param>
        /// <param name="HSV">HS.</param>
        /// <param name="cameraFeed">Camera feed.</param>
        void trackFilteredObject(ColorObject theColorObject, Mat threshold, Mat HSV, Mat cameraFeed)
        {
            List <ColorObject> colorObjects = new List <ColorObject> ();
            Mat temp = new Mat();

            threshold.copyTo(temp);
            //these two vectors needed for output of findContours
            List <MatOfPoint> contours = new List <MatOfPoint> ();
            Mat hierarchy = new Mat();

            //find contours of filtered image using openCV findContours function
            Imgproc.findContours(temp, contours, hierarchy, Imgproc.RETR_CCOMP, Imgproc.CHAIN_APPROX_SIMPLE);

            //use moments method to find our filtered object
            bool colorObjectFound = false;

            if (hierarchy.rows() > 0)
            {
                int numObjects = hierarchy.rows();

//						Debug.Log("hierarchy " + hierarchy.ToString());

                //if number of objects greater than MAX_NUM_OBJECTS we have a noisy filter
                if (numObjects < MAX_NUM_OBJECTS)
                {
                    for (int index = 0; index >= 0; index = (int)hierarchy.get(0, index)[0])
                    {
                        Moments moment = Imgproc.moments(contours [index]);
                        double  area   = moment.get_m00();

                        //if the area is less than 20 px by 20px then it is probably just noise
                        //if the area is the same as the 3/2 of the image size, probably just a bad filter
                        //we only want the object with the largest area so we safe a reference area each
                        //iteration and compare it to the area in the next iteration.
                        if (area > MIN_OBJECT_AREA)
                        {
                            ColorObject colorObject = new ColorObject();

                            colorObject.setXPos((int)(moment.get_m10() / area));
                            colorObject.setYPos((int)(moment.get_m01() / area));
                            colorObject.setType(theColorObject.getType());
                            colorObject.setColor(theColorObject.getColor());

                            colorObjects.Add(colorObject);

                            colorObjectFound = true;
                        }
                        else
                        {
                            colorObjectFound = false;
                        }
                    }
                    //let user know you found an object
                    if (colorObjectFound == true)
                    {
                        //draw object location on screen
                        drawObject(colorObjects, cameraFeed, temp, contours, hierarchy);
                    }
                }
                else
                {
                    Core.putText(cameraFeed, "TOO MUCH NOISE!", new Point(5, cameraFeed.rows() - 10), Core.FONT_HERSHEY_SIMPLEX, 1.0, new Scalar(255, 255, 255, 255), 2, Core.LINE_AA, false);
                }
            }
        }
                /// <summary>
                /// Tracks the filtered object.
                /// </summary>
                /// <param name="theColorObject">The color object.</param>
                /// <param name="threshold">Threshold.</param>
                /// <param name="HSV">HS.</param>
                /// <param name="cameraFeed">Camera feed.</param>
                void trackFilteredObject (ColorObject theColorObject, Mat threshold, Mat HSV, Mat cameraFeed)
                {

                        List<ColorObject> colorObjects = new List<ColorObject> ();
                        Mat temp = new Mat ();
                        threshold.copyTo (temp);
                        //these two vectors needed for output of findContours
                        List<MatOfPoint> contours = new List<MatOfPoint> ();
                        Mat hierarchy = new Mat ();
                        //find contours of filtered image using openCV findContours function
                        Imgproc.findContours (temp, contours, hierarchy, Imgproc.RETR_CCOMP, Imgproc.CHAIN_APPROX_SIMPLE);
                        //use moments method to find our filtered object
                        double refArea = 0;
                        bool colorObjectFound = false;
                        if (hierarchy.rows () > 0) {
                                int numObjects = hierarchy.rows ();

            //						Debug.Log("hierarchy " + hierarchy.ToString());

                                //if number of objects greater than MAX_NUM_OBJECTS we have a noisy filter
                                if (numObjects < MAX_NUM_OBJECTS) {
                                        for (int index = 0; index >= 0; index = (int)hierarchy.get(0, index)[0]) {

                                                Moments moment = Imgproc.moments (contours [index]);
                                                double area = moment.get_m00 ();

                                                //if the area is less than 20 px by 20px then it is probably just noise
                                                //if the area is the same as the 3/2 of the image size, probably just a bad filter
                                                //we only want the object with the largest area so we safe a reference area each
                                                //iteration and compare it to the area in the next iteration.
                                                if (area > MIN_OBJECT_AREA) {

                                                        ColorObject colorObject = new ColorObject ();

                                                        colorObject.setXPos ((int)(moment.get_m10 () / area));
                                                        colorObject.setYPos ((int)(moment.get_m01 () / area));
                                                        colorObject.setType (theColorObject.getType ());
                                                        colorObject.setColor (theColorObject.getColor ());

                                                        colorObjects.Add (colorObject);

                                                        colorObjectFound = true;

                                                } else {
                                                        colorObjectFound = false;
                                                }
                                        }
                                        //let user know you found an object
                                        if (colorObjectFound == true) {
                                                //draw object location on screen
                                                drawObject (colorObjects, cameraFeed, temp, contours, hierarchy);
                                        }

                                } else {
                                        Core.putText (cameraFeed, "TOO MUCH NOISE!", new Point (5, cameraFeed.rows () - 10), Core.FONT_HERSHEY_SIMPLEX, 1.0, new Scalar (255, 255, 255, 255), 2, Core.LINE_AA, false);
                                }
                        }
                }