-
Notifications
You must be signed in to change notification settings - Fork 25
/
disparity-daisy.cpp
160 lines (146 loc) · 3.91 KB
/
disparity-daisy.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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include "daisy/daisy.h"
using namespace cv;
using namespace std;
using namespace kutility;
int im_width, im_height;
Mat leftDesc, rightDesc;
int w = 2;
bool inImg(int x, int y) {
if (x >= 0 && x < im_width && y >= 0 && y < im_height)
return true;
}
double descCost(Point leftpt, Point rightpt, int w) {
double cost = 0;
for (int j = -w; j <= w; j++) {
for (int k = -w; k <= w; k++) {
if (!inImg(leftpt.x+j, leftpt.y+k) ||
!inImg(rightpt.x+j, rightpt.y+k))
continue;
int idxl = (leftpt.x+j)+(leftpt.y+k)*im_width;
int idxr = (rightpt.x+j)+(rightpt.y+k)*im_width;
cost += norm(leftDesc.row(idxl), rightDesc.row(idxr), CV_L1);
}
}
return cost;
}
int getCorresPointRight(Point p, int ndisp) {
double minCost = 1e9;
int chosen_i = 0;
for (int i = p.x-ndisp; i <= p.x; i++) {
double cost = descCost(p, Point(i,p.y), w);
if (cost < minCost) {
minCost = cost;
chosen_i = i;
}
}
if (fabs(minCost) < 0.00001)
return p.x;
return chosen_i;
}
int getCorresPointLeft(Point p, int ndisp) {
double minCost = 1e9;
int chosen_i = 0;
for (int i = p.x; i <= p.x+ndisp; i++) {
double cost = descCost(Point(i,p.y), p, w);
if (cost < minCost) {
minCost = cost;
chosen_i = i;
}
}
if (fabs(minCost) < 0.00001)
return p.x;
return chosen_i;
}
void computeDisparityMap(int ndisp, Mat &img_disp) {
img_disp = Mat(im_height, im_width, CV_8UC1, Scalar(0));
for (int i = ndisp+1; i < im_width; i++) {
for (int j = 0; j < im_height; j++) {
cout << i << ", " << j << endl;
int right_i = getCorresPointRight(Point(i,j), ndisp);
// left-right check
int left_i = getCorresPointLeft(Point(right_i,j), ndisp);
if (abs(left_i-i) > 4)
continue;
int disparity = abs(i - right_i);
img_disp.at<uchar>(j,i) = disparity;
}
}
}
void computeDenseDesc(const Mat &img, Mat &descrpOut) {
// daisy params
int verbose_level = 4;
int rad = 20;
int radq = 5;
int thq = 8;
int histq = 8;
int nrm_type = NRM_FULL;
bool disable_interpolation = false;
// associate pointer
uchar *im = img.data;
int h = img.rows;
int w = img.cols;
daisy* desc = new daisy();
desc->set_image(im, h, w);
desc->verbose(verbose_level);
desc->set_parameters(rad, radq, thq, histq);
desc->set_normalization(NRM_FULL);
desc->initialize_single_descriptor_mode();
desc->compute_descriptors();
desc->normalize_descriptors();
int iy, ix, descSize;
descSize = desc->descriptor_size();
descrpOut.create(h*w, descSize, CV_32FC1);
for (iy=0; iy<h; ++iy)
{
for (ix=0; ix<w; ++ix)
{
float* thor = NULL;
desc->get_descriptor(iy, ix, thor);
memcpy(descrpOut.ptr(iy*w+ix), thor, descSize*sizeof(float));
}
}
}
void preprocess(Mat& img) {
Mat dst;
bilateralFilter(img, dst, 10, 15, 15);
img = dst.clone();
}
void normalizeDisparity(Mat &disp) {
int max_disp = -1;
for (int i = 0; i < disp.cols; i++) {
for (int j = 0; j < disp.rows; j++) {
if ((int)disp.at<uchar>(j,i) > max_disp)
max_disp = disp.at<uchar>(j,i);
}
}
for (int i = 0; i < disp.cols; i++) {
for (int j = 0; j < disp.rows; j++) {
disp.at<uchar>(j,i) *= (255. / (float)max_disp);
}
}
}
int main(int argc, char** argv) {
/*
Mat imgL = imread(argv[1], CV_LOAD_IMAGE_GRAYSCALE);
Mat imgR = imread(argv[2], CV_LOAD_IMAGE_GRAYSCALE);
//preprocess(imgL);
//preprocess(imgR);
im_width = imgL.cols;
im_height = imgL.rows;
computeDenseDesc(imgL, leftDesc);
computeDenseDesc(imgR, rightDesc);
Mat disp;
computeDisparityMap(40, disp);
imshow("left", imgL);
imshow("disp", disp);
imwrite(argv[3], disp);
waitKey(0);
*/
Mat disp = imread(argv[1], CV_LOAD_IMAGE_GRAYSCALE);
normalizeDisparity(disp);
imwrite(argv[1], disp);
return 0;
}