- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我正在使用 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
让我们把这个任务分成几个部分:
InputBitmap0.GetPixel()
x
,y
坐标OutputBitmap.SetPixel()
用于不同的坐标 object_point_x, object_point_y
Cudafy 不支持Bitmap
或Color
类型的数据。所以我将位图转换为 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_color
和 output_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 *
which is solving 1st task (
stride0) + (x * 4) + 1]InputBitmap0.GetPixel()
)
dev_output_rgba_color
is taking the values ofinput_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/
在我正在做的一门课的顶部: private static Bitmap bmp2 = new Bitmap(@"C:\Temp\New folder (17)\radar001486.GIF"); 然
找到一个例子: raster.setPixel( pf, jj, clr ); 但是 clr 有 4 个元素: static int[] clr = {0, 0, 0, 255}; 为什么?第四个元素
我正在尝试在我的 Android 应用程序中将可变位图中的像素区域设置为不同的颜色。不幸的是我无法让 setPixels() 正常工作。我不断收到 ArrayOutOfBoundsExceptions
我喜欢在DesktopWindow 上SetPixel,但有时它的行为很奇怪。 for(i=0;i int main(){ Sleep(4000);//waiting you to be re
我正在编写一个类似于 Windows 中“画图”的程序。起初我试图制作一个“铅笔”函数来处理 WM_MOUSEMOVE 消息并正确调用 SetPixel() 。但是当鼠标移动得太快时,并不是所有的像素
我正在尝试编辑图像。但我遇到了 setPixels 错误。 picw = pic.getWidth(); pich = pic.getHeight();
friend 们,当我尝试在我的应用程序中使用 setPixel(x, y, color) 时,它返回 NullPointerException 并停止工作。这是我的代码。请帮助我摆脱错误。提前致谢
我有课: class MyPic { private Bitmap bmp=null; public MyPic(Bitmap b) { bmp=b;
我正在尝试改变位图中某些像素的颜色。当我从我的可绘制文件夹中更改位图图像数组中的像素颜色时,它会在不同颜色周围留下黑色边框,这是不需要的。如果没有黑色边框,我该怎么做? 初始位图: 我已经尝试了下面的
这是我的第一个问题!我希望你能忍受我。 :) 我正在 Unity 上进行洪水填充,虽然我已经能够在我的 Texture2D 上填充形状,但我只能在使用正交相机放大时看到变化。当我缩小时,它又回到原来的
我有一些关于 ITK 的问题。我首先创建一个图像。然后我使用“for”来设置每个像素的值。之后,当程序步进到“image->SetSpacing”时出现问题。 VS2010 指出这可能是由于堆的损坏。
我在 C++ 中工作,通常控制台的大小是 25*80 像素,但我使用 setpixel() 方法在控制台上显示并且它需要更大的坐标值,为什么会这样以及如何转换从像素到 setpixel() 坐标。 这
我是 MFC/C++ 的新手,我试图用像素填充我的窗口。我发现有一个函数叫做: SetPixel(X,Y,RGB(,,)); 在我尝试在我的循环中使用它之后,我发现这个函数在一定数量的像素后停止。所以
我是QT初学者 我尝试打开二进制文件并逐像素绘制 我在调试的时候收到了这个警告 QImage::setPixel: coordinate (67,303) out of range QImage::s
我有一张黑白图片,需要逐像素编辑。如果像素太暗,则应将其颜色更改为黑色。这是我的代码: for i in range(width): for j in range(height
我一直在尝试使用 SetPixelV for windows 直接在屏幕上显示图像,因为我相信你知道它慢得无法忍受,而且对于我不想使用它的事情来说太慢了。谁能指出我如何直接显示到屏幕(而不是在窗口中)
我正在开发一个小型绘图程序。我在位图上使用 SetPixel 来绘制线条。当画笔尺寸变大时,例如 25 像素,性能会明显下降。我想知道是否有更快的方法来绘制位图。以下是该项目的一些背景: 我使用位图是
这里我尝试用最快的方法将 3 个矩阵(R、G 和 B)保存到 BufferedImage 中。 我在 StackExchange 上找到了这个方法,但它对我不起作用,因为它以灰度颜色保存图像。 如果我
错误 我想用ndk画圆。但是这种错误发生...帮我找到解决方案的 friend ... 这是我的 jni/ndkfoo.c #include #include #include #include
我不明白 Java 的 WritableRaster 类是如何工作的。我试着查看文档,但不明白它是如何从像素数组中获取值的。另外,我不确定像素阵列由什么组成。 这里我解释一下。 我想做的是:Shami
我是一名优秀的程序员,十分优秀!