C#数字图像处理有3种典型方法:提取像素法、内存法、指针法。
来源:互联网 发布:从零开始学淘宝txt 编辑:程序博客网 时间:2024/04/28 19:41
C#数字图像处理有3种典型方法:提取像素法、内存法、指针法。其中提取像素法使用的是GDI+中的Bitmap.GetPixel和Bitmap.SetPixel方法;内存法是通过LockBits方法来获取位图的首地址,从而把图像数据直接复制到内存中进行处理;指针法与内存法相似,但该方法直接应用指针对位图进行操作,由于在默认情况下,C#不支持指针运算,所以该方法只能在unsafe关键字所标记的代码块中使用。以一幅真彩色图像的灰度化为例,下面代码分别展现了这3种方法的使用,方便大家学习图像处理的基本技巧。
(1) 像素提取法
(2) 内存法
(3) 指针法
(1) 像素提取法
if (curBitmap != null){ Color curColor; int gray; for (int i = 0; i < curBitmap.Width; i++) { for (int j = 0; j < curBitmap.Height; j++) { curColor = curBitmap.GetPixel(i, j); gray = (int)(0.3 * curColor.R + 0.59 * curColor.G * 0.11 * curColor.B); curBitmap.SetPixel(i, j, curColor); } }}
(2) 内存法
if (curBitmap != null){ int width = curBitmap.Width; int height = curBitmap.Height; int length = height * 3 * width; RGB = new byte[length]; BitmapData data = curBitmap.LockBits(new Rectangle(0, 0, width, height), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb); System.IntPtr Scan0 = data.Scan0; System.Runtime.InteropServices.Marshal.Copy(Scan0, RGB, 0, length); double gray = 0; for (int i = 0; i < RGB.Length; i=i+3) { gray = RGB[i + 2] * 0.3 + RGB[i + 1] * 0.59 + RGB[i] * 0.11; RGB[i + 2] = RGB[i + 1] = RGB[i] = (byte)gray; } System.Runtime.InteropServices.Marshal.Copy(RGB, 0, Scan0, length); curBitmap.UnlockBits(data);}
(3) 指针法
if (curBitmap != null){ int width = curBitmap.Width; int height = curBitmap.Height; BitmapData data = curBitmap.LockBits(new Rectangle(0, 0, width, height), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb); System.IntPtr Scan0 = data.Scan0; int stride = data.Stride; System.Runtime.InteropServices.Marshal.Copy(Scan0, RGB, 0, length); unsafe { byte* p = (byte*)Scan0; int offset = stride - width * 3; double gray = 0; for (int y = 0; y < height; y++) { for (int x = 0; x < width; x++) { gray = 0.3 * p[2] + 0.59 * p[1] + 0.11 * p[0]; p[2] = p[1] = p[0] = (byte)gray; p += 3; } p += offset; } } curBitmap.UnlockBits(data); }
在以上3种方法中,提取像素法能直观的展示图像处理过程,可读性很好,但效率最低,并不适合做图像处理方面的工程应用;内存法把图像直接复制到内存中,直接对内存中的数据进行处理,速度明显提高,程序难度也不大;指针法直接应用指针来对图像进行处理,所以速度最快。
示例源代码下载
0 0
- C#数字图像处理有3种典型方法:提取像素法、内存法、指针法。
- C#数字图像处理的3种方法::提取像素法、内存法和指针法
- C#图像的灰度化处理:提取像素法介绍
- C#图像的灰度化处理:提取像素法
- c# 数字图像处理的一些方法
- 数字图像处理之特征提取及常用方法
- 数字图像处理10--基于特征提取的拼接方法
- 数字图像处理与OpenCV(3-1)--存取像素
- 数字图像处理成长之路3:操作像素(彩色sobel)
- 数字图像处理7--特征提取
- 数字图像处理-提取A4纸中线条-matlab的轮廓提取方法
- vs+C#处理数字图像
- C#数字图像处理<一>
- C#数字图像处理<二>
- C#数字图像处理<三>
- c#数字图像处理<四>
- C#数字图像处理<五>
- C#数字图像处理<六>
- 字符串中的字符包含问题
- C#中计时器Timer的三种使用方法
- c/c++各种字符、字符串类型转换
- JDBC连接各种数据库方法
- 互联网(internet)又称因特网
- C#数字图像处理有3种典型方法:提取像素法、内存法、指针法。
- socket穿透代理代码(C++版)
- 你知道这些游戏都在阿里云上吗?
- 采购退货从接收退货和从库存退货区别
- 一个基于MySQL的Key-List存储方案
- 解决 Project facet Java version 1.7 is not supported.
- UVA - 11404 Palindromic Subsequence (最长回文子序列)
- 黑马程序员_学习笔记第18天——IO流、字符流
- Linux shell编程 14 ---- 学习shell script