LLCはOpenCVのC+ソースに基づいて実現します。
2644 ワード
「Locality-constraind Liner Coding for Image Class ification」の作者はMatlabのコードを実現します。http://www.ifp.illinois.edu/~in 29/LLC.httm
以下は著者のコードに基づいて、OpenCVに基づいて、C++版のLLCを実現します。
以下は著者のコードに基づいて、OpenCVに基づいて、C++版のLLCを実現します。
cv::Mat BowModel::findKNN(cv::Mat &codebook, cv::Mat &input, int k) {
int nbase = codebook.rows;
int nquery = input.rows;
Mat ii = input.mul(input);
Mat cc = codebook.mul(codebook);
Mat sii(nquery,1,CV_32FC1);
sii.setTo(0);
Mat scc(nbase,1,CV_32FC1);
scc.setTo(0);
for (int i = 0; i(i,0) += ii.at(i,j);
}
}
for (int i = 0; i(i,0) += cc.at(i,j);
}
}
Mat D(nquery,nbase,CV_32FC1);
for (int i = 0; i(i,j) = sii.at(i,0);
}
}
Mat ct;
transpose(codebook, ct);
Mat D1 = 2*input*ct;
Mat scct;
transpose(scc, scct);
Mat D2(nquery, nbase, CV_32FC1);
for (int i = 0; i(i,j) = scct.at(0,j);
}
}
D = D - D1 + D2;
Mat SD;
sortIdx(D, SD, CV_SORT_EVERY_ROW+CV_SORT_ASCENDING);
Mat IDX(nquery,k,CV_8UC1);
for (int i = 0; i(i,j) = SD.row(i).col(j).at(0,0);
}
}
ii.release();
cc.release();
sii.release();
scc.release();
D.release();
ct.release();
D1.release();
scct.release();
D2.release();
SD.release();
return IDX;
}
cv::Mat BowModel::llccode(cv::Mat &codebook, cv::Mat &input, cv::Mat IDX, int k)
{
int nquery = input.rows;
int nbase = codebook.rows;
int dim = codebook.cols;
Mat II = Mat::eye(k, k, CV_32FC1);
Mat Coeff(nquery,nbase,CV_32FC1);
Coeff.setTo(0);
Mat z;
Mat z1(k,dim,CV_32FC1);
Mat z2(k,dim,CV_32FC1);
Mat C;
Mat un(k,1,CV_32FC1);
un.setTo(1);
Mat temp;
Mat temp2;
Mat w;
Mat wt;
for (int n = 0; n(i,j) = codebook.at(IDX.at(n,i),j);
z2.at(i,j) = input.at(n,j);
}
}
z = z1 - z2;
transpose(z, temp);
C = z*temp;
C = C + II*(1e-4)*trace(C)[0];
invert(C,temp2);
w = temp2*un;
float sum_w=0;
for (int i = 0; i(i,0);
}
w = w/sum_w;
transpose(w, wt);
for (int i = 0; i(n,IDX.at(n,i)) = wt.at(0,i);
}
}
II.release();
z.release();
z1.release();
z2.release();
C.release();
un.release();
temp.release();
temp2.release();
w.release();
wt.release();
return Coeff;
}