0

I am running through some samples to get myself more familiar with cudafy for .net. Here is the code I was able to get working sucessfully.

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;
    }

What I am trying to accomplish is adding a region of interest area. Basically if the pixel is not within the area just copy don't check for match. For some reason I am having a hard time wrapping my head around how to accomplish this as I still want to copy all the pixels so I have the image back. I was thinking something like this.

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;

I am unsure of how to apply this to my routine. Anyone have some ideas on an efficient way of doing this?

User5505
  • 31
  • 3

1 Answers1

1

May not work for everyone but I ended up being more interested in using an Arc and determining if the point X/Y was within this Arc and if so do the routine I wanted. Here is the code I am using for that.

float distance = GMath.Sqrt((x - CenterX) * (x - CenterX) + (y - CenterY) * (y - CenterY));
if (distance > Radius)
{
    // Outside Arc                    
}
else
{
    // Inside Arc
}
User5505
  • 31
  • 3