estimated_covariance.cpp 14.5 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
/*
By downloading, copying, installing or using the software you agree to this license.
If you do not agree to this license, do not download, install,
copy or use the software.


                          License Agreement
               For Open Source Computer Vision Library
                       (3-clause BSD License)

Copyright (C) 2000-2015, Intel Corporation, all rights reserved.
Copyright (C) 2009-2011, Willow Garage Inc., all rights reserved.
Copyright (C) 2009-2015, NVIDIA Corporation, all rights reserved.
Copyright (C) 2010-2013, Advanced Micro Devices, Inc., all rights reserved.
Copyright (C) 2015, OpenCV Foundation, all rights reserved.
Copyright (C) 2015, Itseez Inc., all rights reserved.
Third party copyrights are property of their respective owners.

Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:

  * Redistributions of source code must retain the above copyright notice,
    this list of conditions and the following disclaimer.

  * Redistributions in binary form must reproduce the above copyright notice,
    this list of conditions and the following disclaimer in the documentation
    and/or other materials provided with the distribution.

  * Neither the names of the copyright holders nor the names of the contributors
    may be used to endorse or promote products derived from this software
    without specific prior written permission.

This software is provided by the copyright holders and contributors "as is" and
any express or implied warranties, including, but not limited to, the implied
warranties of merchantability and fitness for a particular purpose are disclaimed.
In no event shall copyright holders or contributors be liable for any direct,
indirect, incidental, special, exemplary, or consequential damages
(including, but not limited to, procurement of substitute goods or services;
loss of use, data, or profits; or business interruption) however caused
and on any theory of liability, whether in contract, strict liability,
or tort (including negligence or otherwise) arising in any way out of
the use of this software, even if advised of the possibility of such damage.

Algorithmic details of this algorithm can be found at:
 * 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
A previous and less efficient version of the algorithm can be found:
 * O. Green, L. David, A. Galperin, Y. Birk, "Efficient parallel computation of the estimated covariance matrix", arXiv, 2013


*/

#include "precomp.hpp"

using namespace cv;
using namespace std;


namespace cv{
namespace ximgproc{

class EstimateCovariance{

public:
    EstimateCovariance(int pr_, int pc_);
    ~EstimateCovariance();

    void computeEstimateCovariance(Mat inputData,Mat outputData);
    int combinationCount();

private:
    typedef struct {
        int mult1r;
        int mult1c;
        int mult2r;
        int mult2c;
        int type2; // 0 - for the first P*P. 1 - for the next (P-1)(P-1)
        int id;
    } Combination;

    void initInternalDataStructures();
    void buildCombinationsTable();

    void iterateCombinations(Mat inputData,Mat outputData);
    void computeOneCombination(int comb_id, Mat inputData , Mat outputData,
85
        Mat outputVector,std::vector<int> finalMatPosR, std::vector<int> finalMatPosC);
86 87 88 89 90 91 92 93 94 95 96 97 98 99

    inline void complexSubtract(std::complex<float>& src, std::complex<float>& dst){dst-=src;}
    inline void complexAdd(std::complex<float>& src, std::complex<float>& dst){dst+=src;}
    inline void complexConjMulAndAdd(std::complex<float>& a, std::complex<float>& b,
            std::complex<float>& dst){dst += a*b;}
    inline void complexConjMul(std::complex<float>& a, std::complex<float>& b,
            std::complex<float>& dst){dst = a*b;}

private:
    int nr;
    int nc;
    int pr;
    int pc;

100
    std::vector<Combination> combinationsTable;
101 102 103 104 105 106 107 108 109
};



EstimateCovariance::EstimateCovariance(int pr_, int pc_){
    pr=pr_; pc=pc_;
}

EstimateCovariance::~EstimateCovariance(){
110

111 112 113 114
}

void EstimateCovariance::initInternalDataStructures(){
    int combCount = combinationCount();
115
    combinationsTable.resize(combCount);
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167
    buildCombinationsTable();
}

int EstimateCovariance::combinationCount(){
    return  (pr*pc+(pr-1)*(pc-1));
}

void EstimateCovariance::buildCombinationsTable()
{
    int idx_row,idx_col;
    int comb_idx = 0;
    Combination comb;

    // The first element of the product is [0,0] and the second is to down and to the right of it.
    for (idx_col=0; idx_col<pc; ++idx_col)  {
        for (idx_row=0; idx_row<pr; ++idx_row)      {
            comb.mult1r=0;
            comb.mult1c=0;
            comb.mult2r=idx_row;
            comb.mult2c=idx_col;
            comb.type2 = 0;
            comb.id = comb_idx;
            memcpy(&combinationsTable[comb_idx++], &comb, sizeof(Combination));
        }
    }

    // The first element is on the top right, and the second element is to the left and down of it.
    for (idx_row=1; idx_row<pr; ++idx_row)  {
        for (idx_col=1; idx_col<pc; ++idx_col)      {
            comb.mult1r=idx_row;
            comb.mult1c=0;
            comb.mult2r=0;
            comb.mult2c=idx_col;

            comb.type2 = 1;
            comb.id = comb_idx;
            memcpy(&combinationsTable[comb_idx++], &comb, sizeof(Combination));
        }
    }
}

void EstimateCovariance::computeEstimateCovariance(Mat inputData,Mat outputData){
    initInternalDataStructures();
    nr=inputData.rows;
    nc=inputData.cols;

    iterateCombinations(inputData,outputData);
}


void EstimateCovariance::iterateCombinations(Mat inputData,Mat outputData)
{
Oded Green's avatar
Oded Green committed
168 169 170 171 172 173 174 175 176 177 178 179 180
    Mat outputVector(pr*pc,1,  DataType<std::complex<float> >::type);

    std::vector<int> finalMatPosR(pr*pc,0);
    std::vector<int> finalMatPosC(pr*pc,0);
    int combs=combinationCount();
    for (int idx=0; idx<combs; idx++){
        outputVector.setTo(Scalar(0,0));
        for (unsigned x=0; x<finalMatPosR.size(); x++)
            finalMatPosR[x]=0;
        for (unsigned x=0; x<finalMatPosC.size(); x++)
            finalMatPosC[x]=0;
        computeOneCombination(idx++, inputData, outputData,
                outputVector,finalMatPosR, finalMatPosC);
181 182 183 184
    }
}

void EstimateCovariance::computeOneCombination(int comb_id,Mat inputData, Mat outputData,
185
            Mat outputVector,std::vector<int> finalMatPosR, std::vector<int> finalMatPosC)
186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220
{
    Combination* comb = &combinationsTable[comb_id];
    int type2 = comb->type2;
    int deltaR = (int)abs((int)(comb->mult1r-comb->mult2r));
    int deltaC = (int)abs((int)(comb->mult1c-comb->mult2c));
    int numElementsInBlock = pr-abs(deltaR);
    int numBlocks = pc-deltaC;
    const int DR= nr-pr;
    const int DC= nc-pc;

    int elementC=0;
    std::complex<float> temp_res = std::complex<float>(0,0);
    int i,j,r,c;

    if (!type2) {
        // Computing the first index of the combination.
        // This index is made up
        for(i=0; i<=( DR); i++) {
            int iPdr=i+deltaR;
            for(j=0; j<= (DC); j++) {
                int jPdc = j+deltaC;
                complexConjMulAndAdd(inputData.at<std::complex<float> >(i,j), inputData.at<std::complex<float> >(iPdr,jPdc), temp_res);
            }
        }
    }else{
        // Computing the first index of the combination.
        for(i=0; i<=( DR); i++) {
            int iPdr=i+deltaR;
            for(j=0; j<= (DC); j++) {
                int jPdc = j+deltaC;
                complexConjMulAndAdd(inputData.at<std::complex<float> >(iPdr,j), inputData.at<std::complex<float> >(i,jPdc), temp_res);
            }
        }
    }
    outputVector.at<std::complex<float> >(0,0) = temp_res;
221

222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303
    // Checking if the first element belongs to the first set of combinatons.
    // The combination that the first element is above the second.
    if (!type2) {
        finalMatPosR[0]=0;
        finalMatPosC[0]=pr*deltaC+deltaR;
        elementC++;
    }else{
        finalMatPosR[0]=deltaR;
        finalMatPosC[0]=pr*deltaC;
        elementC++;
    }

    for(r=1;r<(pr-deltaR);r++){
        std::complex<float> newRowSum = std::complex<float>(0,0),oldRowSum = std::complex<float>(0,0);
        std::complex<float> addRows  = std::complex<float>(0,0);

        int rM1 = r-1;
        int k = DR+1 + rM1;
        int kPdr = k+deltaR;
        int rM1Pdr = rM1 + deltaR;
        int cPdc;

        if (!type2) {
            for(c=0;c<=(DC); c++){
                cPdc = c+deltaC;
                complexConjMulAndAdd(inputData.at<std::complex<float> >(k,c),inputData.at<std::complex<float> >(kPdr,cPdc),newRowSum);
                complexConjMulAndAdd(inputData.at<std::complex<float> >(rM1,c),inputData.at<std::complex<float> >(rM1Pdr,cPdc),oldRowSum);
            }
        }else{
            for(c=0;c<=(DC); c++){
                cPdc = c+deltaC;
                complexConjMulAndAdd(inputData.at<std::complex<float> >(kPdr,c),inputData.at<std::complex<float> >(k,cPdc),newRowSum);
                complexConjMulAndAdd(inputData.at<std::complex<float> >(rM1Pdr,c),inputData.at<std::complex<float> >(rM1,cPdc),oldRowSum);
            }
        }
        complexAdd(newRowSum,addRows);
        complexSubtract(oldRowSum,addRows);
        complexAdd(outputVector.at<std::complex<float> >(rM1,0),outputVector.at<std::complex<float> >(r,0));
        complexAdd(addRows,outputVector.at<std::complex<float> >(r,0));

        finalMatPosR[elementC]=finalMatPosR[elementC-1]+1;;
        finalMatPosC[elementC]=finalMatPosC[elementC-1]+1;
        elementC++;
    }

    for(c=1; c<numBlocks; c++)  {
        std::complex<float> newColSum = std::complex<float>(0,0),oldColSum = std::complex<float>(0,0);
        std::complex<float> addCols  = std::complex<float>(0,0);

        // Index arithmetic
        int cM1 = c-1;
        int dcPc = DC+c;
        int q = DC+1 + cM1;
        int cM1PdeltaC = cM1 + deltaC;
        int dcPcPdeltaC = dcPc+deltaC;

        if (!type2) {
            for(r=0;r<=(DR); r++){
                int rPdr = r+deltaR;
                complexConjMulAndAdd(inputData.at<std::complex<float> >(r,q),inputData.at<std::complex<float> >(rPdr,q+deltaC),newColSum);
                complexConjMulAndAdd(inputData.at<std::complex<float> >(r,cM1),inputData.at<std::complex<float> >(rPdr,cM1+deltaC),oldColSum);
            }
        }else{
            for(r=0;r<=(DR); r++){
                int rPdr = r+deltaR;
                complexConjMulAndAdd(inputData.at<std::complex<float> >(rPdr,q),inputData.at<std::complex<float> >(r,q+deltaC),newColSum);
                complexConjMulAndAdd(inputData.at<std::complex<float> >(rPdr,cM1),inputData.at<std::complex<float> >(r,cM1+deltaC),oldColSum);
            }
        }
        complexAdd(newColSum,addCols);
        complexSubtract(oldColSum,addCols);
        complexAdd(outputVector.at<std::complex<float> >((c-1)*numElementsInBlock,0),outputVector.at<std::complex<float> >(c*numElementsInBlock,0));
        complexAdd(addCols,outputVector.at<std::complex<float> >(c*numElementsInBlock,0));

        finalMatPosR[elementC]=finalMatPosR[elementC-numElementsInBlock]+pr;
        finalMatPosC[elementC]=finalMatPosC[elementC-numElementsInBlock]+pr;
        elementC++;

        for(r=1; r<numElementsInBlock; r++) {
            std::complex<float> w = std::complex<float>(0,0),x = std::complex<float>(0,0),
                y = std::complex<float>(0,0),z = std::complex<float>(0,0),deltaRowSum = std::complex<float>(0,0);
            std::complex<float> tempRes = std::complex<float>(0,0);
304
            // Index arithmetic
305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353
            int rM1 = r-1;
            int drPr = DR+r;
            int rM1PdeltaR = rM1 + deltaR;
            int drPrPdeltaR = drPr+deltaR;

            if (!type2) {
                complexConjMul(inputData.at<std::complex<float> >(rM1,cM1),inputData.at<std::complex<float> >(rM1PdeltaR,cM1PdeltaC),w);
                complexConjMul(inputData.at<std::complex<float> >(rM1,dcPc),inputData.at<std::complex<float> >(rM1PdeltaR,dcPcPdeltaC),x);
                complexConjMul(inputData.at<std::complex<float> >(drPr,cM1),inputData.at<std::complex<float> >(drPrPdeltaR,cM1PdeltaC),y);
                complexConjMul(inputData.at<std::complex<float> >(drPr,dcPc),inputData.at<std::complex<float> >(drPrPdeltaR,dcPcPdeltaC),z);
            }else{
                complexConjMul(inputData.at<std::complex<float> >(rM1PdeltaR,cM1),inputData.at<std::complex<float> >(rM1,cM1PdeltaC),w);
                complexConjMul(inputData.at<std::complex<float> >(rM1PdeltaR,dcPc),inputData.at<std::complex<float> >(rM1,dcPcPdeltaC),x);
                complexConjMul(inputData.at<std::complex<float> >(drPrPdeltaR,cM1),inputData.at<std::complex<float> >(drPr,cM1PdeltaC),y);
                complexConjMul(inputData.at<std::complex<float> >(drPrPdeltaR,dcPc),inputData.at<std::complex<float> >(drPr,dcPcPdeltaC),z);
            }
            complexAdd(w,tempRes);
            complexSubtract(x,tempRes);
            complexSubtract(y,tempRes);
            complexAdd(z,tempRes);

            complexAdd(outputVector.at<std::complex<float> >((c-1)*numElementsInBlock + r,0),deltaRowSum);
            complexSubtract(outputVector.at<std::complex<float> >((c-1)*numElementsInBlock+ rM1,0),deltaRowSum);

            complexAdd(deltaRowSum,tempRes);
            complexAdd(outputVector.at<std::complex<float> >(c*numElementsInBlock+rM1,0),tempRes);
            complexAdd(tempRes,outputVector.at<std::complex<float> >(c*numElementsInBlock+r,0));

            finalMatPosR[elementC]=finalMatPosR[elementC-1]+1;
            finalMatPosC[elementC]=finalMatPosC[elementC-1]+1;
            elementC++;
        }
    }

    for(i=0; i<numElementsInBlock*numBlocks; i++){
        outputData.at<std::complex<float> >(finalMatPosR[i],finalMatPosC[i])=outputVector.at<std::complex<float> >(i,0);
    }
}



void covarianceEstimation(InputArray input_, OutputArray output_,int windowRows, int windowCols){

    CV_Assert( input_.channels() <= 2);   // Does not take color images.

    Mat input;

    Mat temp=input_.getMat();
    if(temp.channels() == 1){
354 355
        temp.convertTo(temp,CV_32FC2);
        Mat zmat = Mat::zeros(temp.size(), CV_32F);
356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374
        Mat twoChannelsbefore[] = {temp,zmat};
        cv::merge(twoChannelsbefore,2,input);
    }else{
        temp.convertTo(input, CV_32FC2);

    }

    EstimateCovariance estCov(windowRows,windowCols);

    //input_.getMat().convertTo(input, CV_32FC2);

    output_.create(windowRows*windowCols,windowRows*windowCols,  DataType<std::complex<float> >::type);

    Mat output = output_.getMat();

    estCov.computeEstimateCovariance(input,output);
}

} // namespace ximgproc
375
} // namespace cv