map.cpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639
  1. #include "map.h"
  2. #include <opencv2/core/core.hpp>
  3. #include <opencv2/highgui/highgui.hpp>
  4. namespace hlzn_slam
  5. {
  6. float normalDistribution(const float x, const float expectation, const float variance) {
  7. return exp(-(x - expectation) * (x - expectation) / (2 * variance * variance)) / (sqrt(2 * 3.14) * variance);
  8. }
  9. Eigen::MatrixXf __gaussian(int size, float resolution, float u)
  10. {
  11. const int window_size = 3;
  12. // const float variance = window_size * resolution / 3.f;
  13. Eigen::MatrixXf gaussian_window = Eigen::MatrixXf::Zero(2 * window_size + 1, 2 * window_size + 1);
  14. const float max_expectation = normalDistribution(0, 0, u);
  15. for (int i = -window_size; i < window_size + 1; i++) {
  16. for (int j = -window_size; j < window_size + 1; j++) {
  17. const float x = resolution * sqrt(i * i + j * j);
  18. gaussian_window(i + window_size, j + window_size)
  19. = normalDistribution(x, 0, u) / max_expectation;
  20. }
  21. }
  22. return gaussian_window;
  23. }
  24. void gaussProbabilityMap(Eigen::MatrixXf* input_map, Eigen::MatrixXf* out_map, const float resolution)
  25. {
  26. const int window_size = 3;
  27. const float variance = window_size * resolution / 3.f;
  28. Eigen::MatrixXf gaussian_window = Eigen::MatrixXf::Zero(2 * window_size + 1, 2 * window_size + 1);
  29. const float max_expectation = normalDistribution(0, 0, variance);
  30. for (int i = -window_size; i < window_size + 1; i++) {
  31. for (int j = -window_size; j < window_size + 1; j++) {
  32. const float x = resolution * sqrt(i * i + j * j);
  33. gaussian_window(i + window_size, j + window_size)
  34. = normalDistribution(x, 0, variance) / max_expectation;
  35. }
  36. }
  37. for (int y = 0; y < input_map->rows(); y++) {
  38. for (int x = 0; x < input_map->cols(); x++) {
  39. if ((*input_map)(y, x) > 0) {
  40. for (int i = -window_size; i < window_size + 1; i++) {
  41. for (int j = -window_size; j < window_size + 1; j++) {
  42. bool inmap = (y + i > -1) && (y + i < input_map->rows())
  43. && (x + j > -1) && (x + j < input_map->cols());
  44. if (inmap) {
  45. float p = (*out_map)(y + i, x + j);
  46. float q = gaussian_window(i + window_size, j + window_size);
  47. // if (i == 0 && j == 0) {
  48. // (*out_map)(y + i, x + j) = (p + q) > 1 ? 1 : (p + q);
  49. // }
  50. // else {
  51. (*out_map)(y + i, x + j) = std::max(p, q);
  52. // }
  53. //cost_map_ is same as Hmap.lookup original
  54. // if (p < q)
  55. // (*out_map)(y + i, x + j) = q;
  56. }
  57. }
  58. }
  59. }
  60. }
  61. }
  62. }
  63. inline int RoundToInt(const float x) { return std::lround(x); }
  64. inline int RoundToInt(const double x) { return std::lround(x); }
  65. Map::Map(float length, float width, float resolution) : resolution_(resolution),
  66. reciprocal_resolution_(1.0 / resolution_),
  67. rows_(std::ceil(length / resolution)),
  68. cols_(std::ceil(width / resolution)),
  69. width_(width),
  70. length_(length),
  71. x_origin_(length * 0.5),
  72. y_origin_(width * 0.5),
  73. use_shadow_(false)
  74. {
  75. map_.setZero(rows_, cols_);
  76. shadow_map_.setZero(rows_, cols_);
  77. }
  78. Map::Map(Eigen::MatrixXf& map, float resolution, float x_origin, float y_origin) : use_shadow_(false), x_origin_(x_origin), y_origin_(y_origin)
  79. {
  80. __sharpenMap(map);
  81. Eigen::MatrixXf guss_map = map;
  82. gaussProbabilityMap(&map, &guss_map, resolution);
  83. map_ = guss_map;
  84. rows_ = map_.rows();
  85. cols_ = map_.cols();
  86. shadow_map_.setZero(rows_, cols_);
  87. // for (int i = 0;i < rows_;i++) {
  88. // for (int j = 0;j < cols_;j++) {
  89. // if (map_(i, j) != 0) {
  90. // std::tuple<int /*row*/, int /*col*/> index =
  91. // std::make_tuple(i, j);
  92. // index_table_.emplace(index, &map_(i, j));
  93. // }
  94. // }
  95. // }
  96. resolution_ = resolution;
  97. reciprocal_resolution_ = 1.0 / resolution_;
  98. width_ = static_cast<float>(rows_) * resolution;
  99. length_ = static_cast<float>(cols_) * resolution;
  100. }
  101. void Map::pixelValuePlus(const std::vector<Eigen::Vector3f>& pinot_cloud, const float& probablity, const float& upper)
  102. {
  103. for (const Eigen::Vector3f& a : pinot_cloud) {
  104. int row, col;
  105. __world2Map(a(0), a(1), row, col);
  106. if (__outBound(row, col)) {
  107. continue;
  108. }
  109. __increaseProbablity(row, col, probablity, upper);
  110. }
  111. // __normProbablity(upper);
  112. }
  113. void Map::pixelValuePlus2(const std::vector<Eigen::Vector3f>& pinot_cloud, const float& probablity, const float& upper)
  114. {
  115. for (const Eigen::Vector3f& a : pinot_cloud) {
  116. int row, col;
  117. __world2Map(a(0), a(1), row, col);
  118. if (__outBound(row, col)) {
  119. continue;
  120. }
  121. if (__read(row, col) < 1e-3) {
  122. __increaseProbablity2(row, col, probablity, upper);
  123. }
  124. }
  125. }
  126. void Map::pixelValuePlus3(const std::vector<Eigen::Vector3f>& pinot_cloud, const float& probablity, const float& upper)
  127. {
  128. for (const Eigen::Vector3f& a : pinot_cloud) {
  129. int row, col;
  130. __world2Map(a(0), a(1), row, col);
  131. if (__outBound(row, col)) {
  132. continue;
  133. }
  134. std::tuple<int /*row*/, int /*col*/> index = std::make_tuple(row, col);
  135. std::map<const std::tuple<int /*row*/, int /*col*/>,
  136. float* /*概率*/>::iterator table_iterator = index_table_.find(index);
  137. if (__read(row, col) < 1e-3 || table_iterator != index_table_.end()) {
  138. __increaseProbablity2(row, col, probablity, upper);
  139. }
  140. }
  141. }
  142. void Map::__normProbablity(const float& upper)
  143. {
  144. float max = 0.0;
  145. for(auto& c : index_table_) {
  146. max = std::max(max, *c.second);
  147. }
  148. if (max > upper) {
  149. float norm = (upper / max);
  150. for(auto& c : index_table_) {
  151. *c.second *= norm;
  152. }
  153. }
  154. }
  155. void Map::pixelValuePlusShadow(const std::vector<Eigen::Vector3f>& pinot_cloud, const float& probablity, const float& upper)
  156. {
  157. for (const Eigen::Vector3f& a : pinot_cloud) {
  158. int row, col;
  159. __world2Map(a(0), a(1), row, col);
  160. if (__outBound(shadow_map_, row, col)) {
  161. continue;
  162. }
  163. __increaseProbablityShadow(row, col, probablity, upper);
  164. }
  165. float max = 0.0;
  166. for(auto& c : shadow_) {
  167. max = std::max(max, *c.second);
  168. }
  169. if (max > upper) {
  170. float norm = (upper / max);
  171. for(auto& c : shadow_) {
  172. *c.second *= norm;
  173. }
  174. }
  175. }
  176. void Map::pixelValuePlusShadow2(const std::vector<Eigen::Vector3f>& pinot_cloud, const float& probablity, const float& upper)
  177. {
  178. for (const Eigen::Vector3f& a : pinot_cloud) {
  179. int row, col;
  180. __world2Map(a(0), a(1), row, col);
  181. if (!__outBound(shadow_map_, row, col)) {
  182. if (__readShadow(row, col) < 1e-3) {
  183. __increaseProbablityShadow2(row, col, probablity, upper);
  184. }
  185. }
  186. }
  187. }
  188. void Map::mapAttenuate(const float& attenuate_value)
  189. {
  190. std::map<const std::tuple<int /*row*/, int /*col*/>,
  191. float* /*概率*/>::iterator iter = index_table_.begin();
  192. for (;iter != index_table_.end();iter++) {
  193. *(iter->second) -= attenuate_value;
  194. if (*(iter->second) < 0) {
  195. index_table_.erase(iter);
  196. }
  197. }
  198. }
  199. void Map::mapShadowAttenuate(const float& attenuate_value)
  200. {
  201. std::map<const std::tuple<int /*row*/, int /*col*/>,
  202. float* /*概率*/>::iterator iter = shadow_.begin();
  203. for (;iter != shadow_.end();iter++) {
  204. *iter->second -= attenuate_value;
  205. if (*iter->second < 0) {
  206. shadow_.erase(iter);
  207. }
  208. }
  209. }
  210. void Map::out(std::string path)
  211. {
  212. cv::Mat mat(cv::Size(cols_, rows_), CV_8U, cv::Scalar(0));
  213. for(auto& c : index_table_) {
  214. if (__outBound(std::get<0>(c.first), std::get<1>(c.first))) {
  215. continue;
  216. }
  217. unsigned char *p;
  218. unsigned char pix = 0;
  219. char value = (char)(__read(std::get<0>(c.first), std::get<1>(c.first)) * 255);
  220. p = mat.ptr<uchar>(std::get<0>(c.first));
  221. *(p + std::get<1>(c.first)) = value;
  222. }
  223. for(auto& c : shadow_) {
  224. if (__outBound(std::get<0>(c.first), std::get<1>(c.first))) {
  225. continue;
  226. }
  227. unsigned char *p;
  228. unsigned char pix = 0;
  229. char value = (char)(__readShadow(std::get<0>(c.first), std::get<1>(c.first)) * 255);
  230. p = mat.ptr<uchar>(std::get<0>(c.first));
  231. *(p + std::get<1>(c.first)) = value;
  232. }
  233. cv::imwrite(path, mat);
  234. }
  235. float Map::getCost(const common::PointCloud& point_cloud)
  236. {
  237. if (point_cloud.data.empty()) {
  238. return 0.0;
  239. }
  240. float cost = 0;
  241. for (const Eigen::Vector3f& point : point_cloud.data) {
  242. int row, col;
  243. __world2Map(point(0), point(1), row, col);
  244. cost += getPixelProbablity(row, col);
  245. }
  246. cost /= static_cast<float>(point_cloud.data.size());
  247. return cost;
  248. }
  249. float Map::getPixelProbablity(const int row, const int col)
  250. {
  251. float probablity = 0;
  252. probablity += __read(row, col);
  253. if (use_shadow_) {
  254. probablity += __readShadow(row, col);
  255. }
  256. probablity = probablity > 1.0 ? 1.0 : probablity;
  257. return probablity;
  258. }
  259. void Map::useShadow(bool ok)
  260. {
  261. use_shadow_ = ok;
  262. }
  263. int Map::getRows()
  264. {
  265. return rows_;
  266. }
  267. int Map::getCols()
  268. {
  269. return cols_;
  270. }
  271. float Map::getLength()
  272. {
  273. return length_;
  274. }
  275. float Map::getWidth()
  276. {
  277. return width_;
  278. }
  279. float Map::getResolution()
  280. {
  281. return resolution_;
  282. }
  283. const float& Map::getXorigin()
  284. {
  285. return x_origin_;
  286. }
  287. const float& Map::getYorigin()
  288. {
  289. return y_origin_;
  290. }
  291. const float& Map::getReciprocalResolution()
  292. {
  293. return reciprocal_resolution_;
  294. }
  295. Eigen::MatrixXf& Map::getMapCite()
  296. {
  297. return map_;
  298. }
  299. Eigen::MatrixXf* Map::getMapPtr()
  300. {
  301. return &map_;
  302. }
  303. void Map::clearMap()
  304. {
  305. __clearMap();
  306. }
  307. void Map::clearShadowMap()
  308. {
  309. shadow_.clear();
  310. }
  311. bool Map::empty()
  312. {
  313. return index_table_.empty();
  314. }
  315. void Map::word2Map(float x, float y, int& row, int& col)
  316. {
  317. __world2Map(x, y, row, col);
  318. }
  319. void Map::weakenPixel(float x, float y, float value)
  320. {
  321. int row, col;
  322. __world2Map(x, y, row, col);
  323. std::tuple<int /*row*/, int /*col*/> index = std::make_tuple(row, col);
  324. std::map<const std::tuple<int /*row*/, int /*col*/>,
  325. float* /*概率*/>::iterator table_iterator = index_table_.find(index);
  326. if (table_iterator != index_table_.end()) {
  327. *(table_iterator->second) -= value;
  328. if (*(table_iterator->second) < 0) {
  329. *(table_iterator->second) = 0.0;
  330. index_table_.erase(table_iterator);
  331. }
  332. }
  333. }
  334. void Map::__world2Map(float x, float y, int& row, int& col)
  335. {
  336. // ros的坐标系在右手坐标系上旋转了90度,使得x轴指向图片的列
  337. row = RoundToInt(getRows() - (y + y_origin_) * reciprocal_resolution_);
  338. col = RoundToInt((x + x_origin_) * reciprocal_resolution_);
  339. }
  340. float& Map::__read(int row, int col)
  341. {
  342. static float zero = 0.0;
  343. if (__outBound(row, col)) {
  344. return zero;
  345. }
  346. return map_(row, col);
  347. }
  348. float& Map::__read(Eigen::MatrixXf& map, int row, int col)
  349. {
  350. static float zero = 0.0;
  351. if (__outBound(map, row, col)) {
  352. return zero;
  353. }
  354. return map(row, col);
  355. }
  356. void Map::__writeMap(int row, int col, float value)
  357. {
  358. if (__outBound(row, col)) {
  359. return;
  360. }
  361. map_(row, col) = value;
  362. }
  363. float Map::__readShadow(int row, int col)
  364. {
  365. float shadow_value = 0;
  366. std::tuple<int /*row*/, int /*col*/> index =
  367. std::make_tuple(row, col);
  368. auto table_iterator = shadow_.find(index);
  369. if (table_iterator != shadow_.end()) {
  370. shadow_value = *table_iterator->second;
  371. }
  372. return shadow_value;
  373. }
  374. void Map::__increaseProbablity(int& row, int& col, const float& probablity, const float& upper)
  375. {
  376. if (__outBound(row, col)) {
  377. return;
  378. }
  379. std::tuple<int /*row*/, int /*col*/> index = std::make_tuple(row, col);
  380. std::map<const std::tuple<int /*row*/, int /*col*/>,
  381. float* /*概率*/>::iterator table_iterator = index_table_.find(index);
  382. float plus = probablity;
  383. if (table_iterator == index_table_.end()) {
  384. auto iter = index_table_.emplace(index, &map_(row, col));
  385. *(iter.first->second) += plus;
  386. }
  387. else {
  388. *(table_iterator->second) += plus;
  389. }
  390. return;
  391. }
  392. void Map::__increaseProbablity2(int& row, int& col, const float& probablity, const float& upper)
  393. {
  394. if (__outBound(row, col)) {
  395. return;
  396. }
  397. std::tuple<int /*row*/, int /*col*/> index = std::make_tuple(row, col);
  398. std::map<const std::tuple<int /*row*/, int /*col*/>,
  399. float* /*概率*/>::iterator table_iterator = index_table_.find(index);
  400. float plus = probablity;
  401. if (table_iterator == index_table_.end()) {
  402. auto iter = index_table_.emplace(index, &map_(row, col));
  403. if (*(iter.first->second) > (upper - probablity)) {
  404. plus = upper - *(iter.first->second);
  405. }
  406. plus < 0 ? plus = 0.0 : true;
  407. *(iter.first->second) += plus;
  408. }
  409. else {
  410. if (*(table_iterator->second) > (upper - probablity)) {
  411. plus = upper - *(table_iterator->second);
  412. }
  413. plus < 0 ? plus = 0.0 : true;
  414. *(table_iterator->second) += plus;
  415. }
  416. return;
  417. }
  418. void Map::__increaseProbablityShadow(int& row, int& col, const float& probablity, const float& upper)
  419. {
  420. std::tuple<int /*row*/, int /*col*/> index =
  421. std::make_tuple(row, col);
  422. auto table_iterator = shadow_.find(index);
  423. float plus = probablity;
  424. if (table_iterator == shadow_.end()) {
  425. auto iter = shadow_.emplace(index, &shadow_map_(row, col));
  426. }
  427. else {
  428. *table_iterator->second += plus;
  429. }
  430. return;
  431. }
  432. void Map::__increaseProbablityShadow2(int& row, int& col, const float& probablity, const float& upper)
  433. {
  434. std::tuple<int /*row*/, int /*col*/> index =
  435. std::make_tuple(row, col);
  436. auto table_iterator = shadow_.find(index);
  437. float plus = probablity;
  438. if (table_iterator == shadow_.end()) {
  439. auto iter = shadow_.emplace(index, &shadow_map_(row, col));
  440. }
  441. else {
  442. if (*table_iterator->second > (upper - probablity)) {
  443. plus = upper - *table_iterator->second;
  444. }
  445. plus < 0 ? plus = 0.0 : true;
  446. *table_iterator->second += plus;
  447. }
  448. return;
  449. }
  450. void Map::__clearMap()
  451. {
  452. index_table_.clear();
  453. }
  454. bool Map::__outBound(int row, int col)
  455. {
  456. if (row < 0 || col < 0) {
  457. return true;
  458. }
  459. if (map_.rows() > row && map_.cols() > col) {
  460. return false;
  461. }
  462. return true;
  463. }
  464. bool Map::__outBound(Eigen::MatrixXf& map, int row, int col)
  465. {
  466. if (row < 0 || col < 0) {
  467. return true;
  468. }
  469. if (map.rows() > row && map.cols() > col) {
  470. return false;
  471. }
  472. return true;
  473. }
  474. void Map::__sharpenMap(Eigen::MatrixXf& map)
  475. {
  476. for (int row = 0; row < map.rows(); row += 3) {
  477. for (int col = 0; col < map.cols(); col += 3) {
  478. float max =
  479. std::max(
  480. std::max(
  481. std::max(
  482. std::max(
  483. std::max(
  484. std::max(
  485. std::max(
  486. std::max(
  487. __read(map, row, col),
  488. __read(map, row, col + 1)),
  489. __read(map, row, col + 2)),
  490. __read(map, row + 1, col)),
  491. __read(map, row + 1, col + 1)),
  492. __read(map, row + 1, col + 2)),
  493. __read(map, row + 2, col)),
  494. __read(map, row + 2, col + 1)),
  495. __read(map, row + 2, col + 2));
  496. if (max > 1e-3) {
  497. float k = 1.0 / max;
  498. if (!__outBound(map, row, col)) {
  499. map(row, col) *= k;
  500. }
  501. if (!__outBound(map, row, col + 1)) {
  502. map(row, col + 1) *= k;
  503. }
  504. if (!__outBound(map, row, col + 2)) {
  505. map(row, col + 2) *= k;
  506. }
  507. if (!__outBound(map, row + 1, col)) {
  508. map(row + 1, col) *= k;
  509. }
  510. if (!__outBound(map, row + 1, col + 1)) {
  511. map(row + 1, col + 1) *= k;
  512. }
  513. if (!__outBound(map, row + 1, col + 2)) {
  514. map(row + 1, col + 2) *= k;
  515. }
  516. if (!__outBound(map, row + 2, col)) {
  517. map(row + 2, col) *= k;
  518. }
  519. if (!__outBound(map, row + 2, col + 1)) {
  520. map(row + 2, col + 1) *= k;
  521. }
  522. if (!__outBound(map, row + 2, col + 2)) {
  523. map(row + 2, col + 2) *= k;
  524. }
  525. }
  526. }
  527. }
  528. }
  529. } // namespace name