123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232 |
- #include <iostream>
- #include <string>
- #include "Steger.h"
- #include "pnp.h"
- #include "Steger.h"
- #include "Interpolator.h"
- #include <chrono>
- void test_pnp(){
- cv::Mat K = ( cv::Mat_<double> ( 3,3 ) << 520.9, 0, 325.1, 0, 521.0, 249.7, 0, 0, 1 );
- cv::Mat R=(cv::Mat_<double> ( 3,3 ) <<0.9979193252225095, -0.05138618904650328, 0.03894200717385427,
- 0.05033852907733768, 0.9983556574295407, 0.02742286944795593,
- -0.04028712992732941, -0.02540552801471822, 0.998865109165653);
- cv::Mat T=(cv::Mat_<double>(3,1)<<-0.1255867099750184,-0.007363525258815341,0.0609992658867812);
- P3d2d p3d2d(K);
- p3d2d.R_=R;
- p3d2d.t_=T;
- cv::Mat src = cv::imread("../images/1.bmp", 0);
- cv::Mat image = src(cv::Rect(0, 0, src.cols, src.rows)).clone();
- for (int i = 0; i < image.rows; i++)
- {
- for (int j = 0; j < image.cols; j++)
- {
- if (image.at<uchar>(i, j) < 20)
- image.at<uchar>(i, j) = 0;
- }
- }
- cv::Mat out= cv::Mat::zeros(image.size(), CV_8UC1);
- CSteger steger;
- cv::GaussianBlur(image, image, cv::Size(3, 3), 3, 3);
- std::vector<cv::Point2d> points = steger.StripCenter(image);
- auto t1=std::chrono::steady_clock::now();
- p3d2d.execute(points);
- auto t2=std::chrono::steady_clock::now();
- auto duration = std::chrono::duration_cast<std::chrono::milliseconds>(t2 - t1);
- double tm=double(duration.count()) * std::chrono::milliseconds::period::num /
- std::chrono::milliseconds::period::den;
- printf("points :%d time :%lf s\n",points.size(),tm);
- }
- void pnpcaliber_test()
- {
- cv::FileStorage fread("../cfg/cameraMatrix.yaml", cv::FileStorage::READ);
- //判断是否成功打开文件
- if (!fread.isOpened())
- {
- std::cout << "打开文件失败,请确认文件名称是否正确!" << std::endl;
- return ;
- }
- //读取文件中的数据
- cv::Mat K,distCoeffs;
- fread["cameraMatrix"] >> K;
- fread["distCoeffs"] >> distCoeffs;
- cv::FileStorage Pread("../cfg/P.yaml", cv::FileStorage::READ);
- //判断是否成功打开文件
- if (!Pread.isOpened())
- {
- std::cout << "打开文件失败,请确认文件名称是否正确!" << std::endl;
- return ;
- }
- cv::Mat P;
- Pread["P"] >> P;
- cv::Mat src7= cv::imread("../images/calib/160120.png", 0);
- std::vector<cv::Point3d> pts3d=Interpolator::predict4(src7,K,distCoeffs,P);
- FILE* pf=fopen("../images/pred3d.txt","w");
- for(int i=0;i<pts3d.size();++i)
- {
- fprintf(pf,"%f %f %f\n",pts3d[i].x,pts3d[i].y,pts3d[i].z);
- printf("%.3f %.3f %.5f\n",pts3d[i].x,pts3d[i].y,pts3d[i].z);
- }
- fclose(pf);
- }
- void pnpcalibe()
- {
- cv::FileStorage fread("../cfg/cameraMatrix.yaml", cv::FileStorage::READ);
- //判断是否成功打开文件
- if (!fread.isOpened())
- {
- std::cout << "打开文件失败,请确认文件名称是否正确!" << std::endl;
- return ;
- }
- //读取文件中的数据
- cv::Mat K,distCoeffs;
- fread["cameraMatrix"] >> K;
- fread["distCoeffs"] >> distCoeffs;
- Interpolator caliber;
- cv::Mat src1 = cv::imread("../images/calib/0.png", 0);
- caliber.AddImage(Interpolator::undistored_image(src1,K,distCoeffs), 0.8);
- cv::Mat src2 = cv::imread("../images/calib/40940.png", 0);
- caliber.AddImage(Interpolator::undistored_image(src2,K,distCoeffs), 0.840940);
- cv::Mat src3 = cv::imread("../images/calib/80975.png", 0);
- caliber.AddImage(Interpolator::undistored_image(src3,K,distCoeffs), 0.880970);
- cv::Mat src4 = cv::imread("../images/calib/119645.png", 0);
- caliber.AddImage(Interpolator::undistored_image(src4,K,distCoeffs), 0.919645);
- cv::Mat src5 = cv::imread("../images/calib/199580.png", 0);
- caliber.AddImage(Interpolator::undistored_image(src5,K,distCoeffs), 0.999580);
- cv::Mat src6 = cv::imread("../images/calib/240370.png", 0);
- caliber.AddImage(Interpolator::undistored_image(src6,K,distCoeffs), 1.040370);
- cv::Mat P = caliber.Calib4();
- cv::FileStorage fwrite("../cfg/P.yaml", cv::FileStorage::WRITE);
- fwrite.write("P", P);
- std::cout << P.t() << std::endl;
- }
- #include <list>
- int main()
- {
- pnpcaliber_test();
- //pnpcalibe();
- return 0;
- //test_pnp();
- /*Mat image = cv::imread(file, 0);
- Mat image = src(Rect(0, 800, src.cols, 500));
- for (int i = 0; i < image.rows; i++)
- {
- for (int j = 0; j < image.cols; j++)
- {
- if (image.at<uchar>(i, j) < 10)
- image.at<uchar>(i, j) = 0;
- }
- }
- Mat fimage, fout;
- image.convertTo(fimage, CV_32FC1);
- fout = Mat::zeros(fimage.size(), CV_32FC1);
- double sigma = 2.9;
- double high = 3.5;
- double low = 0.5;
- int rows = fimage.rows;
- int cols = fimage.cols;
- IplImage ipl_image(fimage);
- IplImage ipl_image_out(fout);
- fout = Mat::zeros(fimage.size(), CV_32FC1);
- ipl_image_out = IplImage(fout);
- CSteger(sigma, high, low, &ipl_image, &ipl_image_out);
- int k = 391;
- while (1)
- {
- char file[64];
- memset(file, 0, 64);
- sprintf(file, "../images/h/%d.bmp", k);
- k++;
- //fimage = fimage / 255.0;
- LARGE_INTEGER freq, t0, t1;
- QueryPerformanceFrequency(&freq);
- QueryPerformanceCounter(&t0);
- QueryPerformanceCounter(&t1);
- printf("time=%lf\n", (((t1.QuadPart - t0.QuadPart) * 1000.0) / freq.QuadPart));
- char path[255];
- memset(path, 0, 255);
- sprintf(path, "Steger\\black\\black_%f_%f_%f.bmp", sigma, low, high);
- Mat result;
- fout = fout * 255;
- fout.convertTo(result, CV_8UC1);
- cv::imwrite(path, fout);
- /*printf("2--------------------------------\n");
- for (sigma = 2.5; sigma <3.9;sigma+=0.1)
- {
- for (high = 3.5; high < 4.0; high += 0.5)
- {
- for (low = 0.5; low <1; low += 0.5)
- {
- QueryPerformanceFrequency(&freq);
- QueryPerformanceCounter(&t0);
- Steger(sigma, high, low, &ipl_image, &ipl_image_out);
- QueryPerformanceCounter(&t1);
- printf("time=%lf\n", (((t1.QuadPart - t0.QuadPart) * 1000.0) / freq.QuadPart));
- char path[255];
- memset(path, 0, 255);
- sprintf(path, "Steger\\1\\1_%f_%f_%f.bmp", sigma, low,high);
- cv::imwrite(path, fout * 255);
- }
- }
- }
- }
- std::cout << "ok" << endl;
- getchar();*/
- return 0;
- }
|