用的是破93的RGB2L,不过偶是用CUDA写的:
#define THREAD_N 256
#define THRESHOLD 127
__global__ static void binarizeKernel(unsigned char *in, unsigned char *out)
{
const unsigned long offset = (blockIdx.x * THREAD_N + threadIdx.x);
unsigned char b = in[offset * 3];
unsigned char g = in[offset * 3 + 1];
unsigned char r = in[offset * 3 + 2];
unsigned char mi = __min(r, __min(g, b));
unsigned char ma = __max(r, __max(g, b));
if(((unsigned short)ma + (unsigned short)mi) > THRESHOLD * 2)
out[offset] = 255;
else
out[offset] = 0;
}
extern "C" float binarizeCall(unsigned char *in, unsigned char *out, int w, int h)
{
unsigned char *device_src = 0;
unsigned char *device_dest = 0;
cudaMalloc((void **)&device_src, w * h * 3);
cudaMalloc((void **)&device_dest, w * h);
unsigned int timer = 0;
cutCreateTimer(&timer);
cudaMemcpy(device_src, in, w * h * 3, cudaMemcpyHostToDevice);
cutStartTimer(timer);
binarizeKernel<<<w * h / THREAD_N, THREAD_N>>>(device_src, device_dest);
cudaThreadSynchronize();
cutStopTimer(timer);
cudaMemcpy(out, device_dest, w * h, cudaMemcpyDeviceToHost);
float ms = cutGetTimerValue(timer);
cutDeleteTimer(timer);
cudaFree(device_dest);
cudaFree(device_src);
return ms;
}
200字以内,仅用于支线交流,主线讨论请采用回复功能。