-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmarker_recognition_img.cpp
320 lines (256 loc) · 9.19 KB
/
marker_recognition_img.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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
#include <iostream>
//
//#include <aruco/aruco.h>
//#include <aruco/markerdetector.h>
#include <aruco/aruco.h>
#include <opencv2/aruco.hpp>
#include <opencv2/imgproc.hpp>
#include <opencv2/videoio.hpp>
#include <opencv2/highgui.hpp>
#include <opencv2/core.hpp>
#include <opencv2/calib3d/calib3d.hpp>
#include <opencv2/imgcodecs.hpp>
#include "PointsObjectCoord.h"
#include <vtkSmartPointer.h>
#include <vtkStructuredPoints.h>
#include <vtkPointData.h>
#include <vtkPLYWriter.h>
#include <vtkFloatArray.h>
#include <vtkRenderer.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkMarchingCubes.h>
#include <vtkCleanPolyData.h>
#include <vtkPolyDataMapper.h>
#include <vtkActor.h>
#include <vtkProperty.h>
#include <vtkAutoInit.h>
#include <opencv2/imgproc/types_c.h>
#include "utils.h"
VTK_MODULE_INIT(vtkRenderingOpenGL2);
VTK_MODULE_INIT(vtkInteractionStyle);
using namespace cv;
using namespace std;
#define LINE std::cout<<__LINE__ << "\n"
int IMG_WIDTH = 504;
const int IMG_HEIGHT = 378;
const int VOXEL_DIM = 128;
const int VOXEL_SIZE = VOXEL_DIM * VOXEL_DIM * VOXEL_DIM;
const int NUM_IMAGE = 8;
struct voxel {
float xpos;
float ypos;
float zpos;
float res;
float value;
};
struct coord {
int x;
int y;
};
struct startParams {
float startX;
float startY;
float startZ;
float voxelWidth;
float voxelHeight;
float voxelDepth;
};
struct camera {
cv::Mat Image;
cv::Mat P;
cv::Mat K;
cv::Mat R;
cv::Mat t;
cv::Mat Silhouette;
};
// Resize the image for showing it in the opencv window
cv::Mat resizeImg(cv::Mat preimg) {
cv::Mat img;
double newwidth = ((double)IMG_HEIGHT / preimg.size().height) * preimg.size().width;
IMG_WIDTH = newwidth;
cv::Size s = cv::Size((int) newwidth, IMG_HEIGHT);
cv::resize(preimg, img, s);
return img;
}
coord project(camera cam, voxel v) {
coord im;
// Project voxel to image with the projection matrix
float z = cam.P.at<float>(2, 0) * v.xpos +
cam.P.at<float>(2, 1) * v.ypos +
cam.P.at<float>(2, 2) * v.zpos +
cam.P.at<float>(2, 3);
im.y = (cam.P.at<float>(1, 0) * v.xpos +
cam.P.at<float>(1, 1) * v.ypos +
cam.P.at<float>(1, 2) * v.zpos +
cam.P.at<float>(1, 3)) / z;
im.x = (cam.P.at<float>(0, 0) * v.xpos +
cam.P.at<float>(0, 1) * v.ypos +
cam.P.at<float>(0, 2) * v.zpos +
cam.P.at<float>(0, 3)) / z;
return im;
}
void carve(float fArray[], startParams params, camera cam) {
cv::Mat silhouette, distImage;
cv::threshold(cam.Silhouette, silhouette, 100, 255, THRESH_BINARY);
// Calculates the signed distance values for each pixel of the source image.
cv::distanceTransform(silhouette, distImage, CV_DIST_L2, 3);
for (int i = 0; i < VOXEL_DIM; i++) {
for (int j = 0; j < VOXEL_DIM; j++) {
for (int k = 0; k < VOXEL_DIM; k++) {
// Calculate voxel position
voxel v;
v.xpos = params.startX + i * params.voxelWidth;
v.ypos = params.startY + j * params.voxelHeight;
v.zpos = params.startZ + k * params.voxelDepth;
v.value = 1.0f;
// Project 3d voxel point to 2d
coord im = project(cam, v);
float dist = -1.0f;
// Check if projected voxel is within image coords
if (im.x > 0 && im.y > 0 && im.x < IMG_WIDTH && im.y < IMG_HEIGHT) {
dist = distImage.at<float>(im.y, im.x);
// Optional: filter out single pixels that are accidentally mapped to foreground
//if (dist < 0.05)
//dist = 0;
// If pixel is in the background
if (cam.Silhouette.at<uchar>(im.y, im.x) == 0) {
dist *= -1.0f;
}
}
// Update signed distance values
if (dist < fArray[i * VOXEL_DIM * VOXEL_DIM + j * VOXEL_DIM + k]) {
fArray[i * VOXEL_DIM * VOXEL_DIM + j * VOXEL_DIM + k] = dist;
}
}
}
}
}
void renderModel(float fArray[], startParams params) {
// Create vtk visualization pipeline from voxel grid
vtkSmartPointer<vtkStructuredPoints> sPoints = vtkSmartPointer<vtkStructuredPoints>::New();
sPoints->SetDimensions(VOXEL_DIM, VOXEL_DIM, VOXEL_DIM);
sPoints->SetSpacing(params.voxelDepth, params.voxelHeight, params.voxelWidth);
sPoints->SetOrigin(params.startZ, params.startY, params.startX);
vtkSmartPointer<vtkFloatArray> vtkFArray = vtkSmartPointer<vtkFloatArray>::New();
vtkFArray->SetNumberOfValues(VOXEL_SIZE);
vtkFArray->SetArray(fArray, VOXEL_SIZE, 1);
sPoints->GetPointData()->SetScalars(vtkFArray);
sPoints->GetPointData()->Update();
// Use marching cubes algorithm from lecture
vtkSmartPointer<vtkMarchingCubes> mcSource = vtkSmartPointer<vtkMarchingCubes>::New();
mcSource->SetInputData(sPoints);
mcSource->SetNumberOfContours(1);
mcSource->SetValue(0, 0.5);
mcSource->Update();
// Clean mesh topology: remove unused points and merge duplicates
vtkSmartPointer<vtkCleanPolyData> cleanPolyData = vtkSmartPointer<vtkCleanPolyData>::New();
cleanPolyData->SetInputConnection(mcSource->GetOutputPort());
cleanPolyData->Update();
vtkSmartPointer<vtkPolyDataMapper> mapper = vtkSmartPointer<vtkPolyDataMapper>::New();
mapper->SetInputConnection(cleanPolyData->GetOutputPort());
vtkSmartPointer<vtkActor> actor = vtkSmartPointer<vtkActor>::New();
actor->SetMapper(mapper);
vtkSmartPointer<vtkRenderer> renderer = vtkSmartPointer<vtkRenderer>::New();
renderer->GradientBackgroundOn();
renderer->SetBackground(.45, .45, .8);
renderer->SetBackground2(.0, .0, .0);
vtkSmartPointer<vtkRenderWindow> renderWindow = vtkSmartPointer<vtkRenderWindow>::New();
renderWindow->AddRenderer(renderer);
vtkSmartPointer<vtkRenderWindowInteractor> renderWindowInteractor = vtkSmartPointer<vtkRenderWindowInteractor>::New();
renderWindowInteractor->SetRenderWindow(renderWindow);
actor->GetProperty()->SetSpecular(0.2);
renderer->AddActor(actor);
// Render the carved model
renderWindow->Render();
renderWindowInteractor->Start();
}
int main(int argc, char* argv[]) {
std::vector<camera> cameras;
cv::Ptr<cv::aruco::Dictionary> dictionary = cv::aruco::getPredefinedDictionary(cv::aruco::DICT_ARUCO_ORIGINAL);
Mat frames[NUM_IMAGE];
for (int i = 0; i < NUM_IMAGE; i++) {
std::stringstream path, path_bg;
path << "../../../images/final_db/" << "img_" << i << ".jpg";
path_bg << "../../../images/final_db/" << "bg_" << i << ".jpg";
std::string image_path = cv::samples::findFile(path.str());
std::string image_bg_path = cv::samples::findFile(path_bg.str());
cv::Mat img = cv::imread(image_path);
cv::Mat bg = cv::imread(image_bg_path);
if (img.empty() || bg.empty())
{
std::cout << "Could not read the image: " << path.str() << std::endl;
return 1;
}
frames[i] = resizeImg(img);
// Apply background segmentation on image
// This step can be improved. Optionally, images can be converted to grayscale images
cv::Mat silhouette;
cv::Ptr<cv::BackgroundSubtractor> subtractor = cv::createBackgroundSubtractorMOG2();
subtractor->apply(bg, silhouette);
subtractor->apply(img, silhouette);
// Detect markers in frame
std::vector<int> ids;
std::vector<std::vector<cv::Point2f> > corners;
cv::aruco::detectMarkers(frames[i], dictionary, corners, ids);
if (!ids.empty())
{
// Read camera matrix obtained by camera calibration
::aruco::CameraParameters cam;
cam.readFromXMLFile("../../../images/final_db/out_camera_data.xml");
cv::Mat cameraMatrix = cam.CameraMatrix;
cv::Mat distCoeffs = cam.Distorsion;
// Estimate camera pose with camera matrix and detected marker coordinates
auto cameraPos = findCameraPos(objectCoordMap, corners, ids, cameraMatrix, distCoeffs);
std::cout << "Camera Matrix:\n" << cameraMatrix << "\n";
std::cout << "Camera Rotation:\n" << cameraPos.first << "\n";
std::cout << "Camera Translation:\n" << cameraPos.second << "\n";
camera c;
c.Image = resizeImg(img);
c.K = cameraMatrix;
c.R = cameraPos.first;
c.t = cameraPos.second;
hconcat(c.R, c.t, c.P);
c.K.convertTo(c.K, CV_32FC1);
c.P.convertTo(c.P, CV_32FC1);
//Calculate the projection matrix
c.P = c.K * c.P;
c.Silhouette = resizeImg(silhouette);
cameras.push_back(c);
}
}
//Define bounding box dimensions of object
float xStart = 0, xEnd = 60;
float yStart = 0, yEnd = 70;
float zStart = 0, zEnd = 50;
// Parameters for the dimension of voxel grid
startParams params;
params.startX = xStart;
params.startY = yStart;
params.startZ = zStart;
float bbwidth = std::abs(xEnd - xStart);
float bbheight = std::abs(yEnd - yStart);
float bbdepth = std::abs(zEnd - zStart);
params.voxelWidth = bbwidth / VOXEL_DIM;
params.voxelHeight = bbheight / VOXEL_DIM;
params.voxelDepth = bbdepth / VOXEL_DIM;
// Define voxel grid
float* fArray = new float[VOXEL_SIZE];
std::fill_n(fArray, VOXEL_SIZE, 1000.0f);
// Carve model for every given camera image
for (int i = 0; i < NUM_IMAGE; i++) {
std::cout << cameras.at(i).P;
std::cout << "\n";
carve(fArray, params, cameras.at(i));
}
// Show example of segmented image
cv::Mat original, segmented;
original = resizeImg(cameras.at(1).Image);
segmented = resizeImg(cameras.at(1).Silhouette);
cv::imshow("Object", original);
cv::imshow("Silhouette", segmented);
// Render carved model
renderModel(fArray, params);
cv::waitKey(0);
return 0;
}