Direct Graphical Models  v.1.7.0
NDVI.cpp
1 #include "NDVI.h"
2 #include "LinearMapper.h"
3 #include "macroses.h"
4 
5 namespace DirectGraphicalModels { namespace fex
6 {
7 Mat CNDVI::get(const Mat &img, byte midPoint)
8 {
9  DGM_ASSERT_MSG(img.channels() == 3, "Input image has %d channel(s), but must have 3.", img.channels());
10  Mat res(img.size(), CV_8UC1);
11  vec_mat_t vChannels;
12  split(img, vChannels);
13 
14  for (int y = 0; y < res.rows; y++) {
15  byte *pRes = res.ptr<byte>(y);
16  byte *pR = vChannels.at(2).ptr<byte>(y);
17  byte *pG = vChannels.at(1).ptr<byte>(y);
18  byte *pB = vChannels.at(0).ptr<byte>(y);
19  for (int x = 0; x < res.cols; x++) {
20  float nir = static_cast<float>(pR[x]);
21  float vis = 0.5f * (static_cast<float>(pG[x]) + static_cast<float>(pB[x]));
22  float ndvi = (nir + vis > 0) ? (nir - vis) / (nir + vis) : 0;
23 
24  pRes[x] = two_linear_mapper<byte>(ndvi, -1.0f, 1.0f, 0.0f, midPoint);
25  }
26  } // y
27 
28  return res;
29 }
30 } }
virtual Mat get(void) const
Extracts and returns the required feature.
Definition: NDVI.h:24