Advertisement
Guest User

Untitled

a guest
Dec 8th, 2019
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 7.21 KB | None | 0 0
  1. #include <Windows.h>
  2. #include <ctime>
  3.  
  4. #include <conio.h>
  5. #include <vector>
  6. #include <math.h>
  7.  
  8. #include <iostream>
  9. #include <sstream>
  10. #include <string>
  11. #include <ctime>
  12. #include <cstdio>
  13.  
  14. #include <vtkAutoInit.h>
  15. VTK_MODULE_INIT(vtkRenderingOpenGL2);
  16. VTK_MODULE_INIT(vtkInteractionStyle);
  17. VTK_MODULE_INIT(vtkRenderingFreeType);
  18. #include <vtkPolyData.h>
  19. #include <vtkSmartPointer.h>
  20. #include <vtkPolyDataMapper.h>
  21. #include <vtkProperty.h>
  22. #include <vtkActor.h>
  23. #include <vtkRenderWindow.h>
  24. #include <vtkRenderer.h>
  25. #include <vtkRenderWindowInteractor.h>
  26. #include <vtkInteractorStyle.h>
  27. #include <vtkInteractorStyleTrackballCamera.h>
  28. #include <vtkAxesActor.h>
  29. #include <vtkTextActor.h>
  30. #include <vtkTransform.h>
  31.  
  32. #include <vtkCellArray.h>
  33. #include <vtkCellData.h>
  34. #include <vtkPointData.h>
  35. #include <vtkPolyDataNormals.h>
  36. #include <vtkFloatArray.h>
  37. #include <vtkIdList.h>
  38. #include <vtkCallbackCommand.h>
  39. #include <vtkTransformPolyDataFilter.h>
  40. #include <vtkSphereSource.h>
  41. #include <vtkCubeSource.h>
  42. #include <vtkCylinderSource.h>
  43. #include <vtkIterativeClosestPointTransform.h>
  44. #include <vtkLandmarkTransform.h>
  45. #include <vtkMatrix4x4.h>
  46. #include <vtkVertexGlyphFilter.h>
  47. #include <vtkPLYReader.h>
  48.  
  49. #include <vtkPNGWriter.h>
  50. #include <vtkWindowToImageFilter.h>
  51. #include <vtkTextProperty.h>
  52.  
  53. #include <opencv2/opencv.hpp>
  54. #include <opencv2/core.hpp>
  55. #include <opencv2/core/utility.hpp>
  56. #include <opencv2/imgproc.hpp>
  57. #include <opencv2/calib3d.hpp>
  58. #include <opencv2/imgcodecs.hpp>
  59. #include <opencv2/videoio.hpp>
  60. //#include <opencv2/highgui.hpp>
  61.  
  62. #include <opencv2/objdetect.hpp>
  63. #include <opencv2/features2d.hpp>
  64. #include <opencv2/xfeatures2d.hpp>
  65. #include <opencv2/xfeatures2d/nonfree.hpp>
  66.  
  67.  
  68. #include "RVL3DTools.h"
  69. #include "Display.h"
  70. #include "Body.h"
  71.  
  72. using namespace cv;
  73. using namespace std;
  74.  
  75. struct mousecallbackDataLV3 {
  76.     Mat img;
  77.     int clicks;
  78. };
  79.  
  80. void LV3MouseCallBack(int event, int x, int y, int flags, void* userData) {
  81.     mousecallbackDataLV3* pUserData = (mousecallbackDataLV3*)userData;
  82.  
  83.     if (event == CV_EVENT_LBUTTONDOWN) {
  84.         pUserData->img.at<float>(pUserData->clicks, 0) = x;//x je u in imag
  85.         pUserData->img.at<float>(pUserData->clicks, 1) = y;//y je u in imeage
  86.  
  87.         pUserData->clicks++;
  88.  
  89.     }
  90. }
  91.  
  92. int main(int argc, char* argv[]) {
  93.  
  94.     string paramsPath("cameraparams.xml");
  95.     String imgTakenPath("ImgTaken.jbg");
  96.  
  97.     int c = 0;
  98.     Mat cameraMatrix, distCoeffs;
  99.  
  100.  
  101.     FileStorage fsL(paramsPath.c_str(), FileStorage::READ);
  102.     fsL["camera_matrix"] >> cameraMatrix;
  103.     fsL["dist_Coeffs"] >> distCoeffs;
  104.     fsL.release();
  105.  
  106.     Mat imgTaken;
  107.  
  108.     VideoCapture cap(0);
  109.  
  110.     if (cap.isOpened()) {
  111.  
  112.         namedWindow("OriginalView", 1);
  113.         for (;;) {
  114.             c = waitKey(15);
  115.  
  116.             Mat frame, imgClone, imgUndistored;
  117.  
  118.             cap >> frame;
  119.  
  120.             imshow("Original view", frame);
  121.  
  122.             imgClone = frame.clone();
  123.  
  124.             undistort(imgClone, imgUndistored, cameraMatrix, distCoeffs);
  125.  
  126.             if (c == 'p') {
  127.                 imgTaken.create(imgUndistored.size(), CV_8UC1);
  128.  
  129.                 cvtColor(imgUndistored, imgTaken, COLOR_BGR2GRAY);
  130.  
  131.                 imshow("Source", imgTaken);
  132.                 //Save image
  133.                 //imwrite("ImgTaken.jpg",imgTaken);
  134.  
  135.                 break;
  136.             }
  137.  
  138.             if (c == 27) break; //ESC_KEY = 27
  139.         }
  140.  
  141.         //imgTaken = imread(imgTakenPath.c_str(), 1);
  142.  
  143.         int nCliks = 0;
  144.         Mat image_points = Mat::zeros(4, 2, CV_32F);
  145.         Mat object_points = Mat::zeros(4, 3, CV_32FC1);
  146.  
  147.         mousecallbackDataLV3 callback;
  148.  
  149.         callback.img = image_points;
  150.         callback.clicks = nCliks;
  151.         setMouseCallback("Source", LV3MouseCallBack, &callback);
  152.         //imshow("Source", imgTaken);
  153.  
  154.         while (1) {
  155.             c = waitKey(15);
  156.  
  157.             if ((c == 27) || (callback.clicks == 4)) {
  158.                 setMouseCallback("Source", NULL);
  159.                 break;
  160.             }
  161.         }
  162.  
  163.  
  164.         Rect rect = Rect((int)(image_points.at<float>(0, 0)), (int)(image_points.at<float>(0, 1)),
  165.             (int)(image_points.at<float>(1, 0) - image_points.at<float>(0, 0) ),
  166.             (int)(image_points.at<float>(3, 1) - image_points.at<float>(1, 1) ) );
  167.            
  168.         cout <<endl<< imgTaken.size << endl;
  169.  
  170.         Mat imgTakenROI = imgTaken(rect);
  171.  
  172.         imshow("img ROI Test",imgTakenROI);
  173.  
  174.         //hard coded object points of graph paper
  175.         object_points.at<float>(0, 0) = 0.0f;
  176.         object_points.at<float>(0, 1) = 0.0f;
  177.         object_points.at<float>(0, 2) = 0.0f;
  178.  
  179.         object_points.at<float>(1, 0) = 480.0f;
  180.         object_points.at<float>(1, 1) = 0.0f;
  181.         object_points.at<float>(1, 2) = 0.0f;
  182.  
  183.         object_points.at<float>(2, 0) = 0.0f;
  184.         object_points.at<float>(2, 1) = 640.0f;
  185.         object_points.at<float>(2, 2) = 0.0f;
  186.  
  187.         object_points.at<float>(3, 0) = 480.0f;
  188.         object_points.at<float>(3, 1) = 640.0f;
  189.         object_points.at<float>(3, 2) = 0.0f;
  190.  
  191.  
  192.         Mat dst, color_dst, color_dstP;
  193.  
  194.         //edge detection
  195.         Canny(imgTakenROI, dst, 50, 200, 3);
  196.  
  197.         //copy edges to the images that will display results of the smth
  198.         cvtColor(dst, color_dst, COLOR_GRAY2BGR);
  199.         //probabilistic line transform
  200.  
  201.         vector<Vec2f> lines;
  202.         HoughLines(dst, lines, 1, CV_PI / 180, 150, 0, 0); //namjestiti sam parametre!!!!!!!!!!!!!
  203.  
  204.         //draw the main line
  205.         if (lines.size() > 0) {
  206.             float rho = lines[0][0], theta = lines[0][1];
  207.             Point pt1, pt2;
  208.             double a = cos(theta), d = sin(theta);
  209.             double x0 = a * rho, y0 = d * rho;
  210.             pt1.x = cvRound(x0 + 1000 * (-d));
  211.             pt1.y = cvRound(y0 + 1000 * (a));
  212.             pt2.x = cvRound(x0 - 1000 * (-d));
  213.             pt2.y = cvRound(y0 - 1000 * (a));
  214.             line(color_dst, pt1, pt2, Scalar(0, 0, 255), 2, LINE_AA);
  215.  
  216.             double cosTheta = a, sinTheta = d;
  217.  
  218.             rho = rho + image_points.at<float>(0, 0) * cosTheta + image_points.at<float>(0, 1) * sinTheta;
  219.  
  220.             Mat rotation_vector, translation_vector;
  221.             solvePnP(object_points, image_points, cameraMatrix, distCoeffs, rotation_vector, translation_vector);
  222.  
  223.  
  224.             // ekstrinsicni parametri
  225.             Mat A = Mat::zeros(3, 3, CV_32FC1);  // cameraMatrix * R
  226.             Mat b = Mat::zeros(3, 1, CV_32FC1); // translacija
  227.             Mat R = Mat::zeros(3, 3, CV_32FC1); // rotacija
  228.  
  229.  
  230.             Rodrigues(rotation_vector, R);
  231.  
  232.             gemm(cameraMatrix, R, 1, 0, 0, A);
  233.             gemm(cameraMatrix,translation_vector, 1, 0, 0, b);
  234.  
  235.             cout << "235:  "<< A.at<double>(2, 0) << endl;
  236.             cout << "236:  " << A.at<double>(2, 1) << endl;
  237.  
  238.             double lambdaX = ((A.at<double>(0, 0) * cosTheta) + (A.at<double>(1, 0) * sinTheta) - (A.at<double>(2, 0) * rho)); // provjeri parametre
  239.             cout << lambdaX << endl;
  240.             double lambdaY = ((A.at<double>(0, 1) * cosTheta) + (A.at<double>(1, 1) * sinTheta) - (A.at<double>(2, 1) * rho));
  241.             cout << lambdaY << endl;
  242.             double lambdaR = ((b.at<double>(2, 0) * rho) - (b.at<double>(0, 0) * cosTheta) - (b.at<double>(1, 0) * sinTheta));
  243.             cout << lambdaR << endl;
  244.            
  245.             cout << "250" << endl;
  246.  
  247.             double thetaN = atan2(lambdaY, lambdaX);
  248.             double rhoN = lambdaR / sqrt(lambdaX * lambdaX + lambdaY * lambdaY);
  249.  
  250.             char text[7];
  251.  
  252.             double fontScale = 0.5;
  253.             Scalar blue = CV_RGB(0, 0, 255);
  254.  
  255.             Size imgSize = color_dst.size();
  256.             sprintf(text, "%6.2f (mm)", rhoN);
  257.             putText(color_dst, text, Point(imgSize.width / 2, imgSize.height / 2),FONTDLGORD,12, blue);
  258.  
  259.             sprintf(text, "%6.2f (deg)", thetaN*180/3.141592);
  260.  
  261.             putText(color_dst, text, Point(imgSize.width / 2, imgSize.height / 2 + 20), FONTDLGORD, 12, blue);
  262.  
  263.         }
  264.  
  265.         imshow("Detected Lines (in Red) - standard hough line transformation", color_dst);
  266.  
  267.         waitKey();
  268.  
  269.     }
  270. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement