main.cpp 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. #include <iostream>
  2. #include <string>
  3. #include "Steger.h"
  4. #include "pnp.h"
  5. #include "Steger.h"
  6. #include "Interpolator.h"
  7. #include <chrono>
  8. void test_pnp(){
  9. cv::Mat K = ( cv::Mat_<double> ( 3,3 ) << 520.9, 0, 325.1, 0, 521.0, 249.7, 0, 0, 1 );
  10. cv::Mat R=(cv::Mat_<double> ( 3,3 ) <<0.9979193252225095, -0.05138618904650328, 0.03894200717385427,
  11. 0.05033852907733768, 0.9983556574295407, 0.02742286944795593,
  12. -0.04028712992732941, -0.02540552801471822, 0.998865109165653);
  13. cv::Mat T=(cv::Mat_<double>(3,1)<<-0.1255867099750184,-0.007363525258815341,0.0609992658867812);
  14. P3d2d p3d2d(K);
  15. p3d2d.R_=R;
  16. p3d2d.t_=T;
  17. cv::Mat src = cv::imread("../images/1.bmp", 0);
  18. cv::Mat image = src(cv::Rect(0, 0, src.cols, src.rows)).clone();
  19. for (int i = 0; i < image.rows; i++)
  20. {
  21. for (int j = 0; j < image.cols; j++)
  22. {
  23. if (image.at<uchar>(i, j) < 20)
  24. image.at<uchar>(i, j) = 0;
  25. }
  26. }
  27. cv::Mat out= cv::Mat::zeros(image.size(), CV_8UC1);
  28. CSteger steger;
  29. cv::GaussianBlur(image, image, cv::Size(3, 3), 3, 3);
  30. std::vector<cv::Point2d> points = steger.StripCenter(image);
  31. auto t1=std::chrono::steady_clock::now();
  32. p3d2d.execute(points);
  33. auto t2=std::chrono::steady_clock::now();
  34. auto duration = std::chrono::duration_cast<std::chrono::milliseconds>(t2 - t1);
  35. double tm=double(duration.count()) * std::chrono::milliseconds::period::num /
  36. std::chrono::milliseconds::period::den;
  37. printf("points :%d time :%lf s\n",points.size(),tm);
  38. }
  39. void pnpcaliber_test()
  40. {
  41. cv::FileStorage fread("../cfg/cameraMatrix.yaml", cv::FileStorage::READ);
  42. //判断是否成功打开文件
  43. if (!fread.isOpened())
  44. {
  45. std::cout << "打开文件失败,请确认文件名称是否正确!" << std::endl;
  46. return ;
  47. }
  48. //读取文件中的数据
  49. cv::Mat K,distCoeffs;
  50. fread["cameraMatrix"] >> K;
  51. fread["distCoeffs"] >> distCoeffs;
  52. cv::FileStorage Pread("../cfg/P.yaml", cv::FileStorage::READ);
  53. //判断是否成功打开文件
  54. if (!Pread.isOpened())
  55. {
  56. std::cout << "打开文件失败,请确认文件名称是否正确!" << std::endl;
  57. return ;
  58. }
  59. cv::Mat P;
  60. Pread["P"] >> P;
  61. cv::Mat src7= cv::imread("../images/calib/160120.png", 0);
  62. std::vector<cv::Point3d> pts3d=Interpolator::predict4(src7,K,distCoeffs,P);
  63. FILE* pf=fopen("../images/pred3d.txt","w");
  64. for(int i=0;i<pts3d.size();++i)
  65. {
  66. fprintf(pf,"%f %f %f\n",pts3d[i].x,pts3d[i].y,pts3d[i].z);
  67. printf("%.3f %.3f %.5f\n",pts3d[i].x,pts3d[i].y,pts3d[i].z);
  68. }
  69. fclose(pf);
  70. }
  71. void pnpcalibe()
  72. {
  73. cv::FileStorage fread("../cfg/cameraMatrix.yaml", cv::FileStorage::READ);
  74. //判断是否成功打开文件
  75. if (!fread.isOpened())
  76. {
  77. std::cout << "打开文件失败,请确认文件名称是否正确!" << std::endl;
  78. return ;
  79. }
  80. //读取文件中的数据
  81. cv::Mat K,distCoeffs;
  82. fread["cameraMatrix"] >> K;
  83. fread["distCoeffs"] >> distCoeffs;
  84. Interpolator caliber;
  85. cv::Mat src1 = cv::imread("../images/calib/0.png", 0);
  86. caliber.AddImage(Interpolator::undistored_image(src1,K,distCoeffs), 0.8);
  87. cv::Mat src2 = cv::imread("../images/calib/40940.png", 0);
  88. caliber.AddImage(Interpolator::undistored_image(src2,K,distCoeffs), 0.840940);
  89. cv::Mat src3 = cv::imread("../images/calib/80975.png", 0);
  90. caliber.AddImage(Interpolator::undistored_image(src3,K,distCoeffs), 0.880970);
  91. cv::Mat src4 = cv::imread("../images/calib/119645.png", 0);
  92. caliber.AddImage(Interpolator::undistored_image(src4,K,distCoeffs), 0.919645);
  93. cv::Mat src5 = cv::imread("../images/calib/199580.png", 0);
  94. caliber.AddImage(Interpolator::undistored_image(src5,K,distCoeffs), 0.999580);
  95. cv::Mat src6 = cv::imread("../images/calib/240370.png", 0);
  96. caliber.AddImage(Interpolator::undistored_image(src6,K,distCoeffs), 1.040370);
  97. cv::Mat P = caliber.Calib4();
  98. cv::FileStorage fwrite("../cfg/P.yaml", cv::FileStorage::WRITE);
  99. fwrite.write("P", P);
  100. std::cout << P.t() << std::endl;
  101. }
  102. #include <list>
  103. int main()
  104. {
  105. pnpcaliber_test();
  106. //pnpcalibe();
  107. return 0;
  108. //test_pnp();
  109. /*Mat image = cv::imread(file, 0);
  110. Mat image = src(Rect(0, 800, src.cols, 500));
  111. for (int i = 0; i < image.rows; i++)
  112. {
  113. for (int j = 0; j < image.cols; j++)
  114. {
  115. if (image.at<uchar>(i, j) < 10)
  116. image.at<uchar>(i, j) = 0;
  117. }
  118. }
  119. Mat fimage, fout;
  120. image.convertTo(fimage, CV_32FC1);
  121. fout = Mat::zeros(fimage.size(), CV_32FC1);
  122. double sigma = 2.9;
  123. double high = 3.5;
  124. double low = 0.5;
  125. int rows = fimage.rows;
  126. int cols = fimage.cols;
  127. IplImage ipl_image(fimage);
  128. IplImage ipl_image_out(fout);
  129. fout = Mat::zeros(fimage.size(), CV_32FC1);
  130. ipl_image_out = IplImage(fout);
  131. CSteger(sigma, high, low, &ipl_image, &ipl_image_out);
  132. int k = 391;
  133. while (1)
  134. {
  135. char file[64];
  136. memset(file, 0, 64);
  137. sprintf(file, "../images/h/%d.bmp", k);
  138. k++;
  139. //fimage = fimage / 255.0;
  140. LARGE_INTEGER freq, t0, t1;
  141. QueryPerformanceFrequency(&freq);
  142. QueryPerformanceCounter(&t0);
  143. QueryPerformanceCounter(&t1);
  144. printf("time=%lf\n", (((t1.QuadPart - t0.QuadPart) * 1000.0) / freq.QuadPart));
  145. char path[255];
  146. memset(path, 0, 255);
  147. sprintf(path, "Steger\\black\\black_%f_%f_%f.bmp", sigma, low, high);
  148. Mat result;
  149. fout = fout * 255;
  150. fout.convertTo(result, CV_8UC1);
  151. cv::imwrite(path, fout);
  152. /*printf("2--------------------------------\n");
  153. for (sigma = 2.5; sigma <3.9;sigma+=0.1)
  154. {
  155. for (high = 3.5; high < 4.0; high += 0.5)
  156. {
  157. for (low = 0.5; low <1; low += 0.5)
  158. {
  159. QueryPerformanceFrequency(&freq);
  160. QueryPerformanceCounter(&t0);
  161. Steger(sigma, high, low, &ipl_image, &ipl_image_out);
  162. QueryPerformanceCounter(&t1);
  163. printf("time=%lf\n", (((t1.QuadPart - t0.QuadPart) * 1000.0) / freq.QuadPart));
  164. char path[255];
  165. memset(path, 0, 255);
  166. sprintf(path, "Steger\\1\\1_%f_%f_%f.bmp", sigma, low,high);
  167. cv::imwrite(path, fout * 255);
  168. }
  169. }
  170. }
  171. }
  172. std::cout << "ok" << endl;
  173. getchar();*/
  174. return 0;
  175. }