Exemple #1
0
        public static void GrayScaleMedian(byte[,] InP, byte[,] OutP, int size, int w, int h, ProgressBar P1)
        {
            P1.Minimum = 0;
            P1.Maximum = w;
            P1.Value   = 0;
            int[] curc = new int[size * size + 2];
            int   ind, R, G, B, k1, p1, C;

            for (int i = 0; i < w; i++)
            {
                for (int j = 0; j < h; j++)
                {
                    ind = 0;
                    for (int k = i - size / 2; k <= i + size / 2; k++)
                    {
                        for (int p = j - size / 2; p <= j + size / 2; p++)
                        {
                            k1 = k;
                            p1 = p;
                            while (k < 0)
                            {
                                k++;
                            }
                            while (k >= w)
                            {
                                k--;
                            }
                            while (p < 0)
                            {
                                p++;
                            }
                            while (p >= h)
                            {
                                p--;
                            }
                            curc[ind] = InP[k, p];
                            ind++;
                            k = k1;
                            p = p1;
                        }
                    }
                    C = BitmapFilter.GetKth(curc, 0, size * size - 1, (size * size - 1) / 2, false, 256);


                    OutP[i, j] = (byte)C;
                    P1.Value   = i;
                }
            }
        }
Exemple #2
0
        public static unsafe void Median(Bitmap b, int size)
        {
            int[] curR = new int[size * size];
            int[] curG = new int[size * size];
            int[] curB = new int[size * size];
            byte[,] rc = new byte[b.Width, b.Height];
            byte[,] gc = new byte[b.Width, b.Height];
            byte[,] bc = new byte[b.Width, b.Height];
            ProgressBar progressBar1 = (ProgressBar)ProgressBar.FromHandle(MainForm.ProgressBr);

            progressBar1.Value   = 0;
            progressBar1.Maximum = 2 * b.Width / 10;
            int        pvalue       = 0;
            Color      c            = new Color();
            BitmapData bData        = b.LockBits(new Rectangle(0, 0, b.Width, b.Height), ImageLockMode.ReadWrite, b.PixelFormat);
            byte       bitsPerPixel = (byte)Image.GetPixelFormatSize(b.PixelFormat);
            byte *     scan0        = (byte *)bData.Scan0.ToPointer();

            for (int i = 0; i < b.Width; i++)
            {
                for (int j = 0; j < b.Height; j++)
                {
                    byte *data = scan0 + j * bData.Stride + i * bitsPerPixel / 8;

                    rc[i, j] = data[2];
                    gc[i, j] = data[1];
                    bc[i, j] = data[0];
                }
                pvalue++;
                if (pvalue == 10)
                {
                    progressBar1.Value++;
                    pvalue = 0;
                }
            }
            int ind, R, G, B, k1, p1;

            for (int i = 0; i < b.Width; i++)
            {
                for (int j = 0; j < b.Height; j++)
                {
                    byte *data = scan0 + j * bData.Stride + i * bitsPerPixel / 8;
                    ind = 0;
                    for (int k = i - size / 2; k <= i + size / 2; k++)
                    {
                        for (int p = j - size / 2; p <= j + size / 2; p++)
                        {
                            k1 = k;
                            p1 = p;
                            while (k < 0)
                            {
                                k++;
                            }
                            while (k >= b.Width)
                            {
                                k--;
                            }
                            while (p < 0)
                            {
                                p++;
                            }
                            while (p >= b.Height)
                            {
                                p--;
                            }
                            curR[ind] = rc[k, p];
                            curG[ind] = gc[k, p];
                            curB[ind] = bc[k, p];

                            ind++;
                            k = k1;
                            p = p1;
                        }
                    }


                    data[2] = (byte)(R = (byte)BitmapFilter.GetKth(curR, 0, size * size - 1, (size * size - 1) / 2, false, 256));
                    data[1] = (byte)(G = (byte)BitmapFilter.GetKth(curG, 0, size * size - 1, (size * size - 1) / 2, false, 256));
                    data[0] = (byte)(B = (byte)BitmapFilter.GetKth(curB, 0, size * size - 1, (size * size - 1) / 2, false, 256));
                }
                pvalue++;
                if (pvalue == 10)
                {
                    progressBar1.Value++;
                    pvalue = 0;
                }
            }
            b.UnlockBits(bData);
        }