0
我正在運行一些示例以讓我更熟悉.net的cudafy。這是我能夠成功工作的代碼。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;
我不確定如何將其應用於我的日常工作。任何人都有一個有效的方法來做到這一點?
如果接受您的答案,該問題將放棄未答覆的CUDA標籤列表 – talonmies