From 87760d13fbee8b005800c55246fa59a3e4cc8685 Mon Sep 17 00:00:00 2001 From: Erik Karlsson Date: Mon, 2 Mar 2015 22:33:14 +0100 Subject: [PATCH] Cleanup and addition of 4-component support for ocl_fastNlMeansDenoising --- .../src/fast_nlmeans_denoising_opencl.hpp | 6 ++-- modules/photo/src/opencl/nlmeans.cl | 28 +++++++++++-------- 2 files changed, 19 insertions(+), 15 deletions(-) diff --git a/modules/photo/src/fast_nlmeans_denoising_opencl.hpp b/modules/photo/src/fast_nlmeans_denoising_opencl.hpp index 9c0e40401..41264045c 100644 --- a/modules/photo/src/fast_nlmeans_denoising_opencl.hpp +++ b/modules/photo/src/fast_nlmeans_denoising_opencl.hpp @@ -77,7 +77,7 @@ static bool ocl_fastNlMeansDenoising(InputArray _src, OutputArray _dst, float h, int ctaSize = ocl::Device::getDefault().isIntel() ? CTA_SIZE_INTEL : CTA_SIZE_DEFAULT; Size size = _src.size(); - if (cn != 1 && cn != 2 && cn != 3 && depth != CV_8U && (!abs || depth != CV_16U)) + if (cn != 1 && cn != 2 && cn != 3 && cn != 4 && depth != CV_8U && (!abs || depth != CV_16U)) return false; int templateWindowHalfWize = templateWindowSize / 2; @@ -93,7 +93,7 @@ static bool ocl_fastNlMeansDenoising(InputArray _src, OutputArray _dst, float h, " -D weight_t=%s -D sum_t=%s -D convert_sum_t=%s" " -D BLOCK_COLS=%d -D BLOCK_ROWS=%d" " -D CTA_SIZE=%d -D TEMPLATE_SIZE2=%d -D SEARCH_SIZE2=%d" - " -D convert_int_t=%s -D cn=%d -D convert_pixel_t=%s%s", + " -D convert_int_t=%s -D cn=%d -D psz=%d -D convert_pixel_t=%s%s", templateWindowSize, searchWindowSize, ocl::typeToStr(depth), ocl::typeToStr(type), ocl::typeToStr(CV_32SC(cn)), depth == CV_8U ? ocl::typeToStr(CV_32S) : "long", @@ -103,7 +103,7 @@ static bool ocl_fastNlMeansDenoising(InputArray _src, OutputArray _dst, float h, (sprintf(buf[1], "convert_long%d", cn), buf[1]), BLOCK_COLS, BLOCK_ROWS, ctaSize, templateWindowHalfWize, searchWindowHalfSize, - ocl::convertTypeStr(depth, CV_32S, cn, buf[2]), cn == 3 ? 4 : cn, + ocl::convertTypeStr(depth, CV_32S, cn, buf[2]), cn, cn == 3 ? 4 : cn, ocl::convertTypeStr(CV_32S, depth, cn, buf[3]), abs ? " -D ABS" : ""); ocl::Kernel k("fastNlMeansDenoising", ocl::photo::nlmeans_oclsrc, opts); diff --git a/modules/photo/src/opencl/nlmeans.cl b/modules/photo/src/opencl/nlmeans.cl index 3a104c42a..91b012354 100644 --- a/modules/photo/src/opencl/nlmeans.cl +++ b/modules/photo/src/opencl/nlmeans.cl @@ -60,8 +60,10 @@ inline int calcDist(pixel_t a, pixel_t b) return retval; #elif cn == 2 return retval.x + retval.y; -#elif cn == 3 || cn == 4 /* A is ignored */ +#elif cn == 3 return retval.x + retval.y + retval.z; +#elif cn == 4 + return retval.x + retval.y + retval.z + retval.w; #else #error "cn should be either 1, 2, 3 or 4" #endif @@ -83,8 +85,10 @@ inline int calcDistUpDown(pixel_t down_value, pixel_t down_value_t, pixel_t up_v return retval; #elif cn == 2 return retval.x + retval.y; -#elif cn == 3 || cn == 4 /* A is ignored */ +#elif cn == 3 return retval.x + retval.y + retval.z; +#elif cn == 4 + return retval.x + retval.y + retval.z + retval.w; #else #error "cn should be either 1, 2, 3 or 4" #endif @@ -106,8 +110,8 @@ inline void calcFirstElementInRow(__global const sample_t * src, int src_step, i int dist = 0, value; __global const pixel_t * src_template = (__global const pixel_t *)(src + - mad24(sy + i / SEARCH_SIZE, src_step, mad24(cn, sx + i % SEARCH_SIZE, src_offset))); - __global const pixel_t * src_current = (__global const pixel_t *)(src + mad24(y, src_step, mad24(cn, x, src_offset))); + mad24(sy + i / SEARCH_SIZE, src_step, mad24(psz, sx + i % SEARCH_SIZE, src_offset))); + __global const pixel_t * src_current = (__global const pixel_t *)(src + mad24(y, src_step, mad24(psz, x, src_offset))); __global int * col_dists_current = col_dists + i * TEMPLATE_SIZE; #pragma unroll @@ -148,9 +152,9 @@ inline void calcElementInFirstRow(__global const sample_t * src, int src_step, i for (int i = id; i < SEARCH_SIZE_SQ; i += CTA_SIZE) { - __global const pixel_t * src_current = (__global const pixel_t *)(src + mad24(y, src_step, mad24(cn, x, src_offset))); + __global const pixel_t * src_current = (__global const pixel_t *)(src + mad24(y, src_step, mad24(psz, x, src_offset))); __global const pixel_t * src_template = (__global const pixel_t *)(src + - mad24(sy + i / SEARCH_SIZE, src_step, mad24(cn, sx + i % SEARCH_SIZE, src_offset))); + mad24(sy + i / SEARCH_SIZE, src_step, mad24(psz, sx + i % SEARCH_SIZE, src_offset))); __global int * col_dists_current = col_dists + TEMPLATE_SIZE * i; int col_dist = 0; @@ -178,8 +182,8 @@ inline void calcElement(__global const sample_t * src, int src_step, int src_off int sy_up = y - TEMPLATE_SIZE2 - 1; int sy_down = y + TEMPLATE_SIZE2; - pixel_t up_value = *(__global const pixel_t *)(src + mad24(sy_up, src_step, mad24(cn, sx, src_offset))); - pixel_t down_value = *(__global const pixel_t *)(src + mad24(sy_down, src_step, mad24(cn, sx, src_offset))); + pixel_t up_value = *(__global const pixel_t *)(src + mad24(sy_up, src_step, mad24(psz, sx, src_offset))); + pixel_t down_value = *(__global const pixel_t *)(src + mad24(sy_down, src_step, mad24(psz, sx, src_offset))); sx -= SEARCH_SIZE2; sy_up -= SEARCH_SIZE2; @@ -189,8 +193,8 @@ inline void calcElement(__global const sample_t * src, int src_step, int src_off { int wx = i % SEARCH_SIZE, wy = i / SEARCH_SIZE; - pixel_t up_value_t = *(__global const pixel_t *)(src + mad24(sy_up + wy, src_step, mad24(cn, sx + wx, src_offset))); - pixel_t down_value_t = *(__global const pixel_t *)(src + mad24(sy_down + wy, src_step, mad24(cn, sx + wx, src_offset))); + pixel_t up_value_t = *(__global const pixel_t *)(src + mad24(sy_up + wy, src_step, mad24(psz, sx + wx, src_offset))); + pixel_t down_value_t = *(__global const pixel_t *)(src + mad24(sy_down + wy, src_step, mad24(psz, sx + wx, src_offset))); __global int * col_dists_current = col_dists + mad24(i, TEMPLATE_SIZE, first); __global int * up_col_dists_current = up_col_dists + mad24(x0, SEARCH_SIZE_SQ, i); @@ -215,7 +219,7 @@ inline void convolveWindow(__global const sample_t * src, int src_step, int src_ for (int i = id; i < SEARCH_SIZE_SQ; i += CTA_SIZE) { - int src_index = mad24(sy + i / SEARCH_SIZE, src_step, mad24(i % SEARCH_SIZE + sx, cn, src_offset)); + int src_index = mad24(sy + i / SEARCH_SIZE, src_step, mad24(i % SEARCH_SIZE + sx, psz, src_offset)); sum_t src_value = convert_sum_t(*(__global const pixel_t *)(src + src_index)); int almostAvgDist = dists[i] >> almostTemplateWindowSizeSqBinShift; @@ -242,7 +246,7 @@ inline void convolveWindow(__global const sample_t * src, int src_step, int src_ if (id == 0) { - int dst_index = mad24(y, dst_step, mad24(cn, x, dst_offset)); + int dst_index = mad24(y, dst_step, mad24(psz, x, dst_offset)); sum_t weighted_sum_local_0 = weighted_sum_local[0] + weighted_sum_local[1] + weighted_sum_local[2] + weighted_sum_local[3]; weight_t weights_local_0 = weights_local[0] + weights_local[1] + weights_local[2] + weights_local[3];