-
Notifications
You must be signed in to change notification settings - Fork 0
/
CenCC.cpp
138 lines (129 loc) · 3.64 KB
/
CenCC.cpp
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
#include "CenCC.h"
void buildCV(const Mat& lImg, const Mat& rImg, const int maxDis, Mat* costVol)
{
// for TAD + Grd input image must be CV_64FC3
CV_Assert(lImg.type() == CV_64FC3 && rImg.type() == CV_64FC3);
int hei = lImg.rows;
int wid = lImg.cols;
Mat lGray, rGray;
Mat tmp;
lImg.convertTo(tmp, CV_32F);
cvtColor(tmp, lGray, CV_RGB2GRAY);
lGray.convertTo(lGray, CV_8U, 255);
rImg.convertTo(tmp, CV_32F);
cvtColor(tmp, rGray, CV_RGB2GRAY);
rGray.convertTo(rGray, CV_8U, 255);
// prepare binary code
int H_WD = CENCUS_WND / 2;
bitset<CENCUS_BIT>* lCode = new bitset<CENCUS_BIT>[wid * hei];
bitset<CENCUS_BIT>* rCode = new bitset<CENCUS_BIT>[wid * hei];
bitset<CENCUS_BIT>* pLCode = lCode;
bitset<CENCUS_BIT>* pRCode = rCode;
for (int y = 0; y < hei; y++) {
uchar* pLData = (uchar*)(lGray.ptr<uchar>(y));
uchar* pRData = (uchar*)(rGray.ptr<uchar>(y));
for (int x = 0; x < wid; x++) {
int bitCnt = 0;
for (int wy = -H_WD; wy <= H_WD; wy++) {
int qy = (y + wy + hei) % hei;
uchar* qLData = (uchar*)(lGray.ptr<uchar>(qy));
uchar* qRData = (uchar*)(rGray.ptr<uchar>(qy));
for (int wx = -H_WD; wx <= H_WD; wx++) {
if (wy != 0 || wx != 0) {
int qx = (x + wx + wid) % wid;
(*pLCode)[bitCnt] = (pLData[x] > qLData[qx]);
(*pRCode)[bitCnt] = (pRData[x] > qRData[qx]);
bitCnt++;
}
}
}
pLCode++;
pRCode++;
}
}
// build cost volume
bitset<CENCUS_BIT> lB;
bitset<CENCUS_BIT> rB;
pLCode = lCode;
for (int y = 0; y < hei; y++) {
int index = y * wid;
for (int x = 0; x < wid; x++) {
lB = *pLCode;
for (int d = 0; d < maxDis; d++) {
double* cost = (double*)costVol[d].ptr<double>(y);
cost[x] = CENCUS_BIT;
if (x - d >= 0) {
rB = rCode[index + x - d];
cost[x] = (lB ^ rB).count();
}
}
pLCode++;
}
}
delete[] lCode;
delete[] rCode;
}
void buildRightCV(const Mat& lImg, const Mat& rImg, const int maxDis, Mat* rCostVol)
{
// for TAD + Grd input image must be CV_64FC3
CV_Assert(lImg.type() == CV_64FC3 && rImg.type() == CV_64FC3);
int hei = lImg.rows;
int wid = lImg.cols;
Mat lGray, rGray;
Mat tmp;
lImg.convertTo(tmp, CV_32F);
cvtColor(tmp, lGray, CV_RGB2GRAY);
lGray.convertTo(lGray, CV_8U, 255);
rImg.convertTo(tmp, CV_32F);
cvtColor(tmp, rGray, CV_RGB2GRAY);
rGray.convertTo(rGray, CV_8U, 255);
// prepare binary code
int H_WD = CENCUS_WND / 2;
bitset<CENCUS_BIT>* lCode = new bitset<CENCUS_BIT>[wid * hei];
bitset<CENCUS_BIT>* rCode = new bitset<CENCUS_BIT>[wid * hei];
bitset<CENCUS_BIT>* pLCode = lCode;
bitset<CENCUS_BIT>* pRCode = rCode;
for (int y = 0; y < hei; y++) {
uchar* pLData = (uchar*)(lGray.ptr<uchar>(y));
uchar* pRData = (uchar*)(rGray.ptr<uchar>(y));
for (int x = 0; x < wid; x++) {
int bitCnt = 0;
for (int wy = -H_WD; wy <= H_WD; wy++) {
int qy = (y + wy + hei) % hei;
uchar* qLData = (uchar*)(lGray.ptr<uchar>(qy));
uchar* qRData = (uchar*)(rGray.ptr<uchar>(qy));
for (int wx = -H_WD; wx <= H_WD; wx++) {
if (wy != 0 || wx != 0) {
int qx = (x + wx + wid) % wid;
(*pLCode)[bitCnt] = (pLData[x] > qLData[qx]);
(*pRCode)[bitCnt] = (pRData[x] > qRData[qx]);
bitCnt++;
}
}
}
pLCode++;
pRCode++;
}
}
// build cost volume
bitset<CENCUS_BIT> lB;
bitset<CENCUS_BIT> rB;
pRCode = rCode;
for (int y = 0; y < hei; y++) {
int index = y * wid;
for (int x = 0; x < wid; x++) {
rB = *pRCode;
for (int d = 0; d < maxDis; d++) {
double* cost = (double*)rCostVol[d].ptr<double>(y);
cost[x] = CENCUS_BIT;
if (x + d < wid) {
lB = lCode[index + x + d];
cost[x] = (rB ^ lB).count();
}
}
pRCode++;
}
}
delete[] lCode;
delete[] rCode;
}