欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

C# Bitmap图像处理加速的实现

程序员文章站 2022-03-03 08:37:05
目录bitmapdata类传统代码使用bitmapdata的代码效率对比代码gpu加速生成dll调用dll耗时bitmapdata类bitmapdata类专门用于位图处理,与bitmap的不同点在于,...

bitmapdata类

bitmapdata类专门用于位图处理,与bitmap的不同点在于,它使用指针直接修改内存,而bitmap是使用setpixel()方法间接修改颜色,因此其效率远远超过setpixel()

传统代码

以灰度处理为例,为了便于演示,此处的灰度算法采用 gray=(r+g+b) / 3

private void gray_tradition()
{
    for(int i = 0; i < bitmap.width; i++)
    {
        for(int j = 0; j < bitmap.height; j++)
        {
            color color = bitmap.getpixel(i, j);
            int rgb = (color.r + color.g + color.b) / 3;
            bitmap.setpixel(i, j, color.fromargb(255, rgb, rgb, rgb));
        }
    }
}

使用bitmapdata的代码

private void gray_bitmapdata()
{
    int width = bitmap.width, height = bitmap.height;//图片的宽度和高度
    //在内存中以读写模式锁定bitmap
    bitmapdata bitmapdata = bitmap.lockbits(
    new rectangle(0, 0, width, height),
    imagelockmode.readwrite,
    pixelformat.format24bpprgb);
    //图片像素点数组的长度,由于一个像素点占了3个字节,所以要乘上3
    int size = width * height * 3;
    //缓冲区数组
    byte[] srcarray = new byte[size];
    //获取第一个像素的地址
    intptr ptr = bitmapdata.scan0;
    //把像素值复制到缓冲区
    marshal.copy(ptr, srcarray, 0, size);
    int p;
    for (int i = 0; i < width; i++)
    {
        for (int j = 0; j < height; j++)
        {
            //定位像素点位置
            p = j * width * 3 + i * 3;
            //计算灰度值
            byte color = (byte)((srcarray[p] + srcarray[p + 1] + srcarray[p + 2]) / 3);
            srcarray[p] = srcarray[p + 1] = srcarray[p + 2] = color;
        }
    }
    //从缓冲区复制回bitmapdata
    marshal.copy(srcarray, 0, ptr, size);
    //从内存中解锁
    bitmap.unlockbits(bitmapdata);
}

效率对比

代码

private void ontest()
{
    double t1, t2;
    stopwatch watch = new stopwatch();
    watch.start();
    gray_bitmapdata();
    watch.stop();
    t1 = watch.elapsed.totalmilliseconds;
    watch.reset();
    watch.start();
    gray_tradition();
    watch.stop();
    t2 = watch.elapsed.totalmilliseconds;
    messagebox.show("bitmapdata=" + (long)t1 + "\ntradition=" + (long)t2);
}

图片信息

C# Bitmap图像处理加速的实现

耗时

C# Bitmap图像处理加速的实现

可以看到传统方法的耗时是使用bitmapdata方法的106倍,需要整整14秒,而bitmapdata仅用了0.1秒

gpu加速

使用cuda生成dll后,可以在gpu上高效处理图像,但是这种方式需要使用dll,而且异常繁琐,因此只适合对效率有极高要求时使用

生成dll

#include "cuda_runtime.h"
#include "device_launch_parameters.h"
 
#include <stdio.h>
#include <windows.h>
 
__global__ void doinkernel(byte* o, int num)
{
    int i = blockidx.x * blockdim.x + threadidx.x;
    if (i >= num) return;
    byte* ori = o + i * 3;
    ori[0] = ori[1] = ori[2] = (ori[0] + ori[1] + ori[2]) / 3;
}
 
extern "c" _declspec(dllexport) void gray(byte * oriarray, int num) {
    int size = num * 3 * sizeof(byte);
    byte* dev_ori;
    //在gpu上分配内存
    cudamalloc((void**)&dev_ori, size);
    //把数组复制到显存
    cudamemcpy(dev_ori, oriarray, size, cudamemcpyhosttodevice);
    //计算
    doinkernel << <num / 1024 + 1, 1024 >> > (dev_ori, num);
    //从显存复制到内存
    cudamemcpy(oriarray, dev_ori, size, cudamemcpydevicetohost);
    //释放
    cudafree(dev_ori);
}

实际上gpu的thread和block数量应该根据实际数组大小来动态调整,但是这里为了演示方便,直接定义1024个线程

调用dll

[dllimport("cuda.dll", entrypoint = "gray", callingconvention = callingconvention.cdecl)]
public static extern void gray(intptr ori, int num);

此时不需要定义缓冲区数组了,可以直接把数据复制到显存中使用

private void gray_gpu()
{
    int width = bitmap.width, height = bitmap.height;//图片的宽度和高度
    //在内存中以读写模式锁定bitmap
    bitmapdata bitmapdata = bitmap.lockbits(
    new rectangle(0, 0, width, height),
    imagelockmode.readwrite,
    pixelformat.format24bpprgb);
    //图片像素点数组的长度,由于一个像素点占了3个字节,所以要乘上3
    int size = width * height * 3;
    //获取第一个像素的地址
    intptr ptr = bitmapdata.scan0;
    gray(ptr, width * height);
    //从内存中解锁
    bitmap.unlockbits(bitmapdata);
    picturebox1.refresh();
}

耗时

由于加载dll需要时间,因此第二次执行的耗时才是真正的gpu执行时间 

C# Bitmap图像处理加速的实现

 仅用了34毫秒

到此这篇关于c# bitmap图像处理加速的实现的文章就介绍到这了,更多相关c# bitmap图像处理加速内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!