コード例 #1
0
ファイル: VectorInt.cs プロジェクト: fabianhick/viana
 /// <summary>
 /// The interpolate.
 /// </summary>
 /// <param name="v2">
 /// The v 2.
 /// </param>
 /// <param name="amount">
 /// The amount.
 /// </param>
 /// <returns>
 /// The <see cref="VectorInt"/> .
 /// </returns>
 public VectorInt Interpolate(VectorInt v2, float amount)
 {
     return(new VectorInt((int)(this.X + ((v2.X - this.X) * amount)), (int)(this.Y + ((v2.Y - this.Y) * amount))));
 }
コード例 #2
0
        // public int JitteringCountThreshold { get; set; }
        // public int JitteringAmountThreshold
        // {
        // get { return jitteringAmountThreshold; }
        // set
        // {
        // jitteringAmountThreshold = value;
        // jitteringAmountThresholdSq = value * value;
        // }
        // }
        #region Public Methods and Operators

        /// <summary>
        ///   The process.
        /// </summary>
        /// <returns> The <see cref="Segment" /> . </returns>
        /// <exception cref="InvalidOperationException"></exception>
        public Segment Process()
        {
            if (this.Histogram == null)
            {
                throw new InvalidOperationException("The histogram must be provided");
            }

            int[]     hx = this.Histogram.X;
            int[]     hy = this.Histogram.Y;
            VectorInt histUpperThreshold = this.Histogram.Max * 0.5f;

            // Find seeds for the segmentation:
            // All the x and y histogram indices where the value is above the half maximum and have a min distance
            const int step = 10;

            // x
            List <int> ix = this.GetIndicesAboveThreshold(this.Histogram.MaxIndex.X, -step, hx, histUpperThreshold.X);

            ix.AddRange(this.GetIndicesAboveThreshold(this.Histogram.MaxIndex.X + step, step, hx, histUpperThreshold.X));

            // y
            List <int> iy = this.GetIndicesAboveThreshold(this.Histogram.MaxIndex.Y, -step, hy, histUpperThreshold.Y);

            iy.AddRange(this.GetIndicesAboveThreshold(this.Histogram.MaxIndex.Y + step, step, hy, histUpperThreshold.Y));

            // Find the boundaries for the segments defined by the seeds
            var segments = new List <Segment>();

            foreach (int y0 in iy)
            {
                foreach (int x0 in ix)
                {
                    var segment = new Segment(0, 0, 0, 0);
                    segment.Min.X = this.GetIndexBelowThreshold(x0, -1, hx, this.ThresholdLuminance.X);
                    segment.Max.X = this.GetIndexBelowThreshold(x0, 1, hx, this.ThresholdLuminance.X);
                    segment.Min.Y = this.GetIndexBelowThreshold(y0, -1, hy, this.ThresholdLuminance.Y);
                    segment.Max.Y = this.GetIndexBelowThreshold(y0, 1, hy, this.ThresholdLuminance.Y);

                    // Filter segments by diameter
                    if (segment.Diagonal > this.MinDiameter && segment.Diagonal < this.MaxDiameter)
                    {
                        segments.Add(segment);
                    }
                }
            }

            if (segments.Count > 0)
            {
                // Sort (only pick the largest for now)
                return(segments.OrderByDescending(s => s.DiagonalSq).FirstOrDefault());

                // segments.OrderByDescending(s => s.Center.X);
                // VectorInt lastCenter = VectorInt.Zero;
                // for (int i = 0; i < segments.Count; i++)
                // {
                // if (segments[i].Center.Equals(lastCenter))
                // {
                // continue;
                // }
                // else
                // {
                // groupedSegments.Add(segments[i]);
                // lastCenter = segments[i].Center;
                // }
                // }

                // groupedSegments.OrderByDescending(s => s.DiagonalSq);

                //// Prevent jittering:
                //// If the position doesn't change too much over a certain timespan, the last segment is always returned.

                //// Check for jittering position.
                // bool isJittering = true;
                // if ((lastSegment.Min - foundSegment.Min).LengthSq < jitteringAmountThresholdSq)
                // {
                // // Check for jittering size
                // if (Math.Abs(foundSegment.DiagonalSq - lastSegment.DiagonalSq) < jitteringAmountThresholdSq)
                // {
                // jitteringCount++;
                // isJittering = false;
                // }
                // }

                //// Freeze or not
                // if (isJittering)
                // {
                // jitteringCount = 0;
                // }
                // if (jitteringCount < JitteringCountThreshold)
                // {
                // lastSegment = foundSegment;
                // }
            }
            else
            {
                return(new Segment());
            }
        }
コード例 #3
0
ファイル: VectorInt.cs プロジェクト: fabianhick/viana
 /// <summary>
 /// The dot.
 /// </summary>
 /// <param name="v2">
 /// The v 2.
 /// </param>
 /// <returns>
 /// The <see cref="int"/> .
 /// </returns>
 public int Dot(VectorInt v2)
 {
     return(this.X * v2.X + this.Y * v2.Y);
 }