-
Notifications
You must be signed in to change notification settings - Fork 1
/
interpPix.hpp
97 lines (77 loc) · 2.09 KB
/
interpPix.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
#ifndef INTERPPIX_HPP
#define INTERPPIX_HPP
#include <math.h>
#include <vector>
#include <stdlib.h>
#ifdef __linux__
#include <highgui/highgui.hpp>
#elif _WIN32
#include <highgui\highgui.hpp>
#endif
#include "Matrix.hpp"
typedef std::vector< std::vector<double> > VEC_VEC;
double interpPix(double rPos, double cPos, const cv::Mat& ipImg)
{
if (rPos < 0)
{
rPos = 0;
}
else if (cPos < 0)
{
cPos = 0;
}
int rowTopL = int(std::floor(rPos));
int colTopL = int(std::floor(cPos));
int rowBotL = 0, colBotL = 0, rowBotR = 0, colBotR = 0, rowTopR = 0, colTopR = 0;
if (rPos >= (ipImg.rows-1))
{
rowBotL = rowTopL;
rowBotR = rowTopL;
}
else
{
rowBotL = rowTopL + 1;
rowBotR = rowTopL + 1;
}
if (cPos >= (ipImg.cols-1))
{
colTopR = colTopL;
colBotR = colTopL;
}
else
{
colTopR = colTopL + 1;
colBotR = colTopL + 1;
}
#if 1 //bilinear interpolation according to wikipedia
double x = cPos-colBotL;
double y = rowBotL - rPos;
std::vector<double> lVec(2);
std::vector<double> rVec(2);
std::vector< std::vector<double> > pixMat(2);
lVec[0] = 1 - x;
lVec[1] = x;
rVec[0] = 1 - y;
rVec[1] = y;
std::vector<double> pixVec(2);
pixVec[0] = static_cast<double>(ipImg.at<uchar>(rowBotL,colBotL));
pixVec[1] = static_cast<double>(ipImg.at<uchar>(rowBotR,colBotR));
pixMat[0] = pixVec;
pixVec[0] = static_cast<double>(ipImg.at<uchar>(rowTopL,colTopL));
pixVec[1] = static_cast<double>(ipImg.at<uchar>(rowTopR,colTopR));
pixMat[1] = pixVec;
rVec = pixMat*rVec;
double c = 0;
for (int i = 0; i != rVec.size(); ++i)
{
c += lVec[i]*rVec[i];
}
return c;
#else
return (static_cast<double>(ipImg.at<uchar>(rowBotL,colBotL)) + \
static_cast<double>(ipImg.at<uchar>(rowBotR,colBotR)) + \
static_cast<double>(ipImg.at<uchar>(rowTopL,colTopL)) + \
static_cast<double>(ipImg.at<uchar>(rowTopR,colTopR)))/4;
#endif
}
#endif //INTERPPIX_HPP