Advertisement
Guest User

Untitled

a guest
Jun 17th, 2019
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.14 KB | None | 0 0
  1. struct RcpAndFpOptimizer {
  2. RcpAndFpOptimizer(cv::Mat &V, const cv::Mat I, int i,int j,double width, double height) : V_(V), I_(I), i_(i),
  3. j_(j), width_(width), height_(height){}
  4. bool operator()(const double* const fp, const double* const rotation, const double* const translation, double* residuals) const {
  5.  
  6.  
  7. double intensity = V_.at<double>(j_, i_);
  8.  
  9.  
  10.  
  11. double tmp = (double)I_.at<double>(j_,i_)-(double)intensity;
  12. residuals[0] = tmp;
  13. //std::cout<<"pixels(i,j): "<<i_<<" "<<j_<<" residual: "<<residuals[0]<<std::endl;
  14. return true;
  15. }
  16.  
  17. const cv::Mat S_;
  18. cv::Mat& V_;
  19. const cv::Mat I_;
  20. const int i_,j_;
  21. double width_, height_;
  22. };
  23.  
  24.  
  25. virtual void PrepareForEvaluation(bool evaluateJacobians, bool newEvaluationPoint)
  26. {
  27. if(evaluateJacobians){
  28. std::cout<<"evaluation jacobian is called"<<std::endl;
  29. }
  30. if (newEvaluationPoint)
  31. {
  32. // do your stuff here, e.g. calculate integral image
  33.  
  34. //Mat V(height_, width_, CV_8UC1);
  35.  
  36. std::cout<<"preperation is called"<<std::endl;
  37.  
  38.  
  39. Intrinsic<double> intrinsicC = INTRINSIC_CAMERA;
  40. Intrinsic<double> intrinsicP= {(double)fP_[0],(double)fP_[0], double(width_/2), double(height_/2), 0, 0};
  41.  
  42. //Convertion of array to point3d
  43. Point3d bDist = Point3d(translation_[0],translation_[1], translation_[2]);
  44.  
  45. //Convertion euler array to rotation matrix
  46. const Mat eulerAngles = (cv::Mat_<double>(3,1) << rotArray_[0], rotArray_[1], rotArray_[2]);
  47.  
  48. Mat rotM = rcpFinder::euler2rot(eulerAngles);
  49.  
  50. Mat tempVImg(height_, width_, CV_8UC1);
  51.  
  52. for (int i = 0; i < width_; ++i) {
  53. for (int j = 0; j < height_ ; ++j) {
  54. //std::cout<<"Virtual current x and y pixels: "<<i<<" "<<j<<std::endl;
  55. Point3d unprojPRay = rcpFinder::unprojectPoints(Point2i(i,j),intrinsicC);
  56.  
  57. //Assigning the intensity from images
  58. tempVImg.at<uchar>(j, i)= rcpFinder::genVirtualImg(S_, intrinsicP, bDist, unprojPRay,
  59. planeNormalAndDistance_, rotM);
  60.  
  61. auto pixelIntensity = tempVImg.at<uchar>(Point(j, i));
  62. //std::cout<<"pixel intensity "<< pixelIntensity<<std::endl;
  63.  
  64. }
  65.  
  66. }
  67.  
  68. //imshow("Virtual", tempVImg);
  69. Mat integralV;
  70. cv::integral(tempVImg, integralV);
  71.  
  72. //std::cout<<"integral image type is "<<integralV.type()<<std::endl;
  73.  
  74. rcpFinder::normalizePixelsImg(tempVImg, integralV, V_);
  75.  
  76.  
  77.  
  78.  
  79.  
  80. /*imshow("Normalized Img", V_);
  81. waitKey(0);*/
  82.  
  83. }
  84. }
  85.  
  86. // stuff here
  87. const cv::Mat S_;
  88. cv::Mat& V_;
  89. int width_, height_;
  90. map<int, vector<Point3d>> planeNormalAndDistance_;
  91. double *translation_;
  92. double* rotArray_;
  93. double* fP_;
  94.  
  95. };
  96.  
  97. cv::Mat integralImgI;
  98. cv::integral(im1, integralImgI);
  99. cv::Mat normalizedRealImg;
  100. rcpFinder::normalizePixelsImg(im1, integralImgI, normalizedRealImg);
  101.  
  102. Mat normalizedVirtualImg;
  103.  
  104. //ceres::CostFunction* total_cost_function = 0;
  105. for (int i = 1; i < width-1; ++i) {
  106. for (int j = 1; j < height-1 ; ++j) {
  107. ceres::CostFunction* cost_function =
  108. new ceres::NumericDiffCostFunction<RcpAndFpOptimizer, ceres::CENTRAL, 1, 1, 3, 3>(
  109. new RcpAndFpOptimizer(normalizedVirtualImg, normalizedRealImg, i, j, width, height));
  110.  
  111. problem.AddResidualBlock(cost_function, NULL, fp, rotationArray, translation);
  112.  
  113. }
  114. }
  115.  
  116. ceres::Solver::Options options;
  117. options.minimizer_progress_to_stdout = true;
  118. options.max_num_iterations = 50;
  119. options.update_state_every_iteration = true;
  120.  
  121.  
  122.  
  123. options.evaluation_callback = (new evaluation_callback_functor(S, normalizedVirtualImg,width, height,
  124. mapNormalAndDist, translation,rotationArray, fp));
  125.  
  126. ceres::Solver::Summary summary;
  127. ceres::Solve(options, &problem, &summary);
  128.  
  129. std::cout << summary.BriefReport() << "n";
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement