From 47b01be0a40761000ac718b038c37cfee1686f2b Mon Sep 17 00:00:00 2001 From: Adrien BAK Date: Thu, 2 Oct 2014 12:54:26 +0900 Subject: [PATCH] replace 8 loops by 2 --- modules/photo/src/seamless_cloning_impl.cpp | 70 +++++++++------------ 1 file changed, 30 insertions(+), 40 deletions(-) diff --git a/modules/photo/src/seamless_cloning_impl.cpp b/modules/photo/src/seamless_cloning_impl.cpp index af6d310a1c..e130f841a1 100644 --- a/modules/photo/src/seamless_cloning_impl.cpp +++ b/modules/photo/src/seamless_cloning_impl.cpp @@ -177,17 +177,14 @@ void Cloning::transpose(const std::vector& mat, std::vector& mat void Cloning::solve(const Mat &img, const std::vector& mod_diff, Mat &result) { - int w = img.size().width; - int h = img.size().height; - - unsigned long int idx,idx1; + const int w = img.size().width; + const int h = img.size().height; std::vector sineTransform((h-2)*(w-2), 0.); std::vector sineTranformTranspose((h-2)*(w-2), 0.); std::vector denom((h-2)*(w-2), 0.); std::vector invsineTransform((h-2)*(w-2), 0.); std::vector invsineTransform_t((h-2)*(w-2), 0.); - std::vector img_d((h)*(w), 0.); dst(mod_diff,sineTransform,h-2,w-2); @@ -202,13 +199,13 @@ void Cloning::solve(const Mat &img, const std::vector& mod_diff, Mat &re { for(int i = 0, cy=1 ; i < w-2;i++,cy++) { - idx = j*(w-2) + i; + int idx = j*(w-2) + i; denom[idx] = (float) 2*cos(CV_PI*cy/( (double) (w-1))) - 2 + 2*cos(CV_PI*cx/((double) (h-1))) - 2; } } - for(idx = 0 ; idx < (unsigned)(w-2)*(h-2) ;idx++) + for(int idx = 0 ; idx < (w-2)*(h-2) ;idx++) { sineTranformTranspose[idx] = sineTranformTranspose[idx]/denom[idx]; } @@ -221,45 +218,38 @@ void Cloning::solve(const Mat &img, const std::vector& mod_diff, Mat &re transpose(invsineTransform,invsineTransform_t,w-2,h-2); - for(int i = 0 ; i < h;i++) - { - for(int j = 0 ; j < w; j++) - { - idx = i*w + j; - img_d[idx] = (double)img.at(i,j); - } - } - for(int i = 1 ; i < h-1;i++) - { - for(int j = 1 ; j < w-1; j++) - { - idx = i*w + j; - img_d[idx] = 0.0; - } - } - for(int i = 1,id1=0 ; i < h-1;i++,id1++) - { - for(int j = 1,id2=0 ; j < w-1; j++,id2++) - { - idx = i*w + j; - idx1= id1*(w-2) + id2; - img_d[idx] = invsineTransform_t[idx1]; - } - } + //first col + for(int i = 0 ; i < w ; ++i) + result.ptr(0)[i] = img.ptr(0)[i]; - for(int i = 0 ; i < h;i++) + for(int j = 1 ; j < h-1 ; ++j) { - for(int j = 0 ; j < w; j++) + //first row + result.ptr(j)[0] = img.ptr(j)[0]; + + for(int i = 1 ; i < w-1 ; ++i) { - idx = i*w + j; - if(img_d[idx] < 0.0) - result.at(i,j) = 0; - else if(img_d[idx] > 255.0) - result.at(i,j) = 255; + int idx = (j-1)* (w-2) + (i-1); + //saturate cast is not used here, because it behaves differently from the previous implementation + //most notable, saturate_cast rounds before truncating, here it's the opposite. + double value = invsineTransform_t[idx]; + if(value < 0.) + result.ptr(j)[i] = 0; + else if (value > 255.0) + result.ptr(j)[i] = 255; else - result.at(i,j) = (uchar) img_d[idx]; + result.ptr(j)[i] = static_cast(value); } + + //last row + result.ptr(j)[w-1] = img.ptr(j)[w-1]; } + + //last col + for(int i = 0 ; i < w ; ++i) + result.ptr(h-1)[i] = img.ptr(h-1)[i]; + + } void Cloning::poisson_solver(const Mat &img, Mat &laplacianX , Mat &laplacianY, Mat &result)