Commit c1bfb00d authored by Zhou Chao's avatar Zhou Chao

Add parallel processing for some steps of l0 smooth

parent 0c691ff0
...@@ -44,6 +44,77 @@ using namespace std; ...@@ -44,6 +44,77 @@ using namespace std;
namespace namespace
{ {
class ParallelDft : public ParallelLoopBody {
private:
vector<Mat> src_;
public:
ParallelDft(vector<Mat> &s)
{
src_ = s;
}
void operator() (const Range& range) const
{
for (int i = range.start; i != range.end; i++)
{
dft(src_[i], src_[i]);
}
}
};
class ParallelIdft : public ParallelLoopBody {
private:
vector<Mat> src_;
public:
ParallelIdft(vector<Mat> &s)
{
src_ = s;
}
void operator() (const Range& range) const
{
for (int i = range.start; i != range.end; i++){
idft(src_[i], src_[i],DFT_SCALE);
}
}
};
class ParallelDivComplexByReal : public ParallelLoopBody {
private:
vector<Mat> numer_;
vector<Mat> denom_;
vector<Mat> dst_;
public:
ParallelDivComplexByReal(vector<Mat> &numer, vector<Mat> &denom, vector<Mat> &dst)
{
numer_ = numer;
denom_ = denom;
dst_ = dst;
}
void operator() (const Range& range) const
{
for (int i = range.start; i != range.end; i++)
{
Mat aPanels[2];
Mat bPanels[2];
split(numer_[i], aPanels);
split(denom_[i], bPanels);
Mat realPart;
Mat imaginaryPart;
divide(aPanels[0], denom_[i], realPart);
divide(aPanels[1], denom_[i], imaginaryPart);
aPanels[0] = realPart;
aPanels[1] = imaginaryPart;
merge(aPanels, 2, dst_[i]);
}
}
};
void shift(InputArray src, OutputArray dst, int shift_x, int shift_y) { void shift(InputArray src, OutputArray dst, int shift_x, int shift_y) {
Mat S = src.getMat(); Mat S = src.getMat();
Mat D = dst.getMat(); Mat D = dst.getMat();
...@@ -73,14 +144,15 @@ namespace ...@@ -73,14 +144,15 @@ namespace
// dft after padding imaginary // dft after padding imaginary
void fft(InputArray src, OutputArray dst) { void fft(InputArray src, OutputArray dst) {
Mat S = src.getMat(); Mat S = src.getMat();
Mat planes[] = {S, Mat::zeros(S.size(), S.type())}; Mat planes[] = {S.clone(), Mat::zeros(S.size(), S.type())};
Mat x;
merge(planes, 2, dst); merge(planes, 2, dst);
// compute the result // compute the result
dft(dst, dst); dft(dst, dst);
} }
void psf2otf(InputArray src, OutputArray dst, int height, int width){ void psf2otf(InputArray src, OutputArray dst, int height, int width) {
Mat S = src.getMat(); Mat S = src.getMat();
Mat D = dst.getMat(); Mat D = dst.getMat();
...@@ -100,31 +172,34 @@ namespace ...@@ -100,31 +172,34 @@ namespace
fft(padded, dst); fft(padded, dst);
} }
void dftMultiChannel(InputArray src, vector<Mat> &dst){ void dftMultiChannel(InputArray src, vector<Mat> &dst) {
Mat S = src.getMat(); Mat S = src.getMat();
split(S, dst); split(S, dst);
for(int i = 0; i < S.channels(); i++){ for(int i = 0; i < S.channels(); i++){
fft(dst[i], dst[i]); Mat planes[] = {dst[i].clone(), Mat::zeros(dst[i].size(), dst[i].type())};
merge(planes, 2, dst[i]);
} }
parallel_for_(cv::Range(0,S.channels()), ParallelDft(dst));
} }
void idftMultiChannel(const vector<Mat> &src, OutputArray dst){ void idftMultiChannel(const vector<Mat> &src, OutputArray dst){
Mat *channels = new Mat[src.size()]; vector<Mat> channels(src);
for(int i = 0 ; unsigned(i) < src.size(); i++){ parallel_for_(Range(0, src.size()), ParallelIdft(channels));
idft(src[i], channels[i]);
Mat realImg[2]; for(int i = 0; unsigned(i) < src.size(); i++){
split(channels[i], realImg); Mat panels[2];
channels[i] = realImg[0] / src[i].cols / src[i].rows; split(channels[i], panels);
channels[i] = panels[0];
} }
Mat D; Mat D;
merge(channels, src.size(), D); merge(channels, D);
D.copyTo(dst); D.copyTo(dst);
delete[] channels;
} }
void addComplex(InputArray aSrc, int bSrc, OutputArray dst){ void addComplex(InputArray aSrc, int bSrc, OutputArray dst){
...@@ -134,32 +209,15 @@ namespace ...@@ -134,32 +209,15 @@ namespace
merge(panels, 2, dst); merge(panels, 2, dst);
} }
void divComplexByReal(InputArray aSrc, InputArray bSrc, OutputArray dst){ void divComplexByRealMultiChannel(vector<Mat> &numer,
Mat aPanels[2]; vector<Mat> &denom, vector<Mat> &dst){
Mat bPanels[2];
split(aSrc.getMat(), aPanels);
split(bSrc.getMat(), bPanels);
Mat realPart;
Mat imaginaryPart;
divide(aPanels[0], bSrc.getMat(), realPart);
divide(aPanels[1], bSrc.getMat(), imaginaryPart);
aPanels[0] = realPart;
aPanels[1] = imaginaryPart;
Mat rst;
merge(aPanels, 2, dst);
}
void divComplexByRealMultiChannel(const vector<Mat> &numer,
const vector<Mat> &denom, vector<Mat> &dst)
{
for(int i = 0; unsigned(i) < numer.size(); i++) for(int i = 0; unsigned(i) < numer.size(); i++)
{ {
divComplexByReal(numer[i], denom[i], dst[i]); dst[i].create(numer[i].size(), numer[i].type());
} }
parallel_for_(Range(0, numer.size()), ParallelDivComplexByReal(numer, denom, dst));
} }
// power of 2 of the absolute value of the complex // power of 2 of the absolute value of the complex
...@@ -175,11 +233,11 @@ namespace ...@@ -175,11 +233,11 @@ namespace
namespace cv namespace cv
{ {
namespace ximgproc namespace ximgproc
{ {
void l0Smooth(InputArray src, OutputArray dst, double lambda, double kappa) void l0Smooth(InputArray src, OutputArray dst, double lambda, double kappa)
{ {
Mat S = src.getMat(); Mat S = src.getMat();
CV_Assert(!S.empty()); CV_Assert(!S.empty());
...@@ -222,7 +280,6 @@ void l0Smooth(InputArray src, OutputArray dst, double lambda, double kappa) ...@@ -222,7 +280,6 @@ void l0Smooth(InputArray src, OutputArray dst, double lambda, double kappa)
// input image in frequency domain // input image in frequency domain
vector<Mat> numerConst; vector<Mat> numerConst;
dftMultiChannel(S, numerConst); dftMultiChannel(S, numerConst);
/********************************* /*********************************
* solver * solver
*********************************/ *********************************/
...@@ -303,6 +360,6 @@ void l0Smooth(InputArray src, OutputArray dst, double lambda, double kappa) ...@@ -303,6 +360,6 @@ void l0Smooth(InputArray src, OutputArray dst, double lambda, double kappa)
}else{ }else{
S.copyTo(D); S.copyTo(D);
} }
} }
} }
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment