Direct Graphical Models  v.1.7.0
Variance.cpp
1 #include "Variance.h"
2 #include "LinearMapper.h"
3 
4 namespace DirectGraphicalModels { namespace fex
5 {
6 Mat CVariance::get(const Mat &img, SqNeighbourhood nbhd)
7 {
8  int width = img.cols;
9  int height = img.rows;
10 
11  // Converting to one channel image
12  Mat I;
13  if (img.channels() != 1) cvtColor(img, I, cv::ColorConversionCodes::COLOR_RGB2GRAY);
14  else img.copyTo(I);
15 
16  Mat res(img.size(), CV_8UC1);
17  Mat integralImg;
18  integral(I, integralImg);
19 
20  for (int y = 0; y < height; y++) {
21  int y0 = MAX(0, y - nbhd.upperGap);
22  int y1 = MIN(y + nbhd.lowerGap, height -1);
23  byte *pRes = res.ptr<byte>(y);
24  int *pI0 = integralImg.ptr<int>(y0);
25  int *pI1 = integralImg.ptr<int>(y1 + 1);
26  for (int x = 0; x < width; x++) {
27  int x0 = MAX(0, x - nbhd.leftGap);
28  int x1 = MIN(x + nbhd.rightGap, width - 1);
29  int S = (x1 - x0 + 1) * (y1 - y0 + 1);
30  float med = static_cast<float>(pI1[x1 + 1] - pI1[x0] - pI0[x1 + 1] + pI0[x0]) / S;
31  float Sum = 0;
32  for (int j = y0; j <= y1; j++) {
33  byte *pImg = I.ptr<byte>(j);
34  for (int i = x0; i <= x1; i++) {
35  float dif = static_cast<float>(pImg[i]) - med;
36  Sum += dif*dif;
37  } // i
38  } // j
39  float val = sqrtf(Sum / S);
40  pRes[x] = linear_mapper<byte>(val, 0, 100);
41  } // x
42  } // y
43 
44  return res;
45 }
46 } }
int rightGap
Distance from the base point to the neighborhood&#39;s right boundary.
int leftGap
Distance from the base point to the neighborhood&#39;s left boundary.
int lowerGap
Distance from the base point to the neighborhood&#39;s lower boundary.
int upperGap
Distance from the base point to the neighborhood&#39;s upper boundary.
virtual Mat get(void) const
Extracts and returns the required feature.
Definition: Variance.h:25