-
Notifications
You must be signed in to change notification settings - Fork 872
Expand file tree
/
Copy pathGridMapCvProcessing.cpp
More file actions
173 lines (150 loc) · 6.17 KB
/
GridMapCvProcessing.cpp
File metadata and controls
173 lines (150 loc) · 6.17 KB
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
/*
* GridMapCvProcessing.cpp
*
* Created on: Apr 15, 2016
* Author: Péter Fankhauser, Magnus Gärtner
* Institute: ETH Zurich, ANYbotics
*/
#include "grid_map_cv/GridMapCvProcessing.hpp"
#include "grid_map_cv/GridMapCvConverter.hpp"
#include <Eigen/Geometry>
#include <opencv2/core/eigen.hpp>
namespace grid_map
{
GridMapCvProcessing::GridMapCvProcessing()
{
}
GridMapCvProcessing::~GridMapCvProcessing()
{
}
bool GridMapCvProcessing::changeResolution(
const grid_map::GridMap & gridMapSource,
grid_map::GridMap & gridMapResult,
const double resolution,
const int interpolationAlgorithm)
{
grid_map::GridMap gridMapSourceCopy(gridMapSource);
gridMapSourceCopy.convertToDefaultStartIndex();
const double sizeFactor = gridMapSourceCopy.getResolution() / resolution;
bool firstLayer = true;
for (const auto & layer : gridMapSourceCopy.getLayers()) {
cv::Mat imageSource, imageResult;
const float minValue = gridMapSourceCopy.get(layer).minCoeffOfFinites();
const float maxValue = gridMapSourceCopy.get(layer).maxCoeffOfFinites();
const bool hasNaN = gridMapSourceCopy.get(layer).hasNaN();
bool result;
if (hasNaN) {
result = grid_map::GridMapCvConverter::toImage<uint16_t, 4>(
gridMapSourceCopy, layer, CV_16UC4,
minValue, maxValue, imageSource);
} else {
result = grid_map::GridMapCvConverter::toImage<uint16_t, 1>(
gridMapSourceCopy, layer, CV_16UC1,
minValue, maxValue, imageSource);
}
if (!result) {return false;}
cv::resize(
imageSource, imageResult, cv::Size(
0.0,
0.0), sizeFactor, sizeFactor,
interpolationAlgorithm);
if (firstLayer) {
if (!GridMapCvConverter::initializeFromImage(
imageResult, resolution, gridMapResult,
gridMapSourceCopy.getPosition()))
{
return false;
}
firstLayer = false;
}
if (hasNaN) {
result = grid_map::GridMapCvConverter::addLayerFromImage<uint16_t, 4>(
imageResult, layer,
gridMapResult, minValue,
maxValue);
} else {
result = grid_map::GridMapCvConverter::addLayerFromImage<uint16_t, 1>(
imageResult, layer,
gridMapResult, minValue,
maxValue);
}
if (!result) {return false;}
}
gridMapResult.setFrameId(gridMapSourceCopy.getFrameId());
gridMapResult.setTimestamp(gridMapSourceCopy.getTimestamp());
gridMapResult.setBasicLayers(gridMapSourceCopy.getBasicLayers());
return true;
}
GridMap GridMapCvProcessing::getTransformedMap(
GridMap && gridMapSource,
const Eigen::Isometry3d & transform,
const std::string & heightLayerName,
const std::string & newFrameId)
{
// Check if height layer is valid.
if (!gridMapSource.exists(heightLayerName)) {
throw std::out_of_range("GridMap::getTransformedMap(...) : No map layer '" +
heightLayerName + "' available.");
}
// Check if transformation is z aligned.
if (std::abs(transform(2, 2) - 1) >= 0.05) {
throw std::invalid_argument("The given transform is not Z aligned!");
}
auto yawPitchRoll = transform.rotation().eulerAngles(2, 1, 0); // Double check convention!
double rotationAngle = yawPitchRoll.x() * 180 / CV_PI;
if (std::abs(yawPitchRoll.y()) >= 3 &&
std::abs(yawPitchRoll.z()) >= 3)
{ // Resolve yaw ambiguity in euler angles.
rotationAngle += 180;
}
gridMapSource.convertToDefaultStartIndex();
// Create the rotated gridMap.
GridMap transformedMap(gridMapSource.getLayers());
transformedMap.setBasicLayers(gridMapSource.getBasicLayers());
transformedMap.setTimestamp(gridMapSource.getTimestamp());
transformedMap.setFrameId(newFrameId);
// openCV rotation parameters, initalized on first layer.
cv::Mat imageRotationMatrix;
cv::Rect2f boundingBox;
bool firstLayer = true;
for (const auto & layer : gridMapSource.getLayers()) {
cv::Mat imageSource, imageResult;
// From gridMap to openCV image. Assumes defaultStartIndex.
cv::eigen2cv(gridMapSource[layer], imageSource);
// Calculate transformation matrix and update geometry of the resulting grid map.
if (firstLayer) {
// Get rotation matrix for rotating the image around its center in pixel coordinates. See
// https://answers.opencv.org/question/82708/rotation-center-confusion/
cv::Point2f imageCenter =
cv::Point2f((imageSource.cols - 1) / 2.0, (imageSource.rows - 1) / 2.0);
imageRotationMatrix = cv::getRotationMatrix2D(imageCenter, rotationAngle, 1.0);
boundingBox = cv::RotatedRect(cv::Point2f(0, 0),
imageSource.size(), rotationAngle).boundingRect2f();
// Adjust transformation matrix. See
// https://docs.opencv.org/3.4/da/d54/group__imgproc__transform.html#gafbbc470ce83812914a70abfb604f4326 and https://stackoverflow.com/questions/22041699/rotate-an-image-without-cropping-in-opencv-in-c
imageRotationMatrix.at<double>(0, 2) += boundingBox.width / 2.0 - imageSource.cols / 2.0;
imageRotationMatrix.at<double>(1, 2) += boundingBox.height / 2.0 - imageSource.rows / 2.0;
// Calculate the new center of the gridMap.
Position3 newCenter = transform * Position3{(gridMapSource.getPosition().x()),
(gridMapSource.getPosition().y()), 0.0};
// Set the size of the rotated gridMap.
transformedMap.setGeometry({boundingBox.height * gridMapSource.getResolution(),
boundingBox.width * gridMapSource.getResolution()},
gridMapSource.getResolution(), Position(newCenter.x(), newCenter.y()));
firstLayer = false;
}
// Rotate the layer.
imageResult = cv::Mat(boundingBox.size(), CV_32F, std::numeric_limits<double>::quiet_NaN());
cv::warpAffine(imageSource, imageResult, imageRotationMatrix,
boundingBox.size(), cv::INTER_NEAREST, cv::BORDER_TRANSPARENT);
// Copy result into gridMapLayer. Assumes default start index.
Matrix resultLayer;
cv::cv2eigen(imageResult, resultLayer);
transformedMap.add(layer, resultLayer);
}
// Add height translation.
grid_map::Matrix heightLayer = transformedMap[heightLayerName];
transformedMap[heightLayerName] = heightLayer.array() + transform.translation().z();
return transformedMap;
}
} // namespace grid_map