Guest User

Untitled

a guest
Apr 22nd, 2018
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.16 KB | None | 0 0
  1. #include <opencv2/core/core.hpp>
  2. #include <opencv2/highgui/highgui.hpp>
  3. #include <opencv2/features2d/features2d.hpp>
  4. #include <opencv2/video/tracking.hpp>
  5. //#include <opencv2/calib3d/calib3d.hpp>
  6. //#include <opencv2/imgproc/imgproc.hpp>
  7.  
  8. #include <ros/ros.h>
  9. #include "image_transport/image_transport.h"
  10. #include "cv_bridge/cv_bridge.h"
  11. #include <sensor_msgs/Image.h>
  12.  
  13. #include "undistorter.h"
  14. #include <boost/bind.hpp>
  15.  
  16.  
  17.  
  18. using namespace cv;
  19.  
  20. Mat img1, img2;
  21. std::vector<Point2f> points1;
  22. std::vector<Point2f> points2;
  23.  
  24.  
  25. void featureTracking(Mat _img1, Mat _img2, std::vector<Point2f>& _points1, std::vector<Point2f>& _points2, std::vector<uchar>& status)
  26. {
  27. std::vector<float> err;
  28. Size winSize(21,21);
  29. TermCriteria termcrit(TermCriteria::COUNT+TermCriteria::EPS, 30, 0.01);
  30. calcOpticalFlowPyrLK(_img1, _img2, _points1, _points2, status, err, winSize, 3, termcrit, 0, 0.001);
  31. int iCorr = 0;
  32. for(int i=0; i<status.size(); i++)
  33. {
  34. Point2f pt = _points2.at(i-iCorr);
  35. if((status.at(i) ==0) ||(pt.x<0)||(pt.y<0))
  36. {
  37. if((pt.x<0)||(pt.y<0))
  38. {
  39. status.at(i) = 0;
  40. }
  41. _points1.erase(_points1.begin() + i-iCorr);
  42. _points2.erase(_points2.begin() + i-iCorr);
  43. iCorr++;
  44. }
  45. }
  46. }
  47.  
  48. void fast_features(Mat img, std::vector<Point2f>& points)
  49. {
  50. std::vector<KeyPoint> keypoints;
  51. int threshold = 50;
  52. bool nonmaxSuppression = true;
  53. FAST(img, keypoints, threshold, nonmaxSuppression);
  54. drawKeypoints(img, keypoints, img, Scalar(255,0,0));
  55. imshow("points", img);
  56. waitKey(30);
  57. KeyPoint::convert(keypoints, points, std::vector<int>());
  58. }
  59.  
  60. void image_callback(const sensor_msgs::ImageConstPtr& msg, const undistorter::PinholeUndistorter& undistorter)
  61. {
  62.  
  63. double focal_length = (352.7618998983656+362.2127413903864)/2;
  64. Point2d pp(309.0169477654956, 231.17134547931778);
  65. try
  66. {
  67. img2 = img1;
  68. points2 = points1;
  69. cv_bridge::CvImagePtr img_ptr = cv_bridge::toCvCopy(msg, sensor_msgs::image_encodings::BGR8);
  70. undistorter.undistortImage(img_ptr->image, img1);
  71. // img1 = img_ptr->image;
  72. imshow("view", img1);
  73. waitKey(30);
  74. fast_features(img1, points1);
  75. std::vector<uchar> status;
  76. if(!img1.empty() && !img2.empty())
  77. {
  78. featureTracking(img1, img2, points1, points2, status);
  79. Mat mask, R, t;
  80. Mat E = findEssentialMat(points2, points1, focal_length, pp, RANSAC, 0.999, 1.0, mask);
  81. recoverPose(E, points2, points1, R, t, focal_length, pp, mask);
  82. std::cout << R << std::endl << std::endl;
  83. }
  84.  
  85.  
  86. }
  87. catch(cv_bridge::Exception& e)
  88. {
  89. ROS_ERROR("could not convert from '%s' to 'bgr8'.", msg->encoding.c_str());
  90. }
  91. }
  92.  
  93. int main(int argc, char** argv)
  94. {
  95. Eigen::Vector2d fl(352.7618998983656, 362.2127413903864);
  96. Eigen::Vector2d pp(309.0169477654956, 231.17134547931778);
  97. Eigen::Vector2i res(640,480);
  98.  
  99. Eigen::Vector4d dist(-0.09188870979428575, 0.03877266717716036, -0.040007988295463065, 0.013430413696251163);
  100. undistorter::PinholeGeometry camera(fl,pp,res, undistorter::EquidistantDistortion::create(dist));
  101.  
  102. double alpha = 1.0, scale=1.0;
  103. int interp = INTER_LINEAR;
  104.  
  105. 104,1 69%
  106. undistorter::PinholeUndistorter undistorter(camera, alpha, scale, interp);
  107. ROS_INFO("initializing");
  108. ros::init(argc, argv, "image_listener");
  109. ROS_INFO("node init");
  110. ros::NodeHandle nh;
  111. ROS_INFO("node handle made");
  112. namedWindow("view", CV_WINDOW_AUTOSIZE);
  113. namedWindow("points", CV_WINDOW_AUTOSIZE);
  114. ROS_INFO("window made");
  115. startWindowThread();
  116. ROS_INFO("window thread made");
  117. image_transport::ImageTransport it(nh);
  118. ROS_INFO("image transport made");
  119. image_transport::Subscriber sub = it.subscribe("/cv_camera/image_raw", 1, boost::bind(image_callback, _1, undistorter));
  120. ROS_INFO("subscriber made");
  121. ros::spin();
  122. ROS_INFO("destroying window");
  123. destroyWindow("view");
  124. destroyWindow("points");
  125. return 0;
  126. }
Add Comment
Please, Sign In to add comment