From d1cfcfcafd41d81522f8c4d3b64e62d3e7ecb9dc Mon Sep 17 00:00:00 2001 From: Ilya Lavrenov Date: Tue, 18 Mar 2014 20:02:04 +0400 Subject: [PATCH] added 3-channels support to morphology operations --- modules/imgproc/src/morph.cpp | 54 +++++--------- modules/imgproc/src/opencl/morph.cl | 117 +++++++++++++++--------------- modules/imgproc/test/ocl/test_filters.cpp | 16 ++-- 3 files changed, 86 insertions(+), 101 deletions(-) diff --git a/modules/imgproc/src/morph.cpp b/modules/imgproc/src/morph.cpp index ac958fc..b4011ee 100644 --- a/modules/imgproc/src/morph.cpp +++ b/modules/imgproc/src/morph.cpp @@ -42,7 +42,6 @@ #include "precomp.hpp" #include -#include #include "opencl_kernels.hpp" /****************************************************************************************\ @@ -1291,9 +1290,10 @@ static bool ocl_morphology_op(InputArray _src, OutputArray _dst, Mat kernel, { CV_Assert(op == MORPH_ERODE || op == MORPH_DILATE); + int type = _src.type(), depth = CV_MAT_DEPTH(type), cn = CV_MAT_CN(type); bool doubleSupport = ocl::Device::getDefault().doubleFPConfig() > 0; - if (_src.depth() == CV_64F && !doubleSupport) + if (depth == CV_64F && !doubleSupport) return false; UMat kernel8U; @@ -1324,13 +1324,14 @@ static bool ocl_morphology_op(InputArray _src, OutputArray _dst, Mat kernel, return false; static const char * const op2str[] = { "ERODE", "DILATE" }; - String buildOptions = format("-D RADIUSX=%d -D RADIUSY=%d -D LSIZE0=%d -D LSIZE1=%d -D %s%s%s -D GENTYPE=%s -D DEPTH_%d", - anchor.x, anchor.y, (int)localThreads[0], (int)localThreads[1], op2str[op], + String buildOptions = format("-D RADIUSX=%d -D RADIUSY=%d -D LSIZE0=%d -D LSIZE1=%d -D %s%s%s" + " -D T=%s -D DEPTH_%d -D cn=%d -D T1=%s", anchor.x, anchor.y, + (int)localThreads[0], (int)localThreads[1], op2str[op], doubleSupport ? " -D DOUBLE_SUPPORT" : "", rectKernel ? " -D RECTKERNEL" : "", - ocl::typeToStr(_src.type()), _src.depth() ); + ocl::typeToStr(_src.type()), _src.depth(), cn, ocl::typeToStr(depth)); std::vector kernels; - for (int i = 0; i= (r_edge) ? (elem1) : (elem2) -__kernel void morph(__global const uchar * restrict srcptr, int src_step, int src_offset, +// BORDER_CONSTANT: iiiiii|abcdefgh|iiiiiii +#define ELEM(i, l_edge, r_edge, elem1, elem2) (i) < (l_edge) | (i) >= (r_edge) ? (elem1) : (elem2) + +__kernel void morph(__global const uchar * srcptr, int src_step, int src_offset, __global uchar * dstptr, int dst_step, int dst_offset, - int src_offset_x, int src_offset_y, - int cols, int rows, - __constant uchar * mat_kernel, - int src_whole_cols, int src_whole_rows) + int src_offset_x, int src_offset_y, int cols, int rows, + __constant uchar * mat_kernel, int src_whole_cols, int src_whole_rows) { - int l_x = get_local_id(0); - int l_y = get_local_id(1); - int x = get_group_id(0)*LSIZE0; - int y = get_group_id(1)*LSIZE1; - int start_x = x+src_offset_x-RADIUSX; - int end_x = x + src_offset_x+LSIZE0+RADIUSX; - int width = end_x -(x+src_offset_x-RADIUSX)+1; - int start_y = y+src_offset_y-RADIUSY; - int point1 = mad24(l_y,LSIZE0,l_x); - int point2 = point1 + LSIZE0*LSIZE1; - int tl_x = point1 % width; - int tl_y = point1 / width; - int tl_x2 = point2 % width; - int tl_y2 = point2 / width; - int cur_x = start_x + tl_x; - int cur_y = start_y + tl_y; - int cur_x2 = start_x + tl_x2; - int cur_y2 = start_y + tl_y2; - int start_addr = mad24(cur_y,src_step, cur_x*(int)sizeof(GENTYPE)); - int start_addr2 = mad24(cur_y2,src_step, cur_x2*(int)sizeof(GENTYPE)); - GENTYPE temp0,temp1; - __local GENTYPE LDS_DAT[2*LSIZE1*LSIZE0]; + int gidx = get_global_id(0), gidy = get_global_id(1); + int l_x = get_local_id(0), l_y = get_local_id(1); + int x = get_group_id(0) * LSIZE0, y = get_group_id(1) * LSIZE1; + int start_x = x + src_offset_x - RADIUSX; + int end_x = x + src_offset_x + LSIZE0 + RADIUSX; + int width = end_x - (x + src_offset_x - RADIUSX) + 1; + int start_y = y + src_offset_y - RADIUSY; + int point1 = mad24(l_y, LSIZE0, l_x); + int point2 = point1 + LSIZE0 * LSIZE1; + int tl_x = point1 % width, tl_y = point1 / width; + int tl_x2 = point2 % width, tl_y2 = point2 / width; + int cur_x = start_x + tl_x, cur_y = start_y + tl_y; + int cur_x2 = start_x + tl_x2, cur_y2 = start_y + tl_y2; + int start_addr = mad24(cur_y, src_step, cur_x * TSIZE); + int start_addr2 = mad24(cur_y2, src_step, cur_x2 * TSIZE); + + __local T LDS_DAT[2*LSIZE1*LSIZE0]; - int end_addr = mad24(src_whole_rows - 1,src_step,src_whole_cols*(int)sizeof(GENTYPE)); - //read pixels from src - start_addr = ((start_addr < end_addr) && (start_addr > 0)) ? start_addr : 0; - start_addr2 = ((start_addr2 < end_addr) && (start_addr2 > 0)) ? start_addr2 : 0; - __global const GENTYPE * src; - src = (__global const GENTYPE *)(srcptr+start_addr); - temp0 = src[0]; - src = (__global const GENTYPE *)(srcptr+start_addr2); - temp1 = src[0]; - //judge if read out of boundary - temp0= ELEM(cur_x,0,src_whole_cols,(GENTYPE)VAL,temp0); - temp0= ELEM(cur_y,0,src_whole_rows,(GENTYPE)VAL,temp0); + // read pixels from src + int end_addr = mad24(src_whole_rows - 1, src_step, src_whole_cols * TSIZE); + start_addr = start_addr < end_addr && start_addr > 0 ? start_addr : 0; + start_addr2 = start_addr2 < end_addr && start_addr2 > 0 ? start_addr2 : 0; - temp1= ELEM(cur_x2,0,src_whole_cols,(GENTYPE)VAL,temp1); - temp1= ELEM(cur_y2,0,src_whole_rows,(GENTYPE)VAL,temp1); + T temp0 = loadpix(srcptr + start_addr); + T temp1 = loadpix(srcptr + start_addr2); + + // judge if read out of boundary + temp0 = ELEM(cur_x, 0, src_whole_cols, (T)(VAL),temp0); + temp0 = ELEM(cur_y, 0, src_whole_rows, (T)(VAL),temp0); + + temp1 = ELEM(cur_x2, 0, src_whole_cols, (T)(VAL), temp1); + temp1 = ELEM(cur_y2, 0, src_whole_rows, (T)(VAL), temp1); LDS_DAT[point1] = temp0; LDS_DAT[point2] = temp1; barrier(CLK_LOCAL_MEM_FENCE); - GENTYPE res = (GENTYPE)VAL; - for(int i=0; i<2*RADIUSY+1; i++) - for(int j=0; j<2*RADIUSX+1; j++) + + T res = (T)(VAL); + for (int i = 0, sizey = 2 * RADIUSY + 1; i < sizey; i++) + for (int j = 0, sizex = 2 * RADIUSX + 1; j < sizex; j++) { res = #ifndef RECTKERNEL mat_kernel[i*(2*RADIUSX+1)+j] ? #endif - MORPH_OP(res,LDS_DAT[mad24(l_y+i,width,l_x+j)]) + MORPH_OP(res, LDS_DAT[mad24(l_y + i, width, l_x + j)]) #ifndef RECTKERNEL - :res + : res #endif ; } - int gidx = get_global_id(0); - int gidy = get_global_id(1); - if(gidx