/// <summary>
        ///   Creates a new object that is a copy of the current instance.
        /// </summary>
        ///
        /// <returns>
        ///   A new object that is a copy of this instance.
        /// </returns>
        ///
        public object Clone()
        {
            HaarFeatureNode[][] newTrees = new HaarFeatureNode[Trees.Length][];

            for (int i = 0; i < newTrees.Length; i++)
            {
                HaarFeatureNode[] tree    = Trees[i];
                HaarFeatureNode[] newTree = newTrees[i] =
                    new HaarFeatureNode[tree.Length];

                for (int j = 0; j < newTree.Length; j++)
                {
                    newTree[j] = (HaarFeatureNode)tree[j].Clone();
                }
            }

            HaarCascadeStage r = new HaarCascadeStage();

            r.NextIndex   = NextIndex;
            r.ParentIndex = ParentIndex;
            r.Threshold   = Threshold;
            r.Trees       = newTrees;

            return(r);
        }
Exemple #2
0
        private void writeFeature(HaarFeatureNode node)
        {
            writer.Write("new HaarFeatureNode({0}, {1}, {2}, ",
                         node.Threshold.ToString("R", NumberFormatInfo.InvariantInfo),
                         node.LeftValue.ToString("R", NumberFormatInfo.InvariantInfo),
                         node.RightValue.ToString("R", NumberFormatInfo.InvariantInfo));

            if (node.Feature.Tilted)
            {
                writer.Write("true, ");
            }

            // Write Haar-like rectangular features
            for (int k = 0; k < node.Feature.Rectangles.Length; k++)
            {
                writeRectangle(node.Feature.Rectangles[k]);

                if (k < node.Feature.Rectangles.Length - 1)
                {
                    writer.Write(", ");
                }
            }

            writer.Write(" )");
        }
        /// <summary>
        ///   Classifies an image as having the searched object or not.
        /// </summary>
        ///
        public bool Classify(FastBitmap image, int x, int y, double factor)
        {
            double value = 0;

            // For each feature in the feature tree of the current stage,
            foreach (HaarFeatureNode[] tree in Trees)
            {
                int current = 0;

                do
                {
                    // Get the feature node from the tree
                    HaarFeatureNode node = tree[current];

                    // Evaluate the node's feature
                    double sum = node.Feature.GetSum(image, x, y);

                    // And increase the value accumulator
                    if (sum < node.Threshold * factor)
                    {
                        value  += node.LeftValue;
                        current = node.LeftNodeIndex;
                    }
                    else
                    {
                        value  += node.RightValue;
                        current = node.RightNodeIndex;
                    }
                } while (current > 0);

                // Stop early if we have already surpassed the stage threshold value.
                //if (value > this.Threshold) return true;
            }

            // After we have evaluated the output for the
            //  current stage, we will check if the value
            //  is still lesser than the stage threshold.
            if (value < this.Threshold)
            {
                // If it is, the stage has rejected the current
                // image and it doesn't contains our object.
                return(false);
            }
            else
            {
                // The stage has accepted the current image
                return(true);
            }
        }
        /// <summary>
        ///   Creates a new object that is a copy of the current instance.
        /// </summary>
        /// <returns>
        ///   A new object that is a copy of this instance.
        /// </returns>
        ///
        public object Clone()
        {
            HaarFeatureNode r = new HaarFeatureNode();

            r.Feature   = (HaarFeature)Feature.Clone();
            r.Threshold = Threshold;

            r.RightValue = RightValue;
            r.LeftValue  = LeftValue;

            r.LeftNodeIndex  = leftNodeIndex;
            r.RightNodeIndex = rightNodeIndex;

            return(r);
        }
        /// <summary>
        ///   Creates a new object that is a copy of the current instance.
        /// </summary>
        /// <returns>
        ///   A new object that is a copy of this instance.
        /// </returns>
        /// 
        public object Clone()
        {
            HaarFeatureNode r = new HaarFeatureNode();

            r.Feature = (HaarFeature)Feature.Clone();
            r.Threshold = Threshold;

            r.RightValue = RightValue;
            r.LeftValue = LeftValue;

            r.LeftNodeIndex = leftNodeIndex;
            r.RightNodeIndex = rightNodeIndex;

            return r;
        }
        /// <summary>
        ///   Creates a new object that is a copy of the current instance.
        /// </summary>
        /// 
        /// <returns>
        ///   A new object that is a copy of this instance.
        /// </returns>
        /// 
        public object Clone()
        {
            HaarFeatureNode[][] newTrees = new HaarFeatureNode[Trees.Length][];

            for (int i = 0; i < newTrees.Length; i++)
            {
                HaarFeatureNode[] tree = Trees[i];
                HaarFeatureNode[] newTree = newTrees[i] =
                    new HaarFeatureNode[tree.Length];

                for (int j = 0; j < newTree.Length; j++)
                    newTree[j] = (HaarFeatureNode)tree[j].Clone();
            }

            HaarCascadeStage r = new HaarCascadeStage();
            r.NextIndex = NextIndex;
            r.ParentIndex = ParentIndex;
            r.Threshold = Threshold;
            r.Trees = newTrees;

            return r;
        }
        private void writeFeature(HaarFeatureNode node)
        {

            writer.Write("new HaarFeatureNode({0}, {1}, {2}, ",
                node.Threshold.ToString("R", NumberFormatInfo.InvariantInfo),
                node.LeftValue.ToString("R", NumberFormatInfo.InvariantInfo),
                node.RightValue.ToString("R", NumberFormatInfo.InvariantInfo));

            if (node.Feature.Tilted)
                writer.Write("true, ");

            // Write Haar-like rectangular features
            for (int k = 0; k < node.Feature.Rectangles.Length; k++)
            {
                writeRectangle(node.Feature.Rectangles[k]);

                if (k < node.Feature.Rectangles.Length - 1)
                    writer.Write(", ");
            }

            writer.Write(" )");
        }