@@ -42,8 +42,9 @@ double gammaCorrection_(const double& element, const double& gamma);
4242 \f]
4343 @param src the input array,type of Mat.
4444 @param gamma a constant for gamma correction.
45+ @param dst the output array, type of Mat.
4546 */
46- Mat gammaCorrection (const Mat& src, const double & gamma);
47+ Mat gammaCorrection (const Mat& src, const double & gamma, Mat dst=Mat() );
4748
4849/* * @brief maskCopyTo a function to delete unsatisfied elementwise.
4950 @param src the input array, type of Mat.
@@ -77,10 +78,26 @@ Mat rgb2gray(const Mat& rgb);
7778 @param lambda a for operation
7879 */
7980template <typename F>
80- Mat elementWise (const Mat& src, F&& lambda)
81+ Mat elementWise (const Mat& src, F&& lambda, Mat dst=Mat() )
8182{
82- Mat dst = src.clone ();
83+ if (dst.empty () || !dst.isContinuous () || dst.total () != src.total () || dst.type () != src.type ())
84+ dst = Mat (src.rows , src.cols , src.type ());
8385 const int channel = src.channels ();
86+ if (src.isContinuous ()) {
87+ const int num_elements = (int )src.total ()*channel;
88+ const double *psrc = (double *)src.data ;
89+ double *pdst = (double *)dst.data ;
90+ const int batch = getNumThreads () > 1 ? 128 : num_elements;
91+ const int N = (num_elements / batch) + ((num_elements % batch) > 0 );
92+ parallel_for_ (Range (0 , N),[&](const Range& range) {
93+ const int start = range.start * batch;
94+ const int end = std::min (range.end *batch, num_elements);
95+ for (int i = start; i < end; i++) {
96+ pdst[i] = lambda (psrc[i]);
97+ }
98+ });
99+ return dst;
100+ }
84101 switch (channel)
85102 {
86103 case 1 :
0 commit comments