From: Anatoly Baksheev Date: Mon, 27 Aug 2012 10:27:13 +0000 (+0400) Subject: renamed device::bilaterl_filter kernel to device::disp_bilaterl_filter for correct... X-Git-Tag: accepted/2.0/20130307.220821~364^2~215^2~36^2~1 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=a09679044e5334aebe3dbd1929a950ccee40ac56;p=profile%2Fivi%2Fopencv.git renamed device::bilaterl_filter kernel to device::disp_bilaterl_filter for correct naming fixed some warnings --- diff --git a/modules/gpu/src/bilateral_filter.cpp b/modules/gpu/src/bilateral_filter.cpp index 04c3126..facd1ee 100644 --- a/modules/gpu/src/bilateral_filter.cpp +++ b/modules/gpu/src/bilateral_filter.cpp @@ -57,16 +57,16 @@ void cv::gpu::DisparityBilateralFilter::operator()(const GpuMat&, const GpuMat&, namespace cv { namespace gpu { namespace device { - namespace bilateral_filter + namespace disp_bilateral_filter { - void load_constants(float* table_color, PtrStepSzf table_space, int ndisp, int radius, short edge_disc, short max_disc); + void disp_load_constants(float* table_color, PtrStepSzf table_space, int ndisp, int radius, short edge_disc, short max_disc); - void bilateral_filter_gpu(PtrStepSzb disp, PtrStepSzb img, int channels, int iters, cudaStream_t stream); - void bilateral_filter_gpu(PtrStepSz disp, PtrStepSzb img, int channels, int iters, cudaStream_t stream); + template + void disp_bilateral_filter(PtrStepSz disp, PtrStepSzb img, int channels, int iters, cudaStream_t stream); } }}} -using namespace ::cv::gpu::device::bilateral_filter; +using namespace ::cv::gpu::device::disp_bilateral_filter; namespace { @@ -103,14 +103,14 @@ namespace } template - void bilateral_filter_operator(int ndisp, int radius, int iters, float edge_threshold,float max_disc_threshold, + void disp_bilateral_filter_operator(int ndisp, int radius, int iters, float edge_threshold,float max_disc_threshold, GpuMat& table_color, GpuMat& table_space, const GpuMat& disp, const GpuMat& img, GpuMat& dst, Stream& stream) { short edge_disc = max(short(1), short(ndisp * edge_threshold + 0.5)); short max_disc = short(ndisp * max_disc_threshold + 0.5); - load_constants(table_color.ptr(), table_space, ndisp, radius, edge_disc, max_disc); + disp_load_constants(table_color.ptr(), table_space, ndisp, radius, edge_disc, max_disc); if (&dst != &disp) { @@ -120,7 +120,7 @@ namespace disp.copyTo(dst); } - bilateral_filter_gpu((PtrStepSz)dst, img, img.channels(), iters, StreamAccessor::getStream(stream)); + disp_bilateral_filter(dst, img, img.channels(), iters, StreamAccessor::getStream(stream)); } typedef void (*bilateral_filter_operator_t)(int ndisp, int radius, int iters, float edge_threshold, float max_disc_threshold, @@ -128,7 +128,7 @@ namespace const GpuMat& disp, const GpuMat& img, GpuMat& dst, Stream& stream); const bilateral_filter_operator_t operators[] = - {bilateral_filter_operator, 0, 0, bilateral_filter_operator, 0, 0, 0, 0}; + {disp_bilateral_filter_operator, 0, 0, disp_bilateral_filter_operator, 0, 0, 0, 0}; } cv::gpu::DisparityBilateralFilter::DisparityBilateralFilter(int ndisp_, int radius_, int iters_) diff --git a/modules/gpu/src/cuda/disp_bilateral_filter.cu b/modules/gpu/src/cuda/disp_bilateral_filter.cu new file mode 100644 index 0000000..e91cf9c --- /dev/null +++ b/modules/gpu/src/cuda/disp_bilateral_filter.cu @@ -0,0 +1,219 @@ +/*M/////////////////////////////////////////////////////////////////////////////////////// +// +// IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING. +// +// By downloading, copying, installing or using the software you agree to this license. +// If you do not agree to this license, do not download, install, +// copy or use the software. +// +// +// License Agreement +// For Open Source Computer Vision Library +// +// Copyright (C) 2000-2008, Intel Corporation, all rights reserved. +// Copyright (C) 2009, Willow Garage Inc., all rights reserved. +// Third party copyrights are property of their respective owners. +// +// Redistribution and use in source and binary forms, with or without modification, +// are permitted provided that the following conditions are met: +// +// * Redistribution's of source code must retain the above copyright notice, +// this list of conditions and the following disclaimer. +// +// * Redistribution's in binary form must reproduce the above copyright notice, +// this list of conditions and the following disclaimer in the documentation +// and/or other materials provided with the distribution. +// +// * The name of the copyright holders may not be used to endorse or promote products +// derived from this software without specific prior written permission. +// +// This software is provided by the copyright holders and contributors "as is" and +// any express or implied warranties, including, but not limited to, the implied +// warranties of merchantability and fitness for a particular purpose are disclaimed. +// In no event shall the Intel Corporation or contributors be liable for any direct, +// indirect, incidental, special, exemplary, or consequential damages +// (including, but not limited to, procurement of substitute goods or services; +// loss of use, data, or profits; or business interruption) however caused +// and on any theory of liability, whether in contract, strict liability, +// or tort (including negligence or otherwise) arising in any way out of +// the use of this software, even if advised of the possibility of such damage. +// +//M*/ + +#include "internal_shared.hpp" +#include "opencv2/gpu/device/limits.hpp" + +namespace cv { namespace gpu { namespace device +{ + namespace disp_bilateral_filter + { + __constant__ float* ctable_color; + __constant__ float* ctable_space; + __constant__ size_t ctable_space_step; + + __constant__ int cndisp; + __constant__ int cradius; + + __constant__ short cedge_disc; + __constant__ short cmax_disc; + + void disp_load_constants(float* table_color, PtrStepSzf table_space, int ndisp, int radius, short edge_disc, short max_disc) + { + cudaSafeCall( cudaMemcpyToSymbol(ctable_color, &table_color, sizeof(table_color)) ); + cudaSafeCall( cudaMemcpyToSymbol(ctable_space, &table_space.data, sizeof(table_space.data)) ); + size_t table_space_step = table_space.step / sizeof(float); + cudaSafeCall( cudaMemcpyToSymbol(ctable_space_step, &table_space_step, sizeof(size_t)) ); + + cudaSafeCall( cudaMemcpyToSymbol(cndisp, &ndisp, sizeof(int)) ); + cudaSafeCall( cudaMemcpyToSymbol(cradius, &radius, sizeof(int)) ); + + cudaSafeCall( cudaMemcpyToSymbol(cedge_disc, &edge_disc, sizeof(short)) ); + cudaSafeCall( cudaMemcpyToSymbol(cmax_disc, &max_disc, sizeof(short)) ); + } + + template + struct DistRgbMax + { + static __device__ __forceinline__ uchar calc(const uchar* a, const uchar* b) + { + uchar x = ::abs(a[0] - b[0]); + uchar y = ::abs(a[1] - b[1]); + uchar z = ::abs(a[2] - b[2]); + return (::max(::max(x, y), z)); + } + }; + + template <> + struct DistRgbMax<1> + { + static __device__ __forceinline__ uchar calc(const uchar* a, const uchar* b) + { + return ::abs(a[0] - b[0]); + } + }; + + template + __global__ void disp_bilateral_filter(int t, T* disp, size_t disp_step, const uchar* img, size_t img_step, int h, int w) + { + const int y = blockIdx.y * blockDim.y + threadIdx.y; + const int x = ((blockIdx.x * blockDim.x + threadIdx.x) << 1) + ((y + t) & 1); + + T dp[5]; + + if (y > 0 && y < h - 1 && x > 0 && x < w - 1) + { + dp[0] = *(disp + (y ) * disp_step + x + 0); + dp[1] = *(disp + (y-1) * disp_step + x + 0); + dp[2] = *(disp + (y ) * disp_step + x - 1); + dp[3] = *(disp + (y+1) * disp_step + x + 0); + dp[4] = *(disp + (y ) * disp_step + x + 1); + + if(::abs(dp[1] - dp[0]) >= cedge_disc || ::abs(dp[2] - dp[0]) >= cedge_disc || ::abs(dp[3] - dp[0]) >= cedge_disc || ::abs(dp[4] - dp[0]) >= cedge_disc) + { + const int ymin = ::max(0, y - cradius); + const int xmin = ::max(0, x - cradius); + const int ymax = ::min(h - 1, y + cradius); + const int xmax = ::min(w - 1, x + cradius); + + float cost[] = {0.0f, 0.0f, 0.0f, 0.0f, 0.0f}; + + const uchar* ic = img + y * img_step + channels * x; + + for(int yi = ymin; yi <= ymax; yi++) + { + const T* disp_y = disp + yi * disp_step; + + for(int xi = xmin; xi <= xmax; xi++) + { + const uchar* in = img + yi * img_step + channels * xi; + + uchar dist_rgb = DistRgbMax::calc(in, ic); + + const float weight = ctable_color[dist_rgb] * (ctable_space + ::abs(y-yi)* ctable_space_step)[::abs(x-xi)]; + + const T disp_reg = disp_y[xi]; + + cost[0] += ::min(cmax_disc, ::abs(disp_reg - dp[0])) * weight; + cost[1] += ::min(cmax_disc, ::abs(disp_reg - dp[1])) * weight; + cost[2] += ::min(cmax_disc, ::abs(disp_reg - dp[2])) * weight; + cost[3] += ::min(cmax_disc, ::abs(disp_reg - dp[3])) * weight; + cost[4] += ::min(cmax_disc, ::abs(disp_reg - dp[4])) * weight; + } + } + + float minimum = numeric_limits::max(); + int id = 0; + + if (cost[0] < minimum) + { + minimum = cost[0]; + id = 0; + } + if (cost[1] < minimum) + { + minimum = cost[1]; + id = 1; + } + if (cost[2] < minimum) + { + minimum = cost[2]; + id = 2; + } + if (cost[3] < minimum) + { + minimum = cost[3]; + id = 3; + } + if (cost[4] < minimum) + { + minimum = cost[4]; + id = 4; + } + + *(disp + y * disp_step + x) = dp[id]; + } + } + } + + template + void disp_bilateral_filter(PtrStepSz disp, PtrStepSzb img, int channels, int iters, cudaStream_t stream) + { + dim3 threads(32, 8, 1); + dim3 grid(1, 1, 1); + grid.x = divUp(disp.cols, threads.x << 1); + grid.y = divUp(disp.rows, threads.y); + + switch (channels) + { + case 1: + for (int i = 0; i < iters; ++i) + { + disp_bilateral_filter<1><<>>(0, disp.data, disp.step/sizeof(T), img.data, img.step, disp.rows, disp.cols); + cudaSafeCall( cudaGetLastError() ); + + disp_bilateral_filter<1><<>>(1, disp.data, disp.step/sizeof(T), img.data, img.step, disp.rows, disp.cols); + cudaSafeCall( cudaGetLastError() ); + } + break; + case 3: + for (int i = 0; i < iters; ++i) + { + disp_bilateral_filter<3><<>>(0, disp.data, disp.step/sizeof(T), img.data, img.step, disp.rows, disp.cols); + cudaSafeCall( cudaGetLastError() ); + + disp_bilateral_filter<3><<>>(1, disp.data, disp.step/sizeof(T), img.data, img.step, disp.rows, disp.cols); + cudaSafeCall( cudaGetLastError() ); + } + break; + default: + cv::gpu::error("Unsupported channels count", __FILE__, __LINE__, "disp_bilateral_filter"); + } + + if (stream == 0) + cudaSafeCall( cudaDeviceSynchronize() ); + } + + template void disp_bilateral_filter(PtrStepSz disp, PtrStepSzb img, int channels, int iters, cudaStream_t stream); + template void disp_bilateral_filter(PtrStepSz disp, PtrStepSzb img, int channels, int iters, cudaStream_t stream); + } // namespace bilateral_filter +}}} // namespace cv { namespace gpu { namespace device