コード例 #1
0
ファイル: BitmapFilter.cs プロジェクト: totsucom/classes
        public static Bitmap Median(Bitmap bmp, int size = 3)
        {
            int adj = -size / 2;

            Bitmap dstBmp = new Bitmap(bmp.Width, bmp.Height, PixelFormat.Format8bppIndexed);
            var    pal    = dstBmp.Palette;

            for (int i = 0; i < 256; i++)
            {
                pal.Entries[i] = Color.FromArgb(i, i, i);
            }
            dstBmp.Palette = pal;

            BitmapEx src = BitmapEx.Begin(bmp, ImageLockMode.ReadOnly);
            BitmapEx dst = BitmapEx.Begin(dstBmp, ImageLockMode.WriteOnly);

            int bmpWidth  = bmp.Width;
            int bmpHeight = bmp.Height;

            if (bmp.PixelFormat == PixelFormat.Format8bppIndexed)
            {
                System.Threading.Tasks.Parallel.For(0, bmpHeight, i =>
                                                    //for (int i = 0; i < bmp.Height; i++)
                {
                    byte[] brightness = new byte[size * size];

                    for (int j = 0; j < bmpWidth; j++)
                    {
                        int m = 0;
                        for (int k = 0; k < size; k++)
                        {
                            int y = i + k + adj;
                            for (int n = 0; n < size; n++)
                            {
                                int x = j + n + adj;
                                if (x >= 0 && x < bmpWidth && y >= 0 && y < bmpHeight)
                                {
                                    brightness[m++] = (byte)src.GetPixelDirect(x, y);
                                }
                            }
                        }

                        Array.Sort(brightness, 0, m);
                        dst.SetPixelDirect(j, i, brightness[m / 2]);
                    }
                });
            }
            else
            {
                System.Threading.Tasks.Parallel.For(0, bmpHeight, i =>
                                                    //for (int i = 0; i < bmp.Height; i++)
                {
                    byte[] brightness = new byte[size * size];

                    for (int j = 0; j < bmpWidth; j++)
                    {
                        int m = 0;
                        for (int k = 0; k < size; k++)
                        {
                            int y = i + k + adj;
                            for (int n = 0; n < size; n++)
                            {
                                int x = j + n + adj;
                                if (x >= 0 && x < bmpWidth && y >= 0 && y < bmpHeight)
                                {
                                    brightness[m++] = (byte)(src.GetPixel(x, y).GetBrightness() * 255.0F);
                                }
                            }
                        }

                        Array.Sort(brightness, 0, m);
                        dst.SetPixelDirect(j, i, brightness[m / 2]);
                    }
                });
            }

            src.End();
            dst.End();
            return(dstBmp);
        }
コード例 #2
0
ファイル: BitmapFilter.cs プロジェクト: totsucom/classes
        //ope1,ope2は同じサイズであること
        //
        private static Bitmap _filtering(Bitmap bmp, float[,] ope1, float[,] ope2 = null)
        {
            int xadj = -ope1.GetLength(0) / 2;
            int yadj = -ope1.GetLength(1) / 2;

            Bitmap dstBmp = new Bitmap(bmp.Width, bmp.Height, PixelFormat.Format8bppIndexed);
            var    pal    = dstBmp.Palette;

            for (int i = 0; i < 256; i++)
            {
                pal.Entries[i] = Color.FromArgb(i, i, i);
            }
            dstBmp.Palette = pal;

            BitmapEx src = BitmapEx.Begin(bmp, ImageLockMode.ReadOnly);
            BitmapEx dst = BitmapEx.Begin(dstBmp, ImageLockMode.WriteOnly);

            int bmpWidth  = bmp.Width;
            int bmpHeight = bmp.Height;

            if (bmp.PixelFormat == PixelFormat.Format8bppIndexed)
            {
                System.Threading.Tasks.Parallel.For(0, bmpHeight, i =>
                                                    //for (int i = 0; i < bmp.Height; i++)
                {
                    for (int j = 0; j < bmpWidth; j++)
                    {
                        float sum1 = 0, sum2 = 0;
                        for (int k = 0; k < ope1.GetLength(1); k++)
                        {
                            int y = i + k + yadj;
                            for (int n = 0; n < ope1.GetLength(0); n++)
                            {
                                int x = j + n + xadj;
                                if (x >= 0 && x < bmpWidth && y >= 0 && y < bmpHeight)
                                {
                                    int d = src.GetPixelDirect(x, y);
                                    sum1 += d * ope1[n, k];
                                    if (ope2 != null)
                                    {
                                        sum2 += d * ope2[n, k];
                                    }
                                }
                            }
                        }

                        if (ope2 != null)
                        {
                            sum1 = (float)Math.Sqrt(sum1 * sum1 + sum2 * sum2) / 2.0F;
                        }
                        if (sum1 < 0.0F)
                        {
                            sum1 = 0.0F;
                        }
                        else if (sum1 > 255.0F)
                        {
                            sum1 = 255.0F;
                        }
                        dst.SetPixelDirect(j, i, (int)sum1);
                    }
                });
            }
            else
            {
                System.Threading.Tasks.Parallel.For(0, bmpHeight, i =>
                                                    //for (int i = 0; i < bmp.Height; i++)
                {
                    for (int j = 0; j < bmpWidth; j++)
                    {
                        float sum1 = 0, sum2 = 0;
                        for (int k = 0; k < ope1.GetLength(1); k++)
                        {
                            int y = i + k + yadj;
                            for (int n = 0; n < ope1.GetLength(0); n++)
                            {
                                int x = j + n + xadj;
                                if (x >= 0 && x < bmpWidth && y >= 0 && y < bmpHeight)
                                {
                                    float br = src.GetPixel(x, y).GetBrightness() * 255.0F;
                                    sum1    += br * ope1[n, k];
                                    if (ope2 != null)
                                    {
                                        sum2 += br * ope2[n, k];
                                    }
                                }
                            }
                        }

                        if (ope2 != null)
                        {
                            sum1 = (float)Math.Sqrt(sum1 * sum1 + sum2 * sum2) / 2.0F;
                        }
                        if (sum1 < 0.0F)
                        {
                            sum1 = 0.0F;
                        }
                        else if (sum1 > 255.0F)
                        {
                            sum1 = 255.0F;
                        }
                        dst.SetPixelDirect(j, i, (int)sum1);
                    }
                });
            }

            src.End();
            dst.End();
            return(dstBmp);
        }