C#对图像像素处理的三种方式

简介:

在C#中,可以采用直接获取像素法(GetPixel)、内存拷贝法和指针法(unsafe)来获取图像像素并进行处理。

下面以图像的灰度化为例说明具体的处理方法和速度的比较(1G内存,P4处理器测试)。

1.GetPixel方法

GetPixel(i,j)和SetPixel(i, j,Color)可以直接得到图像的一个像素的Color结构,但是处理速度比较慢,处理一副180*180的图像大约需要100.48ms。

private void pixel_Click(object sender, EventArgs e)
{
    if(curBitmap != null)
    {
        myTimer.ClearTimer();
        myTimer.Start();
        Color curColor;
        int ret;
        for (int i = 0; i < curBitmap.Width; i++)
        {
            for (int j = 0; j < curBitmap.Height ; j++)
            {
                curColor = curBitmap.GetPixel(i,j);
                ret = (int)(curColor.R * 0.299 + curColor.G * 0.587 + curColor.B * 0.114);
                curBitmap.SetPixel(i, j, Color.FromArgb(ret, ret, ret));
            }
        }
        myTimer.Stop();
        timeBox.Text = myTimer.Duration.ToString("####.##") + " 毫秒"; 
        Invalidate();
    }
}

2.内存拷贝法

内存拷贝法就是采用System.Runtime.InteropServices.Marshal.Copy将图像数据拷贝到数组中,然后进行处理,这不需要直接对指针进行操作,不需采用unsafe,处理速度和指针处理相差不大,
处理一副180*180的图像大约需要1.32ms。
private void memory_Click(object sender, EventArgs e)
{
    if (curBitmap != null)
    {
        myTimer.ClearTimer();
        myTimer.Start();
        Rectangle rect = new Rectangle(0, 0, curBitmap.Width, curBitmap.Height);
        System.Drawing.Imaging.BitmapData bmpData = curBitmap.LockBits(rect, System.Drawing.Imaging.ImageLockMode.ReadWrite, curBitmap.PixelFormat);
        IntPtr ptr = bmpData.Scan0;
        int bytes = curBitmap.Width * curBitmap.Height * 3;
        byte[] rgbValues = new byte[bytes];
        System.Runtime.InteropServices.Marshal.Copy(ptr, rgbValues, 0, bytes);
        double colorTemp = 0;
        for (int i = 0; i < rgbValues.Length; i += 3)
        {
            colorTemp = rgbValues[i + 2] * 0.299 + rgbValues[i + 1] * 0.587 + rgbValues[i] * 0.114;
            rgbValues[i] = rgbValues[i + 1] = rgbValues[i + 2] = (byte)colorTemp;
        }
        System.Runtime.InteropServices.Marshal.Copy(rgbValues, 0, ptr, bytes);
        curBitmap.UnlockBits(bmpData);
        myTimer.Stop();
        timeBox.Text = myTimer.Duration.ToString("####.##") + " 毫秒"; 
        Invalidate();
    }
}

3.指针法

指针在c#中属于unsafe操作,需要用unsafe括起来进行处理,速度最快,处理一副180*180的图像大约需要1.16ms。
采用byte* ptr = (byte*)(bmpData.Scan0); 获取图像数据根位置的指针,然后用bmpData.Scan0获取图像的扫描宽度,就可以进行指针操作了。
 
private void pointer_Click(object sender, EventArgs e)
{
    if (curBitmap != null)
    {
        myTimer.ClearTimer();
        myTimer.Start();
        Rectangle rect = new Rectangle(0, 0, curBitmap.Width, curBitmap.Height);
        System.Drawing.Imaging.BitmapData bmpData = curBitmap.LockBits(rect, System.Drawing.Imaging.ImageLockMode.ReadWrite, curBitmap.PixelFormat);
        byte temp = 0;
        unsafe
        {
            byte* ptr = (byte*)(bmpData.Scan0);
            for (int i = 0; i < bmpData.Height; i++)
            {
                for (int j = 0; j < bmpData.Width; j++)
                {
                    temp = (byte)(0.299 * ptr[2] + 0.587 * ptr[1] + 0.114 * ptr[0]);
                    ptr[0] = ptr[1] = ptr[2] = temp;
                    ptr += 3;
                }
                ptr += bmpData.Stride - bmpData.Width * 3;
            }
        }
        curBitmap.UnlockBits(bmpData);
        myTimer.Stop();
        timeBox.Text = myTimer.Duration.ToString("####.##") + " 毫秒"; 
        Invalidate();
    }

}

本文转自feisky博客园博客,原文链接:http://www.cnblogs.com/feisky/archive/2009/11/02/1594840.html,如需转载请自行联系原作者



相关文章
|
8月前
|
机器学习/深度学习 传感器 算法
【交互式阈值二进制图像】采用彩色或单色图像通过交互/手动方式阈值单色图像或彩色图像的单个色带研究(Matlab代码实现)
【交互式阈值二进制图像】采用彩色或单色图像通过交互/手动方式阈值单色图像或彩色图像的单个色带研究(Matlab代码实现)
|
9天前
|
API 计算机视觉
【OpenCV】—图像对比度、亮度值调整
【OpenCV】—图像对比度、亮度值调整
|
9天前
|
计算机视觉 索引
【OpenCV】—ROI区域图像叠加&图像混合
【OpenCV】—ROI区域图像叠加&图像混合
|
9天前
|
存储 算法 C语言
OpenCV—访问图像中的像素
OpenCV—访问图像中的像素
|
5月前
[Halcon&图像] 图像、区域和轮廓相互转化
[Halcon&图像] 图像、区域和轮廓相互转化
91 1
|
7月前
|
C++ 计算机视觉
C++-图像目标区裁剪ImageCropping
C++-图像目标区裁剪ImageCropping
【IMAQ】imaqSetImageSize() 设置图像大小
【IMAQ】imaqSetImageSize() 设置图像大小
利用矩阵进行平移,旋转,缩放等图像变换、创建第二个一模一样的图像并使之进行缩放等操作
利用矩阵进行平移,旋转,缩放等图像变换、创建第二个一模一样的图像并使之进行缩放等操作
|
Java Maven
thumbmailator组件对图像的使用缩放、裁剪、旋转、格式钻换
thumbmailator组件对图像的使用缩放、裁剪、旋转、格式钻换
122 0
|
算法 Java 计算机视觉
常用的像素操作算法:图像加法、像素混合、提取图像中的ROI
常用的像素操作算法:图像加法、像素混合、提取图像中的ROI
314 0
常用的像素操作算法:图像加法、像素混合、提取图像中的ROI

热门文章

最新文章