1 #include "SparseCoding.h" 2 #include "SparseDictionary.h" 3 #include "LinearMapper.h" 10 const word nWords = D.rows;
11 DGM_ASSERT_MSG(nWords <= CV_CN_MAX,
"The number of words %d exceeds the maximal allowed number of channels %d. Use get_v() function instead.", nWords, CV_CN_MAX);
14 vec_mat_t vFeatures =
get_v(img, D, nbhd);
15 merge(vFeatures, res);
21 DGM_ASSERT_MSG(!D.empty(),
"The dictionary must me trained or loaded before using this function");
23 const word nWords = D.rows;
24 const int blockSize =
static_cast<int>(sqrt(D.cols));
25 const int dataWidth = img.cols - blockSize + 1;
26 const int dataHeight = img.rows - blockSize + 1;
32 int normalizer = (X.depth() == CV_8U) ? 255 : 65535;
34 vec_mat_t res(nWords);
35 for (word w = 0; w < nWords; w++)
36 res[w] = Mat(img.size(), CV_8UC1, cv::Scalar(0));
39 concurrency::parallel_for(0, dataHeight, 1, [&](
int y) {
41 for (
int y = 0; y < dataHeight; y++) {
44 for (
int x = 0; x < dataWidth; x += 1) {
45 int s = y * dataWidth + x;
46 Mat sample = X.row(s);
47 sample.convertTo(sample, CV_32FC1, 1.0 / normalizer);
49 gemm(D, sample.t(), 1.0, Mat(), 0.0, _W);
51 for (
int w = 0; w < W.cols; w++)
52 W.col(w) /= norm(D.row(w), NORM_L2);
56 for (word w = 0; w < nWords; w++)
57 res[w].at<byte>(y + nbhd.
upperGap, x + nbhd.
leftGap) = linear_mapper<byte>(W.at<
float>(0, w), -1.0f, 1.0f);
int rightGap
Distance from the base point to the neighborhood's right boundary.
int leftGap
Distance from the base point to the neighborhood's left boundary.
static void calculate_W(const Mat &X, const Mat &D, Mat &W, float lambda, float epsilon, unsigned int nIt=800, float lRate=SC_LRATE_W)
Evaluates weighting coefficients matrix .
const float SC_LRATE_W
Learning rate (speed) for weights .
const float SC_EPSILON
: L1-regularisation epsilon
virtual Mat get(void) const
Extracts and returns the required feature.
int lowerGap
Distance from the base point to the neighborhood's lower boundary.
static Mat img2data(const Mat &img, int blockSize, float varianceThreshold=0.0f)
Converts image into data .
int upperGap
Distance from the base point to the neighborhood's upper boundary.
static vec_mat_t get_v(const Mat &img, const Mat &D, SqNeighbourhood nbhd=sqNeighbourhood(3))
Extracts the sparse coding feature.
Square neighborhood structure.
const float SC_LAMBDA
: L1-regularisation parameter (on features)