C# 内存法图像处理

http://www.linuxidc.com/Linux/2015-05/117551.htm

内存法通过把图像储存在内存中进行处理,效率大大高于GetPixel方法,安全性高于指针法。

笔者当初写图像处理的时候发现网上多是用GetPixel方法实现,提到内存法的时候也没有具体实现,所以笔者在这里具体实现一下- -,望指正。

首先讲一下用到的一些方法。

1.LockBits和UnlockBits:使用 LockBits 方法,可在系统内存中锁定现有的位图,以便通过编程方式进行更改,每调用LockBits之后都应该调用一次UnlockBits。

2.Scan0:图像的第一个字节地址。

3.Stride:步幅,扫描宽度,形象的说就是一行的长度。

4.PixelFormat:数据的实际像素格式。

给出原图:

一、灰度

对每个像素点进行加权平均,(方法不唯一)。

/// <summary>
        /// 灰化实现方法
        /// </summary>
        void Image_Ashing()
        {
            if (pbshowbox.Image != null)
            {
                int Height = this.pbshowbox.Image.Height;
                int Width = this.pbshowbox.Image.Width;
                Bitmap bitmap = new Bitmap(Width, Height, PixelFormat.Format24bppRgb);
                Bitmap MyBitmap = (Bitmap)this.pbshowbox.Image;

BitmapData oldData = MyBitmap.LockBits(new Rectangle(0, 0, Width, Height), ImageLockMode.ReadOnly, PixelFormat.Format24bppRgb);
                BitmapData newData = bitmap.LockBits(new Rectangle(0, 0, Width, Height), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);
                unsafe
                {
                    byte* pin = (byte*)(oldData.Scan0.ToPointer());
                    byte* pout = (byte*)(newData.Scan0.ToPointer());
                    for (int y = 0; y < oldData.Height; y++)
                    {
                        for (int x = 0; x < oldData.Width; x++)
                        {
                            byte Result = (byte)(pin[0] * 0.1 + pin[1] * 0.2 + pin[2] * 0.7);//加权平均实现灰化
                            pout[0] = (byte)(Result);
                            pout[1] = (byte)(Result);
                            pout[2] = (byte)(Result);
                            pin = pin + 3;
                            pout = pout + 3;
                        }
                        pin += oldData.Stride - oldData.Width * 3;
                        pout += newData.Stride - newData.Width * 3;
                    }

bitmap.UnlockBits(newData);
                    MyBitmap.UnlockBits(oldData);
                    this.pbshowbox.Image = bitmap;

}

}
            else
            {
                MessageBox.Show("请先打开一张图片!");
            }

}

二、柔化

像素点与周围像素点差别较大时取平均值。

/// <summary>
        /// 柔化实现方法
        /// </summary>
        void Image_Soften()
        {
            if (pbshowbox.Image != null)
            {
                int Height = this.pbshowbox.Image.Height;
                int Width = this.pbshowbox.Image.Width;
                Bitmap bitmap = new Bitmap(Width, Height, PixelFormat.Format32bppRgb);
                Bitmap MyBitmap = (Bitmap)this.pbshowbox.Image;

BitmapData oldData = MyBitmap.LockBits(new Rectangle(0, 0, Width, Height), ImageLockMode.ReadOnly, PixelFormat.Format32bppRgb);
                BitmapData newData = bitmap.LockBits(new Rectangle(0, 0, Width, Height), ImageLockMode.ReadWrite, PixelFormat.Format32bppRgb);
                unsafe
                {
                    byte* pin = (byte*)(oldData.Scan0.ToPointer());
                    byte* pout = (byte*)(newData.Scan0.ToPointer());
                    //高斯模板
                    int[] Gauss = { 1, 2, 1, 2, 4, 2, 1, 2, 1 };
                    for (int i = 1; i < Width - 1; i++)
                    {
                        for (int j = 1; j < Height - 1; j++)
                        {
                            int r = 0, g = 0, b = 0;
                            int Index = 0;

for (int col = -1; col <= 1; col++)
                            {
                                for (int row = -1; row <= 1; row++)
                                {
                                    int off = ((j + row) * (Width) + (i + col)) * 4;
                                    r += pin[off + 0] * Gauss[Index];
                                    g += pin[off + 1] * Gauss[Index];
                                    b += pin[off + 2] * Gauss[Index];
                                    Index++;
                                }
                            }
                            r /= 16;
                            g /= 16;
                            b /= 16;
                            //处理颜色值溢出
                            if (r < 0) r = 0;
                            if (r > 255) r = 255;
                            if (g < 0) g = 0;
                            if (g > 255) g = 255;
                            if (b < 0) b = 0;
                            if (b > 255) b = 255;
                            int off2 = (j * Width + i) * 4;
                            pout[off2 + 0] = (byte)r;
                            pout[off2 + 1] = (byte)g;
                            pout[off2 + 2] = (byte)b;
                        }
                    }
                    bitmap.UnlockBits(newData);
                    MyBitmap.UnlockBits(oldData);
                    this.pbshowbox.Image = bitmap;
                }

}
            else
            {
                MessageBox.Show("请先打开一张图片!");
            }

}

三、锐化

突出显示颜色值大的像素点。

/// <summary>
        /// 锐化实现方法,显示数值最大像素点
        /// </summary>
        void Image_Sharpen()
        {
            if (this.pbshowbox.Image != null)
            {
                int Height = this.pbshowbox.Image.Height;
                int Width = this.pbshowbox.Image.Width;
                Bitmap bitmap = new Bitmap(Width, Height, PixelFormat.Format32bppRgb);
                Bitmap MyBitmap = (Bitmap)this.pbshowbox.Image;

BitmapData oldData = MyBitmap.LockBits(new Rectangle(0, 0, Width, Height), ImageLockMode.ReadOnly, PixelFormat.Format32bppRgb);
                BitmapData newData = bitmap.LockBits(new Rectangle(0, 0, Width, Height), ImageLockMode.ReadWrite, PixelFormat.Format32bppRgb);
                unsafe
                {
                    byte* pin = (byte*)(oldData.Scan0.ToPointer());
                    byte* pout = (byte*)(newData.Scan0.ToPointer());
                    //拉普拉斯模板
                    int[] Laplacian = { -1, -1, -1, -1, 9, -1, -1, -1, -1 };
                    for (int i = 1; i < Width - 1; i++)
                    {
                        for (int j = 1; j < Height - 1; j++)
                        {
                            int r = 0, g = 0, b = 0;
                            int Index = 0;

for (int col = -1; col <= 1; col++)
                            {
                                for (int row = -1; row <= 1; row++)
                                {
                                    int off = ((j + row) * (Width) + (i + col)) * 4;
                                    r += pin[off + 0] * Laplacian[Index];
                                    g += pin[off + 1] * Laplacian[Index];
                                    b += pin[off + 2] * Laplacian[Index];
                                    Index++;
                                }
                            }

if (r < 0) r = 0;
                            if (r > 255) r = 255;
                            if (g < 0) g = 0;
                            if (g > 255) g = 255;
                            if (b < 0) b = 0;
                            if (b > 255) b = 255;
                            int off2 = (j * Width + i) * 4;
                            pout[off2 + 0] = (byte)r;
                            pout[off2 + 1] = (byte)g;
                            pout[off2 + 2] = (byte)b;
                        }
                    }
                    bitmap.UnlockBits(newData);
                    MyBitmap.UnlockBits(oldData);
                    this.pbshowbox.Image = bitmap;
                }

}
            else
            {
                MessageBox.Show("请先打开一张图片!");
            }
        }

四、浮雕

对图像像素点的像素值分别与相邻像素点的像素值相减后加上128, 然后将其作为新的像素点的值。

/// <summary>
        /// 浮雕实现方法
        /// </summary>
        void Image_Relief()
        {
            if (this.pbshowbox.Image != null)
            {
              
                int Height = this.pbshowbox.Image.Height;
                int Width = this.pbshowbox.Image.Width;
                Bitmap bitmap = new Bitmap(Width, Height, PixelFormat.Format24bppRgb);
                Bitmap MyBitmap = (Bitmap)this.pbshowbox.Image;
                BitmapData oldData = MyBitmap.LockBits(new Rectangle(0, 0, Width, Height), ImageLockMode.ReadOnly, PixelFormat.Format24bppRgb);
                BitmapData newData = bitmap.LockBits(new Rectangle(0, 0, Width, Height), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);
                unsafe
                {
                    byte* pin_1 = (byte*)(oldData.Scan0.ToPointer());
                    byte* pin_2 = pin_1 + (oldData.Stride);                
                    byte* pout = (byte*)(newData.Scan0.ToPointer());
                    for (int y = 0; y < oldData.Height - 1; y++)
                    {
                        for (int x = 0; x < oldData.Width; x++)
                        {                          
                            int b = (int)pin_1[0] - (int)pin_2[0] + 128;
                            int g = (int)pin_1[1] - (int)pin_2[1] + 128;
                            int r = (int)pin_1[2] - (int)pin_2[2] + 128;

if (r < 0) r = 0;
                            if (r > 255) r = 255;
                            if (g < 0) g = 0;
                            if (g > 255) g = 255;
                            if (b < 0) b = 0;
                            if (b > 255) b = 255;
                            pout[0] = (byte)(b);
                            pout[1] = (byte)(g);
                            pout[2] = (byte)(r);
                            pin_1 = pin_1 + 3;
                            pin_2 = pin_2 + 3;
                            pout = pout + 3;                          
                        }
                        pin_1 += oldData.Stride - oldData.Width * 3;
                        pin_2 += oldData.Stride - oldData.Width * 3;
                        pout += newData.Stride - newData.Width * 3;
                    }
                    bitmap.UnlockBits(newData);
                    MyBitmap.UnlockBits(oldData);
                    this.pbshowbox.Image = bitmap;
                }

}
            else
            {
                MessageBox.Show("请先打开一张图片!");
            }
        }

五、底片

颜色值取反。

/// <summary>
        /// 底片实现方法
        /// </summary>
        void Image_Negative()
        {
            if (pbshowbox.Image != null)
            {
                int Height = this.pbshowbox.Image.Height;
                int Width = this.pbshowbox.Image.Width;
                Bitmap bitmap = new Bitmap(Width, Height, PixelFormat.Format24bppRgb);
                Bitmap MyBitmap = (Bitmap)this.pbshowbox.Image;
                BitmapData oldData = MyBitmap.LockBits(new Rectangle(0, 0, Width, Height), ImageLockMode.ReadOnly, PixelFormat.Format24bppRgb);
                BitmapData newData = bitmap.LockBits(new Rectangle(0, 0, Width, Height), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);
                unsafe
                {
                    byte* pin = (byte*)(oldData.Scan0.ToPointer());
                    byte* pout = (byte*)(newData.Scan0.ToPointer());
                    for (int y = 0; y < oldData.Height; y++)
                    {
                        for (int x = 0; x < oldData.Width; x++)
                        {
                            pout[0] = (byte)(255 - pin[0]);
                            pout[1] = (byte)(255 - pin[1]);
                            pout[2] = (byte)(255 - pin[2]);
                            pin = pin + 3;
                            pout = pout + 3;
                        }
                        pin += oldData.Stride - oldData.Width * 3;
                        pout += newData.Stride - newData.Width * 3;
                    }
                    bitmap.UnlockBits(newData);
                    MyBitmap.UnlockBits(oldData);
                    this.pbshowbox.Image = bitmap;
                }
            }
            else
            {
                MessageBox.Show("请先打开一张图片!");
            }
        }

六、积木

低像素置0,高像素置255。

/// <summary>
        /// 积木实现方法
        /// </summary>
        private void Image_Block()
        {
            if (this.pbshowbox.Image != null)
            {
                int Height = this.pbshowbox.Image.Height;
                int Width = this.pbshowbox.Image.Width;
                Bitmap bitmap = new Bitmap(Width, Height);
                Bitmap Mybitmap = (Bitmap)this.pbshowbox.Image;
                BitmapData oldData = Mybitmap.LockBits(new Rectangle(0, 0, Width, Height), ImageLockMode.ReadOnly, PixelFormat.Format24bppRgb);
                BitmapData newData = bitmap.LockBits(new Rectangle(0, 0, Width, Height), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);
                unsafe
                {
                    byte* pin = (byte*)(oldData.Scan0.ToPointer());
                    byte* pout = (byte*)(newData.Scan0.ToPointer());
                    for (int y = 0; y < oldData.Height; y++)
                    {
                        for (int x = 0; x < oldData.Width; x++)
                        {
                            int avg = (pin[0] + pin[1] + pin[2]) / 3;
                            if (avg > 128)
                            {
                                pout[0] = 255;
                                pout[1] = 255;
                                pout[2] = 255;
                            }
                            else
                            {
                                pout[0] = 0;
                                pout[1] = 0;
                                pout[2] = 0;
                            }
                            pin = pin + 3;
                            pout = pout + 3;
                        }
                        pin = pin + oldData.Stride - oldData.Width * 3;
                        pout = pout + newData.Stride - newData.Width * 3;
                    }
                    bitmap.UnlockBits(newData);
                    Mybitmap.UnlockBits(oldData);
                    this.pbshowbox.Image = bitmap;

}
            }
            else
            {
                MessageBox.Show("请先打开一张图片!");
            }

}

有些图片效果看起来不明显是因为笔者把图缩小了,其实效果挺明显的- -。

本文永久更新链接地址http://www.linuxidc.com/Linux/2015-05/117551.htm

时间: 2024-08-26 17:04:13

C# 内存法图像处理的相关文章

C#getPixel和内存法读取灰度图信息

getPixel方法: private int getPixels(Bitmap bmpobj) { int[,] data = new int[1920, 1200]; int max = 0; for (int i = 0; i < bmpobj.Height; i++) { for (int j = 0; j < bmpobj.Width; j++) { Color b = bmpobj.GetPixel(j, i); if (max < b.R) { max = b.R; } }

c# 内存法二值化图像

之前在网上看的一些方法都是通过指针来操作的,下面这个方法是通过c#内存操作的 保存下来,方便以后自己查看 1 private static Bitmap PBinary(Bitmap src, int v) 2 { 3 int w = src.Width; 4 int h = src.Height; 5 Bitmap dstBitmap = new Bitmap(src.Width, src.Height, System.Drawing.Imaging.PixelFormat.Format24b

C#数字图像处理算法学习笔记(一)--C#图像处理的3中方法

C#数字图像处理算法学习笔记(一)--C#图像处理的3中方法 Bitmap类:此类封装了GDI+中的一个位图,次位图有图形图像及其属性的像素数据组成.因此此类是用于处理像素数据定义的图形的对象.该类的主要方法和属性如下:  GetPixel与SetPixel方法:获取或设置一个图像的指定像素的颜色. PixelFormat属性:返回图像的像素格式. Height和Width:返回图像的高度和宽度. LockBits与UnLockBits方法:分别锁定和解锁系统内存中的位图像素. LockBits

C#数字图像处理的3种方法

本文主要通过彩色图象灰度化来介绍C#处理数字图像的3种方法,Bitmap类.BitmapData类和Graphics类是C#处理图像的的3个重要的类. Bitmap只要用于处理由像素数据定义的图像的对象,主要方法和属性如下: GetPixel方法和SetPixel方法,获取和设置一个图像的指定像素的颜色. PixelFormat属性,返回图像的像素格式. Palette属性,获取或折纸图像所使用的颜色调色板. Height属性和Width属性,返回图像的高度和宽度. LockBits方法和Unl

c#图像处理入门(-bitmap类和图像像素值获取方法)

c#图像处理入门 -bitmap类和图像像素值获取方法 一.Bitmap类 Bitmap对象封装了GDI+中的一个位图,此位图由图形图像及其属性的像素数据组成.因此Bitmap是用于处理由像素数据定义的图像的对象.该类的主要方法和属性如下: 1. GetPixel方法和SetPixel方法:获取和设置一个图像的指定像素的颜色. 2. PixelFormat属性:返回图像的像素格式. 3. Palette属性:获取和设置图像所使用的颜色调色板. 4. Height Width属性:返回图像的高度和

c#图像处理入门

一.Bitmap类 Bitmap对象封装了GDI+中的一个位图,此位图由图形图像及其属性的像素数据组成.因此Bitmap是用于处理由像素数据定义的图像的对象.该类的主要方法和属性如下: 1. GetPixel方法和SetPixel方法:获取和设置一个图像的指定像素的颜色. 2. PixelFormat属性:返回图像的像素格式. 3. Palette属性:获取和设置图像所使用的颜色调色板. 4. Height Width属性:返回图像的高度和宽度. 5. LockBits方法和UnlockBits

C# 图像处理(转)

一.Bitmap类 Bitmap对象封装了GDI+中的一个位图,此位图由图形图像及其属性的像素数据组成.因此Bitmap是用于处理由像素数据定义的图像的对象.该类的主要方法和属性如下: 1. GetPixel方法和SetPixel方法:获取和设置一个图像的指定像素的颜色. 2. PixelFormat属性:返回图像的像素格式. 3. Palette属性:获取和设置图像所使用的颜色调色板. 4. Height Width属性:返回图像的高度和宽度. 5. LockBits方法和UnlockBits

C#中的bitmap类和图像像素值获取方法

一.Bitmap类 Bitmap对象封装了GDI+中的一个位图,此位图由图形图像及其属性的像素数据组成.因此Bitmap是用于处理由像素数据定义的图像的对象.该类的主要方法和属性如下: 1. GetPixel方法和SetPixel方法:获取和设置一个图像的指定像素的颜色. 2. PixelFormat属性:返回图像的像素格式. 3. Palette属性:获取和设置图像所使用的颜色调色板. 4. Height Width属性:返回图像的高度和宽度. 5. LockBits方法和UnlockBits

C# 处理图像三种方法对比

C#本身自带有一定的图像处理能力,即使在不依赖Emgu CV的情况下,也是有很大的潜质的. 不过,最近在处理大量图片时,发现图片数量较少时,处理本身所带来的延时不会让人敏感,但是数量较大时,程序花费大量时间在预处理图片上,导致程序很容易误报线程时延过大,导致误判程序异常.对于这个问题苦恼很久,毕竟不是CS专业出身,对于图像处理以及一些算法和库的运用上感到毕竟吃力. 今天在阅读了一些数字图像处理的书之后感到收益很大,现在来做点试验对照一下之前自己的错误在哪里. 之前看MS的C#API时候发现有一个