我正在浏览一些示例,以使自己更熟悉 .net 的 cudafy。这是我能够成功工作的代码。
private Answer GpuTsp()
{
var stopWatch = Stopwatch.StartNew();
byte[] buffer = new byte[source.Length];
byte[] src_dev_bitmap = _gpu.CopyToDevice(source);
byte[] dst_dev_bitmap = _gpu.Allocate<byte>(source.Length);
_gpu.Launch(new dim3(_bmp.Width, _bmp.Height), 1).thekernel(dst_dev_bitmap, src_dev_bitmap);
_gpu.CopyFromDevice(dst_dev_bitmap, buffer);
_gpu.FreeAll();
return new Answer { Result = buffer, Milliseconds = stopWatch.ElapsedMilliseconds };
}
[Cudafy]
public static void thekernel(GThread thread, byte[] dst, byte[] src)
{
int x = thread.blockIdx.x;
int y = thread.blockIdx.y;
int offset = x + y * thread.gridDim.x;
if (x < N)
{
byte b, g, r;
b = src[offset * 3 + 0];
g = src[offset * 3 + 1];
r = src[offset * 3 + 2];
if (IsMatch(r, g, b, Red, Green, Blue, 35))
{
dst[offset * 3 + 0] = 255; //Mark Match
dst[offset * 3 + 1] = 252; //Mark Match
dst[offset * 3 + 2] = 201; //Mark Match
}
else
{
dst[offset * 3 + 0] = src[offset * 3 + 0]; //Copy
dst[offset * 3 + 1] = src[offset * 3 + 1]; //Copy
dst[offset * 3 + 2] = src[offset * 3 + 2]; //Copy
}
}
}
[Cudafy]
private static bool IsMatch(byte r1, byte g1, byte b1, byte r2, byte g2, byte b2, int threshold = 35)
{
int r = (int)r1 - r2,
g = (int)g1 - g2,
b = (int)b1 - b2;
return (r * r + g * g + b * b) <= threshold * threshold;
}
我想要完成的是添加一个感兴趣区域。基本上,如果像素不在该区域内,只需复制不要检查匹配。出于某种原因,我很难思考如何实现这一点,因为我仍然想复制所有像素,以便将图像恢复。我在想这样的事情。
Rectangle rect = new Rectangle(200, 100, 640, 600);
int startX = rect.Left;
int startY = rect.Top;
int stopX = startX + rect.Width;
int stopY = startY + rect.Height;
我不确定如何将它应用到我的日常生活中。有人对有效的方法有一些想法吗?