double f_i(double *X1,double x1,double *X2,double x2, int n,double h11,double h12,double h21,double h22,int i) { double sum = 0; for(int j=0;j<n;j++){ if(j!=i) sum += epanechnikov(X1[j],x1,X2[j],x2,h11,h12,h21,h22); } return sum; }
void computeSpatiogram(const cv::Mat &imagePatch, int m, spatiogram &sg){ int n = 256/m; std::vector<double> bins; linspace(bins, 0, 256, n); int height = imagePatch.rows; int width = imagePatch.cols; cv::Mat X,Y, Xm, Ym, dist2, K; meshgrid(cv::Range(1,width), cv::Range(1,height), X, Y); meshgrid(cv::Range(-width/2,width/2), cv::Range(-height/2,height/2), Xm, Ym); X.convertTo(X, CV_64FC1); Y.convertTo(Y, CV_64FC1); robustnorm(Xm,Ym,dist2); dist2 = dist2(cv::Rect(0,0,width,height)); epanechnikov(dist2, K); // initialize spatiogram sg.C = 1/cv::sum(K)[0]; sg.C = sg.C/(imagePatch.channels()); sg.cd = cv::Mat::zeros(1, m*imagePatch.channels(), CV_64FC1); sg.mu = cv::Mat::zeros(2, m*imagePatch.channels(), CV_64FC1); sg.cm = cv::Mat::zeros(2, m*imagePatch.channels(), CV_64FC1); sg.bins = m*imagePatch.channels(); for (int l=0;l<imagePatch.channels();l++){ for (int j=0;j<m;j++){ cv::Mat temp; binelements(imagePatch, bins, l, j, temp); // zeroth order spatiogram sg.cd.at<double>(0,l*m+j) = cv::sum( K.mul(temp) )[0]; double den = cv::sum(temp)[0]; if (den==0.0) den=std::numeric_limits<double>::epsilon(); if (sg.cd.at<double>(0,l*m+j)==0.0) sg.cd.at<double>(0,l*m+j)=std::numeric_limits<double>::epsilon(); // Mean vector of the pixels' coordinates mu: first order spatiogram double mu_x = cv::sum( X.mul(temp) )[0]/den; double mu_y = cv::sum( Y.mul(temp) )[0]/den; sg.mu.at<double>(0, l*m+j) = mu_x; sg.mu.at<double>(1, l*m+j) = mu_y; // Covariance matrix of the pixels' coordinates Cm[2x2]: 2nd order spatiogram cv::Mat C11; cv::pow(X - mu_x, 2, C11); //(x-mu_x)^2 cv::Mat C22; cv::pow(Y - mu_y, 2, C22); //(y-mu_y)^2 sg.cm.at<double>(0, l*m+j) = cv::sum(C11.mul(temp))[0]/den; //Cov(1,1) sg.cm.at<double>(1, l*m+j) = cv::sum(C22.mul(temp))[0]/den; //Cov(2,2) } } //normalize color distribution sg.cd = sg.C*sg.cd; }