1. 程式人生 > >c#影象處理-邊緣檢測

c#影象處理-邊緣檢測

 //邊緣檢測
        public Bitmap cn(Bitmap x)
        {
            Color c1 = new Color();
            Color c2 = new Color();
            Color c3 = new Color();
            Color c4 = new Color();
            int i, j;
            int rr, gg, bb;//
            int r1, r2, r3, r4, fxr, fyr;//
            int g1, g2, g3, g4, fxg, fyg;//
            int b1, b2, b3, b4, fxb, fyb;//
            //把圖片中的圖片傳給一個BITMAP型別
            Bitmap box1 = new Bitmap(x);
            for (i = 0; i <= x.Width - 2; i++)
            {
                for (j = 0; j <= x.Height - 2; j++)
                {
                    c1 = box1.GetPixel(i, j);
                    c2 = box1.GetPixel(i + 1, j + 1);
                    c3 = box1.GetPixel(i + 1, j);
                    c4 = box1.GetPixel(i, j + 1);
                    r1 = c1.R;
                    r2 = c2.R;
                    r3 = c3.R;
                    r4 = c4.R;
                    fxr = r1 - r2;
                    fyr = r3 - r4;
                    rr = Math.Abs(fxr) + Math.Abs(fyr) + 128;
                    if (rr < 0) rr = 0;
                    if (rr > 255) rr = 255;
                    g1 = c1.G;
                    g2 = c2.G;
                    g3 = c3.G;
                    g4 = c4.G;
                    fxg = g1 - g2;
                    fyg = g3 - g4;
                    gg = Math.Abs(fxg) + Math.Abs(fyg) + 128;
                    if (gg < 0) gg = 0;
                    if (gg > 255) gg = 255;
                    b1 = c1.B;
                    b2 = c2.B;
                    b3 = c3.B;
                    b4 = c4.B;
                    fxb = b1 - b2;
                    fyb = b3 - b4;
                    bb = Math.Abs(fxb) + Math.Abs(fyb) + 128;
                    if (bb < 0) bb = 0;
                    if (bb > 255) bb = 255;
                    Color cc = Color.FromArgb(rr, gg, bb);
                    box1.SetPixel(i, j, cc);
                }


            }
            return BitmapToBlack(box1, 0.8);
        }

處理前:

處理後: