MatViewer.cpp 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. #include <stdint.h>
  2. #include <stdio.h>
  3. #include "MatViewer.hpp"
  4. int GraphicItem::globalID = 0;
  5. void OpencvViewer::_onMouseCallback(int event, int x, int y, int /*flags*/, void* ustc)
  6. {
  7. OpencvViewer* p = (OpencvViewer*)ustc;
  8. // NOTE: This callback will be called very frequently while mouse moving,
  9. // keep it simple
  10. bool repaint = false;
  11. p->onMouseCallback(p->_orgImg, event, cv::Point(x,y), repaint);
  12. if(repaint){
  13. p->showImage();
  14. }
  15. }
  16. void OpencvViewer::showImage()
  17. {
  18. _showImg = _orgImg.clone();
  19. for(std::map<int, GraphicItem*>::iterator it = _items.begin()
  20. ; it != _items.end(); it++){
  21. it->second->draw(_showImg);
  22. }
  23. cv::imshow(_win.c_str(), _showImg);
  24. cv::setMouseCallback(_win, _onMouseCallback, this);
  25. }
  26. ///////////////////////////// DepthViewer ///////////////////////////////////////
  27. DepthViewer::DepthViewer(const std::string& win)
  28. : OpencvViewer(win)
  29. , _centerDepthItem(std::string(), cv::Point(0,20), 0.5, cv::Scalar(0,255,0), 2)
  30. , _pickedDepthItem(std::string(), cv::Point(0,40), 0.5, cv::Scalar(0,255,0), 2)
  31. {
  32. OpencvViewer::addGraphicItem(&_centerDepthItem);
  33. OpencvViewer::addGraphicItem(&_pickedDepthItem);
  34. depth_scale_unit = 1.f;
  35. }
  36. void DepthViewer::show(const cv::Mat& img)
  37. {
  38. if(img.type() != CV_16U || img.total() == 0){
  39. return;
  40. }
  41. char str[128];
  42. float val = img.at<uint16_t>(img.rows / 2, img.cols / 2)*depth_scale_unit;
  43. sprintf(str, "Depth at center: %.1f", val);
  44. _centerDepthItem.set(str);
  45. val = img.at<uint16_t>(_fixLoc.y, _fixLoc.x)*depth_scale_unit;
  46. sprintf(str, "Depth at (%d,%d): %.1f", _fixLoc.x, _fixLoc.y , val);
  47. _pickedDepthItem.set(str);
  48. _depth = img.clone();
  49. _renderedDepth = _render.Compute(img);
  50. OpencvViewer::show(_renderedDepth);
  51. }
  52. void DepthViewer::onMouseCallback(cv::Mat& img, int event, const cv::Point pnt
  53. , bool& repaint)
  54. {
  55. repaint = false;
  56. switch(event){
  57. case cv::EVENT_LBUTTONDOWN: {
  58. _fixLoc = pnt;
  59. char str[64];
  60. float val = _depth.at<uint16_t>(pnt.y, pnt.x)*depth_scale_unit;
  61. sprintf(str, "Depth at (%d,%d): %.1f", pnt.x, pnt.y, val);
  62. printf(">>>>>>>>>>>>>>>> depth(%.1f)\n", val);
  63. _pickedDepthItem.set(str);
  64. repaint = true;
  65. break;
  66. }
  67. case cv::EVENT_MOUSEMOVE:
  68. // uint16_t val = _img.at<uint16_t>(pnt.y, pnt.x);
  69. // char str[32];
  70. // sprintf(str, "Depth at mouse: %d", val);
  71. // drawText(img, str, cv::Point(0,60), 0.5, cv::Scalar(0,255,0), 2);
  72. break;
  73. }
  74. }