Advertisement
Guest User

Untitled

a guest
Oct 21st, 2017
172
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.28 KB | None | 0 0
  1. #include "opencv2/core/core.hpp"
  2. #include "opencv2/imgproc/imgproc.hpp"
  3. #include "opencv2/calib3d/calib3d.hpp"
  4. #include "opencv2/highgui/highgui.hpp"
  5. #include <iostream>
  6.  
  7. using namespace cv;
  8. using namespace std;
  9.  
  10. #define EPSILON 1E-5
  11.  
  12.  
  13. cv::Point2f center(0, 0);
  14.  
  15. cv::Point2f computeIntersect(cv::Vec4i a,
  16. cv::Vec4i b)
  17. {
  18. int x1 = a[0], y1 = a[1], x2 = a[2], y2 = a[3], x3 = b[0], y3 = b[1], x4 = b[2], y4 = b[3];
  19. float denom;
  20.  
  21. if (float d = ((float)(x1 - x2) * (y3 - y4)) - ((y1 - y2) * (x3 - x4)))
  22. {
  23. cv::Point2f pt;
  24. pt.x = ((x1 * y2 - y1 * x2) * (x3 - x4) - (x1 - x2) * (x3 * y4 - y3 * x4)) / d;
  25. pt.y = ((x1 * y2 - y1 * x2) * (y3 - y4) - (y1 - y2) * (x3 * y4 - y3 * x4)) / d;
  26. return pt;
  27. }
  28. else
  29. return cv::Point2f(-1, -1);
  30. }
  31.  
  32. void sortCorners(std::vector<cv::Point2f>& corners,
  33. cv::Point2f center)
  34. {
  35. std::vector<cv::Point2f> top, bot;
  36.  
  37. for (int i = 0; i < corners.size(); i++)
  38. {
  39. if (corners[i].y < center.y)
  40. top.push_back(corners[i]);
  41. else
  42. bot.push_back(corners[i]);
  43. }
  44. corners.clear();
  45.  
  46. if (top.size() == 2 && bot.size() == 2) {
  47. cv::Point2f tl = top[0].x > top[1].x ? top[1] : top[0];
  48. cv::Point2f tr = top[0].x > top[1].x ? top[0] : top[1];
  49. cv::Point2f bl = bot[0].x > bot[1].x ? bot[1] : bot[0];
  50. cv::Point2f br = bot[0].x > bot[1].x ? bot[0] : bot[1];
  51.  
  52.  
  53. corners.push_back(tl);
  54. corners.push_back(tr);
  55. corners.push_back(br);
  56. corners.push_back(bl);
  57. }
  58. }
  59.  
  60. struct Ray
  61. {
  62. cv::Vec3f origin;
  63. cv::Vec3f direction;
  64.  
  65. };
  66.  
  67.  
  68.  
  69. std::pair<bool, double> linePlaneIntersection(Vec3f& contact, Vec3f direction, Vec3f rayOrigin,
  70. Vec3f normal, Vec3f coord) {
  71. double denom = normalize(normal).dot(direction);
  72. double d = -(normalize(normal).dot(coord));
  73. if (std::abs(d) < std::numeric_limits<double>::epsilon())
  74. {
  75. cout << "point" << contact;
  76. }
  77. if (std::abs(denom) < std::numeric_limits<double>::epsilon())
  78. {
  79. // Parallel
  80. return std::pair<bool, double>(false, (double)0);
  81. }
  82. else
  83. {
  84. double nom = normalize(normal).dot(normalize(rayOrigin)) + d;
  85. double t = -(nom / denom);
  86. return std::pair<bool, double>(t >= 0, (double)t);
  87. }
  88.  
  89. }
  90.  
  91. cv::Point2f snapPoint;
  92. bool isClicked = false;
  93.  
  94. void onmouse(int event, int x, int y, int flags, void* param)
  95. {
  96. if (event == CV_EVENT_LBUTTONDOWN)
  97. {
  98. snapPoint.x = x;
  99. snapPoint.y = y;
  100.  
  101. //Mat &img = *((Mat*)(param)); // 1st cast it back, then deref
  102. //circle(img, Point(x, y), 50, Scalar(0, 255, 0), 1);
  103. isClicked = true;
  104. }
  105. }
  106. std::vector<cv::Point3d> Generate3DPoints()
  107. {
  108. std::vector<cv::Point3d> points;
  109.  
  110. double x, y, z;
  111.  
  112. x = .5; y = .5; z = -.5;
  113. points.push_back(cv::Point3d(x, y, z));
  114.  
  115. x = .5; y = .5; z = .5;
  116. points.push_back(cv::Point3d(x, y, z));
  117.  
  118. x = -.5; y = .5; z = .5;
  119. points.push_back(cv::Point3d(x, y, z));
  120.  
  121. x = -.5; y = .5; z = -.5;
  122. points.push_back(cv::Point3d(x, y, z));
  123.  
  124. x = .5; y = -.5; z = -.5;
  125. points.push_back(cv::Point3d(x, y, z));
  126.  
  127. x = -.5; y = -.5; z = -.5;
  128. points.push_back(cv::Point3d(x, y, z));
  129.  
  130. x = -.5; y = -.5; z = .5;
  131. points.push_back(cv::Point3d(x, y, z));
  132.  
  133. /*
  134. for(unsigned int i = 0; i < points.size(); ++i)
  135. {
  136. std::cout << points[i] << std::endl;
  137. }
  138. */
  139. return points;
  140. }
  141.  
  142. std::vector<cv::Point2d> Generate2DPoints()
  143. {
  144. std::vector<cv::Point2d> points;
  145.  
  146. float x, y;
  147.  
  148. x = 282; y = 274;
  149. points.push_back(cv::Point2d(x, y));
  150.  
  151. x = 397; y = 227;
  152. points.push_back(cv::Point2d(x, y));
  153.  
  154. x = 577; y = 271;
  155. points.push_back(cv::Point2d(x, y));
  156.  
  157. x = 462; y = 318;
  158. points.push_back(cv::Point2d(x, y));
  159.  
  160. x = 270; y = 479;
  161. points.push_back(cv::Point2d(x, y));
  162.  
  163. x = 450; y = 523;
  164. points.push_back(cv::Point2d(x, y));
  165.  
  166. x = 566; y = 475;
  167. points.push_back(cv::Point2d(x, y));
  168.  
  169. for (unsigned int i = 0; i < points.size(); ++i)
  170. {
  171. std::cout << points[i] << std::endl;
  172. }
  173.  
  174. return points;
  175. }
  176.  
  177. int main(int argc, char** argv)
  178. {
  179.  
  180.  
  181. Mat src, src_copy, edges, dst;
  182. src = imread("freezeFrame__1508152029892.png", 0);
  183.  
  184. namedWindow("My window", 1);
  185.  
  186. imshow("My window", src);
  187. while (1 && isClicked == false)
  188. {
  189. setMouseCallback("My window", onmouse, &src); // pass address of img here
  190. cv::waitKey(10);
  191. while (isClicked != false)
  192. {
  193. isClicked = false;
  194.  
  195. std::vector< cv::Point2f > corners;
  196.  
  197. // maxCorners – The maximum number of corners to return. If there are more corners
  198. // than that will be found, the strongest of them will be returned
  199. int maxCorners = 10;
  200.  
  201. // qualityLevel – Characterizes the minimal accepted quality of image corners;
  202. // the value of the parameter is multiplied by the by the best corner quality
  203. // measure (which is the min eigenvalue, see cornerMinEigenVal() ,
  204. // or the Harris function response, see cornerHarris() ).
  205. // The corners, which quality measure is less than the product, will be rejected.
  206. // For example, if the best corner has the quality measure = 1500,
  207. // and the qualityLevel=0.01 , then all the corners which quality measure is
  208. // less than 15 will be rejected.
  209. double qualityLevel = 0.01;
  210.  
  211. // minDistance – The minimum possible Euclidean distance between the returned corners
  212. double minDistance = 20.;
  213.  
  214. // mask – The optional region of interest. If the image is not empty (then it
  215. // needs to have the type CV_8UC1 and the same size as image ), it will specify
  216. // the region in which the corners are detected
  217. cv::Mat mask;
  218.  
  219. // blockSize – Size of the averaging block for computing derivative covariation
  220. // matrix over each pixel neighborhood, see cornerEigenValsAndVecs()
  221. int blockSize = 3;
  222.  
  223. // useHarrisDetector – Indicates, whether to use operator or cornerMinEigenVal()
  224. bool useHarrisDetector = false;
  225.  
  226. // k – Free parameter of Harris detector
  227. double k = 0.04;
  228.  
  229. cv::goodFeaturesToTrack(src, corners, maxCorners, qualityLevel, minDistance, mask, blockSize, useHarrisDetector, k);
  230.  
  231. for (size_t i = 0; i < corners.size(); i++)
  232. {
  233. cv::circle(src, corners[i], 10, cv::Scalar(0, 255, 255), -1);
  234. }
  235.  
  236. for (size_t i = 0; i < corners.size(); i++)
  237. {
  238. cv::Point2f pt1 = corners[i];
  239. for (int j = 0; j < corners.size(); j++)
  240. {
  241. cv::Point2f pt2 = corners[j];
  242. line(src, pt1, pt2, cv::Scalar(0, 255, 0));
  243. }
  244. }
  245.  
  246. corners.push_back(snapPoint);
  247.  
  248. Mat cameraIntrinsics(3, 3, CV_64FC1);
  249.  
  250. cameraIntrinsics.at<double>(0, 0) = 1600;
  251. cameraIntrinsics.at<double>(0, 1) = 0;
  252. cameraIntrinsics.at<double>(0, 2) = src.cols/2;
  253. cameraIntrinsics.at<double>(1, 0) = 0;
  254. cameraIntrinsics.at<double>(1, 1) = 1600;
  255. cameraIntrinsics.at<double>(1, 2) = src.rows/2;
  256. cameraIntrinsics.at<double>(2, 0) = 0;
  257. cameraIntrinsics.at<double>(2, 1) = 0;
  258. cameraIntrinsics.at<double>(2, 2) = 1;
  259.  
  260.  
  261.  
  262.  
  263. Mat invCameraIntrinsics = cameraIntrinsics.inv();
  264. cout << "inv" << invCameraIntrinsics;
  265. std::vector<cv::Vec3d> pointsTransformed3D;
  266.  
  267. std::vector<cv::Vec3d> points3D;
  268. for (int i = 0; i < corners.size(); i++)
  269. {
  270. cv::Vec3d pt;
  271.  
  272. pt[2] = 1.0f;
  273.  
  274. pt[0] = (corners[i].y );
  275. pt[1] = (corners[i].x );
  276.  
  277. points3D.push_back(pt);
  278. }
  279.  
  280. cv::transform(points3D, pointsTransformed3D, invCameraIntrinsics);
  281.  
  282. /*
  283. std::vector<Ray> rays;
  284. for (int i = 0; i < pointsTransformed3D.size(); i++)
  285. {
  286.  
  287. Ray ray;
  288.  
  289. ray.origin = Vec3f(0, 0, 0);
  290. ray.direction = Vec3f(pointsTransformed3D[i][0], pointsTransformed3D[i][1], pointsTransformed3D[i][2]);
  291.  
  292. rays.push_back(ray);
  293. }
  294.  
  295.  
  296. std::vector<cv::Vec3f> contacts;
  297.  
  298. for (int i = 0; i < pointsTransformed3D.size(); i++)
  299. {
  300. Vec3f pt(pointsTransformed3D[i][0], pointsTransformed3D[i][1], pointsTransformed3D[i][2]);
  301.  
  302. cv::Vec3f contact;
  303. std::pair<bool, double> test = linePlaneIntersection(contact, rays[i].direction, rays[i].origin, Vec3f(0, 1, 0), pt);
  304. if (test.first == true)
  305. {
  306. cv::Vec3f contact(rays[i].origin + (rays[i].direction) * test.second);
  307. contacts.push_back(contact);
  308. }
  309. }
  310. */
  311.  
  312. for (int i = 0; i < pointsTransformed3D.size(); i++)
  313. {
  314. double d = -(normalize(Vec3d(0,1,0)).dot(pointsTransformed3D[i]));
  315. if (std::abs(d) < std::numeric_limits<double>::epsilon())
  316. {
  317. cout << "point" << pointsTransformed3D[i];
  318. }
  319. }
  320. Mat rotationMatrix(3, 3, CV_64FC1);
  321.  
  322. rotationMatrix.at<double>(0, 0) = 0.9115078799790896;
  323. rotationMatrix.at<double>(0, 1) = -0.1883612409043686;
  324. rotationMatrix.at<double>(0, 2) = -0.3656137684237178;
  325. rotationMatrix.at<double>(1, 0) = -0.3046835686704949;
  326. rotationMatrix.at<double>(1, 1) = 0.2878667580409447;
  327. rotationMatrix.at<double>(1, 2) = -0.9079100465339108;
  328. rotationMatrix.at<double>(2, 0) = 0.2762631132059388;
  329. rotationMatrix.at<double>(2, 1) = 0.9389636694462479;
  330. rotationMatrix.at<double>(2, 2) = 0.2050022432604093;
  331.  
  332.  
  333. cv::Mat rVec(3, 1, CV_64FC1); // Rotation vector
  334. Rodrigues(rotationMatrix, rVec);
  335. cout << "rVec" << rVec << endl;
  336.  
  337. double norm = cv::norm(rVec);
  338.  
  339. double theta = (double)(sqrt(rVec.at<double>(0)*rVec.at<double>(0) + rVec.at<double>(1)*rVec.at<double>(1) + rVec.at<double>(2)*rVec.at<double>(2)) * 180 / 3.14567898726);
  340.  
  341. cv::Mat tVec(3, 1, CV_64FC1); // Translation vector
  342. tVec.at<double>(1) = 21.408294677734375;
  343. tVec.at<double>(2) = 531.1319580078125; //
  344. tVec.at<double>(0) = 705.74224853515625;
  345.  
  346. cv::Mat distCoeffs(5, 1, CV_64FC1); // Distortion vector
  347. distCoeffs.at<double>(0) = 0;
  348. distCoeffs.at<double>(1) = 0;
  349. distCoeffs.at<double>(2) = 0;
  350. distCoeffs.at<double>(3) = 0;
  351. distCoeffs.at<double>(4) = 0;
  352.  
  353. std::vector<cv::Point2d> projectedPoints;
  354. std::vector < cv::Point3d> ContactPoints;
  355.  
  356. for (int i = 0; i < pointsTransformed3D.size(); i++)
  357. {
  358. cv::Point3d pt;
  359.  
  360. pt.x = pointsTransformed3D[i][0];
  361. pt.y = pointsTransformed3D[i][1];
  362. pt.z = pointsTransformed3D[i][2];
  363.  
  364. ContactPoints.push_back(pt);
  365. }
  366.  
  367. //ContactPoints = Generate3DPoints();
  368. cv::projectPoints(ContactPoints, rVec, tVec, cameraIntrinsics, distCoeffs, projectedPoints);
  369.  
  370.  
  371. for (size_t i = 0; i < projectedPoints.size(); i++)
  372. {
  373. cv::Point2d pt;
  374.  
  375. pt.x = projectedPoints[i].y;
  376. pt.y = projectedPoints[i].x;
  377.  
  378. cv::circle(src, pt, 10, cv::Scalar(255, 0, 255), -1);
  379. }
  380.  
  381. imshow("My window", src);
  382. }
  383. }
  384.  
  385. cv:waitKey(0);
  386. return 0;
  387. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement