ImageProcessor.Processor.ThresholdGS C# (CSharp) Method

ThresholdGS() public method

public ThresholdGS ( float thresh ) : Image
thresh float
return Image
        public Image ThresholdGS(float thresh)
        {
            Bitmap b = new Bitmap(_image);

            for (int i = 0; i < b.Height; ++i)
            {
                for (int j = 0; j < b.Width; ++j)
                {
                    Color c = b.GetPixel(j, i);

                    double magnitude = 1 / 3d * (c.B+c.G+c.R);

                    if (magnitude < thresh)
                    {
                        b.SetPixel(j,i,Color.FromArgb(0,0,0));
                    }
                    else
                    {
                        b.SetPixel(j,i, Color.FromArgb(255,255,255));
                    }
                }
            }

            return b;
        }

Usage Example

        private void DoTest(int numOps, bool doGS)
        {
            Processor p = new Processor();
            p.Image = _img;

            DisplayMessage("Starting managed average method"+Environment.NewLine);

            Stopwatch s = Stopwatch.StartNew();
            for (int i = 0; i < numOps; ++i)
                p.ThresholdMA(125);
            s.Stop();

            DisplayMessage("Managed average method took "+s.ElapsedMilliseconds.ToString()+" milliseconds, for an average of "+(s.ElapsedMilliseconds/(float)numOps)+" per threshold."+Environment.NewLine);

            s.Reset();

            DisplayMessage("Starting unsafe average method" + Environment.NewLine);
            s.Start();
            for (int i = 0; i < numOps; ++i)
                p.ThresholdUA(125);
            s.Stop();
            DisplayMessage("Unsafe average method took " + s.ElapsedMilliseconds + " milliseconds, for an average of " + (s.ElapsedMilliseconds / (float)numOps) + " per threshold." + Environment.NewLine);

            s.Reset();

            if (doGS)
            {
                DisplayMessage("Starting GetPixel\\SetPixel average method" + Environment.NewLine);
                s.Start();
                for (int i = 0; i < numOps; ++i)
                    p.ThresholdGS(125);
                s.Stop();
                DisplayMessage("GetPixel\\SetPixel average method took " + s.ElapsedMilliseconds + " milliseconds, for an average of " + (s.ElapsedMilliseconds / (float)numOps) + " per threshold." + Environment.NewLine);
            }
        }