设为首页 加入收藏

TOP

C# 内存法图像处理(一)
2015-07-16 12:56:37 来源: 作者: 【 】 浏览:23
Tags:内存 图像处理

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


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


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


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


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


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


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


给出原图:



一、灰度


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



///


? ? ? ? /// 灰化实现方法
? ? ? ? ///

? ? ? ? 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("请先打开一张图片!");
? ? ? ? ? ? }


? ? ? ? }


二、柔化


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



///


? ? ? ? /// 柔化实现方法
? ? ? ? ///

? ? ? ? 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;
? ? ? ? ? ? ?

首页 上一页 1 2 3 4 下一页 尾页 1/4/4
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇C++ sizeof总结 下一篇C++ 不使用virtual实现多态

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: