Skip to content

Commit

Permalink
add parallel_for_ to elementWise
Browse files Browse the repository at this point in the history
  • Loading branch information
AleksandrPanov committed Mar 15, 2024
1 parent 2a8db6d commit 5bac197
Show file tree
Hide file tree
Showing 7 changed files with 33 additions and 16 deletions.
1 change: 1 addition & 0 deletions modules/mcc/perf/perf_mcc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ PERF_TEST(CV_mcc_perf, detect) {
}

PERF_TEST(CV_mcc_perf, infer) {

// read gold chartsRGB
string path = cvtest::findDataFile("cv/mcc/mcc_ccm_test.yml");
FileStorage fs(path, FileStorage::READ);
Expand Down
1 change: 1 addition & 0 deletions modules/mcc/perf/perf_precomp.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ namespace opencv_test
{
using namespace cv::mcc;
using namespace cv::ccm;
using namespace perf;
}

#endif
7 changes: 3 additions & 4 deletions modules/mcc/src/ccm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -289,14 +289,13 @@ Mat ColorCorrectionModel::infer(const Mat& img, bool islinear)
CV_Error(Error::StsBadArg, "No CCM values!" );
}
Mat img_lin = (p->linear)->linearize(img);
Mat img_ccm(img_lin.size(), img_lin.type());
Mat ccm_ = p->ccm.reshape(0, p->shape / 3);
img_ccm = multiple(p->prepare(img_lin), ccm_);
Mat ccm = p->ccm.reshape(0, p->shape / 3);
Mat img_ccm = multiple(p->prepare(img_lin), ccm);
if (islinear == true)
{
return img_ccm;
}
return p->cs.fromLFunc(img_ccm);
return p->cs.fromLFunc(img_ccm, img_lin);
}

void ColorCorrectionModel::Impl::getColor(CONST_COLOR constcolor)
Expand Down
9 changes: 4 additions & 5 deletions modules/mcc/src/colorspace.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ void RGBBase_::calOperations()

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

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

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

Mat AdobeRGBBase_::fromLFunc(Mat& rgbl, Mat dst) const
{
return gammaCorrection(rgbl, 1. / gamma);
return gammaCorrection(rgbl, 1. / gamma, dst);
}

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

/* @brief Used by fromLFunc.
*/
double sRGBBase_::fromLFuncEW(double& x) const
double sRGBBase_::fromLFuncEW(const double& x) const
{
if (x > beta)
{
Expand All @@ -225,8 +225,7 @@ double sRGBBase_::fromLFuncEW(double& x) const
*/
Mat sRGBBase_::fromLFunc(Mat& rgbl, Mat dst) const
{
return elementWise(rgbl,
[this](double a_) -> double { return fromLFuncEW(a_); });
return elementWise(rgbl, [this](double a_) -> double { return fromLFuncEW(a_); }, dst);
}

/* @brief sRGB color space.
Expand Down
2 changes: 1 addition & 1 deletion modules/mcc/src/colorspace.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ class sRGBBase_ : public RGBBase_

/** @brief Used by fromLFunc.
*/
double fromLFuncEW(double& x) const;
double fromLFuncEW(const double& x) const;

/** @brief Delinearization.
@param rgbl the input array, type of cv::Mat.
Expand Down
6 changes: 3 additions & 3 deletions modules/mcc/src/utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,14 @@
namespace cv {
namespace ccm {

double gammaCorrection_(const double& element, const double& gamma)
inline double gammaCorrection_(const double& element, const double& gamma)
{
return (element >= 0 ? pow(element, gamma) : -pow((-element), gamma));
}

Mat gammaCorrection(const Mat& src, const double& gamma)
Mat gammaCorrection(const Mat& src, const double& gamma, Mat dst)
{
return elementWise(src, [gamma](double element) -> double { return gammaCorrection_(element, gamma); });
return elementWise(src, [gamma](double element) -> double { return gammaCorrection_(element, gamma); }, dst);
}

Mat maskCopyTo(const Mat& src, const Mat& mask)
Expand Down
23 changes: 20 additions & 3 deletions modules/mcc/src/utils.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,9 @@ double gammaCorrection_(const double& element, const double& gamma);
\f]
@param src the input array,type of Mat.
@param gamma a constant for gamma correction.
@param dst the output array, type of Mat.
*/
Mat gammaCorrection(const Mat& src, const double& gamma);
Mat gammaCorrection(const Mat& src, const double& gamma, Mat dst=Mat());

/** @brief maskCopyTo a function to delete unsatisfied elementwise.
@param src the input array, type of Mat.
Expand Down Expand Up @@ -77,10 +78,26 @@ Mat rgb2gray(const Mat& rgb);
@param lambda a for operation
*/
template <typename F>
Mat elementWise(const Mat& src, F&& lambda)
Mat elementWise(const Mat& src, F&& lambda, Mat dst=Mat())
{
Mat dst = src.clone();
if (dst.empty() || !dst.isContinuous() || dst.total() != src.total() || dst.type() != src.type())
dst = src.clone();
const int channel = src.channels();
if (src.isContinuous()) {
const int num_elements = (int)src.total()*channel;
const double *psrc = (double*)src.data;
double *pdst = (double*)dst.data;
const int batch = 128;
const int N = (num_elements / batch) + ((num_elements % batch) > 0);
parallel_for_(Range(0, num_elements),[&](const Range& range) {
const int start = range.start;
const int end = range.end;
for (int i = start; i < end; i++) {
pdst[i] = lambda(psrc[i]);
}
});
return dst;
}
switch (channel)
{
case 1:
Expand Down

0 comments on commit 5bac197

Please sign in to comment.