GPU 中的 C# 位图 GetPixel()、SetPixel()

标签 c# image bitmap gpgpu cudafy.net

我正在使用 Cudafy 作为 C# 包装器 我需要获取位图的颜色信息 InputBitmap0.GetPixel(x, y) 并为输出创建一个新的位图。

我需要在 GPU 中完成以下工作。

在 CPU 中

OutputBitmap.SetPixel(object_point_x, object_point_y, InputBitmap0.GetPixel(x, y));

简而言之:

如何在 GPU 中为输入位图的每个像素点获取 GetPixel(),为输出位图位图的每个像素点设置像素点。

最佳答案

OutputBitmap.SetPixel(object_point_x, object_point_y, InputBitmap0.GetPixel(x, y))

这花了一些时间,但最终,我破获了我的案子。

我们有两个 Bitmap :一个用于输出 OutputBitmap 另一个用于输入 InputBitmap0

让我们把这个任务分成几个部分:

  1. InputBitmap0.GetPixel() x ,y 坐标
  2. 然后,OutputBitmap.SetPixel() 用于不同的坐标 object_point_x, object_point_y

Cudafy 不支持BitmapColor 类型的数据。所以我将位图转换为 byte 类型。

BitmapData InputBitmapData0 = InputBitmap0.LockBits(new Rectangle(0, 0, InputBitmap0.Width, InputBitmap0.Height), ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb);

IntPtr ptr0 = InputBitmapData0.Scan0;//pointer for color

int stride0 = InputBitmapData0.Stride;

byte[]  input_ragba_color = new byte[InputBitmapData0.Stride * InputBitmap0.Height];

Marshal.Copy(ptr0, input_ragba_color, 0, bytes0);// Copy the RGB values of color value into the array.

我们已经将 InputBitmap0 的内容复制到 rgbValues 数组中。现在我们需要完成 GetPixel() 的工作(获取 R、G、B、A 的值)。

我们也需要为 OutputBitmap 完成上述工作(制作数组),因为我们将在 GPU 中执行 SetPixel() 但我们会将数组复制回稍后位图。

BitmapData OutputBitmapData = OutputBitmap.LockBits(new Rectangle(0, 0, OutputBitmap.Width, OutputBitmap.Height), ImageLockMode.WriteOnly, OutputBitmap.PixelFormat);
IntPtr ptr_output = OutputBitmapData.Scan0;

byte[] output_ragba = new byte[OutputBitmapData.Stride * OutputBitmap.Height]; 

它的 GPU 计算时间。让我们初始化 gpu。

CudafyModule km = new CudafyTranslator.Cudafy();
GPGPU gpu = new CudafyHost.getDevice(CudafyModes.Target, CudafyModes.DeviceId);
gpu.LoadModule(km);

现在将 input_ragba_coloroutput_ragba 发送到 gpu,因为我们可以迭代数组并进行任何计算。

byte[] dev_output_rgba_color = gpu.Allocate<byte>(output_ragba.Length);
byte[] dev_input_ragba_color = gpu.CopyToDevice(input_ragba_color);
gpu.Launch(N, 1).update_bitmap(x, y, object_point_x, object_point_y,int stride0, int OutputBitmapData.Stride,dev_input_ragba_color,dev_output_rgba_color);

现在在 GPU(内核)中

[Cudafy]
public static void update_bitmap(GThread thread, int x,int y,int object_point_x,int  object_point_y,int stride0, int OutputBitmapData_Stride,byte [] dev_input_ragba_color,byte [] dev_output_rgba_color)
{
   dev_output_rgba_color[(object_point_y * OutputBitmapData_Stride) + (object_point_x * 4)] = input_ragba_color[(y * stride0) + (x * 4)];  
   dev_output_rgba_color[(object_point_y * OutputBitmapData_Stride) + (object_point_x * 4) + 1] = input_ragba_color[(y * stride0) + (x * 4) + 1];
   dev_output_rgba_color[(object_point_y * OutputBitmapData_Stride) + (object_point_x * 4) + 2] = input_ragba_color[(y * stride0) + (x * 4) + 2];
   dev_output_rgba_color[(object_point_y * OutputBitmapData_Stride) + (object_point_x * 4) + 3] = input_ragba_color[(y * stride0) + (x * 4) + 3];

}

I am taking values of each R,G,B,A ,ex: input_ragba_color[(y * stride0) + (x * 4) + 1] which is solving 1st task (InputBitmap0.GetPixel())

dev_output_rgba_color is taking the values of input_ragba_color example:

dev_output_rgba_color[(object_point_y * OutputBitmapData_Stride) + (object_point_x * 4)] = input_ragba_color[(y * stride0) + (x * 4)]; 

which is solves our 2nd task (OutputBitmap.SetPixel())

我们现在知道 gpu 已经为我们的 OutputBitmap 填充了一个数组(dev_output_rgba_color)。

gpu.CopyFromDevice(dev_output_rgba_color, output_ragba);   //dev_output_rgba_color values will be assigned to output_ragba
gpu.FreeAll();

使用内存指针将结果复制回 OutputBitmap 并从内存中解锁。

Marshal.Copy(output_ragba, 0, ptr_output, output_bytes);// Copy the RGB values of color value into the array.
OutputBitmap.UnlockBits(OutputBitmapData);

现在 OutputBitmap 包含更新后的值。

关于GPU 中的 C# 位图 GetPixel()、SetPixel(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42037700/

相关文章:

html - 我可以使用 CSS border-radius 属性制作椭圆吗?

c# - 64 位定点乘法错误

c# - 了解用户是否已从 C#/.NET 应用程序连接

c# - 您认为 Silverlight 是否已为企业做好准备?关于平台渗透率的任何统计数据?任何 F500 comps 认真投资 Silverlight 吗?

css - 图片库中的 float 图片有间距问题

Django:缓存图像

java - 如何将真实尺寸的图像保存到SD卡中

android - Android Profiler 中什么占用显存?

android - 在 Android camera2 下将 YUV_420_888 转换为位图的图像不正确

c# - 如何在 MVC Controller 中获取 DropDownList 选定的文本