|
1 |
| -#ifndef CLEANTOOL |
2 |
| -#define CLEANTOOL |
3 |
| -#include <opencv2/core/core.hpp> |
4 |
| -#include <opencv2/imgproc/imgproc.hpp> |
5 |
| -#include <opencv2/highgui/highgui.hpp> |
6 |
| -#include <iostream> |
7 |
| -using namespace cv; |
8 |
| -using namespace std; |
9 |
| -float fcxcy[3];//SET THE CAMERA PARAMETERS F CX CY BEFORE USE calplanenormal. |
10 |
| -int WINDOWSIZE=15;//SET SEARCH WINDOWSIZE(SUGGEST 15) BEFORE USE calplanenormal. |
11 |
| -float Tthrehold;//SET THE threshold (SUGGEST 0.1-0.2)BEFORE USE calplanenormal. |
12 |
| -// Ax+by+cz=D |
13 |
| -void |
14 |
| -CallFitPlane(const Mat& depth,int * points,int i,int j,float *plane12) { |
15 |
| - float f =fcxcy[0]; |
16 |
| - float cx=fcxcy[1]; |
17 |
| - float cy=fcxcy[2]; |
18 |
| - vector<float>X_vector; |
19 |
| - vector<float>Y_vector; |
20 |
| - vector<float>Z_vector; |
21 |
| - for(int num_point=0; num_point<WINDOWSIZE*WINDOWSIZE;num_point++ ) |
22 |
| - if (points[num_point]==1) {//search 已经处理了边界,此处不需要再处理了 |
23 |
| - int point_i,point_j; |
24 |
| - point_i=floor(num_point/WINDOWSIZE); |
25 |
| - point_j=num_point-(point_i*WINDOWSIZE); |
26 |
| - point_i+=i-int(WINDOWSIZE/2);point_j+=j-int(WINDOWSIZE/2); |
27 |
| - float x = (point_j - cx) * depth.at<float>(point_i, point_j ) * 1.0 / f; |
28 |
| - float y = (point_i - cy) * depth.at<float>(point_i, point_j )* 1.0 / f; |
29 |
| - float z = depth.at<float>(point_i,point_j); |
30 |
| - X_vector.push_back(x); |
31 |
| - Y_vector.push_back(y); |
32 |
| - Z_vector.push_back(z); |
33 |
| - } |
34 |
| - CvMat*points_mat = cvCreateMat(X_vector.size(), 3, CV_32FC1);//定义用来存储需要拟合点的矩阵 |
35 |
| - if(X_vector.size()<3){ plane12[0]=-1;plane12[1]=-1;plane12[2]=-1;plane12[3]=-1;return;} |
36 |
| - for (int ii=0;ii < X_vector.size(); ++ii){ |
37 |
| - points_mat->data.fl[ii * 3 + 0] = X_vector[ii];//矩阵的值进行初始化 X的坐标值 |
38 |
| - points_mat->data.fl[ii * 3 + 1] = Y_vector[ii];// Y的坐标值 |
39 |
| - points_mat->data.fl[ii * 3 + 2] = Z_vector[ii];// |
40 |
| - } |
41 |
| - // float plane12[4] = { 0 };//定义用来储存平面参数的数组 |
42 |
| - cvFitPlane(points_mat, plane12);//调用方程 |
43 |
| - if( telldirection(plane12,i,j,depth.at<float>(i,j)) ){ |
44 |
| - plane12[0]=-plane12[0]; |
45 |
| - plane12[1]=-plane12[1]; |
46 |
| - plane12[2]=-plane12[2];} |
47 |
| - X_vector.clear(); |
48 |
| - Y_vector.clear(); |
49 |
| - Z_vector.clear(); |
50 |
| - cvReleaseMat(&points_mat); |
51 |
| -} |
52 |
| - |
53 |
| -void |
54 |
| -cvFitPlane(const CvMat* points, float* plane){ |
55 |
| - // Estimate geometric centroid. |
56 |
| - int nrows = points->rows; |
57 |
| - int ncols = points->cols; |
58 |
| - int type = points->type; |
59 |
| - CvMat* centroid = cvCreateMat(1, ncols, type); |
60 |
| - cvSet(centroid, cvScalar(0)); |
61 |
| - for (int c = 0; c<ncols; c++){ |
62 |
| - for (int r = 0; r < nrows; r++) |
63 |
| - { |
64 |
| - centroid->data.fl[c] += points->data.fl[ncols*r + c]; |
65 |
| - } |
66 |
| - centroid->data.fl[c] /= nrows; |
67 |
| - } |
68 |
| - // Subtract geometric centroid from each point. |
69 |
| - CvMat* points2 = cvCreateMat(nrows, ncols, type); |
70 |
| - for (int r = 0; r<nrows; r++) |
71 |
| - for (int c = 0; c<ncols; c++) |
72 |
| - points2->data.fl[ncols*r + c] = points->data.fl[ncols*r + c] - centroid->data.fl[c]; |
73 |
| - // Evaluate SVD of covariance matrix. |
74 |
| - CvMat* A = cvCreateMat(ncols, ncols, type); |
75 |
| - CvMat* W = cvCreateMat(ncols, ncols, type); |
76 |
| - CvMat* V = cvCreateMat(ncols, ncols, type); |
77 |
| - cvGEMM(points2, points, 1, NULL, 0, A, CV_GEMM_A_T); |
78 |
| - cvSVD(A, W, NULL, V, CV_SVD_V_T); |
79 |
| - // Assign plane coefficients by singular vector corresponding to smallest singular value. |
80 |
| - plane[ncols] = 0; |
81 |
| - for (int c = 0; c<ncols; c++){ |
82 |
| - plane[c] = V->data.fl[ncols*(ncols - 1) + c]; |
83 |
| - plane[ncols] += plane[c] * centroid->data.fl[c]; |
84 |
| - } |
85 |
| - // Release allocated resources. |
86 |
| - cvReleaseMat(¢roid); |
87 |
| - cvReleaseMat(&points2); |
88 |
| - cvReleaseMat(&A); |
89 |
| - cvReleaseMat(&W); |
90 |
| - cvReleaseMat(&V); |
91 |
| -} |
92 |
| -void |
93 |
| -search_plane_neighbor(Mat &img,int i,int j ,float threhold,int* result){ |
94 |
| - int cols =img.cols; |
95 |
| - int rows =img.rows; |
96 |
| - for (int ii=0; ii<WINDOWSIZE*WINDOWSIZE;ii++) |
97 |
| - result[ii]=0; |
98 |
| - float center_depth = img.at<float>(i,j); |
99 |
| - for (int idx=0; idx<WINDOWSIZE;idx++) |
100 |
| - for (int idy=0; idy<WINDOWSIZE;idy++){ |
101 |
| - int rx= i-int(WINDOWSIZE/2)+idx; |
102 |
| - int ry= j-int(WINDOWSIZE/2)+idy; |
103 |
| - if( rx>= rows || ry>=cols )continue; |
104 |
| - if( img.at<float>(rx,ry)==0.0)continue; |
105 |
| - if( abs(img.at<float>(rx,ry)-center_depth)<=Tthrehold*center_depth ) |
106 |
| - result[idx*WINDOWSIZE+idy]=1; |
107 |
| - } |
108 |
| -} |
109 |
| -int |
110 |
| -telldirection(float * abc,int i,int j,float d){ |
111 |
| - float f =fcxcy[0]; |
112 |
| - float cx=fcxcy[1]; |
113 |
| - float cy=fcxcy[2]; |
114 |
| - float x = (j - cx) *d * 1.0 / f; |
115 |
| - float y = (i - cy) *d * 1.0 / f; |
116 |
| - float z = d; |
117 |
| - // Vec3f camera_center=Vec3f(cx,cy,0); |
118 |
| - Vec3f cor = Vec3f(0-x, 0-y, 0-z); |
119 |
| - Vec3f abcline = Vec3f(abc[0],abc[1],abc[2]); |
120 |
| - float corner = cor.dot(abcline); |
121 |
| - // float corner =(cx-x)*abc[0]+(cy-y) *abc[1]+(0-z)*abc[2]; |
122 |
| - if (corner>=0) |
123 |
| - return 1; |
124 |
| - else return 0; |
125 |
| - |
126 |
| -} |
127 |
| -Mat |
128 |
| -calplanenormal(Mat &src){ |
129 |
| - float f =fcxcy[0]; |
130 |
| - float cx=fcxcy[1]; |
131 |
| - float cy=fcxcy[2]; |
132 |
| - Mat normals = Mat::zeros(src.size(),CV_32FC3); |
133 |
| - src.convertTo(src,CV_32FC1); |
134 |
| - src*=1.0; |
135 |
| - int cols =src.cols; |
136 |
| - int rows =src.rows; |
137 |
| - // int plane_points[WINDOWSIZE*WINDOWSIZE]={0}; |
138 |
| - int * plane_points = new int[WINDOWSIZE*WINDOWSIZE]; |
139 |
| - float * plane12 = new float[4]; |
140 |
| - for (int i=0;i< rows;i++) |
141 |
| - for (int j=0;j< cols;j++){ |
142 |
| - //for kitti and nyud test |
143 |
| - if(src.at<float>(i,j)==0.0)continue; |
144 |
| - //for:nyud train |
145 |
| - // if(src.at<float>(i,j)<=4000.0)continue; |
146 |
| - |
147 |
| - search_plane_neighbor(src,i,j,15.0,plane_points); |
148 |
| - CallFitPlane(src,plane_points,i,j,plane12); |
149 |
| - Vec3f d = Vec3f(plane12[0],plane12[1],plane12[2]); |
150 |
| - Vec3f n = normalize(d); |
151 |
| - normals.at<Vec3f>(i, j) = n; |
152 |
| - } |
153 |
| - Mat res = Mat::zeros(src.size(),CV_32FC3); |
154 |
| - for (int i=0;i<rows;i++) |
155 |
| - for (int j=0;j<cols;j++){ |
156 |
| - res.at<Vec3f>(i, j)[0] = -1.0 * normals.at<Vec3f>(i, j)[0]; |
157 |
| - res.at<Vec3f>(i, j)[2] = -1.0 * normals.at<Vec3f>(i, j)[1]; |
158 |
| - res.at<Vec3f>(i, j)[1] = -1.0 * normals.at<Vec3f>(i, j)[2]; |
159 |
| - } |
160 |
| - |
161 |
| - delete[] plane12; |
162 |
| - delete[] plane_points; |
163 |
| - normals.release(); |
164 |
| - for (int i=0;i<rows;i++) |
165 |
| - for (int j=0;j<cols;j++){ |
166 |
| - if(!(res.at<Vec3f>(i, j)[0]==0&&res.at<Vec3f>(i, j)[1]==0&&res.at<Vec3f>(i, j)[2]==0)){ |
167 |
| - res.at<Vec3f>(i, j)[0] += 1.0 ; |
168 |
| - res.at<Vec3f>(i, j)[2] += 1.0 ; |
169 |
| - res.at<Vec3f>(i, j)[1] += 1.0; |
170 |
| - } |
171 |
| - } |
172 |
| - |
173 |
| - res =res * 127.5; |
174 |
| - res.convertTo(res, CV_8UC3); |
175 |
| - cvtColor(res, res, COLOR_BGR2RGB); |
176 |
| - return res; |
177 |
| -} |
178 |
| - |
179 |
| -void |
180 |
| -demo{ |
181 |
| -//set parameters here:fcxcy[0]=0;fcxcy[1]=0;fcxcy[2]=0; |
182 |
| -Mat src=imread(INPUT_FILE_NAME,CV_LOAD_IMAGE_ANYDEPTH); |
183 |
| -Mat res=calplanenormal(src); |
184 |
| -imwrite(OUTPUT_NAME,res); |
185 |
| -} |
186 |
| -#endif |
| 1 | +#ifndef CLEANTOOL |
| 2 | +#define CLEANTOOL |
| 3 | +#include <iostream> |
| 4 | +#include <opencv2/core/core.hpp> |
| 5 | +#include <opencv2/highgui/highgui.hpp> |
| 6 | +#include <opencv2/imgproc/imgproc.hpp> |
| 7 | +using namespace cv; |
| 8 | +using namespace std; |
| 9 | +float fcxcy[3]; // SET THE CAMERA PARAMETERS F CX CY BEFORE USE calplanenormal. |
| 10 | +int WINDOWSIZE = 15; // SET SEARCH WINDOWSIZE(SUGGEST 15) BEFORE USE calplanenormal. |
| 11 | +float Tthrehold; // SET THE threshold (SUGGEST 0.1-0.2)BEFORE USE |
| 12 | + // calplanenormal. |
| 13 | +// Ax+by+cz=D |
| 14 | +void CallFitPlane(const Mat &depth, int *points, int i, int j, float *plane12) { |
| 15 | + float f = fcxcy[0]; |
| 16 | + float cx = fcxcy[1]; |
| 17 | + float cy = fcxcy[2]; |
| 18 | + vector<float> X_vector; |
| 19 | + vector<float> Y_vector; |
| 20 | + vector<float> Z_vector; |
| 21 | + for (int num_point = 0; num_point < WINDOWSIZE * WINDOWSIZE; num_point++) |
| 22 | + if (points[num_point] == 1) { // search 已经处理了边界,此处不需要再处理了 |
| 23 | + int point_i, point_j; |
| 24 | + point_i = floor(num_point / WINDOWSIZE); |
| 25 | + point_j = num_point - (point_i * WINDOWSIZE); |
| 26 | + point_i += i - int(WINDOWSIZE / 2); |
| 27 | + point_j += j - int(WINDOWSIZE / 2); |
| 28 | + float x = (point_j - cx) * depth.at<float>(point_i, point_j) * 1.0 / f; |
| 29 | + float y = (point_i - cy) * depth.at<float>(point_i, point_j) * 1.0 / f; |
| 30 | + float z = depth.at<float>(point_i, point_j); |
| 31 | + X_vector.push_back(x); |
| 32 | + Y_vector.push_back(y); |
| 33 | + Z_vector.push_back(z); |
| 34 | + } |
| 35 | + CvMat *points_mat = cvCreateMat(X_vector.size(), 3, CV_32FC1); //定义用来存储需要拟合点的矩阵 |
| 36 | + if (X_vector.size() < 3) { |
| 37 | + plane12[0] = -1; |
| 38 | + plane12[1] = -1; |
| 39 | + plane12[2] = -1; |
| 40 | + plane12[3] = -1; |
| 41 | + return; |
| 42 | + } |
| 43 | + for (int ii = 0; ii < X_vector.size(); ++ii) { |
| 44 | + points_mat->data.fl[ii * 3 + 0] = X_vector[ii]; //矩阵的值进行初始化 X的坐标值 |
| 45 | + points_mat->data.fl[ii * 3 + 1] = Y_vector[ii]; // Y的坐标值 |
| 46 | + points_mat->data.fl[ii * 3 + 2] = Z_vector[ii]; // |
| 47 | + } |
| 48 | + // float plane12[4] = { 0 };//定义用来储存平面参数的数组 |
| 49 | + cvFitPlane(points_mat, plane12); //调用方程 |
| 50 | + if (telldirection(plane12, i, j, depth.at<float>(i, j))) { |
| 51 | + plane12[0] = -plane12[0]; |
| 52 | + plane12[1] = -plane12[1]; |
| 53 | + plane12[2] = -plane12[2]; |
| 54 | + } |
| 55 | + X_vector.clear(); |
| 56 | + Y_vector.clear(); |
| 57 | + Z_vector.clear(); |
| 58 | + cvReleaseMat(&points_mat); |
| 59 | +} |
| 60 | + |
| 61 | +void cvFitPlane(const CvMat *points, float *plane) { |
| 62 | + // Estimate geometric centroid. |
| 63 | + int nrows = points->rows; |
| 64 | + int ncols = points->cols; |
| 65 | + int type = points->type; |
| 66 | + CvMat *centroid = cvCreateMat(1, ncols, type); |
| 67 | + cvSet(centroid, cvScalar(0)); |
| 68 | + for (int c = 0; c < ncols; c++) { |
| 69 | + for (int r = 0; r < nrows; r++) { |
| 70 | + centroid->data.fl[c] += points->data.fl[ncols * r + c]; |
| 71 | + } |
| 72 | + centroid->data.fl[c] /= nrows; |
| 73 | + } |
| 74 | + // Subtract geometric centroid from each point. |
| 75 | + CvMat *points2 = cvCreateMat(nrows, ncols, type); |
| 76 | + for (int r = 0; r < nrows; r++) |
| 77 | + for (int c = 0; c < ncols; c++) |
| 78 | + points2->data.fl[ncols * r + c] = points->data.fl[ncols * r + c] - centroid->data.fl[c]; |
| 79 | + // Evaluate SVD of covariance matrix. |
| 80 | + CvMat *A = cvCreateMat(ncols, ncols, type); |
| 81 | + CvMat *W = cvCreateMat(ncols, ncols, type); |
| 82 | + CvMat *V = cvCreateMat(ncols, ncols, type); |
| 83 | + cvGEMM(points2, points, 1, NULL, 0, A, CV_GEMM_A_T); |
| 84 | + cvSVD(A, W, NULL, V, CV_SVD_V_T); |
| 85 | + // Assign plane coefficients by singular vector corresponding to smallest |
| 86 | + // singular value. |
| 87 | + plane[ncols] = 0; |
| 88 | + for (int c = 0; c < ncols; c++) { |
| 89 | + plane[c] = V->data.fl[ncols * (ncols - 1) + c]; |
| 90 | + plane[ncols] += plane[c] * centroid->data.fl[c]; |
| 91 | + } |
| 92 | + // Release allocated resources. |
| 93 | + cvReleaseMat(¢roid); |
| 94 | + cvReleaseMat(&points2); |
| 95 | + cvReleaseMat(&A); |
| 96 | + cvReleaseMat(&W); |
| 97 | + cvReleaseMat(&V); |
| 98 | +} |
| 99 | +void search_plane_neighbor(Mat &img, int i, int j, float threhold, int *result) { |
| 100 | + int cols = img.cols; |
| 101 | + int rows = img.rows; |
| 102 | + for (int ii = 0; ii < WINDOWSIZE * WINDOWSIZE; ii++) |
| 103 | + result[ii] = 0; |
| 104 | + float center_depth = img.at<float>(i, j); |
| 105 | + for (int idx = 0; idx < WINDOWSIZE; idx++) |
| 106 | + for (int idy = 0; idy < WINDOWSIZE; idy++) { |
| 107 | + int rx = i - int(WINDOWSIZE / 2) + idx; |
| 108 | + int ry = j - int(WINDOWSIZE / 2) + idy; |
| 109 | + if (rx >= rows || ry >= cols) |
| 110 | + continue; |
| 111 | + if (img.at<float>(rx, ry) == 0.0) |
| 112 | + continue; |
| 113 | + if (abs(img.at<float>(rx, ry) - center_depth) <= Tthrehold * center_depth) |
| 114 | + result[idx * WINDOWSIZE + idy] = 1; |
| 115 | + } |
| 116 | +} |
| 117 | +int telldirection(float *abc, int i, int j, float d) { |
| 118 | + float f = fcxcy[0]; |
| 119 | + float cx = fcxcy[1]; |
| 120 | + float cy = fcxcy[2]; |
| 121 | + float x = (j - cx) * d * 1.0 / f; |
| 122 | + float y = (i - cy) * d * 1.0 / f; |
| 123 | + float z = d; |
| 124 | + // Vec3f camera_center=Vec3f(cx,cy,0); |
| 125 | + Vec3f cor = Vec3f(0 - x, 0 - y, 0 - z); |
| 126 | + Vec3f abcline = Vec3f(abc[0], abc[1], abc[2]); |
| 127 | + float corner = cor.dot(abcline); |
| 128 | + // float corner =(cx-x)*abc[0]+(cy-y) *abc[1]+(0-z)*abc[2]; |
| 129 | + if (corner >= 0) |
| 130 | + return 1; |
| 131 | + else |
| 132 | + return 0; |
| 133 | +} |
| 134 | +Mat calplanenormal(Mat &src) { |
| 135 | + float f = fcxcy[0]; |
| 136 | + float cx = fcxcy[1]; |
| 137 | + float cy = fcxcy[2]; |
| 138 | + Mat normals = Mat::zeros(src.size(), CV_32FC3); |
| 139 | + src.convertTo(src, CV_32FC1); |
| 140 | + src *= 1.0; |
| 141 | + int cols = src.cols; |
| 142 | + int rows = src.rows; |
| 143 | + // int plane_points[WINDOWSIZE*WINDOWSIZE]={0}; |
| 144 | + int *plane_points = new int[WINDOWSIZE * WINDOWSIZE]; |
| 145 | + float *plane12 = new float[4]; |
| 146 | + for (int i = 0; i < rows; i++) |
| 147 | + for (int j = 0; j < cols; j++) { |
| 148 | + // for kitti and nyud test |
| 149 | + if (src.at<float>(i, j) == 0.0) |
| 150 | + continue; |
| 151 | + // for:nyud train |
| 152 | + // if(src.at<float>(i,j)<=4000.0)continue; |
| 153 | + |
| 154 | + search_plane_neighbor(src, i, j, 15.0, plane_points); |
| 155 | + CallFitPlane(src, plane_points, i, j, plane12); |
| 156 | + Vec3f d = Vec3f(plane12[0], plane12[1], plane12[2]); |
| 157 | + Vec3f n = normalize(d); |
| 158 | + normals.at<Vec3f>(i, j) = n; |
| 159 | + } |
| 160 | + Mat res = Mat::zeros(src.size(), CV_32FC3); |
| 161 | + for (int i = 0; i < rows; i++) |
| 162 | + for (int j = 0; j < cols; j++) { |
| 163 | + res.at<Vec3f>(i, j)[0] = -1.0 * normals.at<Vec3f>(i, j)[0]; |
| 164 | + res.at<Vec3f>(i, j)[2] = -1.0 * normals.at<Vec3f>(i, j)[1]; |
| 165 | + res.at<Vec3f>(i, j)[1] = -1.0 * normals.at<Vec3f>(i, j)[2]; |
| 166 | + } |
| 167 | + |
| 168 | + delete[] plane12; |
| 169 | + delete[] plane_points; |
| 170 | + normals.release(); |
| 171 | + for (int i = 0; i < rows; i++) |
| 172 | + for (int j = 0; j < cols; j++) { |
| 173 | + if (!(res.at<Vec3f>(i, j)[0] == 0 && res.at<Vec3f>(i, j)[1] == 0 && |
| 174 | + res.at<Vec3f>(i, j)[2] == 0)) { |
| 175 | + res.at<Vec3f>(i, j)[0] += 1.0; |
| 176 | + res.at<Vec3f>(i, j)[2] += 1.0; |
| 177 | + res.at<Vec3f>(i, j)[1] += 1.0; |
| 178 | + } |
| 179 | + } |
| 180 | + |
| 181 | + res = res * 127.5; |
| 182 | + res.convertTo(res, CV_8UC3); |
| 183 | + cvtColor(res, res, COLOR_BGR2RGB); |
| 184 | + return res; |
| 185 | +} |
| 186 | + |
| 187 | +void demo { |
| 188 | + // set parameters here:fcxcy[0]=0;fcxcy[1]=0;fcxcy[2]=0; |
| 189 | + Mat src = imread(INPUT_FILE_NAME, CV_LOAD_IMAGE_ANYDEPTH); |
| 190 | + Mat res = calplanenormal(src); |
| 191 | + imwrite(OUTPUT_NAME, res); |
| 192 | +} |
| 193 | +#endif |
0 commit comments