estimated_covariance.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  1. /*
  2. By downloading, copying, installing or using the software you agree to this license.
  3. If you do not agree to this license, do not download, install,
  4. copy or use the software.
  5. License Agreement
  6. For Open Source Computer Vision Library
  7. (3-clause BSD License)
  8. Copyright (C) 2000-2015, Intel Corporation, all rights reserved.
  9. Copyright (C) 2009-2011, Willow Garage Inc., all rights reserved.
  10. Copyright (C) 2009-2015, NVIDIA Corporation, all rights reserved.
  11. Copyright (C) 2010-2013, Advanced Micro Devices, Inc., all rights reserved.
  12. Copyright (C) 2015, OpenCV Foundation, all rights reserved.
  13. Copyright (C) 2015, Itseez Inc., all rights reserved.
  14. Third party copyrights are property of their respective owners.
  15. Redistribution and use in source and binary forms, with or without modification,
  16. are permitted provided that the following conditions are met:
  17. * Redistributions of source code must retain the above copyright notice,
  18. this list of conditions and the following disclaimer.
  19. * Redistributions in binary form must reproduce the above copyright notice,
  20. this list of conditions and the following disclaimer in the documentation
  21. and/or other materials provided with the distribution.
  22. * Neither the names of the copyright holders nor the names of the contributors
  23. may be used to endorse or promote products derived from this software
  24. without specific prior written permission.
  25. This software is provided by the copyright holders and contributors "as is" and
  26. any express or implied warranties, including, but not limited to, the implied
  27. warranties of merchantability and fitness for a particular purpose are disclaimed.
  28. In no event shall copyright holders or contributors be liable for any direct,
  29. indirect, incidental, special, exemplary, or consequential damages
  30. (including, but not limited to, procurement of substitute goods or services;
  31. loss of use, data, or profits; or business interruption) however caused
  32. and on any theory of liability, whether in contract, strict liability,
  33. or tort (including negligence or otherwise) arising in any way out of
  34. the use of this software, even if advised of the possibility of such damage.
  35. Algorithmic details of this algorithm can be found at:
  36. * O. Green, Y. Birk, "A Computationally Efficient Algorithm for the 2D Covariance Method", ACM/IEEE International Conference on High Performance Computing, Networking, Storage and Analysis, Denver, Colorado, 2013
  37. A previous and less efficient version of the algorithm can be found:
  38. * O. Green, L. David, A. Galperin, Y. Birk, "Efficient parallel computation of the estimated covariance matrix", arXiv, 2013
  39. */
  40. #include "precomp.hpp"
  41. using namespace cv;
  42. using namespace std;
  43. namespace cv{
  44. namespace ximgproc{
  45. class EstimateCovariance{
  46. public:
  47. EstimateCovariance(int pr_, int pc_);
  48. ~EstimateCovariance();
  49. void computeEstimateCovariance(Mat inputData,Mat outputData);
  50. int combinationCount();
  51. private:
  52. typedef struct {
  53. int mult1r;
  54. int mult1c;
  55. int mult2r;
  56. int mult2c;
  57. int type2; // 0 - for the first P*P. 1 - for the next (P-1)(P-1)
  58. int id;
  59. } Combination;
  60. void initInternalDataStructures();
  61. void buildCombinationsTable();
  62. void iterateCombinations(Mat inputData,Mat outputData);
  63. void computeOneCombination(int comb_id, Mat inputData , Mat outputData,
  64. Mat outputVector,std::vector<int> finalMatPosR, std::vector<int> finalMatPosC);
  65. inline void complexSubtract(std::complex<float>& src, std::complex<float>& dst){dst-=src;}
  66. inline void complexAdd(std::complex<float>& src, std::complex<float>& dst){dst+=src;}
  67. inline void complexConjMulAndAdd(std::complex<float>& a, std::complex<float>& b,
  68. std::complex<float>& dst){dst += a*b;}
  69. inline void complexConjMul(std::complex<float>& a, std::complex<float>& b,
  70. std::complex<float>& dst){dst = a*b;}
  71. private:
  72. int nr;
  73. int nc;
  74. int pr;
  75. int pc;
  76. std::vector<Combination> combinationsTable;
  77. };
  78. EstimateCovariance::EstimateCovariance(int pr_, int pc_){
  79. pr=pr_; pc=pc_;
  80. }
  81. EstimateCovariance::~EstimateCovariance(){
  82. }
  83. void EstimateCovariance::initInternalDataStructures(){
  84. int combCount = combinationCount();
  85. combinationsTable.resize(combCount);
  86. buildCombinationsTable();
  87. }
  88. int EstimateCovariance::combinationCount(){
  89. return (pr*pc+(pr-1)*(pc-1));
  90. }
  91. void EstimateCovariance::buildCombinationsTable()
  92. {
  93. int idx_row,idx_col;
  94. int comb_idx = 0;
  95. Combination comb;
  96. // The first element of the product is [0,0] and the second is to down and to the right of it.
  97. for (idx_col=0; idx_col<pc; ++idx_col) {
  98. for (idx_row=0; idx_row<pr; ++idx_row) {
  99. comb.mult1r=0;
  100. comb.mult1c=0;
  101. comb.mult2r=idx_row;
  102. comb.mult2c=idx_col;
  103. comb.type2 = 0;
  104. comb.id = comb_idx;
  105. memcpy(&combinationsTable[comb_idx++], &comb, sizeof(Combination));
  106. }
  107. }
  108. // The first element is on the top right, and the second element is to the left and down of it.
  109. for (idx_row=1; idx_row<pr; ++idx_row) {
  110. for (idx_col=1; idx_col<pc; ++idx_col) {
  111. comb.mult1r=idx_row;
  112. comb.mult1c=0;
  113. comb.mult2r=0;
  114. comb.mult2c=idx_col;
  115. comb.type2 = 1;
  116. comb.id = comb_idx;
  117. memcpy(&combinationsTable[comb_idx++], &comb, sizeof(Combination));
  118. }
  119. }
  120. }
  121. void EstimateCovariance::computeEstimateCovariance(Mat inputData,Mat outputData){
  122. initInternalDataStructures();
  123. nr=inputData.rows;
  124. nc=inputData.cols;
  125. iterateCombinations(inputData,outputData);
  126. }
  127. void EstimateCovariance::iterateCombinations(Mat inputData,Mat outputData)
  128. {
  129. Mat outputVector(pr*pc,1, DataType<std::complex<float> >::type);
  130. std::vector<int> finalMatPosR(pr*pc,0);
  131. std::vector<int> finalMatPosC(pr*pc,0);
  132. int combs=combinationCount();
  133. for (int idx=0; idx<combs; idx++){
  134. outputVector.setTo(Scalar(0,0));
  135. for (unsigned x=0; x<finalMatPosR.size(); x++)
  136. finalMatPosR[x]=0;
  137. for (unsigned x=0; x<finalMatPosC.size(); x++)
  138. finalMatPosC[x]=0;
  139. computeOneCombination(idx++, inputData, outputData,
  140. outputVector,finalMatPosR, finalMatPosC);
  141. }
  142. }
  143. void EstimateCovariance::computeOneCombination(int comb_id,Mat inputData, Mat outputData,
  144. Mat outputVector,std::vector<int> finalMatPosR, std::vector<int> finalMatPosC)
  145. {
  146. Combination* comb = &combinationsTable[comb_id];
  147. int type2 = comb->type2;
  148. int deltaR = (int)abs((int)(comb->mult1r-comb->mult2r));
  149. int deltaC = (int)abs((int)(comb->mult1c-comb->mult2c));
  150. int numElementsInBlock = pr-abs(deltaR);
  151. int numBlocks = pc-deltaC;
  152. const int DR= nr-pr;
  153. const int DC= nc-pc;
  154. int elementC=0;
  155. std::complex<float> temp_res = std::complex<float>(0,0);
  156. int i,j,r,c;
  157. if (!type2) {
  158. // Computing the first index of the combination.
  159. // This index is made up
  160. for(i=0; i<=( DR); i++) {
  161. int iPdr=i+deltaR;
  162. for(j=0; j<= (DC); j++) {
  163. int jPdc = j+deltaC;
  164. complexConjMulAndAdd(inputData.at<std::complex<float> >(i,j), inputData.at<std::complex<float> >(iPdr,jPdc), temp_res);
  165. }
  166. }
  167. }else{
  168. // Computing the first index of the combination.
  169. for(i=0; i<=( DR); i++) {
  170. int iPdr=i+deltaR;
  171. for(j=0; j<= (DC); j++) {
  172. int jPdc = j+deltaC;
  173. complexConjMulAndAdd(inputData.at<std::complex<float> >(iPdr,j), inputData.at<std::complex<float> >(i,jPdc), temp_res);
  174. }
  175. }
  176. }
  177. outputVector.at<std::complex<float> >(0,0) = temp_res;
  178. // Checking if the first element belongs to the first set of combinatons.
  179. // The combination that the first element is above the second.
  180. if (!type2) {
  181. finalMatPosR[0]=0;
  182. finalMatPosC[0]=pr*deltaC+deltaR;
  183. elementC++;
  184. }else{
  185. finalMatPosR[0]=deltaR;
  186. finalMatPosC[0]=pr*deltaC;
  187. elementC++;
  188. }
  189. for(r=1;r<(pr-deltaR);r++){
  190. std::complex<float> newRowSum = std::complex<float>(0,0),oldRowSum = std::complex<float>(0,0);
  191. std::complex<float> addRows = std::complex<float>(0,0);
  192. int rM1 = r-1;
  193. int k = DR+1 + rM1;
  194. int kPdr = k+deltaR;
  195. int rM1Pdr = rM1 + deltaR;
  196. int cPdc;
  197. if (!type2) {
  198. for(c=0;c<=(DC); c++){
  199. cPdc = c+deltaC;
  200. complexConjMulAndAdd(inputData.at<std::complex<float> >(k,c),inputData.at<std::complex<float> >(kPdr,cPdc),newRowSum);
  201. complexConjMulAndAdd(inputData.at<std::complex<float> >(rM1,c),inputData.at<std::complex<float> >(rM1Pdr,cPdc),oldRowSum);
  202. }
  203. }else{
  204. for(c=0;c<=(DC); c++){
  205. cPdc = c+deltaC;
  206. complexConjMulAndAdd(inputData.at<std::complex<float> >(kPdr,c),inputData.at<std::complex<float> >(k,cPdc),newRowSum);
  207. complexConjMulAndAdd(inputData.at<std::complex<float> >(rM1Pdr,c),inputData.at<std::complex<float> >(rM1,cPdc),oldRowSum);
  208. }
  209. }
  210. complexAdd(newRowSum,addRows);
  211. complexSubtract(oldRowSum,addRows);
  212. complexAdd(outputVector.at<std::complex<float> >(rM1,0),outputVector.at<std::complex<float> >(r,0));
  213. complexAdd(addRows,outputVector.at<std::complex<float> >(r,0));
  214. finalMatPosR[elementC]=finalMatPosR[elementC-1]+1;;
  215. finalMatPosC[elementC]=finalMatPosC[elementC-1]+1;
  216. elementC++;
  217. }
  218. for(c=1; c<numBlocks; c++) {
  219. std::complex<float> newColSum = std::complex<float>(0,0),oldColSum = std::complex<float>(0,0);
  220. std::complex<float> addCols = std::complex<float>(0,0);
  221. // Index arithmetic
  222. int cM1 = c-1;
  223. int dcPc = DC+c;
  224. int q = DC+1 + cM1;
  225. int cM1PdeltaC = cM1 + deltaC;
  226. int dcPcPdeltaC = dcPc+deltaC;
  227. if (!type2) {
  228. for(r=0;r<=(DR); r++){
  229. int rPdr = r+deltaR;
  230. complexConjMulAndAdd(inputData.at<std::complex<float> >(r,q),inputData.at<std::complex<float> >(rPdr,q+deltaC),newColSum);
  231. complexConjMulAndAdd(inputData.at<std::complex<float> >(r,cM1),inputData.at<std::complex<float> >(rPdr,cM1+deltaC),oldColSum);
  232. }
  233. }else{
  234. for(r=0;r<=(DR); r++){
  235. int rPdr = r+deltaR;
  236. complexConjMulAndAdd(inputData.at<std::complex<float> >(rPdr,q),inputData.at<std::complex<float> >(r,q+deltaC),newColSum);
  237. complexConjMulAndAdd(inputData.at<std::complex<float> >(rPdr,cM1),inputData.at<std::complex<float> >(r,cM1+deltaC),oldColSum);
  238. }
  239. }
  240. complexAdd(newColSum,addCols);
  241. complexSubtract(oldColSum,addCols);
  242. complexAdd(outputVector.at<std::complex<float> >((c-1)*numElementsInBlock,0),outputVector.at<std::complex<float> >(c*numElementsInBlock,0));
  243. complexAdd(addCols,outputVector.at<std::complex<float> >(c*numElementsInBlock,0));
  244. finalMatPosR[elementC]=finalMatPosR[elementC-numElementsInBlock]+pr;
  245. finalMatPosC[elementC]=finalMatPosC[elementC-numElementsInBlock]+pr;
  246. elementC++;
  247. for(r=1; r<numElementsInBlock; r++) {
  248. std::complex<float> w = std::complex<float>(0,0),x = std::complex<float>(0,0),
  249. y = std::complex<float>(0,0),z = std::complex<float>(0,0),deltaRowSum = std::complex<float>(0,0);
  250. std::complex<float> tempRes = std::complex<float>(0,0);
  251. // Index arithmetic
  252. int rM1 = r-1;
  253. int drPr = DR+r;
  254. int rM1PdeltaR = rM1 + deltaR;
  255. int drPrPdeltaR = drPr+deltaR;
  256. if (!type2) {
  257. complexConjMul(inputData.at<std::complex<float> >(rM1,cM1),inputData.at<std::complex<float> >(rM1PdeltaR,cM1PdeltaC),w);
  258. complexConjMul(inputData.at<std::complex<float> >(rM1,dcPc),inputData.at<std::complex<float> >(rM1PdeltaR,dcPcPdeltaC),x);
  259. complexConjMul(inputData.at<std::complex<float> >(drPr,cM1),inputData.at<std::complex<float> >(drPrPdeltaR,cM1PdeltaC),y);
  260. complexConjMul(inputData.at<std::complex<float> >(drPr,dcPc),inputData.at<std::complex<float> >(drPrPdeltaR,dcPcPdeltaC),z);
  261. }else{
  262. complexConjMul(inputData.at<std::complex<float> >(rM1PdeltaR,cM1),inputData.at<std::complex<float> >(rM1,cM1PdeltaC),w);
  263. complexConjMul(inputData.at<std::complex<float> >(rM1PdeltaR,dcPc),inputData.at<std::complex<float> >(rM1,dcPcPdeltaC),x);
  264. complexConjMul(inputData.at<std::complex<float> >(drPrPdeltaR,cM1),inputData.at<std::complex<float> >(drPr,cM1PdeltaC),y);
  265. complexConjMul(inputData.at<std::complex<float> >(drPrPdeltaR,dcPc),inputData.at<std::complex<float> >(drPr,dcPcPdeltaC),z);
  266. }
  267. complexAdd(w,tempRes);
  268. complexSubtract(x,tempRes);
  269. complexSubtract(y,tempRes);
  270. complexAdd(z,tempRes);
  271. complexAdd(outputVector.at<std::complex<float> >((c-1)*numElementsInBlock + r,0),deltaRowSum);
  272. complexSubtract(outputVector.at<std::complex<float> >((c-1)*numElementsInBlock+ rM1,0),deltaRowSum);
  273. complexAdd(deltaRowSum,tempRes);
  274. complexAdd(outputVector.at<std::complex<float> >(c*numElementsInBlock+rM1,0),tempRes);
  275. complexAdd(tempRes,outputVector.at<std::complex<float> >(c*numElementsInBlock+r,0));
  276. finalMatPosR[elementC]=finalMatPosR[elementC-1]+1;
  277. finalMatPosC[elementC]=finalMatPosC[elementC-1]+1;
  278. elementC++;
  279. }
  280. }
  281. for(i=0; i<numElementsInBlock*numBlocks; i++){
  282. outputData.at<std::complex<float> >(finalMatPosR[i],finalMatPosC[i])=outputVector.at<std::complex<float> >(i,0);
  283. }
  284. }
  285. void covarianceEstimation(InputArray input_, OutputArray output_,int windowRows, int windowCols){
  286. CV_Assert( input_.channels() <= 2); // Does not take color images.
  287. Mat input;
  288. Mat temp=input_.getMat();
  289. if(temp.channels() == 1){
  290. temp.convertTo(temp,CV_32FC2);
  291. Mat zmat = Mat::zeros(temp.size(), CV_32F);
  292. Mat twoChannelsbefore[] = {temp,zmat};
  293. cv::merge(twoChannelsbefore,2,input);
  294. }else{
  295. temp.convertTo(input, CV_32FC2);
  296. }
  297. EstimateCovariance estCov(windowRows,windowCols);
  298. //input_.getMat().convertTo(input, CV_32FC2);
  299. output_.create(windowRows*windowCols,windowRows*windowCols, DataType<std::complex<float> >::type);
  300. Mat output = output_.getMat();
  301. estCov.computeEstimateCovariance(input,output);
  302. }
  303. } // namespace ximgproc
  304. } // namespace cv