Skip to content

Commit 8ca90eb

Browse files
add parallel_for_ to elementWise
1 parent 2a8db6d commit 8ca90eb

File tree

7 files changed

+34
-19
lines changed

7 files changed

+34
-19
lines changed

modules/mcc/perf/perf_mcc.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,8 @@ PERF_TEST(CV_mcc_perf, infer) {
3636
ColorCorrectionModel model(chartsRGB.col(1).clone().reshape(3, chartsRGB.rows/3) / 255., COLORCHECKER_Macbeth);
3737
model.run();
3838

39-
Mat img = randomMat(Size(4000, 1000), CV_8UC3);
39+
Mat img(1000, 4000, CV_8UC3);
40+
randu(img, 0, 255);
4041
img.convertTo(img, CV_64F, 1. / 255.);
4142

4243
TEST_CYCLE() {

modules/mcc/perf/perf_precomp.hpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,13 @@
66
#define __OPENCV_PERF_PRECOMP_HPP__
77

88
#include "opencv2/ts.hpp"
9-
#include "opencv2/ts/cuda_test.hpp"
109
#include "opencv2/mcc.hpp"
11-
#include "opencv2/mcc/ccm.hpp"
1210

1311
namespace opencv_test
1412
{
1513
using namespace cv::mcc;
1614
using namespace cv::ccm;
15+
using namespace perf;
1716
}
1817

1918
#endif

modules/mcc/src/ccm.cpp

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -289,14 +289,13 @@ Mat ColorCorrectionModel::infer(const Mat& img, bool islinear)
289289
CV_Error(Error::StsBadArg, "No CCM values!" );
290290
}
291291
Mat img_lin = (p->linear)->linearize(img);
292-
Mat img_ccm(img_lin.size(), img_lin.type());
293-
Mat ccm_ = p->ccm.reshape(0, p->shape / 3);
294-
img_ccm = multiple(p->prepare(img_lin), ccm_);
292+
Mat ccm = p->ccm.reshape(0, p->shape / 3);
293+
Mat img_ccm = multiple(p->prepare(img_lin), ccm);
295294
if (islinear == true)
296295
{
297296
return img_ccm;
298297
}
299-
return p->cs.fromLFunc(img_ccm);
298+
return p->cs.fromLFunc(img_ccm, img_lin);
300299
}
301300

302301
void ColorCorrectionModel::Impl::getColor(CONST_COLOR constcolor)

modules/mcc/src/colorspace.cpp

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ void RGBBase_::calOperations()
150150

151151
Mat RGBBase_::toLFunc(Mat& /*rgb*/) const { return Mat(); }
152152

153-
Mat RGBBase_::fromLFunc(Mat& /*rgbl*/, Mat dst) const { return Mat(); }
153+
Mat RGBBase_::fromLFunc(Mat& /*rgbl*/, Mat dst) const { return dst; }
154154

155155
/* @brief Base of Adobe RGB color space;
156156
*/
@@ -159,7 +159,7 @@ Mat AdobeRGBBase_::toLFunc(Mat& rgb) const { return gammaCorrection(rgb, gamma);
159159

160160
Mat AdobeRGBBase_::fromLFunc(Mat& rgbl, Mat dst) const
161161
{
162-
return gammaCorrection(rgbl, 1. / gamma);
162+
return gammaCorrection(rgbl, 1. / gamma, dst);
163163
}
164164

165165
/* @brief Base of sRGB color space;
@@ -203,7 +203,7 @@ Mat sRGBBase_::toLFunc(Mat& rgb) const
203203

204204
/* @brief Used by fromLFunc.
205205
*/
206-
double sRGBBase_::fromLFuncEW(double& x) const
206+
double sRGBBase_::fromLFuncEW(const double& x) const
207207
{
208208
if (x > beta)
209209
{
@@ -225,8 +225,7 @@ double sRGBBase_::fromLFuncEW(double& x) const
225225
*/
226226
Mat sRGBBase_::fromLFunc(Mat& rgbl, Mat dst) const
227227
{
228-
return elementWise(rgbl,
229-
[this](double a_) -> double { return fromLFuncEW(a_); });
228+
return elementWise(rgbl, [this](double a_) -> double { return fromLFuncEW(a_); }, dst);
230229
}
231230

232231
/* @brief sRGB color space.

modules/mcc/src/colorspace.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ class sRGBBase_ : public RGBBase_
167167

168168
/** @brief Used by fromLFunc.
169169
*/
170-
double fromLFuncEW(double& x) const;
170+
double fromLFuncEW(const double& x) const;
171171

172172
/** @brief Delinearization.
173173
@param rgbl the input array, type of cv::Mat.

modules/mcc/src/utils.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,14 +30,14 @@
3030
namespace cv {
3131
namespace ccm {
3232

33-
double gammaCorrection_(const double& element, const double& gamma)
33+
inline double gammaCorrection_(const double& element, const double& gamma)
3434
{
3535
return (element >= 0 ? pow(element, gamma) : -pow((-element), gamma));
3636
}
3737

38-
Mat gammaCorrection(const Mat& src, const double& gamma)
38+
Mat gammaCorrection(const Mat& src, const double& gamma, Mat dst)
3939
{
40-
return elementWise(src, [gamma](double element) -> double { return gammaCorrection_(element, gamma); });
40+
return elementWise(src, [gamma](double element) -> double { return gammaCorrection_(element, gamma); }, dst);
4141
}
4242

4343
Mat maskCopyTo(const Mat& src, const Mat& mask)

modules/mcc/src/utils.hpp

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -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
*/
7980
template <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

Comments
 (0)