Renesas GR-PEACH OpenCV Development / gr-peach-opencv-project-sd-card_update

Fork of gr-peach-opencv-project-sd-card by the do

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers blend.cpp Source File

blend.cpp

00001 /*M///////////////////////////////////////////////////////////////////////////////////////
00002 //
00003 //  IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
00004 //
00005 //  By downloading, copying, installing or using the software you agree to this license.
00006 //  If you do not agree to this license, do not download, install,
00007 //  copy or use the software.
00008 //
00009 //
00010 //                           License Agreement
00011 //                For Open Source Computer Vision Library
00012 //
00013 // Copyright (C) 2010-2012, Multicoreware, Inc., all rights reserved.
00014 // Copyright (C) 2010-2012, Advanced Micro Devices, Inc., all rights reserved.
00015 // Third party copyrights are property of their respective owners.
00016 //
00017 // @Authors
00018 //    Nathan, liujun@multicorewareinc.com
00019 //
00020 // Redistribution and use in source and binary forms, with or without modification,
00021 // are permitted provided that the following conditions are met:
00022 //
00023 //   * Redistribution's of source code must retain the above copyright notice,
00024 //     this list of conditions and the following disclaimer.
00025 //
00026 //   * Redistribution's in binary form must reproduce the above copyright notice,
00027 //     this list of conditions and the following disclaimer in the documentation
00028 //     and/or other materials provided with the distribution.
00029 //
00030 //   * The name of the copyright holders may not be used to endorse or promote products
00031 //     derived from this software without specific prior written permission.
00032 //
00033 // This software is provided by the copyright holders and contributors "as is" and
00034 // any express or implied warranties, including, but not limited to, the implied
00035 // warranties of merchantability and fitness for a particular purpose are disclaimed.
00036 // In no event shall the Intel Corporation or contributors be liable for any direct,
00037 // indirect, incidental, special, exemplary, or consequential damages
00038 // (including, but not limited to, procurement of substitute goods or services;
00039 // loss of use, data, or profits; or business interruption) however caused
00040 // and on any theory of liability, whether in contract, strict liability,
00041 // or tort (including negligence or otherwise) arising in any way out of
00042 // the use of this software, even if advised of the possibility of such damage.
00043 //
00044 //M*/
00045 
00046 #include "precomp.hpp"
00047 #include "opencl_kernels_imgproc.hpp"
00048 
00049 namespace cv {
00050 
00051 template <typename T>
00052 class BlendLinearInvoker :
00053         public ParallelLoopBody
00054 {
00055 public:
00056     BlendLinearInvoker(const Mat & _src1, const Mat & _src2, const Mat & _weights1,
00057                        const Mat & _weights2, Mat & _dst) :
00058         src1(&_src1), src2(&_src2), weights1(&_weights1), weights2(&_weights2), dst(&_dst)
00059     {
00060     }
00061 
00062     virtual void operator() (const Range & range) const
00063     {
00064         int cn = src1->channels(), width = src1->cols * cn;
00065 
00066         for (int y = range.start; y < range.end; ++y)
00067         {
00068             const float * const weights1_row = weights1->ptr<float>(y);
00069             const float * const weights2_row = weights2->ptr<float>(y);
00070             const T * const src1_row = src1->ptr<T>(y);
00071             const T * const src2_row = src2->ptr<T>(y);
00072             T * const dst_row = dst->ptr<T>(y);
00073 
00074             for (int x = 0; x < width; ++x)
00075             {
00076                 int x1 = x / cn;
00077                 float w1 = weights1_row[x1], w2 = weights2_row[x1];
00078                 float den = (w1 + w2 + 1e-5f);
00079                 float num = (src1_row[x] * w1 + src2_row[x] * w2);
00080 
00081                 dst_row[x] = saturate_cast<T>(num / den);
00082             }
00083         }
00084     }
00085 
00086 private:
00087     const BlendLinearInvoker & operator= (const BlendLinearInvoker &);
00088     BlendLinearInvoker(const BlendLinearInvoker &);
00089 
00090     const Mat * src1, * src2, * weights1, * weights2;
00091     Mat * dst;
00092 };
00093 
00094 #ifdef HAVE_OPENCL
00095 
00096 static bool ocl_blendLinear( InputArray _src1, InputArray _src2, InputArray _weights1, InputArray _weights2, OutputArray _dst )
00097 {
00098     int type = _src1.type(), depth = CV_MAT_DEPTH(type), cn = CV_MAT_CN(type);
00099 
00100     char cvt[30];
00101     ocl::Kernel k("blendLinear", ocl::imgproc::blend_linear_oclsrc,
00102                   format("-D T=%s -D cn=%d -D convertToT=%s", ocl::typeToStr(depth),
00103                          cn, ocl::convertTypeStr(CV_32F, depth, 1, cvt)));
00104     if (k.empty())
00105         return false;
00106 
00107     UMat src1 = _src1.getUMat(), src2 = _src2.getUMat(), weights1 = _weights1.getUMat(),
00108             weights2 = _weights2.getUMat(), dst = _dst.getUMat();
00109 
00110     k.args(ocl::KernelArg::ReadOnlyNoSize(src1), ocl::KernelArg::ReadOnlyNoSize(src2),
00111            ocl::KernelArg::ReadOnlyNoSize(weights1), ocl::KernelArg::ReadOnlyNoSize(weights2),
00112            ocl::KernelArg::WriteOnly(dst));
00113 
00114     size_t globalsize[2] = { (size_t)dst.cols, (size_t)dst.rows };
00115     return k.run(2, globalsize, NULL, false);
00116 }
00117 
00118 #endif
00119 
00120 }
00121 
00122 void cv::blendLinear( InputArray _src1, InputArray _src2, InputArray _weights1, InputArray _weights2, OutputArray _dst )
00123 {
00124     int type = _src1.type(), depth = CV_MAT_DEPTH(type);
00125     Size size = _src1.size();
00126 
00127     CV_Assert(depth == CV_8U || depth == CV_32F);
00128     CV_Assert(size == _src2.size() && size == _weights1.size() && size == _weights2.size());
00129     CV_Assert(type == _src2.type() && _weights1.type() == CV_32FC1 && _weights2.type() == CV_32FC1);
00130 
00131     _dst.create(size, type);
00132 
00133 #ifdef HAVE_OPENCL
00134     CV_OCL_RUN(_dst.isUMat(),
00135                ocl_blendLinear(_src1, _src2, _weights1, _weights2, _dst))
00136 #endif
00137 
00138     Mat src1 = _src1.getMat(), src2 = _src2.getMat(), weights1 = _weights1.getMat(),
00139             weights2 = _weights2.getMat(), dst = _dst.getMat();
00140 
00141     if (depth == CV_8U)
00142     {
00143         BlendLinearInvoker<uchar> invoker(src1, src2, weights1, weights2, dst);
00144         parallel_for_(Range(0, src1.rows), invoker, dst.total()/(double)(1<<16));
00145     }
00146     else if (depth == CV_32F)
00147     {
00148         BlendLinearInvoker<float> invoker(src1, src2, weights1, weights2, dst);
00149         parallel_for_(Range(0, src1.rows), invoker, dst.total()/(double)(1<<16));
00150     }
00151 }
00152