diff --git a/AboutUs.ui b/AboutUs.ui index a8ef785..e1cb357 100644 --- a/AboutUs.ui +++ b/AboutUs.ui @@ -56,7 +56,7 @@ - <html><head/><body><p>产品: TStoneCalibration</p><p>版本: 1.2.6</p><p>开发: 基于<a href="https://www.qt.io/"><span style=" text-decoration: underline; color:#0000ff;">Qt</span></a>和开源项目<a href="https://opencv.org/"><span style=" text-decoration: underline; color:#0000ff;">OpenCV</span></a>开发</p></body></html> + <html><head/><body><p>产品: TStoneCalibration</p><p>版本: 1.3.0</p><p>开发: 基于<a href="https://www.qt.io/"><span style=" text-decoration: underline; color:#0000ff;">Qt</span></a>和开源项目<a href="https://opencv.org/"><span style=" text-decoration: underline; color:#0000ff;">OpenCV</span></a>开发</p></body></html> Qt::AlignLeading|Qt::AlignLeft|Qt::AlignTop @@ -93,7 +93,7 @@ - <html><head/><body><p>实验室官网: <a href="http://ri.cuhk.edu.hk/"><span style=" text-decoration: underline; color:#0000ff;">http://ri.cuhk.edu.hk/</span></a></p><p>联系我们: big.uncle@foxmail.com</p><p><span style=" font-size:7pt;">Copyright © 2020 CUHK CURI Embedded AI Group</span></p></body></html> + <html><head/><body><p>实验室官网: <a href="http://ri.cuhk.edu.hk/"><span style=" text-decoration: underline; color:#0000ff;">http://ri.cuhk.edu.hk/</span></a></p><p>联系我们: big.uncle@foxmail.com</p><p><span style=" font-size:7pt;">Copyright © 2021 CUHK CURI Embedded AI Group</span></p></body></html> Qt::AlignLeading|Qt::AlignLeft|Qt::AlignTop diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 0000000..61e7c55 --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,37 @@ +cmake_minimum_required(VERSION 2.8.11) + +project(TStoneCalib) + +set(CMAKE_CXX_STANDARD 11) +set(CMAKE_CXX_STANDARD_REQUIRED ON) + +# init_qt: Let's do the CMake job for us +set(CMAKE_AUTOMOC ON) # For meta object compiler +set(CMAKE_AUTORCC ON) # Resource files +set(CMAKE_AUTOUIC ON) # UI files + +# Find includes in corresponding build directories +set(CMAKE_INCLUDE_CURRENT_DIR ON) + +# Find the QtWidgets library +find_package(Qt5 REQUIRED COMPONENTS Widgets Core Gui) +find_package(OpenCV REQUIRED) + +include_directories( + DetectCorner + Calibrate + ${OpenCV_INCLUDE_DIRS} + ${CMAKE_CURRENT_SOURCE_DIR} + ${CMAKE_CURRENT_BINARY_DIR} +) + +file(GLOB project_SOURCES "*.cpp" "res/*.qrc" "DetectCorner/*.cpp" "Calibrate/*.cpp") + +add_executable(${PROJECT_NAME} ${project_SOURCES}) + +target_link_libraries(${PROJECT_NAME} +${OpenCV_LIBRARIES} +Qt5::Core +Qt5::Gui +Qt5::Widgets +) diff --git a/Calibrate/binocular_calib.cpp b/Calibrate/binocular_calib.cpp new file mode 100644 index 0000000..452f5d4 --- /dev/null +++ b/Calibrate/binocular_calib.cpp @@ -0,0 +1,357 @@ +#include "binocular_calib.h" + +BinoCalib::BinoCalib(): + has_param_(false), + chessboard_size_(0), + fisheye_flag_(false), + map_init_(false) +{ + +} + +BinoCalib::~BinoCalib() +{ + +} + +int BinoCalib::calibrate(std::vector& imgs, bool fisheye, bool tangential, bool k3) +{ + if(imgs.size() <= 3) + { + return 1; + } + fisheye_flag_ = fisheye; + cv::Mat img = cv::imread(imgs[0].left_path); + cv::Size img_size = img.size(); + std::vector> left_img_points; + std::vector> right_img_points; + std::vector> world_points; + std::vector left_tvecsMat; + std::vector left_rvecsMat; + std::vector right_tvecsMat; + std::vector right_rvecsMat; + std::vector > left_imagePoints; + std::vector > right_imagePoints; + std::vector > objectPoints; + std::vector left_rvecs; + std::vector left_tvecs; + std::vector right_rvecs; + std::vector right_tvecs; + for (unsigned int j = 0; j < imgs.size(); j++) + { + left_img_points.push_back(imgs[j].left_img_points); + right_img_points.push_back(imgs[j].right_img_points); + world_points.push_back(imgs[j].world_points); + std::vector img_p, img_p_; + std::vector obj_p; + for(unsigned int i = 0; i < imgs[j].left_img_points.size(); i++) + { + img_p.push_back(imgs[j].left_img_points[i]); + img_p_.push_back(imgs[j].right_img_points[i]); + obj_p.push_back(imgs[j].world_points[i]); + } + left_imagePoints.push_back(img_p); + right_imagePoints.push_back(img_p_); + objectPoints.push_back(obj_p); + } + cameraMatrix1 = cv::Mat::zeros(cv::Size(3, 3), CV_32F); + cameraMatrix2 = cv::Mat::zeros(cv::Size(3, 3), CV_32F); + if(fisheye == false) + { + distCoefficients1 = cv::Mat::zeros(cv::Size(5, 1), CV_32F); + distCoefficients2 = cv::Mat::zeros(cv::Size(5, 1), CV_32F); + } + + // 单目标定 + if(fisheye == false) + { + int flag = 0; + if(!tangential) + flag |= cv::CALIB_ZERO_TANGENT_DIST; + if(!k3) + flag |= cv::CALIB_FIX_K3; + cv::calibrateCamera(world_points, left_img_points, img_size, cameraMatrix1, distCoefficients1, left_rvecsMat, left_tvecsMat, flag); + cv::calibrateCamera(world_points, right_img_points, img_size, cameraMatrix2, distCoefficients2, right_rvecsMat, right_tvecsMat, flag); + } + else + { + int flag = 0; + flag |= cv::fisheye::CALIB_RECOMPUTE_EXTRINSIC; + flag |= cv::fisheye::CALIB_FIX_SKEW; + cv::fisheye::calibrate(objectPoints, left_imagePoints, img_size, K1, D1, left_rvecs, left_tvecs, flag); + cv::fisheye::calibrate(objectPoints, right_imagePoints, img_size, K2, D2, right_rvecs, right_tvecs, flag); + } + for(unsigned int i = 0; i < imgs.size(); i++) + { + if(fisheye == false) + { + imgs[i].left_tvec = left_tvecsMat[i]; + imgs[i].left_rvec = left_rvecsMat[i]; + imgs[i].right_tvec = right_tvecsMat[i]; + imgs[i].right_rvec = right_rvecsMat[i]; + } + else + { + imgs[i].left_fish_tvec = left_tvecs[i]; + imgs[i].left_fish_rvec = left_rvecs[i]; + imgs[i].right_fish_tvec = right_tvecs[i]; + imgs[i].right_fish_rvec = right_rvecs[i]; + } + } + + // 评估标定结果 + std::vector left_error, right_error; + for(unsigned int i = 0; i < imgs.size(); i++) + { + std::vector world_p = imgs[i].world_points; + std::vector left_img_p = imgs[i].left_img_points, right_img_p = imgs[i].right_img_points; + std::vector left_reproject_img_p, right_reproject_img_p; + std::vector left_fisheye_reproject_p, right_fisheye_reproject_p; + if(fisheye == false) + { + projectPoints(world_p, left_rvecsMat[i], left_tvecsMat[i], cameraMatrix1, distCoefficients1, left_reproject_img_p); + projectPoints(world_p, right_rvecsMat[i], right_tvecsMat[i], cameraMatrix2, distCoefficients2, right_reproject_img_p); + } + else + { + cv::fisheye::projectPoints(objectPoints[i], left_fisheye_reproject_p, left_rvecs[i], left_tvecs[i], K1, D1); + cv::fisheye::projectPoints(objectPoints[i], right_fisheye_reproject_p, right_rvecs[i], right_tvecs[i], K2, D2); + } + float left_err = 0, right_err = 0; + for (unsigned int j = 0; j < world_p.size(); j++) + { + if(fisheye == false) + { + left_err += sqrt((left_img_p[j].x-left_reproject_img_p[j].x)*(left_img_p[j].x-left_reproject_img_p[j].x)+ + (left_img_p[j].y-left_reproject_img_p[j].y)*(left_img_p[j].y-left_reproject_img_p[j].y)); + right_err += sqrt((right_img_p[j].x-right_reproject_img_p[j].x)*(right_img_p[j].x-right_reproject_img_p[j].x)+ + (right_img_p[j].y-right_reproject_img_p[j].y)*(right_img_p[j].y-right_reproject_img_p[j].y)); + } + else + { + left_err += sqrt((left_img_p[j].x-left_fisheye_reproject_p[j].x)*(left_img_p[j].x-left_fisheye_reproject_p[j].x)+ + (left_img_p[j].y-left_fisheye_reproject_p[j].y)*(left_img_p[j].y-left_fisheye_reproject_p[j].y)); + right_err += sqrt((right_img_p[j].x-right_fisheye_reproject_p[j].x)*(right_img_p[j].x-right_fisheye_reproject_p[j].x)+ + (right_img_p[j].y-right_fisheye_reproject_p[j].y)*(right_img_p[j].y-right_fisheye_reproject_p[j].y)); + } + } + left_error.push_back(left_err/world_p.size()); + right_error.push_back(right_err/world_p.size()); + } + int max_idx = max_element(left_error.begin(), left_error.end()) - left_error.begin(); + float max_error = left_error[max_idx]; + max_idx = max_element(right_error.begin(), right_error.end()) - right_error.begin(); + max_error = std::max(max_error, right_error[max_idx]); + int width = 480 / imgs.size(); + cv::Mat error_plot = cv::Mat(260, 560, CV_32FC3, cv::Scalar(255,255,255)); + cv::rectangle(error_plot, cv::Rect(40, 20, 480, 200), cv::Scalar(0, 0, 0),1, cv::LINE_8,0); + cv::putText(error_plot, "0", cv::Point(20, 220), cv::FONT_HERSHEY_SIMPLEX, 0.3, cv::Scalar(0, 0, 0), 1, 8, 0); + char *chCode; + chCode = new(std::nothrow)char[20]; + sprintf(chCode, "%.2lf", max_error*200/195); + std::string strCode(chCode); + delete []chCode; + cv::putText(error_plot, strCode, cv::Point(10, 20), cv::FONT_HERSHEY_SIMPLEX, 0.3, cv::Scalar(0, 0, 0), 1, 8, 0); + for(unsigned int i = 0; i < imgs.size(); i++) + { + int height = 195*left_error[i]/max_error; + cv::rectangle(error_plot, cv::Rect(i*width+43, 220-height, width/2-4, height), cv::Scalar(255,0,0), -1, cv::LINE_8,0); + height = 195*right_error[i]/max_error; + cv::rectangle(error_plot, cv::Rect(i*width+41+width/2, 220-height, width/2-4, height), cv::Scalar(0,255,0), -1, cv::LINE_8,0); + cv::putText(error_plot, std::to_string(i), cv::Point(i*width+40+width/2-2, 240), cv::FONT_HERSHEY_SIMPLEX, 0.3, cv::Scalar(0, 0, 0), 1, 8, 0); + } + + // 双目标定 + if(fisheye == false) + { + int flag = 0; + if(!tangential) + flag |= cv::CALIB_ZERO_TANGENT_DIST; + if(!k3) + flag |= cv::CALIB_FIX_K3; + flag |= cv::CALIB_USE_INTRINSIC_GUESS; + cv::stereoCalibrate(world_points, left_img_points, right_img_points, + cameraMatrix1, distCoefficients1, + cameraMatrix2, distCoefficients2, + img_size, R, T, cv::noArray(), cv::noArray(), flag); + cv::stereoRectify(cameraMatrix1, distCoefficients1, + cameraMatrix2, distCoefficients2, + img_size, R, T, + R1, R2, P1, P2, Q, cv::CALIB_ZERO_DISPARITY, 0, img_size); + } + else + { + int flag = 0; + flag |= cv::fisheye::CALIB_FIX_SKEW; + flag |= cv::fisheye::CALIB_USE_INTRINSIC_GUESS; + try { + cv::fisheye::stereoCalibrate(objectPoints, left_imagePoints, right_imagePoints, + K1, D1, + K2, D2, + img_size, R, T, flag); + } catch (cv::Exception& e) { + return 2; + } + + cv::fisheye::stereoRectify(K1, D1, + K2, D2, + img_size, R, T, + R1, R2, P1, P2, Q, 0, img_size); + } + has_param_ = true; + cv::imshow("error", error_plot); +} + +void BinoCalib::exportParam(std::string file_name) +{ + cv::FileStorage fs_write(file_name, cv::FileStorage::WRITE); + if(fisheye_flag_ == false) + { + fs_write << "left_camera_Matrix" << cameraMatrix1 << "left_camera_distCoeffs" << distCoefficients1; + fs_write << "right_camera_Matrix" << cameraMatrix2 << "right_camera_distCoeffs" << distCoefficients2; + fs_write << "Rotate_Matrix" << R << "Translate_Matrix" << T; + fs_write << "R1" << R1 << "R2" << R2 << "P1" << P1 << "P2" << P2 << "Q" << Q; + } + else + { + cv::Mat camera_matrix = cv::Mat::zeros(cv::Size(3,3), CV_64F); + camera_matrix.at(0,0) = K1(0,0); + camera_matrix.at(0,2) = K1(0,2); + camera_matrix.at(1,1) = K1(1,1); + camera_matrix.at(1,2) = K1(1,2); + camera_matrix.at(2,2) = 1; + cv::Mat Dist = (cv::Mat_(1,4) << D1[0], D1[1], D1[2], D1[3]); + fs_write << "left_camera_Matrix" << camera_matrix << "left_camera_distCoeffs" << Dist; + camera_matrix.at(0,0) = K2(0,0); + camera_matrix.at(0,2) = K2(0,2); + camera_matrix.at(1,1) = K2(1,1); + camera_matrix.at(1,2) = K2(1,2); + Dist = (cv::Mat_(1,4) << D2[0], D2[1], D2[2], D2[3]); + fs_write << "right_camera_Matrix" << camera_matrix << "right_camera_distCoeffs" << Dist; + fs_write << "Rotate_Matrix" << R << "Translate_Matrix" << T; + fs_write << "R1" << R1 << "R2" << R2 << "P1" << P1 << "P2" << P2 << "Q" << Q; + } + fs_write.release(); +} + +void BinoCalib::setCameraParam(cv::Mat intrinsic1, cv::Mat distCoefficients1_, + cv::Mat intrinsic2, cv::Mat distCoefficients2_, bool fisheye, + cv::Mat R_, cv::Mat T_, cv::Mat R1_, cv::Mat R2_, cv::Mat P1_, cv::Mat P2_, cv::Mat Q_) +{ + R = R_; + T = T_; + P1 = P1_; + P2 = P2_; + R1 = R1_; + R2 = R2_; + Q = Q_; + if(fisheye == false) + { + cameraMatrix1 = intrinsic1; + distCoefficients1 = distCoefficients1_; + cameraMatrix2 = intrinsic2; + distCoefficients2 = distCoefficients2_; + } + else + { + K1(0,0) = intrinsic1.at(0,0); + K1(0,1) = 0; + K1(0,2) = intrinsic1.at(0,2); + K1(1,0) = 0; + K1(1,1) = intrinsic1.at(1,1); + K1(1,2) = intrinsic1.at(1,2); + K1(1,0) = 0; + K1(2,1) = 0; + K1(2,2) = 1; + D1[0] = distCoefficients1_.at(0,0); + D1[1] = distCoefficients1_.at(0,1); + D1[2] = distCoefficients1_.at(0,2); + D1[3] = distCoefficients1_.at(0,3); + + K2(0,0) = intrinsic2.at(0,0); + K2(0,1) = 0; + K2(0,2) = intrinsic2.at(0,2); + K2(1,0) = 0; + K2(1,1) = intrinsic2.at(1,1); + K2(1,2) = intrinsic2.at(1,2); + K2(1,0) = 0; + K2(2,1) = 0; + K2(2,2) = 1; + D2[0] = distCoefficients2_.at(0,0); + D2[1] = distCoefficients2_.at(0,1); + D2[2] = distCoefficients2_.at(0,2); + D2[3] = distCoefficients2_.at(0,3); + } + has_param_ = true; +} + +void BinoCalib::undistort(cv::Mat& left, cv::Mat& right) +{ + if(!has_param_) return; + if(map_init_ == false) + { + if(fisheye_flag_ == false) + { + cv::initUndistortRectifyMap(cameraMatrix1, distCoefficients1, R1, P1, left.size(), CV_16SC2, left_mapx, left_mapy); + cv::initUndistortRectifyMap(cameraMatrix2, distCoefficients2, R2, P2, left.size(), CV_16SC2, right_mapx, right_mapy); + } + else + { + cv::fisheye::initUndistortRectifyMap(K1, D1, R1, P1, left.size(), CV_16SC2, left_mapx, left_mapy); + cv::fisheye::initUndistortRectifyMap(K2, D2, R2, P2, left.size(), CV_16SC2, right_mapx, right_mapy); + } + map_init_ = true; + } + cv::remap(left, left, left_mapx, left_mapy, cv::INTER_LINEAR); + cv::remap(right, right, right_mapx, right_mapy, cv::INTER_LINEAR); +} + +void BinoCalib::showCorners(cv::Mat& left, cv::Mat& right, Stereo_Img_t& img) +{ + // 非矫正模式下显示角点位置 + for(unsigned int i = 0; i < img.left_img_points.size(); i++) + { + cv::circle(left, img.left_img_points[i], 5, cv::Scalar(0,0,255), 2); + } + for(unsigned int i = 0; i < img.right_img_points.size(); i++) + { + cv::circle(right, img.right_img_points[i], 5, cv::Scalar(0,0,255), 2); + } + if(has_param_) + { + // 显示标定后角点的重投影 + if(fisheye_flag_ == false) + { + std::vector reproject_img_p; + projectPoints(img.world_points, img.left_rvec, img.left_tvec, cameraMatrix1, distCoefficients1, reproject_img_p); + for(unsigned int i = 0; i < reproject_img_p.size(); i++) + { + cv::circle(left, reproject_img_p[i], 3, cv::Scalar(255,0,0), 2); + } + projectPoints(img.world_points, img.right_rvec, img.right_tvec, cameraMatrix2, distCoefficients2, reproject_img_p); + for(unsigned int i = 0; i < reproject_img_p.size(); i++) + { + cv::circle(right, reproject_img_p[i], 3, cv::Scalar(255,0,0), 2); + } + } + else + { + std::vector reproject_img_p; + std::vector w_p_; + for(unsigned int i = 0; i < img.world_points.size(); i++) + { + w_p_.push_back(img.world_points[i]); + } + cv::fisheye::projectPoints(w_p_, reproject_img_p, img.left_fish_rvec, img.left_fish_tvec, K1, D1); + for(unsigned int i = 0; i < reproject_img_p.size(); i++) + { + cv::circle(left, reproject_img_p[i], 3, cv::Scalar(255,0,0), 2); + } + cv::fisheye::projectPoints(w_p_, reproject_img_p, img.right_fish_rvec, img.right_fish_tvec, K2, D2); + for(unsigned int i = 0; i < reproject_img_p.size(); i++) + { + cv::circle(right, reproject_img_p[i], 3, cv::Scalar(255,0,0), 2); + } + } + } +} \ No newline at end of file diff --git a/Calibrate/binocular_calib.h b/Calibrate/binocular_calib.h new file mode 100644 index 0000000..8431f62 --- /dev/null +++ b/Calibrate/binocular_calib.h @@ -0,0 +1,54 @@ +#ifndef BINOCULAR_CALIB_H +#define BINOCULAR_CALIB_H + +#include +#include "types.h" + +class BinoCalib +{ + public: + BinoCalib(); + ~BinoCalib(); + int calibrate(std::vector& imgs, bool fisheye, bool tangential, bool k3); + void setChessboardSize(double chessboard_size) + { + chessboard_size_ = chessboard_size; + } + bool isChessboardSizeValid() + { + if(chessboard_size_ > 0) return true; + else return false; + } + void undistort(cv::Mat& left, cv::Mat &right); + void setCameraParam(cv::Mat intrinsic1, cv::Mat distCoefficients1_, + cv::Mat intrinsic2, cv::Mat distCoefficients2_, bool fisheye, + cv::Mat R, cv::Mat T, cv::Mat R1, cv::Mat R2, cv::Mat P1, cv::Mat P2, cv::Mat Q); + void reset() + { + chessboard_size_ = 0; + has_param_ = false; + } + void exportParam(std::string file_name); + void showCorners(cv::Mat& left, cv::Mat& right, Stereo_Img_t& img); + double getChessboardSize() + { + return chessboard_size_; + } + bool hasParam() + { + return has_param_; + } + private: + cv::Mat cameraMatrix1, cameraMatrix2; + cv::Mat distCoefficients1, distCoefficients2; + cv::Matx33d K1, K2; + cv::Vec4d D1, D2; + bool has_param_; + bool fisheye_flag_; + float chessboard_size_; + cv::Mat R, T, R1, R2, P1, P2, Q; + bool map_init_; + cv::Mat left_mapx, left_mapy, right_mapx, right_mapy; +}; + +#endif \ No newline at end of file diff --git a/Calibrate/monocular_calib.cpp b/Calibrate/monocular_calib.cpp new file mode 100644 index 0000000..4a2ae85 --- /dev/null +++ b/Calibrate/monocular_calib.cpp @@ -0,0 +1,226 @@ +#include "monocular_calib.h" + +MonoCalib::MonoCalib(): + has_param_(false), + chessboard_size_(20.0), + fisheye_flag_(false), + map_init_(false) +{ + +} + +MonoCalib::~MonoCalib() +{ + +} + +int MonoCalib::calibrate(std::vector& imgs, bool fisheye, bool tangential, bool k3) +{ + if(imgs.size() <= 3) + { + return 1; + } + cv::Mat img = cv::imread(imgs[0].file_path); + cv::Size img_size = img.size(); + fisheye_flag_ = fisheye; + std::vector> img_points; + std::vector> world_points; + std::vector tvecsMat; + std::vector rvecsMat; + std::vector > imagePoints; + std::vector > objectPoints; + std::vector rvecs; + std::vector tvecs; + for (unsigned int j = 0; j < imgs.size(); j++) + { + img_points.push_back(imgs[j].img_points); + world_points.push_back(imgs[j].world_points); + std::vector img_p; + std::vector obj_p; + for(unsigned int i = 0; i < imgs[j].img_points.size(); i++) + { + img_p.push_back(imgs[j].img_points[i]); + obj_p.push_back(imgs[j].world_points[i]); + } + imagePoints.push_back(img_p); + objectPoints.push_back(obj_p); + } + intrinsic_ = cv::Mat::zeros(cv::Size(3, 3), CV_32F); + if(fisheye_flag_ == false) + distCoefficients_ = cv::Mat::zeros(cv::Size(5, 1), CV_32F); + + if(fisheye_flag_ == false) + { + int flag = 0; + if(!tangential) + flag |= cv::CALIB_ZERO_TANGENT_DIST; + if(!k3) + flag |= cv::CALIB_FIX_K3; + cv::calibrateCamera(world_points, img_points, img_size, intrinsic_, distCoefficients_, rvecsMat, tvecsMat, flag); + } + else + { + int flag = 0; + flag |= cv::fisheye::CALIB_RECOMPUTE_EXTRINSIC; + flag |= cv::fisheye::CALIB_FIX_SKEW; + try { + cv::fisheye::calibrate(objectPoints, imagePoints, img_size, K, D, rvecs, tvecs, flag); + } catch (cv::Exception& e) { + return 2; + } + } + has_param_ = true; + for(unsigned int i = 0; i < imgs.size(); i++) + { + if(fisheye_flag_ == false) + { + imgs[i].tvec = tvecsMat[i]; + imgs[i].rvec = rvecsMat[i]; + } + else + { + imgs[i].fish_tvec = tvecs[i]; + imgs[i].fish_rvec = rvecs[i]; + } + } + // 评估标定结果 + std::vector error; + for(unsigned int i = 0; i < imgs.size(); i++) + { + std::vector world_p = imgs[i].world_points; + std::vector img_p = imgs[i].img_points, reproject_img_p; + std::vector fisheye_reproject_p; + if(fisheye_flag_ == false) + projectPoints(world_p, rvecsMat[i], tvecsMat[i], intrinsic_, distCoefficients_, reproject_img_p); + else + cv::fisheye::projectPoints(objectPoints[i], fisheye_reproject_p, rvecs[i], tvecs[i], K, D); + float err = 0; + for (unsigned int j = 0; j < img_p.size(); j++) + { + if(fisheye_flag_ == false) + err += sqrt((img_p[j].x-reproject_img_p[j].x)*(img_p[j].x-reproject_img_p[j].x)+ + (img_p[j].y-reproject_img_p[j].y)*(img_p[j].y-reproject_img_p[j].y)); + else + err += sqrt((img_p[j].x-fisheye_reproject_p[j].x)*(img_p[j].x-fisheye_reproject_p[j].x)+ + (img_p[j].y-fisheye_reproject_p[j].y)*(img_p[j].y-fisheye_reproject_p[j].y)); + } + error.push_back(err/img_p.size()); + } + int max_idx = max_element(error.begin(), error.end()) - error.begin(); + float max_error = error[max_idx]; + int width = 240 / imgs.size(); + cv::Mat error_plot = cv::Mat(260, 320, CV_32FC3, cv::Scalar(255,255,255)); + cv::rectangle(error_plot, cv::Rect(40, 20, 240, 200), cv::Scalar(0, 0, 0),1, cv::LINE_8,0); + cv::putText(error_plot, "0", cv::Point(20, 220), cv::FONT_HERSHEY_SIMPLEX, 0.3, cv::Scalar(0, 0, 0), 1, 8, 0); + char *chCode; + chCode = new(std::nothrow)char[20]; + sprintf(chCode, "%.2lf", max_error*200/195); + std::string strCode(chCode); + delete []chCode; + cv::putText(error_plot, strCode, cv::Point(10, 20), cv::FONT_HERSHEY_SIMPLEX, 0.3, cv::Scalar(0, 0, 0), 1, 8, 0); + for(unsigned int i = 0; i < imgs.size(); i++) + { + int height = 195*error[i]/max_error; + cv::rectangle(error_plot, cv::Rect(i*width+41, 220-height, width-2, height), cv::Scalar(255,0,0), -1, cv::LINE_8,0); + cv::putText(error_plot, std::to_string(i), cv::Point(i*width+40, 240), cv::FONT_HERSHEY_SIMPLEX, 0.3, cv::Scalar(0, 0, 0), 1, 8, 0); + } + cv::imshow("error", error_plot); + return 0; +} + +void MonoCalib::exportParam(std::string file_name) +{ + cv::FileStorage fs_write(file_name, cv::FileStorage::WRITE); + if(fisheye_flag_ == false) + fs_write << "cameraMatrix" << intrinsic_ << "distCoeffs" << distCoefficients_; + else + { + cv::Mat camera_matrix = cv::Mat::zeros(cv::Size(3,3), CV_64F); + camera_matrix.at(0,0) = K(0,0); + camera_matrix.at(0,2) = K(0,2); + camera_matrix.at(1,1) = K(1,1); + camera_matrix.at(1,2) = K(1,2); + camera_matrix.at(2,2) = 1; + cv::Mat Dist = (cv::Mat_(1,4) << D[0], D[1], D[2], D[3]); + fs_write << "cameraMatrix" << camera_matrix << "distCoeffs" << Dist; + } + fs_write.release(); +} + +void MonoCalib::setCameraParam(cv::Mat intrinsic, cv::Mat distCoefficients, bool fisheye) +{ + if(fisheye == false) + { + intrinsic_ = intrinsic; + distCoefficients_ = distCoefficients; + } + else + { + K(0,0) = intrinsic.at(0,0); + K(0,1) = 0; + K(0,2) = intrinsic.at(0,2); + K(1,0) = 0; + K(1,1) = intrinsic.at(1,1); + K(1,2) = intrinsic.at(1,2); + K(1,0) = 0; + K(2,1) = 0; + K(2,2) = 1; + D[0] = distCoefficients.at(0,0); + D[1] = distCoefficients.at(0,1); + D[2] = distCoefficients.at(0,2); + D[3] = distCoefficients.at(0,3); + } + has_param_ = true; +} + +void MonoCalib::undistort(cv::Mat& img) +{ + if(!has_param_) return; + if(map_init_ == false) + { + if(fisheye_flag_ == false) + { + cv::initUndistortRectifyMap(intrinsic_, distCoefficients_, cv::noArray(), intrinsic_, img.size(), CV_16SC2, mapx, mapy); + } + else + { + cv::fisheye::initUndistortRectifyMap(K, D, cv::noArray(), intrinsic_, img.size(), CV_16SC2, mapx, mapy); + } + map_init_ = true; + } + cv::remap(img, img, mapx, mapy, cv::INTER_LINEAR); +} + +void MonoCalib::showCorners(cv::Mat& left, Img_t& img) +{ + for(unsigned int i = 0; i < img.img_points.size(); i++) + { + cv::circle(left, img.img_points[i], 5, cv::Scalar(0,0,255), 2); + } + if(has_param_ == true) + { + if(fisheye_flag_ == false) + { + std::vector reproject_img_p; + projectPoints(img.world_points, img.rvec, img.tvec, intrinsic_, distCoefficients_, reproject_img_p); + for(unsigned int i = 0; i < reproject_img_p.size(); i++) + { + cv::circle(left, reproject_img_p[i], 3, cv::Scalar(255,0,0), 2); + } + } + else + { + std::vector reproject_img_p; + std::vector w_p_; + for(unsigned int i = 0; i < img.world_points.size(); i++) + { + w_p_.push_back(img.world_points[i]); + } + cv::fisheye::projectPoints(w_p_, reproject_img_p, img.fish_rvec, img.fish_tvec, K, D); + for(unsigned int i = 0; i < reproject_img_p.size(); i++) + { + cv::circle(left, reproject_img_p[i], 3, cv::Scalar(255,0,0), 2); + } + } + } +} \ No newline at end of file diff --git a/Calibrate/monocular_calib.h b/Calibrate/monocular_calib.h new file mode 100644 index 0000000..62c6add --- /dev/null +++ b/Calibrate/monocular_calib.h @@ -0,0 +1,42 @@ +#ifndef MONOCULAR_CALIB_H +#define MONOCULAR_CALIB_H + +#include "types.h" +#include + +class MonoCalib +{ + public: + MonoCalib(); + ~MonoCalib(); + int calibrate(std::vector& imgs, bool fisheye, bool tangential, bool k3); + void undistort(cv::Mat& img); + void setCameraParam(cv::Mat intrinsic, cv::Mat distCoefficients, bool fisheye); + void reset() + { + has_param_ = false; + map_init_ = false; + } + void exportParam(std::string file_name); + void showCorners(cv::Mat& left, Img_t& img); + double getChessboardSize() + { + return chessboard_size_; + } + bool hasParam() + { + return has_param_; + } + private: + cv::Mat intrinsic_; + cv::Mat distCoefficients_; + bool has_param_; + bool fisheye_flag_; + double chessboard_size_; + cv::Matx33d K; + cv::Vec4d D; + cv::Mat mapx, mapy; + bool map_init_; +}; + +#endif \ No newline at end of file diff --git a/CameraCalibration.cpp b/CameraCalibration.cpp new file mode 100644 index 0000000..447b95c --- /dev/null +++ b/CameraCalibration.cpp @@ -0,0 +1,798 @@ +#include "CameraCalibration.h" +#include "ui_CameraCalibration.h" + +cv::Mat find_corner_thread_img; +struct Chessboarder_t find_corner_thread_chessboard; + +CameraCalibration::CameraCalibration(QWidget *parent) + : QMainWindow(parent) + , ui(new Ui::CameraCalibration) +{ + ui->setupUi(this); + setMinimumSize(this->width(), this->height()); + ui->addImage->setFlat(true); + ui->calibrate->setFlat(true); + ui->export_1->setFlat(true); + ui->delete_1->setFlat(true); + ui->undistort->setFlat(true); + findcorner_thread = new FindcornerThread(); + connect(findcorner_thread, SIGNAL(isDone()), this, SLOT(DealThreadDone())); + ShowIntro(); + QFont font = ui->menu->font(); + font.setPixelSize(12); + ui->listWidget->setSelectionMode(QAbstractItemView::ExtendedSelection); // 按ctrl多选 + connect(ui->addImage, SIGNAL(clicked()), this, SLOT(addImage())); + connect(ui->delete_1, SIGNAL(clicked()), this, SLOT(deleteImage())); + connect(ui->calibrate, SIGNAL(clicked()), this, SLOT(calibrate())); + connect(ui->fisheye, SIGNAL(stateChanged(int)), this, SLOT(fisheyeModeSwitch(int))); + connect(ui->export_1, SIGNAL(clicked()), this, SLOT(exportParam())); + connect(ui->undistort, SIGNAL(clicked()), this, SLOT(distortModeSwitch())); + connect(ui->listWidget, SIGNAL(currentItemChanged(QListWidgetItem*, QListWidgetItem*)), this, SLOT(chooseImage(QListWidgetItem*, QListWidgetItem*))); + connect(ui->double_camera, SIGNAL(toggled(bool)), this, SLOT(reset())); + connect(ui->single_camera, SIGNAL(toggled(bool)), this, SLOT(reset())); + connect(ui->double_undistort, SIGNAL(toggled(bool)), this, SLOT(reset())); + connect(ui->single_undistort, SIGNAL(toggled(bool)), this, SLOT(reset())); + saveImage = ui->menu->addAction("导出矫正图片"); + font = saveImage->font(); + font.setPixelSize(12); + saveImage->setFont(font); + about = ui->menu->addAction("关于"); + about->setFont(font); + connect(saveImage, SIGNAL(triggered()), this, SLOT(saveUndistort())); + connect(about, SIGNAL(triggered()), this, SLOT(showIntro())); +} + +CameraCalibration::~CameraCalibration() +{ + delete ui; + deleteLater(); +} + +void CameraCalibration::resizeEvent(QResizeEvent* event) +{ + QSize window_size = event->size(); + int left_list_width = window_size.width()*5/16; + int left_list_height = window_size.height()-80; + if(left_list_width > 500) left_list_width = 500; + ui->scrollArea->setGeometry(0,80,left_list_width, left_list_height); + ui->scrollAreaWidgetContents->setGeometry(0,0,left_list_width-2, left_list_height-2); + ui->intro->setGeometry(0,0,left_list_width-2, 100); + ui->listWidget->setGeometry(0,0,left_list_width-2, left_list_height-2); + + ui->Image->setGeometry(left_list_width,80,window_size.width()-left_list_width, left_list_height); +} + +void CameraCalibration::showIntro() +{ + a = new AboutUs(this); + QFont font = a->font(); + font.setPixelSize(12); + a->setFont(font); + a->setWindowTitle("关于TStoneCalibration"); + a->show(); +} + +// 显示简单的文字引导 +void CameraCalibration::ShowIntro() +{ + ui->intro->setFixedHeight(100); + if(ui->double_camera->isChecked() || ui->double_undistort->isChecked()) + { + QString str = "点击左上角添加图片"; + str += "\n"; + str += "左右相机图片分别放于两个文件夹"; + str += "\n"; + str += "对应图片名称需一致。"; + str += "\n"; + str += "\n"; + str += "单/双目纠正模式可纠正无棋盘的图片。"; + ui->intro->setText(str); + } + else if(ui->single_camera->isChecked() || ui->single_undistort->isChecked()) + { + QString str = "点击左上角添加图片"; + str += "\n"; + str += "15至20张较为适宜"; + str += "\n"; + str += "\n"; + str += "单/双目纠正模式可纠正无棋盘的图片。"; + ui->intro->setText(str); + } +} + +// 隐藏文字指导 +void CameraCalibration::HiddenIntro() +{ + ui->intro->setText(""); + ui->intro->setFixedHeight(0); +} + +// 导出矫正图片 +void CameraCalibration::saveUndistort() +{ + if((!mono_calib.hasParam() && ui->single_undistort->isChecked()) || + (!bino_calib.hasParam() && ui->double_undistort->isChecked())) + { + QMessageBox::warning(this, "警告", "还未获取到相机参数,请点击UNDISTORT按钮加载yaml文件。", QMessageBox::Yes, QMessageBox::Yes); + return; + } + QString srcDirPath = QFileDialog::getExistingDirectory(this, "选择保存路径", "./"); + if(srcDirPath.length() <= 0) + return; + QTextCodec *code = QTextCodec::codecForName("GB2312");//解决中文路径问题 + if(ui->double_camera->isChecked() || ui->double_undistort->isChecked()) + { + QDir dir; + dir.mkdir(srcDirPath+"/left/"); + dir.mkdir(srcDirPath+"/right/"); + for(unsigned int i = 0; i < stereo_imgs.size(); i++) + { + struct Stereo_Img_t img = stereo_imgs[i]; + cv::Mat left_dst = cv::imread(img.left_path); + cv::Mat right_dst = cv::imread(img.right_path); + bino_calib.undistort(left_dst, right_dst); + QString save_name = srcDirPath + "/left/" + img.left_file_name; + std::string str = code->fromUnicode(save_name).data(); + cv::imwrite(str, left_dst); + save_name = srcDirPath + "/right/" + img.left_file_name; + str = code->fromUnicode(save_name).data(); + cv::imwrite(str, right_dst); + cv::Mat combian_img; + cv::hconcat(left_dst, right_dst, combian_img); + for(int i = 1; i < 10; i++) + { + cv::line(combian_img, cv::Point(0, combian_img.rows*i/10), cv::Point(combian_img.cols-1, combian_img.rows*i/10), cv::Scalar(0,0,255), 2); + } + save_name = srcDirPath + "/" + img.left_file_name; + str = code->fromUnicode(save_name).data(); + cv::imwrite(str, combian_img); + } + } + else + { + for(unsigned int i = 0; i < imgs.size(); i++) + { + cv::Mat dst = cv::imread(imgs[i].file_path); + mono_calib.undistort(dst); + QString save_name = srcDirPath + "/" + imgs[i].file_name; + std::string str = code->fromUnicode(save_name).data(); + cv::imwrite(str, dst); + } + } +} + +// 切换模式时删除所有图片缓存 +void CameraCalibration::reset() +{ + ShowIntro(); + ui->k_2->setEnabled(true); + ui->k_3->setEnabled(true); + ui->tangential->setEnabled(true); + ui->calibrate->setEnabled(true); + ui->export_1->setEnabled(true); + bino_calib.reset(); + mono_calib.reset(); + distort_flag = false; + disconnect(ui->listWidget, SIGNAL(currentItemChanged(QListWidgetItem*, QListWidgetItem*)), this, SLOT(chooseImage(QListWidgetItem*, QListWidgetItem*))); + ui->listWidget->clear(); + connect(ui->listWidget, SIGNAL(currentItemChanged(QListWidgetItem*, QListWidgetItem*)), this, SLOT(chooseImage(QListWidgetItem*, QListWidgetItem*))); + if(imgs.size() > 0) + imgs.clear(); + if(stereo_imgs.size() > 0) + stereo_imgs.clear(); + list_select_ = false; +} + +// 显示选择的图像 +void CameraCalibration::chooseImage(QListWidgetItem* item, QListWidgetItem*) +{ + list_select_ = true; + int id = ui->listWidget->row(item); + if(ui->double_camera->isChecked()) + { + // 如果是双目模式 + struct Stereo_Img_t img = stereo_imgs[id]; + cv::Mat left_dst = cv::imread(img.left_path); + cv::Mat right_dst = cv::imread(img.right_path); + if(distort_flag == true && bino_calib.hasParam()) + { + // 对左右图像进行矫正 + bino_calib.undistort(left_dst, right_dst); + // 绘制横线以观察左右图像极线是否对齐 + for(int i = 1; i < 10; i++) + { + cv::line(left_dst, cv::Point(0, left_dst.rows*i/10), cv::Point(left_dst.cols-1, left_dst.rows*i/10), cv::Scalar(0,0,255), 2); + cv::line(right_dst, cv::Point(0, right_dst.rows*i/10), cv::Point(right_dst.cols-1, right_dst.rows*i/10), cv::Scalar(0,0,255), 2); + } + } + else + { + bino_calib.showCorners(left_dst, right_dst, img); + } + cv::Mat combian_img; + cv::hconcat(left_dst, right_dst, combian_img); + QImage qimage = Mat2QImage(combian_img); + ui->Image->SetImage(qimage); + ui->Image->repaint(); + } + else if(ui->single_camera->isChecked()) + { + // 单目逻辑同双目 + cv::Mat img; + std::vector corners; + std::vector w_p; + cv::Mat rvecs, tvecs; + cv::Vec3d fish_rvecs, fish_tvecs; + img = cv::imread(imgs[id].file_path); + + if(distort_flag == true && mono_calib.hasParam()) + { + mono_calib.undistort(img); + } + else + { + mono_calib.showCorners(img, imgs[id]); + } + QImage qimage = Mat2QImage(img); + ui->Image->SetImage(qimage); + ui->Image->repaint(); + } + else if(ui->single_undistort->isChecked()) + { + // 单目纠正 + cv::Mat img = cv::imread(imgs[id].file_path); + if(distort_flag == true && mono_calib.hasParam()) + { + mono_calib.undistort(img); + } + QImage qimage = Mat2QImage(img); + ui->Image->SetImage(qimage); + ui->Image->repaint(); + } + else + { + // 双目纠正 + struct Stereo_Img_t img = stereo_imgs[id]; + cv::Mat left_dst = cv::imread(img.left_path); + cv::Mat right_dst = cv::imread(img.right_path); + if(distort_flag == true && bino_calib.hasParam()) + { + bino_calib.undistort(left_dst, right_dst); + for(int i = 1; i < 10; i++) + { + cv::line(left_dst, cv::Point(0, left_dst.rows*i/10), cv::Point(left_dst.cols-1, left_dst.rows*i/10), cv::Scalar(0,0,255), 2); + cv::line(right_dst, cv::Point(0, right_dst.rows*i/10), cv::Point(right_dst.cols-1, right_dst.rows*i/10), cv::Scalar(0,0,255), 2); + } + } + cv::Mat combian_img; + cv::hconcat(left_dst, right_dst, combian_img); + QImage qimage = Mat2QImage(combian_img); + ui->Image->SetImage(qimage); + ui->Image->repaint(); + } +} + +// 角点寻找线程结束时的回调函数 +void CameraCalibration::DealThreadDone() +{ + findcorner_thread->quit(); + findcorner_thread->wait(); + thread_done = true; +} + +void CameraCalibration::receiveYamlPath(QString str) +{ + cv::FileStorage fs_read(str.toStdString(), cv::FileStorage::READ); + if(ui->single_undistort->isChecked()) + { + if(fs_read["cameraMatrix"].empty() || fs_read["distCoeffs"].empty()) + { + QMessageBox::critical(this, "错误", "YAML文件格式错误", QMessageBox::Yes, QMessageBox::Yes); + return; + } + cv::Mat cameraMatrix, distCoeffs; + fs_read["cameraMatrix"] >> cameraMatrix; + fs_read["distCoeffs"] >> distCoeffs; + mono_calib.setCameraParam(cameraMatrix, distCoeffs, fisheye_flag); + } + else + { + if(fs_read["left_camera_Matrix"].empty() || fs_read["left_camera_distCoeffs"].empty() || fs_read["right_camera_Matrix"].empty() || fs_read["right_camera_distCoeffs"].empty()) + { + QMessageBox::critical(this, "错误", "YAML文件格式错误", QMessageBox::Yes, QMessageBox::Yes); + return; + } + if(fs_read["Rotate_Matrix"].empty() || fs_read["Translate_Matrix"].empty() || fs_read["R1"].empty() || fs_read["R2"].empty() || fs_read["P1"].empty() || + fs_read["P2"].empty() || fs_read["Q"].empty()) + { + QMessageBox::critical(this, "错误", "YAML文件格式错误", QMessageBox::Yes, QMessageBox::Yes); + return; + } + cv::Mat left_in, right_in, left_d, right_d, r, t, r1, r2, p1, p2, q; + fs_read["left_camera_Matrix"] >> left_in; + fs_read["right_camera_Matrix"] >> right_in; + fs_read["left_camera_distCoeffs"] >> left_d; + fs_read["right_camera_distCoeffs"] >> right_d; + fs_read["Rotate_Matrix"] >> r; + fs_read["Translate_Matrix"] >> t; + fs_read["R1"] >> r1; + fs_read["R2"] >> r2; + fs_read["P1"] >> p1; + fs_read["P2"] >> p2; + fs_read["Q"] >> q; + bino_calib.setCameraParam(left_in, left_d, right_in, right_d, fisheye_flag, r, t, r1, r2, p1, p2, q); + } +} + +// 加载双目图像 +void CameraCalibration::receiveFromDialog(QString str) +{ + HiddenIntro(); + QStringList list = str.split(","); + QString left_src = list[0]; + QString right_src = list[1]; + double chessboard_size = list[2].toDouble(); + bino_calib.setChessboardSize(chessboard_size); + QDir dir(left_src); + dir.setFilter(QDir::Files | QDir::NoSymLinks); + QStringList filters; + filters << "*.png" << "*.jpg" << "*.jpeg" << "*.jpe" << "*.pbm" << "*.pgm" << "*.ppm" << "*.tiff" << "*.tif" << "*.bmp" << "*.dib"; + dir.setNameFilters(filters); + QStringList imagesList = dir.entryList(); + if(ui->double_camera->isChecked()) + { + if(imagesList.length() <= 3) + { + QMessageBox::critical(this, "错误", "至少需要四组图片", QMessageBox::Yes, QMessageBox::Yes); + return; + } + QProgressDialog *dialog = new QProgressDialog(tr("检测角点..."),tr("取消"),0,imagesList.length(),this); + dialog->setWindowModality(Qt::WindowModal); + dialog->setMinimumDuration(0); + dialog->setWindowTitle("请稍候"); + dialog->setValue(0); + QFont font = dialog->font(); + font.setPixelSize(12); + dialog->setFont(font); + dialog->show(); + QTextCodec *code = QTextCodec::codecForName("GB2312");//解决中文路径问题 + for(int i = 0; i < imagesList.length(); i++) + { + QString left_file_name = left_src + "/" + imagesList[i]; + QString right_file_name = right_src + "/" + imagesList[i]; + std::string str = code->fromUnicode(right_file_name).data(); + cv::Mat right_image = cv::imread(str); + if(right_image.empty()) + { + dialog->setValue(i+1); + continue; + } + str = code->fromUnicode(left_file_name).data(); + cv::Mat left_image = cv::imread(str); + if(left_image.cols != right_image.cols || left_image.rows != right_image.rows) + { + QMessageBox::critical(this, "错误", "左右相机图片尺寸不一致", QMessageBox::Yes, QMessageBox::Yes); + delete dialog; + return; + } + find_corner_thread_img = left_image; + thread_done = false; + findcorner_thread->start(); + while(thread_done == false) + { + if(dialog->wasCanceled()) + { + DealThreadDone(); + delete dialog; + return; + } + // 强制Windows消息循环,防止程序出现未响应情况 + QCoreApplication::processEvents(); + } + struct Chessboarder_t left_chess = find_corner_thread_chessboard; + // 如果检测到多个棋盘,则剔除该图片 + if(left_chess.chessboard.size() != 1) + { + dialog->setValue(i+1); + continue; + } + find_corner_thread_img = right_image; + thread_done = false; + findcorner_thread->start(); + while(thread_done == false) + { + if(dialog->wasCanceled()) + { + DealThreadDone(); + delete dialog; + return; + } + QCoreApplication::processEvents(); + } + struct Chessboarder_t right_chess = find_corner_thread_chessboard; + // 如果检测到多个棋盘,则剔除该图片 + if(right_chess.chessboard.size() != 1) + { + dialog->setValue(i+1); + continue; + } + // 如果左右图片检测到的棋盘尺寸不对,则剔除该组图片 + if(left_chess.chessboard[0].rows != right_chess.chessboard[0].rows || + left_chess.chessboard[0].cols != right_chess.chessboard[0].cols) + { + dialog->setValue(i+1); + continue; + } + // 缓存图片及角点 + struct Stereo_Img_t img_; + img_.left_path = code->fromUnicode(left_file_name).data(); + img_.right_path = code->fromUnicode(right_file_name).data(); + img_.left_file_name = imagesList[i]; + img_.right_file_name = imagesList[i]; + for (unsigned int j = 0; j < left_chess.chessboard.size(); j++) + { + for (int u = 0; u < left_chess.chessboard[j].rows; u++) + { + for (int v = 0; v < left_chess.chessboard[j].cols; v++) + { + img_.left_img_points.push_back(left_chess.corners.p[left_chess.chessboard[j].at(u, v)]); + img_.world_points.push_back(cv::Point3f(u*chessboard_size, v*chessboard_size, 0)); + img_.right_img_points.push_back(right_chess.corners.p[right_chess.chessboard[j].at(u, v)]); + } + } + } + stereo_imgs.push_back(img_); + img_size = left_image.size(); + int img_height = left_image.rows*90/left_image.cols; + cv::resize(left_image, left_image, cv::Size(90, img_height)); + cv::resize(right_image, right_image, cv::Size(90, img_height)); + cv::Mat combian_img = cv::Mat::zeros(cv::Size(180, img_height), CV_8UC3); + cv::Mat new_roi = combian_img(cv::Rect(0, 0, 90, img_height)); + left_image.convertTo(new_roi, new_roi.type()); + new_roi = combian_img(cv::Rect(90, 0, 90, img_height)); + right_image.convertTo(new_roi, new_roi.type()); + QPixmap pixmap = QPixmap::fromImage(Mat2QImage(combian_img)); + QListWidgetItem* temp = new QListWidgetItem(); + temp->setSizeHint(QSize(220, img_height)); + temp->setIcon(QIcon(pixmap)); + temp->setText(imagesList[i]); + ui->listWidget->addItem(temp); + ui->listWidget->setIconSize(QSize(180, img_height)); + dialog->setValue(i+1); + } + delete dialog; + } + else if(ui->double_undistort->isChecked()) + { + QTextCodec *code = QTextCodec::codecForName("GB2312");//解决中文路径问题 + for(int i = 0; i < imagesList.length(); i++) + { + QString left_file_name = left_src + "/" + imagesList[i]; + QString right_file_name = right_src + "/" + imagesList[i]; + std::string str = code->fromUnicode(right_file_name).data(); + cv::Mat right_image = cv::imread(str); + if(right_image.empty()) + { + continue; + } + str = code->fromUnicode(left_file_name).data(); + cv::Mat left_image = cv::imread(str); + if(left_image.cols != right_image.cols || left_image.rows != right_image.rows) + { + QMessageBox::critical(this, "错误", "左右相机图片尺寸不一致", QMessageBox::Yes, QMessageBox::Yes); + return; + } + struct Stereo_Img_t img_; + img_.left_path = code->fromUnicode(left_file_name).data(); + img_.right_path = code->fromUnicode(right_file_name).data(); + img_.left_file_name = imagesList[i]; + img_.right_file_name = imagesList[i]; + stereo_imgs.push_back(img_); + img_size = left_image.size(); + int img_height = left_image.rows*90/left_image.cols; + cv::resize(left_image, left_image, cv::Size(90, img_height)); + cv::resize(right_image, right_image, cv::Size(90, img_height)); + cv::Mat combian_img = cv::Mat::zeros(cv::Size(180, img_height), CV_8UC3); + cv::Mat new_roi = combian_img(cv::Rect(0, 0, 90, img_height)); + left_image.convertTo(new_roi, new_roi.type()); + new_roi = combian_img(cv::Rect(90, 0, 90, img_height)); + right_image.convertTo(new_roi, new_roi.type()); + QPixmap pixmap = QPixmap::fromImage(Mat2QImage(combian_img)); + QListWidgetItem* temp = new QListWidgetItem(); + temp->setSizeHint(QSize(220, img_height)); + temp->setIcon(QIcon(pixmap)); + temp->setText(imagesList[i]); + ui->listWidget->addItem(temp); + ui->listWidget->setIconSize(QSize(180, img_height)); + } + } +} + +// 鱼眼相机切换 +void CameraCalibration::fisheyeModeSwitch(int state) +{ + fisheye_flag = state==0 ? false : true; + if(fisheye_flag == true) + { + ui->k_2->setDisabled(true); + ui->k_3->setDisabled(true); + ui->tangential->setDisabled(true); + } + else + { + ui->k_2->setEnabled(true); + ui->k_3->setEnabled(true); + ui->tangential->setEnabled(true); + } +} + +// 畸变矫正切换 +void CameraCalibration::distortModeSwitch() +{ + if((!mono_calib.hasParam() && ui->single_undistort->isChecked()) || + (!bino_calib.hasParam() && ui->double_undistort->isChecked())) + { + chooseYaml = new choose_yaml(this); + chooseYaml->setWindowTitle("选择相机参数文件"); + QFont font = chooseYaml->font(); + font.setPixelSize(12); + chooseYaml->setFont(font); + chooseYaml->show(); + connect(chooseYaml, SIGNAL(SendSignal(QString)), this, SLOT(receiveYamlPath(QString))); + return; + } + static int cnt = 0; + cnt++; + if(cnt%2 == 1) + { + distort_flag = true; + } + else + { + distort_flag = false; + } + if(list_select_) + { + int id = ui->listWidget->selectionModel()->selectedIndexes()[0].row(); + chooseImage(ui->listWidget->item(id), ui->listWidget->item(id)); + } +} + +// Mat格式转QImage格式 +QImage CameraCalibration::Mat2QImage(cv::Mat cvImg) +{ + QImage qImg; + if(cvImg.channels()==3) + { + cv::cvtColor(cvImg,cvImg,cv::COLOR_BGR2RGB); + qImg =QImage((const unsigned char*)(cvImg.data), + cvImg.cols, cvImg.rows, + cvImg.cols*cvImg.channels(), + QImage::Format_RGB888); + } + else if(cvImg.channels()==1) + { + qImg =QImage((const unsigned char*)(cvImg.data), + cvImg.cols,cvImg.rows, + cvImg.cols*cvImg.channels(), + QImage::Format_Indexed8); + } + else + { + qImg =QImage((const unsigned char*)(cvImg.data), + cvImg.cols,cvImg.rows, + cvImg.cols*cvImg.channels(), + QImage::Format_RGB888); + } + return qImg; +} + +// 单目相机图片加载,逻辑同双目相机图片加载 +void CameraCalibration::addImage() +{ + HiddenIntro(); + if(ui->double_camera->isChecked() || ui->double_undistort->isChecked()) + { + d = new choose_two_dir(this); + d->setWindowTitle("选择图片文件夹"); + QFont font = d->font(); + font.setPixelSize(12); + d->setFont(font); + d->show(); + connect(d, SIGNAL(SendSignal(QString)), this, SLOT(receiveFromDialog(QString))); + return; + } + else if(ui->single_camera->isChecked()) + { + QStringList path_list = QFileDialog::getOpenFileNames(this, tr("选择图片"), tr("./"), tr("图片文件(*.jpg *.png *.pgm);;所有文件(*.*);;")); + QProgressDialog *dialog = new QProgressDialog(tr("检测角点..."),tr("取消"),0,path_list.size(), this); + dialog->setWindowModality(Qt::WindowModal); + dialog->setMinimumDuration(0); + dialog->setWindowTitle("请稍候"); + dialog->setValue(0); + QFont font = dialog->font(); + font.setPixelSize(12); + dialog->setFont(font); + dialog->show(); + QTextCodec *code = QTextCodec::codecForName("GB2312");//解决中文路径问题 + for(int i = 0; i < path_list.size(); i++) + { + QFileInfo file = QFileInfo(path_list[i]); + QString file_name = file.fileName(); + std::string str = code->fromUnicode(path_list[i]).data(); + cv::Mat img = cv::imread(str); + find_corner_thread_img = img; + thread_done = false; + findcorner_thread->start(); + while(thread_done == false) + { + if(dialog->wasCanceled()) + { + DealThreadDone(); + delete dialog; + return; + } + QCoreApplication::processEvents(); + } + struct Chessboarder_t chess = find_corner_thread_chessboard; + if(chess.chessboard.size() != 1) + { + dialog->setValue(i+1); + continue; + } + struct Img_t img_; + img_.file_path = str; + img_.file_name = file_name; + std::vector img_p; + std::vector world_p; + for (unsigned int j = 0; j < chess.chessboard.size(); j++) + { + for (int u = 0; u < chess.chessboard[j].rows; u++) + { + for (int v = 0; v < chess.chessboard[j].cols; v++) + { + img_p.push_back(chess.corners.p[chess.chessboard[j].at(u, v)]); + world_p.push_back(cv::Point3f(u*mono_calib.getChessboardSize(), v*mono_calib.getChessboardSize(), 0)); + } + } + } + img_.img_points = img_p; + img_.world_points = world_p; + imgs.push_back(img_); + img_size = img.size(); + int img_height = img.rows*124/img.cols; + cv::resize(img, img, cv::Size(124, img_height)); + QPixmap pixmap = QPixmap::fromImage(Mat2QImage(img)); + QListWidgetItem* temp = new QListWidgetItem(); + temp->setSizeHint(QSize(200, img_height)); + temp->setIcon(QIcon(pixmap)); + temp->setText(file_name); + ui->listWidget->addItem(temp); + ui->listWidget->setIconSize(QSize(124, img_height)); + dialog->setValue(i+1); + } + delete dialog; + } + else if(ui->single_undistort->isChecked()) + { + QStringList path_list = QFileDialog::getOpenFileNames(this, tr("选择图片"), tr("./"), tr("图片文件(*.jpg *.png *.pgm);;所有文件(*.*);;")); + QTextCodec *code = QTextCodec::codecForName("GB2312");//解决中文路径问题 + for(int i = 0; i < path_list.size(); i++) + { + QFileInfo file = QFileInfo(path_list[i]); + QString file_name = file.fileName(); + std::string str = code->fromUnicode(path_list[i]).data(); + cv::Mat img = cv::imread(str); + img_size = img.size(); + Img_t single_img; + single_img.file_path = str; + single_img.file_name = file_name; + imgs.push_back(single_img); + int img_height = img.rows*124/img.cols; + cv::resize(img, img, cv::Size(124, img_height)); + QPixmap pixmap = QPixmap::fromImage(Mat2QImage(img)); + QListWidgetItem* temp = new QListWidgetItem(); + temp->setSizeHint(QSize(200, img_height)); + temp->setIcon(QIcon(pixmap)); + temp->setText(file_name); + ui->listWidget->addItem(temp); + ui->listWidget->setIconSize(QSize(124, img_height)); + } + } +} + +// 删除选中的图片 +void CameraCalibration::deleteImage() +{ + std::vector delete_idx; + foreach(QModelIndex index,ui->listWidget->selectionModel()->selectedIndexes()){ + delete_idx.push_back(index.row()); + } + if(delete_idx.size() == 0) + return; + std::sort(delete_idx.begin(), delete_idx.end()); + disconnect(ui->listWidget, SIGNAL(currentItemChanged(QListWidgetItem*, QListWidgetItem*)), this, SLOT(chooseImage(QListWidgetItem*, QListWidgetItem*))); + for(int i = delete_idx.size()-1; i >= 0; i--) + { + ui->listWidget->takeItem(delete_idx[i]); + if(ui->double_camera->isChecked()) + stereo_imgs.erase(stereo_imgs.begin()+delete_idx[i]); + else + imgs.erase(imgs.begin()+delete_idx[i]); + } + connect(ui->listWidget, SIGNAL(currentItemChanged(QListWidgetItem*, QListWidgetItem*)), this, SLOT(chooseImage(QListWidgetItem*, QListWidgetItem*))); + // 如果图片全部删除,则重新显示文字引导 + if(ui->listWidget->count() == 0) + { + reset(); + } +} + +// 相机标定 +void CameraCalibration::calibrate() +{ + if(!ui->double_camera->isChecked()) + { + // 单目标定 + int state = mono_calib.calibrate(imgs, fisheye_flag, ui->tangential->isChecked(), ui->k_3->isChecked()); + if(state == 1) + { + QMessageBox::critical(this, "错误", "至少需要四张图片", QMessageBox::Yes, QMessageBox::Yes); + return; + } + else if(state == 2) + { + QMessageBox::critical(this, "错误", "请采集更多方位的图片或者检查角点识别!", QMessageBox::Yes, QMessageBox::Yes); + return; + } + } + else + { + if(!bino_calib.isChessboardSizeValid()) + { + bool ok; + double chessboard_size = QInputDialog::getDouble(this,tr("角点间距"),tr("请输入角点间距(mm)"),20,0,1000,2,&ok); + if(!ok) + { + chessboard_size = 0; + return; + } + bino_calib.setChessboardSize(chessboard_size); + } + // 双目标定 + int state = bino_calib.calibrate(stereo_imgs, fisheye_flag, ui->tangential->isChecked(), ui->k_3->isChecked()); + if(state == 1) + { + QMessageBox::critical(this, "错误", "至少需要四组图片", QMessageBox::Yes, QMessageBox::Yes); + return; + } + else if(state == 2) + { + QMessageBox::critical(this, "错误", "请采集更多方位的图片!", QMessageBox::Yes, QMessageBox::Yes); + return; + } + } +} + +// 导出标定参数 +void CameraCalibration::exportParam() +{ + if((!mono_calib.hasParam() && ui->single_undistort->isChecked()) || + (!bino_calib.hasParam() && ui->double_undistort->isChecked())) + { + QMessageBox::critical(this, "错误", "请先标定相机", QMessageBox::Yes); + return; + } + QString strSaveName = QFileDialog::getSaveFileName(this,tr("保存的文件"),tr("calibration_param.yaml"),tr("yaml files(*.yaml)")); + if(strSaveName.isNull() || strSaveName.isEmpty() || strSaveName.length() == 0) + return; + cv::FileStorage fs_write(strSaveName.toStdString(), cv::FileStorage::WRITE); + if(ui->double_camera->isChecked()) + { + bino_calib.exportParam(strSaveName.toStdString()); + } + else + { + mono_calib.exportParam(strSaveName.toStdString()); + } +} diff --git a/CameraCalibration.h b/CameraCalibration.h new file mode 100644 index 0000000..bc46d86 --- /dev/null +++ b/CameraCalibration.h @@ -0,0 +1,78 @@ +#ifndef CameraCalibration_H +#define CameraCalibration_H + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include "findCorner.h" +#include "chessboard.h" +#include "choose_two_dir.h" +#include "choose_yaml.h" +#include "types.h" +#include "monocular_calib.h" +#include "binocular_calib.h" +#include "AboutUs.h" + +QT_BEGIN_NAMESPACE +namespace Ui { class CameraCalibration; } +QT_END_NAMESPACE + +extern cv::Mat find_corner_thread_img; +extern struct Chessboarder_t find_corner_thread_chessboard; + +class CameraCalibration : public QMainWindow +{ + Q_OBJECT + +public: + CameraCalibration(QWidget *parent = nullptr); + ~CameraCalibration(); + +private slots: + void addImage(); + void deleteImage(); + void calibrate(); + void fisheyeModeSwitch(int state); + void exportParam(); + void distortModeSwitch(); + void receiveFromDialog(QString str); + void receiveYamlPath(QString str); + void chooseImage(QListWidgetItem* item, QListWidgetItem*); + void reset(); + void saveUndistort(); + void DealThreadDone(); + void showIntro(); + +private: + Ui::CameraCalibration *ui; + choose_two_dir *d; + choose_yaml *chooseYaml; + AboutUs* a; + QImage Mat2QImage(cv::Mat cvImg); + void ShowIntro(); + void HiddenIntro(); + QAction *saveImage; + QAction *about; + std::vector imgs; + std::vector stereo_imgs; + MonoCalib mono_calib; + BinoCalib bino_calib; + cv::Size img_size; + bool fisheye_flag = false; + bool distort_flag = false; + FindcornerThread *findcorner_thread; + bool thread_done = false; + bool list_select_ = false; +protected: + void resizeEvent(QResizeEvent* event) override; +}; +#endif // CameraCalibration_H diff --git a/camera_calibration/CameraCalibration.ui b/CameraCalibration.ui similarity index 88% rename from camera_calibration/CameraCalibration.ui rename to CameraCalibration.ui index a78f2ff..972e0e9 100644 --- a/camera_calibration/CameraCalibration.ui +++ b/CameraCalibration.ui @@ -44,7 +44,7 @@ 10 15 60 - 40 + 60 @@ -87,27 +87,6 @@ - - - - 10 - 50 - 60 - 25 - - - - - 8 - - - - PointingHandCursor - - - 添加 - - @@ -324,12 +303,6 @@ 150 - - - 248 - 16777215 - - @@ -345,12 +318,6 @@ 518 - - - 248 - 16777215 - - @@ -375,40 +342,21 @@ - - - - 250 - 80 - 550 - 520 - - - - - 微软雅黑 - - - - Image - - + + - 10 - 15 - 530 - 495 + 250 + 80 + 550 + 520 - - - - - Qt::AlignCenter - + + Image + - + @@ -539,7 +487,7 @@ 0 0 800 - 22 + 28 @@ -550,6 +498,14 @@ + + + QImgViewWidget + QWidget +
QImgViewWidget.h
+ 1 +
+
diff --git a/camera_calibration/chessboard.cpp b/DetectCorner/chessboard.cpp similarity index 100% rename from camera_calibration/chessboard.cpp rename to DetectCorner/chessboard.cpp diff --git a/camera_calibration/chessboard.h b/DetectCorner/chessboard.h similarity index 100% rename from camera_calibration/chessboard.h rename to DetectCorner/chessboard.h diff --git a/camera_calibration/findCorner.cpp b/DetectCorner/findCorner.cpp similarity index 97% rename from camera_calibration/findCorner.cpp rename to DetectCorner/findCorner.cpp index 10dfa28..883ab71 100644 --- a/camera_calibration/findCorner.cpp +++ b/DetectCorner/findCorner.cpp @@ -18,7 +18,7 @@ void FindcornerThread::run() struct Chessboarder_t findCorner(cv::Mat img, int sigma) { if (img.channels() == 3) - cv::cvtColor(img, img, CV_BGR2GRAY); + cv::cvtColor(img, img, cv::COLOR_BGR2GRAY); cv::Mat du = (cv::Mat_(3, 3) << -1, 0, 1, -1, 0, 1, -1, 0, 1); cv::Mat dv = du.t(); cv::Mat img_dv, img_du; @@ -26,8 +26,8 @@ struct Chessboarder_t findCorner(cv::Mat img, int sigma) cv::Mat img_weight = cv::Mat::zeros(img.size(), CV_64F); cv::Mat img_double; img.convertTo(img_double, CV_64F); - cv::filter2D(img_double, img_du, img_double.depth(), du, cvPoint(-1, -1)); - cv::filter2D(img_double, img_dv, img_double.depth(), dv, cvPoint(-1, -1)); + cv::filter2D(img_double, img_du, img_double.depth(), du, cv::Point(-1, -1)); + cv::filter2D(img_double, img_dv, img_double.depth(), dv, cv::Point(-1, -1)); for (int i = 0; i < img.rows; i++) { for (int j = 0; j < img.cols; j++) @@ -122,8 +122,8 @@ void secondDerivCornerMetric(cv::Mat I, int sigma, cv::Mat* cxy, cv::Mat* c45, c cv::GaussianBlur(I, Ig, cv::Size(sigma*7+1, sigma*7+1), sigma, sigma); cv::Mat du = (cv::Mat_(1, 3) << 1, 0, -1); cv::Mat dv = du.t(); - cv::filter2D(Ig, *Ix, Ig.depth(), du, cvPoint(-1, -1)); - cv::filter2D(Ig, *Iy, Ig.depth(), dv, cvPoint(-1, -1)); + cv::filter2D(Ig, *Ix, Ig.depth(), du, cv::Point(-1, -1)); + cv::filter2D(Ig, *Iy, Ig.depth(), dv, cv::Point(-1, -1)); cv::Mat I_45 = I.clone(); cv::Mat I_n45 = I.clone(); @@ -140,10 +140,10 @@ void secondDerivCornerMetric(cv::Mat I, int sigma, cv::Mat* cxy, cv::Mat* c45, c } } - cv::filter2D(*Ix, *Ixy, Ix->depth(), dv, cvPoint(-1, -1)); + cv::filter2D(*Ix, *Ixy, Ix->depth(), dv, cv::Point(-1, -1)); cv::Mat I_45_x, I_45_y; - cv::filter2D(I_45, I_45_x, I_45.depth(), du, cvPoint(-1, -1)); - cv::filter2D(I_45, I_45_y, I_45.depth(), dv, cvPoint(-1, -1)); + cv::filter2D(I_45, I_45_x, I_45.depth(), du, cv::Point(-1, -1)); + cv::filter2D(I_45, I_45_y, I_45.depth(), dv, cv::Point(-1, -1)); for (int i = 0; i < I.rows; i++) { for (int j = 0; j < I.cols; j++) diff --git a/camera_calibration/findCorner.h b/DetectCorner/findCorner.h similarity index 100% rename from camera_calibration/findCorner.h rename to DetectCorner/findCorner.h diff --git a/LICENSE b/LICENSE deleted file mode 100644 index f288702..0000000 --- a/LICENSE +++ /dev/null @@ -1,674 +0,0 @@ - GNU GENERAL PUBLIC LICENSE - Version 3, 29 June 2007 - - Copyright (C) 2007 Free Software Foundation, Inc. - Everyone is permitted to copy and distribute verbatim copies - of this license document, but changing it is not allowed. - - Preamble - - The GNU General Public License is a free, copyleft license for -software and other kinds of works. - - The licenses for most software and other practical works are designed -to take away your freedom to share and change the works. By contrast, -the GNU General Public License is intended to guarantee your freedom to -share and change all versions of a program--to make sure it remains free -software for all its users. We, the Free Software Foundation, use the -GNU General Public License for most of our software; it applies also to -any other work released this way by its authors. You can apply it to -your programs, too. - - When we speak of free software, we are referring to freedom, not -price. Our General Public Licenses are designed to make sure that you -have the freedom to distribute copies of free software (and charge for -them if you wish), that you receive source code or can get it if you -want it, that you can change the software or use pieces of it in new -free programs, and that you know you can do these things. - - To protect your rights, we need to prevent others from denying you -these rights or asking you to surrender the rights. Therefore, you have -certain responsibilities if you distribute copies of the software, or if -you modify it: responsibilities to respect the freedom of others. - - For example, if you distribute copies of such a program, whether -gratis or for a fee, you must pass on to the recipients the same -freedoms that you received. You must make sure that they, too, receive -or can get the source code. And you must show them these terms so they -know their rights. - - Developers that use the GNU GPL protect your rights with two steps: -(1) assert copyright on the software, and (2) offer you this License -giving you legal permission to copy, distribute and/or modify it. - - For the developers' and authors' protection, the GPL clearly explains -that there is no warranty for this free software. For both users' and -authors' sake, the GPL requires that modified versions be marked as -changed, so that their problems will not be attributed erroneously to -authors of previous versions. - - Some devices are designed to deny users access to install or run -modified versions of the software inside them, although the manufacturer -can do so. This is fundamentally incompatible with the aim of -protecting users' freedom to change the software. The systematic -pattern of such abuse occurs in the area of products for individuals to -use, which is precisely where it is most unacceptable. Therefore, we -have designed this version of the GPL to prohibit the practice for those -products. If such problems arise substantially in other domains, we -stand ready to extend this provision to those domains in future versions -of the GPL, as needed to protect the freedom of users. - - Finally, every program is threatened constantly by software patents. -States should not allow patents to restrict development and use of -software on general-purpose computers, but in those that do, we wish to -avoid the special danger that patents applied to a free program could -make it effectively proprietary. To prevent this, the GPL assures that -patents cannot be used to render the program non-free. - - The precise terms and conditions for copying, distribution and -modification follow. - - TERMS AND CONDITIONS - - 0. Definitions. - - "This License" refers to version 3 of the GNU General Public License. - - "Copyright" also means copyright-like laws that apply to other kinds of -works, such as semiconductor masks. - - "The Program" refers to any copyrightable work licensed under this -License. Each licensee is addressed as "you". "Licensees" and -"recipients" may be individuals or organizations. - - To "modify" a work means to copy from or adapt all or part of the work -in a fashion requiring copyright permission, other than the making of an -exact copy. The resulting work is called a "modified version" of the -earlier work or a work "based on" the earlier work. - - A "covered work" means either the unmodified Program or a work based -on the Program. - - To "propagate" a work means to do anything with it that, without -permission, would make you directly or secondarily liable for -infringement under applicable copyright law, except executing it on a -computer or modifying a private copy. Propagation includes copying, -distribution (with or without modification), making available to the -public, and in some countries other activities as well. - - To "convey" a work means any kind of propagation that enables other -parties to make or receive copies. Mere interaction with a user through -a computer network, with no transfer of a copy, is not conveying. - - An interactive user interface displays "Appropriate Legal Notices" -to the extent that it includes a convenient and prominently visible -feature that (1) displays an appropriate copyright notice, and (2) -tells the user that there is no warranty for the work (except to the -extent that warranties are provided), that licensees may convey the -work under this License, and how to view a copy of this License. If -the interface presents a list of user commands or options, such as a -menu, a prominent item in the list meets this criterion. - - 1. Source Code. - - The "source code" for a work means the preferred form of the work -for making modifications to it. "Object code" means any non-source -form of a work. - - A "Standard Interface" means an interface that either is an official -standard defined by a recognized standards body, or, in the case of -interfaces specified for a particular programming language, one that -is widely used among developers working in that language. - - The "System Libraries" of an executable work include anything, other -than the work as a whole, that (a) is included in the normal form of -packaging a Major Component, but which is not part of that Major -Component, and (b) serves only to enable use of the work with that -Major Component, or to implement a Standard Interface for which an -implementation is available to the public in source code form. A -"Major Component", in this context, means a major essential component -(kernel, window system, and so on) of the specific operating system -(if any) on which the executable work runs, or a compiler used to -produce the work, or an object code interpreter used to run it. - - The "Corresponding Source" for a work in object code form means all -the source code needed to generate, install, and (for an executable -work) run the object code and to modify the work, including scripts to -control those activities. However, it does not include the work's -System Libraries, or general-purpose tools or generally available free -programs which are used unmodified in performing those activities but -which are not part of the work. For example, Corresponding Source -includes interface definition files associated with source files for -the work, and the source code for shared libraries and dynamically -linked subprograms that the work is specifically designed to require, -such as by intimate data communication or control flow between those -subprograms and other parts of the work. - - The Corresponding Source need not include anything that users -can regenerate automatically from other parts of the Corresponding -Source. - - The Corresponding Source for a work in source code form is that -same work. - - 2. Basic Permissions. - - All rights granted under this License are granted for the term of -copyright on the Program, and are irrevocable provided the stated -conditions are met. This License explicitly affirms your unlimited -permission to run the unmodified Program. The output from running a -covered work is covered by this License only if the output, given its -content, constitutes a covered work. This License acknowledges your -rights of fair use or other equivalent, as provided by copyright law. - - You may make, run and propagate covered works that you do not -convey, without conditions so long as your license otherwise remains -in force. You may convey covered works to others for the sole purpose -of having them make modifications exclusively for you, or provide you -with facilities for running those works, provided that you comply with -the terms of this License in conveying all material for which you do -not control copyright. Those thus making or running the covered works -for you must do so exclusively on your behalf, under your direction -and control, on terms that prohibit them from making any copies of -your copyrighted material outside their relationship with you. - - Conveying under any other circumstances is permitted solely under -the conditions stated below. Sublicensing is not allowed; section 10 -makes it unnecessary. - - 3. Protecting Users' Legal Rights From Anti-Circumvention Law. - - No covered work shall be deemed part of an effective technological -measure under any applicable law fulfilling obligations under article -11 of the WIPO copyright treaty adopted on 20 December 1996, or -similar laws prohibiting or restricting circumvention of such -measures. - - When you convey a covered work, you waive any legal power to forbid -circumvention of technological measures to the extent such circumvention -is effected by exercising rights under this License with respect to -the covered work, and you disclaim any intention to limit operation or -modification of the work as a means of enforcing, against the work's -users, your or third parties' legal rights to forbid circumvention of -technological measures. - - 4. Conveying Verbatim Copies. - - You may convey verbatim copies of the Program's source code as you -receive it, in any medium, provided that you conspicuously and -appropriately publish on each copy an appropriate copyright notice; -keep intact all notices stating that this License and any -non-permissive terms added in accord with section 7 apply to the code; -keep intact all notices of the absence of any warranty; and give all -recipients a copy of this License along with the Program. - - You may charge any price or no price for each copy that you convey, -and you may offer support or warranty protection for a fee. - - 5. Conveying Modified Source Versions. - - You may convey a work based on the Program, or the modifications to -produce it from the Program, in the form of source code under the -terms of section 4, provided that you also meet all of these conditions: - - a) The work must carry prominent notices stating that you modified - it, and giving a relevant date. - - b) The work must carry prominent notices stating that it is - released under this License and any conditions added under section - 7. This requirement modifies the requirement in section 4 to - "keep intact all notices". - - c) You must license the entire work, as a whole, under this - License to anyone who comes into possession of a copy. This - License will therefore apply, along with any applicable section 7 - additional terms, to the whole of the work, and all its parts, - regardless of how they are packaged. This License gives no - permission to license the work in any other way, but it does not - invalidate such permission if you have separately received it. - - d) If the work has interactive user interfaces, each must display - Appropriate Legal Notices; however, if the Program has interactive - interfaces that do not display Appropriate Legal Notices, your - work need not make them do so. - - A compilation of a covered work with other separate and independent -works, which are not by their nature extensions of the covered work, -and which are not combined with it such as to form a larger program, -in or on a volume of a storage or distribution medium, is called an -"aggregate" if the compilation and its resulting copyright are not -used to limit the access or legal rights of the compilation's users -beyond what the individual works permit. Inclusion of a covered work -in an aggregate does not cause this License to apply to the other -parts of the aggregate. - - 6. Conveying Non-Source Forms. - - You may convey a covered work in object code form under the terms -of sections 4 and 5, provided that you also convey the -machine-readable Corresponding Source under the terms of this License, -in one of these ways: - - a) Convey the object code in, or embodied in, a physical product - (including a physical distribution medium), accompanied by the - Corresponding Source fixed on a durable physical medium - customarily used for software interchange. - - b) Convey the object code in, or embodied in, a physical product - (including a physical distribution medium), accompanied by a - written offer, valid for at least three years and valid for as - long as you offer spare parts or customer support for that product - model, to give anyone who possesses the object code either (1) a - copy of the Corresponding Source for all the software in the - product that is covered by this License, on a durable physical - medium customarily used for software interchange, for a price no - more than your reasonable cost of physically performing this - conveying of source, or (2) access to copy the - Corresponding Source from a network server at no charge. - - c) Convey individual copies of the object code with a copy of the - written offer to provide the Corresponding Source. This - alternative is allowed only occasionally and noncommercially, and - only if you received the object code with such an offer, in accord - with subsection 6b. - - d) Convey the object code by offering access from a designated - place (gratis or for a charge), and offer equivalent access to the - Corresponding Source in the same way through the same place at no - further charge. You need not require recipients to copy the - Corresponding Source along with the object code. If the place to - copy the object code is a network server, the Corresponding Source - may be on a different server (operated by you or a third party) - that supports equivalent copying facilities, provided you maintain - clear directions next to the object code saying where to find the - Corresponding Source. Regardless of what server hosts the - Corresponding Source, you remain obligated to ensure that it is - available for as long as needed to satisfy these requirements. - - e) Convey the object code using peer-to-peer transmission, provided - you inform other peers where the object code and Corresponding - Source of the work are being offered to the general public at no - charge under subsection 6d. - - A separable portion of the object code, whose source code is excluded -from the Corresponding Source as a System Library, need not be -included in conveying the object code work. - - A "User Product" is either (1) a "consumer product", which means any -tangible personal property which is normally used for personal, family, -or household purposes, or (2) anything designed or sold for incorporation -into a dwelling. In determining whether a product is a consumer product, -doubtful cases shall be resolved in favor of coverage. For a particular -product received by a particular user, "normally used" refers to a -typical or common use of that class of product, regardless of the status -of the particular user or of the way in which the particular user -actually uses, or expects or is expected to use, the product. A product -is a consumer product regardless of whether the product has substantial -commercial, industrial or non-consumer uses, unless such uses represent -the only significant mode of use of the product. - - "Installation Information" for a User Product means any methods, -procedures, authorization keys, or other information required to install -and execute modified versions of a covered work in that User Product from -a modified version of its Corresponding Source. The information must -suffice to ensure that the continued functioning of the modified object -code is in no case prevented or interfered with solely because -modification has been made. - - If you convey an object code work under this section in, or with, or -specifically for use in, a User Product, and the conveying occurs as -part of a transaction in which the right of possession and use of the -User Product is transferred to the recipient in perpetuity or for a -fixed term (regardless of how the transaction is characterized), the -Corresponding Source conveyed under this section must be accompanied -by the Installation Information. But this requirement does not apply -if neither you nor any third party retains the ability to install -modified object code on the User Product (for example, the work has -been installed in ROM). - - The requirement to provide Installation Information does not include a -requirement to continue to provide support service, warranty, or updates -for a work that has been modified or installed by the recipient, or for -the User Product in which it has been modified or installed. Access to a -network may be denied when the modification itself materially and -adversely affects the operation of the network or violates the rules and -protocols for communication across the network. - - Corresponding Source conveyed, and Installation Information provided, -in accord with this section must be in a format that is publicly -documented (and with an implementation available to the public in -source code form), and must require no special password or key for -unpacking, reading or copying. - - 7. Additional Terms. - - "Additional permissions" are terms that supplement the terms of this -License by making exceptions from one or more of its conditions. -Additional permissions that are applicable to the entire Program shall -be treated as though they were included in this License, to the extent -that they are valid under applicable law. If additional permissions -apply only to part of the Program, that part may be used separately -under those permissions, but the entire Program remains governed by -this License without regard to the additional permissions. - - When you convey a copy of a covered work, you may at your option -remove any additional permissions from that copy, or from any part of -it. (Additional permissions may be written to require their own -removal in certain cases when you modify the work.) You may place -additional permissions on material, added by you to a covered work, -for which you have or can give appropriate copyright permission. - - Notwithstanding any other provision of this License, for material you -add to a covered work, you may (if authorized by the copyright holders of -that material) supplement the terms of this License with terms: - - a) Disclaiming warranty or limiting liability differently from the - terms of sections 15 and 16 of this License; or - - b) Requiring preservation of specified reasonable legal notices or - author attributions in that material or in the Appropriate Legal - Notices displayed by works containing it; or - - c) Prohibiting misrepresentation of the origin of that material, or - requiring that modified versions of such material be marked in - reasonable ways as different from the original version; or - - d) Limiting the use for publicity purposes of names of licensors or - authors of the material; or - - e) Declining to grant rights under trademark law for use of some - trade names, trademarks, or service marks; or - - f) Requiring indemnification of licensors and authors of that - material by anyone who conveys the material (or modified versions of - it) with contractual assumptions of liability to the recipient, for - any liability that these contractual assumptions directly impose on - those licensors and authors. - - All other non-permissive additional terms are considered "further -restrictions" within the meaning of section 10. If the Program as you -received it, or any part of it, contains a notice stating that it is -governed by this License along with a term that is a further -restriction, you may remove that term. If a license document contains -a further restriction but permits relicensing or conveying under this -License, you may add to a covered work material governed by the terms -of that license document, provided that the further restriction does -not survive such relicensing or conveying. - - If you add terms to a covered work in accord with this section, you -must place, in the relevant source files, a statement of the -additional terms that apply to those files, or a notice indicating -where to find the applicable terms. - - Additional terms, permissive or non-permissive, may be stated in the -form of a separately written license, or stated as exceptions; -the above requirements apply either way. - - 8. Termination. - - You may not propagate or modify a covered work except as expressly -provided under this License. Any attempt otherwise to propagate or -modify it is void, and will automatically terminate your rights under -this License (including any patent licenses granted under the third -paragraph of section 11). - - However, if you cease all violation of this License, then your -license from a particular copyright holder is reinstated (a) -provisionally, unless and until the copyright holder explicitly and -finally terminates your license, and (b) permanently, if the copyright -holder fails to notify you of the violation by some reasonable means -prior to 60 days after the cessation. - - Moreover, your license from a particular copyright holder is -reinstated permanently if the copyright holder notifies you of the -violation by some reasonable means, this is the first time you have -received notice of violation of this License (for any work) from that -copyright holder, and you cure the violation prior to 30 days after -your receipt of the notice. - - Termination of your rights under this section does not terminate the -licenses of parties who have received copies or rights from you under -this License. If your rights have been terminated and not permanently -reinstated, you do not qualify to receive new licenses for the same -material under section 10. - - 9. Acceptance Not Required for Having Copies. - - You are not required to accept this License in order to receive or -run a copy of the Program. Ancillary propagation of a covered work -occurring solely as a consequence of using peer-to-peer transmission -to receive a copy likewise does not require acceptance. However, -nothing other than this License grants you permission to propagate or -modify any covered work. These actions infringe copyright if you do -not accept this License. Therefore, by modifying or propagating a -covered work, you indicate your acceptance of this License to do so. - - 10. Automatic Licensing of Downstream Recipients. - - Each time you convey a covered work, the recipient automatically -receives a license from the original licensors, to run, modify and -propagate that work, subject to this License. You are not responsible -for enforcing compliance by third parties with this License. - - An "entity transaction" is a transaction transferring control of an -organization, or substantially all assets of one, or subdividing an -organization, or merging organizations. If propagation of a covered -work results from an entity transaction, each party to that -transaction who receives a copy of the work also receives whatever -licenses to the work the party's predecessor in interest had or could -give under the previous paragraph, plus a right to possession of the -Corresponding Source of the work from the predecessor in interest, if -the predecessor has it or can get it with reasonable efforts. - - You may not impose any further restrictions on the exercise of the -rights granted or affirmed under this License. For example, you may -not impose a license fee, royalty, or other charge for exercise of -rights granted under this License, and you may not initiate litigation -(including a cross-claim or counterclaim in a lawsuit) alleging that -any patent claim is infringed by making, using, selling, offering for -sale, or importing the Program or any portion of it. - - 11. Patents. - - A "contributor" is a copyright holder who authorizes use under this -License of the Program or a work on which the Program is based. The -work thus licensed is called the contributor's "contributor version". - - A contributor's "essential patent claims" are all patent claims -owned or controlled by the contributor, whether already acquired or -hereafter acquired, that would be infringed by some manner, permitted -by this License, of making, using, or selling its contributor version, -but do not include claims that would be infringed only as a -consequence of further modification of the contributor version. For -purposes of this definition, "control" includes the right to grant -patent sublicenses in a manner consistent with the requirements of -this License. - - Each contributor grants you a non-exclusive, worldwide, royalty-free -patent license under the contributor's essential patent claims, to -make, use, sell, offer for sale, import and otherwise run, modify and -propagate the contents of its contributor version. - - In the following three paragraphs, a "patent license" is any express -agreement or commitment, however denominated, not to enforce a patent -(such as an express permission to practice a patent or covenant not to -sue for patent infringement). To "grant" such a patent license to a -party means to make such an agreement or commitment not to enforce a -patent against the party. - - If you convey a covered work, knowingly relying on a patent license, -and the Corresponding Source of the work is not available for anyone -to copy, free of charge and under the terms of this License, through a -publicly available network server or other readily accessible means, -then you must either (1) cause the Corresponding Source to be so -available, or (2) arrange to deprive yourself of the benefit of the -patent license for this particular work, or (3) arrange, in a manner -consistent with the requirements of this License, to extend the patent -license to downstream recipients. "Knowingly relying" means you have -actual knowledge that, but for the patent license, your conveying the -covered work in a country, or your recipient's use of the covered work -in a country, would infringe one or more identifiable patents in that -country that you have reason to believe are valid. - - If, pursuant to or in connection with a single transaction or -arrangement, you convey, or propagate by procuring conveyance of, a -covered work, and grant a patent license to some of the parties -receiving the covered work authorizing them to use, propagate, modify -or convey a specific copy of the covered work, then the patent license -you grant is automatically extended to all recipients of the covered -work and works based on it. - - A patent license is "discriminatory" if it does not include within -the scope of its coverage, prohibits the exercise of, or is -conditioned on the non-exercise of one or more of the rights that are -specifically granted under this License. You may not convey a covered -work if you are a party to an arrangement with a third party that is -in the business of distributing software, under which you make payment -to the third party based on the extent of your activity of conveying -the work, and under which the third party grants, to any of the -parties who would receive the covered work from you, a discriminatory -patent license (a) in connection with copies of the covered work -conveyed by you (or copies made from those copies), or (b) primarily -for and in connection with specific products or compilations that -contain the covered work, unless you entered into that arrangement, -or that patent license was granted, prior to 28 March 2007. - - Nothing in this License shall be construed as excluding or limiting -any implied license or other defenses to infringement that may -otherwise be available to you under applicable patent law. - - 12. No Surrender of Others' Freedom. - - If conditions are imposed on you (whether by court order, agreement or -otherwise) that contradict the conditions of this License, they do not -excuse you from the conditions of this License. If you cannot convey a -covered work so as to satisfy simultaneously your obligations under this -License and any other pertinent obligations, then as a consequence you may -not convey it at all. For example, if you agree to terms that obligate you -to collect a royalty for further conveying from those to whom you convey -the Program, the only way you could satisfy both those terms and this -License would be to refrain entirely from conveying the Program. - - 13. Use with the GNU Affero General Public License. - - Notwithstanding any other provision of this License, you have -permission to link or combine any covered work with a work licensed -under version 3 of the GNU Affero General Public License into a single -combined work, and to convey the resulting work. The terms of this -License will continue to apply to the part which is the covered work, -but the special requirements of the GNU Affero General Public License, -section 13, concerning interaction through a network will apply to the -combination as such. - - 14. Revised Versions of this License. - - The Free Software Foundation may publish revised and/or new versions of -the GNU General Public License from time to time. Such new versions will -be similar in spirit to the present version, but may differ in detail to -address new problems or concerns. - - Each version is given a distinguishing version number. If the -Program specifies that a certain numbered version of the GNU General -Public License "or any later version" applies to it, you have the -option of following the terms and conditions either of that numbered -version or of any later version published by the Free Software -Foundation. If the Program does not specify a version number of the -GNU General Public License, you may choose any version ever published -by the Free Software Foundation. - - If the Program specifies that a proxy can decide which future -versions of the GNU General Public License can be used, that proxy's -public statement of acceptance of a version permanently authorizes you -to choose that version for the Program. - - Later license versions may give you additional or different -permissions. However, no additional obligations are imposed on any -author or copyright holder as a result of your choosing to follow a -later version. - - 15. Disclaimer of Warranty. - - THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY -APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT -HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY -OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, -THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR -PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM -IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF -ALL NECESSARY SERVICING, REPAIR OR CORRECTION. - - 16. Limitation of Liability. - - IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING -WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS -THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY -GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE -USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF -DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD -PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), -EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF -SUCH DAMAGES. - - 17. Interpretation of Sections 15 and 16. - - If the disclaimer of warranty and limitation of liability provided -above cannot be given local legal effect according to their terms, -reviewing courts shall apply local law that most closely approximates -an absolute waiver of all civil liability in connection with the -Program, unless a warranty or assumption of liability accompanies a -copy of the Program in return for a fee. - - END OF TERMS AND CONDITIONS - - How to Apply These Terms to Your New Programs - - If you develop a new program, and you want it to be of the greatest -possible use to the public, the best way to achieve this is to make it -free software which everyone can redistribute and change under these terms. - - To do so, attach the following notices to the program. It is safest -to attach them to the start of each source file to most effectively -state the exclusion of warranty; and each file should have at least -the "copyright" line and a pointer to where the full notice is found. - - - Copyright (C) - - This program is free software: you can redistribute it and/or modify - it under the terms of the GNU General Public License as published by - the Free Software Foundation, either version 3 of the License, or - (at your option) any later version. - - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU General Public License for more details. - - You should have received a copy of the GNU General Public License - along with this program. If not, see . - -Also add information on how to contact you by electronic and paper mail. - - If the program does terminal interaction, make it output a short -notice like this when it starts in an interactive mode: - - Copyright (C) - This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'. - This is free software, and you are welcome to redistribute it - under certain conditions; type `show c' for details. - -The hypothetical commands `show w' and `show c' should show the appropriate -parts of the General Public License. Of course, your program's commands -might be different; for a GUI interface, you would use an "about box". - - You should also get your employer (if you work as a programmer) or school, -if any, to sign a "copyright disclaimer" for the program, if necessary. -For more information on this, and how to apply and follow the GNU GPL, see -. - - The GNU General Public License does not permit incorporating your program -into proprietary programs. If your program is a subroutine library, you -may consider it more useful to permit linking proprietary applications with -the library. If this is what you want to do, use the GNU Lesser General -Public License instead of this License. But first, please read -. diff --git a/QImgViewWidget.cpp b/QImgViewWidget.cpp new file mode 100644 index 0000000..d80e73d --- /dev/null +++ b/QImgViewWidget.cpp @@ -0,0 +1,93 @@ +#include "QImgViewWidget.h" +#include +#include + +QImgViewWidget::QImgViewWidget(QWidget * parent):QWidget(parent) +{ + zoom_scale_ = 1.0f; + move_start_ = false; + is_moving_ = false; +} + +void QImgViewWidget::SetImage(const QImage& img) +{ + // reset the transformation + ResetTransform(); + QSize view_size = size(); + // loading by QImage + img_display_ = img; + pix_ori_ = QPixmap::fromImage(img_display_); + if(pix_ori_.isNull()) return; + pix_display_ = pix_ori_.scaled(zoom_scale_ * size(), Qt::KeepAspectRatio); +} + +void QImgViewWidget::ResetTransform() +{ + // reset the zoom scale and move step + zoom_scale_ = 1.0f; + move_step_ = QPoint(0, 0); +} + +void QImgViewWidget::paintEvent(QPaintEvent* event) +{ + // rander the loading image + // TODO(Ethan): It's slow and memory confusing when zooming in + if(pix_display_.isNull()) return; + QPainter painter(this); + painter.drawPixmap(move_step_.x() +(width() - pix_display_.width()) / 2, move_step_ .y()+ (height() - pix_display_.height()) / 2, pix_display_); +} + +void QImgViewWidget::wheelEvent(QWheelEvent* event) +{ + // zoom in and out when scrolling mouse + if (event->delta() > 0) { + zoom_scale_ *= 1.1; + } + else { + zoom_scale_ *= 0.9; + } + // TODO(Ethan): It's slow and memory confusing when zooming in + if(!pix_ori_.isNull()) + pix_display_ = pix_ori_.scaled(zoom_scale_ * size(), Qt::KeepAspectRatio); + update(); +} + +void QImgViewWidget::mousePressEvent(QMouseEvent* event) +{ + if (event->button() == Qt::LeftButton) { + if (!move_start_) { + move_start_ = true; + is_moving_ = false; + mouse_point_ = event->globalPos(); + } + } +} + +void QImgViewWidget::mouseReleaseEvent(QMouseEvent* event) +{ + if (event->button() == Qt::LeftButton) { + if(move_start_) { + move_start_ = false; + is_moving_ = false; + } + } +} + +void QImgViewWidget::mouseMoveEvent(QMouseEvent* event) +{ + // move image when moving mouse + if (move_start_) { + const QPoint mos_pt = event->globalPos(); + move_step_ += mos_pt - mouse_point_; + is_moving_ = true; + mouse_point_ = mos_pt; + repaint(); + } +} + +void QImgViewWidget::resizeEvent(QResizeEvent* event) +{ + if(!pix_ori_.isNull()) + pix_display_ = pix_ori_.scaled(zoom_scale_ * size(), Qt::KeepAspectRatio); + update(); +} diff --git a/QImgViewWidget.h b/QImgViewWidget.h new file mode 100644 index 0000000..2d0ce3f --- /dev/null +++ b/QImgViewWidget.h @@ -0,0 +1,44 @@ +#ifndef QIMGVIEW_WIDGET_H +#define QIMGVIEW_WIDGET_H + +#include +#include + +// widget for displaying 2d image +class QImgViewWidget : + public QWidget +{ +public: + QImgViewWidget(QWidget * parent = 0); + ~QImgViewWidget() = default; + + /**\brief load image date from file path */ + void SetImage(const QImage& img); + + void ResetTransform(); +private: + /* image data for displaying */ + QImage img_display_; + /* original pixmap data*/ + QPixmap pix_ori_; + /* pixmap data for displaying */ + QPixmap pix_display_; + + /* zoom scale controlled by mouse wheel */ + float zoom_scale_; + /* move step controlled by mouse moving*/ + QPoint move_step_; + bool move_start_; + bool is_moving_; + QPoint mouse_point_; + +protected: + void paintEvent(QPaintEvent* event) override; + void wheelEvent(QWheelEvent* event) override; + void mousePressEvent(QMouseEvent* event) override; + void mouseReleaseEvent(QMouseEvent* event) override; + void mouseMoveEvent(QMouseEvent* event) override; + void resizeEvent(QResizeEvent* event) override; +}; + +#endif \ No newline at end of file diff --git a/README.md b/README.md index 6d0201c..f7bce9d 100644 --- a/README.md +++ b/README.md @@ -1,132 +1,32 @@ # TStoneCalibration 计算机视觉标定工具箱 -计算机视觉标定工具箱项目由香港中文大学天石机器人研究所嵌入式AI组发起,基于Qt和OpenCV开发,目标是做一款可以满足各种视觉设备标定的工具箱。 +计算机视觉标定工具箱项目由香港中文大学天石机器人研究所嵌入式AI组发起,目标是做一款可以满足各种视觉设备标定的工具箱。 目前已开发的标定工具有: * 光学相机内参标定工具 -## 文件结构 -|文件名/文件夹名|功能| -|:--|:--| -|main.cpp|程序入口| -|mainwindow.cpp|工具箱主界面| -|AboutUs.cpp|【关于我们】对话框的实现| -|camera_calibration|光学相机内参标定工具| - -每一个标定工具文件夹里有更详细的README介绍文件。 - -## 下载 -Release页面有最新的Windows程序下载。 - -## Contribute -### 环境配置 -1. 安装Qt以及Qt Creator,使用Qt Creator打开`TStoneCalibration.pro` - -2. 修改`TStoneCalibration.pro`中OpenCV的头文件路径和动态链接库的路径为你当前电脑里对应的路径 -```txt -INCLUDEPATH += C:/Users/uncle/Desktop/OpenCV/install/include\ - C:/Users/uncle/Desktop/OpenCV/install/include/opencv\ - C:/Users/uncle/Desktop/OpenCV/install/include/opencv2 - -LIBS += C:/Users/uncle/Desktop/OpenCV/install/x86/mingw/lib/libopencv_core310.dll.a\ - C:/Users/uncle/Desktop/OpenCV/install/x86/mingw/lib/libopencv_calib3d310.dll.a\ - C:/Users/uncle/Desktop/OpenCV/install/x86/mingw/lib/libopencv_highgui310.dll.a\ - C:/Users/uncle/Desktop/OpenCV/install/x86/mingw/lib/libopencv_imgcodecs310.dll.a\ - C:/Users/uncle/Desktop/OpenCV/install/x86/mingw/lib/libopencv_imgproc310.dll.a\ - C:/Users/uncle/Desktop/OpenCV/install/x86/mingw/lib/libopencv_features2d310.dll.a -``` -修改上述内容。 - -3. 如果你在Linux上开发,需安装`v4l2`库。 +## 编译 +项目基于`Qt`和`OpenCV 4`,在Ubuntu 20.04下开发。 +* 安装Qt ```bash -sudo apt-get install libv4l-dev -``` - -### 开发 -在此基础上可以很容易添加新的工具,添加流程如下(以下假设要添加一个名为`Hand_eye_calibration`的工具): - -#### 1.新建文件夹 -在根目录下新建一个名为`hand_eye_calibration`的文件夹。 - -#### 2.创建程序入口函数 -使用Qt Creator在该文件夹下新建一个UI,命名为HandEyeCalibration - -![new_ui](guide/new_ui.jpg) - -![new_ui](guide/new_ui_dir.jpg) - -这会自动创建`handeyecalibration.cpp`,`handeyecalibration.h`,`handeyecalibration.ui`三个文件。 - -#### 3.在工具箱界面中添加图标 -打开`mainwindow.ui`,添加工具图标。 - -![new_ui](guide/new_ui_3.jpg) - -该图标由一个QPushButton和一个QLable组成,QPushButton的名字我设置为`HandEyeCalib`。 - -#### 4.添加相关逻辑 -打开`mainwindow.h`,添加头文件: -```cpp -#include "hand_eye_calibration/handeyecalibration.h" +sudo apt-get install qt5-default ``` - -添加私有成员变量和槽函数 -```cpp -class MainWindow : public QMainWindow -{ - Q_OBJECT - -public: - explicit MainWindow(QWidget *parent = nullptr); - ~MainWindow(); - -private slots: - void startCameraCalib(); - void showIntro(); - void startHandEyeCalib(); // 添加这一行 - -private: - Ui::MainWindow *ui; - CameraCalibration* camera_calibration; - QAction *about; - AboutUs *a; - HandEyeCalibration *hand_eye; // 添加这一行 -}; +* 安装OpenCV 4 +```bash +sudo apt-get install libopencv-core4.2 ``` -打开`mainwindow.cpp`,添加槽函数的实现: -```cpp -MainWindow::MainWindow(QWidget *parent) : - QMainWindow(parent), - ui(new Ui::MainWindow) -{ - ui->setupUi(this); - setFixedSize(this->width(), this->height()); - ui->CameraCalib->setFlat(true); - connect(ui->CameraCalib, SIGNAL(clicked()), this, SLOT(startCameraCalib())); - about = ui->menu->addAction("关于"); - QFont font = about->font(); - font.setPixelSize(12); - about->setFont(font); - connect(about, SIGNAL(triggered()), this, SLOT(showIntro())); - // 添加下面这两行,关联按钮信号和槽函数 - ui->HandEyeCalib->setFlat(true); // 设置QPushButton颜色透明 - connect(ui->HandEyeCalib, SIGNAL(clicked()), this, SLOT(startHandEyeCalib())); -} +也可以使用OpenCV 3,里面有一些OpenCV的宏定义需要修改一下。 -void MainWindow::startHandEyeCalib() -{ - hand_eye = new HandEyeCalibration(); - QFont font = hand_eye->font(); - font.setPixelSize(12); - hand_eye->setFont(font); - hand_eye->setWindowTitle("手眼标定"); - hand_eye->show(); -} +* 编译 +```bash +mkdir build +cd build +cmake .. +make ``` -#### 5.编译运行 -![new_tool](guide/new_tool.jpg) +## 下载 +Release页面有最新的Windows程序下载。 -添加成功!你可以专心开发你的工具了。 \ No newline at end of file diff --git a/TStoneCalibration.pro b/TStoneCalibration.pro deleted file mode 100644 index adaeaf5..0000000 --- a/TStoneCalibration.pro +++ /dev/null @@ -1,81 +0,0 @@ -QT += core gui -QT += multimedia multimediawidgets -QT += network -greaterThan(QT_MAJOR_VERSION, 4): QT += widgets - -CONFIG += c++11 - -# The following define makes your compiler emit warnings if you use -# any Qt feature that has been marked deprecated (the exact warnings -# depend on your compiler). Please consult the documentation of the -# deprecated API in order to know how to port your code away from it. -DEFINES += QT_DEPRECATED_WARNINGS - -# You can also make your code fail to compile if it uses deprecated APIs. -# In order to do so, uncomment the following line. -# You can also select to disable deprecated APIs only up to a certain version of Qt. -#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0 - -RC_ICONS = icon.ico - -SOURCES += \ - AboutUs.cpp \ - camera_calibration/CameraCalibration.cpp \ - camera_calibration/chessboard.cpp \ - camera_calibration/choose_two_dir.cpp \ - camera_calibration/double_capture.cpp \ - camera_calibration/double_capture_linux.cpp \ - camera_calibration/findCorner.cpp \ - camera_calibration/single_capture_linux.cpp \ - hand_eye_calibration/HandEyeCalibration.cpp \ - hand_eye_calibration/TSAIleastSquareCalibration.cpp \ - main.cpp \ - camera_calibration/single_capture.cpp \ - camera_calibration/choose_yaml.cpp \ - mainwindow.cpp - -HEADERS += \ - AboutUs.h \ - camera_calibration/CameraCalibration.h \ - camera_calibration/chessboard.h \ - camera_calibration/choose_two_dir.h \ - camera_calibration/double_capture.h \ - camera_calibration/double_capture_linux.h \ - camera_calibration/findCorner.h \ - camera_calibration/single_capture.h \ - camera_calibration/single_capture_linux.h \ - camera_calibration/v4l2.hpp \ - camera_calibration/choose_yaml.h \ - hand_eye_calibration/HandEyeCalibration.h \ - hand_eye_calibration/TSAIleastSquareCalibration.h \ - mainwindow.h - -FORMS += \ - AboutUs.ui \ - camera_calibration/CameraCalibration.ui \ - camera_calibration/choose_two_dir.ui \ - camera_calibration/double_capture.ui \ - camera_calibration/single_capture.ui \ - camera_calibration/choose_yaml.ui \ - hand_eye_calibration/HandEyeCalibration.ui \ - mainwindow.ui - -INCLUDEPATH += C:/Users/uncle/Desktop/OpenCV_3.2.0/include\ - C:/Users/uncle/Desktop/OpenCV_3.2.0/include/opencv\ - C:/Users/uncle/Desktop/OpenCV_3.2.0/include/opencv2 - -LIBS += C:/Users/uncle/Desktop/OpenCV_3.2.0/x64/mingw/lib/libopencv_core320.dll.a\ - C:/Users/uncle/Desktop/OpenCV_3.2.0/x64/mingw/lib/libopencv_calib3d320.dll.a\ - C:/Users/uncle/Desktop/OpenCV_3.2.0/x64/mingw/lib/libopencv_highgui320.dll.a\ - C:/Users/uncle/Desktop/OpenCV_3.2.0/x64/mingw/lib/libopencv_imgcodecs320.dll.a\ - C:/Users/uncle/Desktop/OpenCV_3.2.0/x64/mingw/lib/libopencv_imgproc320.dll.a\ - C:/Users/uncle/Desktop/OpenCV_3.2.0/x64/mingw/lib/libopencv_features2d320.dll.a - -# Default rules for deployment. -qnx: target.path = /tmp/$${TARGET}/bin -else: unix:!android: target.path = /opt/$${TARGET}/bin -!isEmpty(target.path): INSTALLS += target - -RESOURCES += \ - camera_calibration/res/image.qrc \ - hand_eye_calibration/res/hand_eye_image.qrc diff --git a/camera_calibration/CameraCalibration.cpp b/camera_calibration/CameraCalibration.cpp deleted file mode 100644 index f8cdbf7..0000000 --- a/camera_calibration/CameraCalibration.cpp +++ /dev/null @@ -1,1411 +0,0 @@ -#include "CameraCalibration.h" -#include "ui_CameraCalibration.h" - -cv::Mat find_corner_thread_img; -struct Chessboarder_t find_corner_thread_chessboard; - -CameraCalibration::CameraCalibration(QWidget *parent) - : QMainWindow(parent) - , ui(new Ui::CameraCalibration) -{ - ui->setupUi(this); - setFixedSize(this->width(), this->height()); - ui->addImage->setFlat(true); - ui->calibrate->setFlat(true); - ui->export_1->setFlat(true); - ui->delete_1->setFlat(true); - ui->undistort->setFlat(true); - ui->Camera->setFlat(true); - findcorner_thread = new FindcornerThread(); - connect(findcorner_thread, SIGNAL(isDone()), this, SLOT(DealThreadDone())); - ShowIntro(); - QMenu *menu = new QMenu(); - QAction *open_camera = menu->addAction("打开相机"); - QFont font = menu->font(); - font.setPixelSize(12); - menu->setFont(font); - connect(open_camera, SIGNAL(triggered()), this,SLOT(OpenCamera())); - ui->Camera->setMenu(menu); - ui->listWidget->setSelectionMode(QAbstractItemView::ExtendedSelection); // 按ctrl多选 - connect(ui->addImage, SIGNAL(clicked()), this, SLOT(addImage())); - connect(ui->delete_1, SIGNAL(clicked()), this, SLOT(deleteImage())); - chessboard_size = 0; - connect(ui->calibrate, SIGNAL(clicked()), this, SLOT(calibrate())); - connect(ui->fisheye, SIGNAL(stateChanged(int)), this, SLOT(fisheyeModeSwitch(int))); - connect(ui->export_1, SIGNAL(clicked()), this, SLOT(exportParam())); - connect(ui->undistort, SIGNAL(clicked()), this, SLOT(distortModeSwitch())); - connect(ui->listWidget, SIGNAL(currentItemChanged(QListWidgetItem*, QListWidgetItem*)), this, SLOT(chooseImage(QListWidgetItem*, QListWidgetItem*))); - connect(ui->double_camera, SIGNAL(toggled(bool)), this, SLOT(reset())); - connect(ui->single_camera, SIGNAL(toggled(bool)), this, SLOT(reset())); - connect(ui->double_undistort, SIGNAL(toggled(bool)), this, SLOT(undistortReset())); - connect(ui->single_undistort, SIGNAL(toggled(bool)), this, SLOT(undistortReset())); - saveImage = ui->menu->addAction("导出矫正图片"); - font = saveImage->font(); - font.setPixelSize(12); - saveImage->setFont(font); - connect(saveImage, SIGNAL(triggered()), this, SLOT(saveUndistort())); -} - -CameraCalibration::~CameraCalibration() -{ - delete ui; -} - -// 显示简单的文字引导 -void CameraCalibration::ShowIntro() -{ - ui->intro->setFixedHeight(100); - if(ui->double_camera->isChecked() || ui->double_undistort->isChecked()) - { - QString str = "点击左上角添加图片"; - str += "\n"; - str += "左右相机图片分别放于两个文件夹"; - str += "\n"; - str += "对应图片名称需一致。"; - str += "\n"; - str += "\n"; - str += "单/双目纠正模式可纠正无棋盘的图片。"; - ui->intro->setText(str); - } - else if(ui->single_camera->isChecked() || ui->single_undistort->isChecked()) - { - QString str = "点击左上角添加图片"; - str += "\n"; - str += "15至20张较为适宜"; - str += "\n"; - str += "\n"; - str += "单/双目纠正模式可纠正无棋盘的图片。"; - ui->intro->setText(str); - } -} - -// 隐藏文字指导 -void CameraCalibration::HiddenIntro() -{ - ui->intro->setText(""); - ui->intro->setFixedHeight(0); -} - -// 使用相机采集图片,根据标定模式选择采集模式 -void CameraCalibration::OpenCamera() -{ - if(ui->single_camera->isChecked()) - { -#ifdef Q_OS_LINUX - int camcount = v4l2.GetDeviceCount(); - if(camcount < 1) - { - QMessageBox::warning(this, "警告", "没有检测到摄像头,请插上摄像头后再打开该窗口", QMessageBox::Yes); - return; - } - single_c = new single_capture_linux(); -#elif defined(Q_OS_WIN32) - QList camera_list = QCameraInfo::availableCameras(); - if(camera_list.length() < 1) - { - QMessageBox::warning(this, "警告", "没有检测到摄像头,请插上摄像头后再打开该窗口", QMessageBox::Yes); - return; - } - single_c = new single_capture(); -#endif - single_c->setWindowTitle("单个相机拍照"); - QFont font = single_c->font(); - font.setPixelSize(12); - single_c->setFont(font); - single_c->show(); - } - else - { -#ifdef Q_OS_LINUX - int camcount = v4l2_left.GetDeviceCount(); - if(camcount < 2) - { - QMessageBox::warning(this, "警告", "摄像头少于两个,请插上摄像头后再打开该窗口", QMessageBox::Yes); - return; - } - double_c = new double_capture_linux(); -#elif defined(Q_OS_WIN32) - QList camera_list = QCameraInfo::availableCameras(); - if(camera_list.length() < 2) - { - QMessageBox::warning(this, "警告", "摄像头少于两个,请插上摄像头后再打开该窗口", QMessageBox::Yes); - return; - } - double_c = new double_capture(); -#endif - double_c->setWindowTitle("两个相机拍照"); - QFont font = double_c->font(); - font.setPixelSize(12); - double_c->setFont(font); - double_c->show(); - } -} - -// 导出矫正图片 -void CameraCalibration::saveUndistort() -{ - if(calibrate_flag == false) - { - if(ui->single_undistort->isChecked() || ui->double_undistort->isChecked()) - { - QMessageBox::warning(this, "警告", "还未获取到相机参数,请点击UNDISTORT按钮加载yaml文件。", QMessageBox::Yes, QMessageBox::Yes); - return; - } - else - { - QMessageBox::critical(NULL, "错误", "请先标定", QMessageBox::Yes, QMessageBox::Yes); - return; - } - } - QString srcDirPath = QFileDialog::getExistingDirectory(nullptr, "选择保存路径", "./"); - if(srcDirPath.length() <= 0) - return; - QTextCodec *code = QTextCodec::codecForName("GB2312");//解决中文路径问题 - if(ui->double_camera->isChecked() || ui->double_undistort->isChecked()) - { - QDir dir; - dir.mkdir(srcDirPath+"/left/"); - dir.mkdir(srcDirPath+"/right/"); - for(unsigned int i = 0; i < stereo_imgs.size(); i++) - { - struct Stereo_Img_t img = stereo_imgs[i]; - cv::Mat left_dst = img.left_img.clone(); - cv::Mat right_dst = img.right_img.clone(); - if(fisheye_flag == false) - { - cv::Mat mapx, mapy; - cv::initUndistortRectifyMap(cameraMatrix, distCoefficients, R1, P1, img_size, CV_16SC2, mapx, mapy); - cv::remap(img.left_img, left_dst, mapx, mapy, cv::INTER_LINEAR); - cv::initUndistortRectifyMap(cameraMatrix2, distCoefficients2, R2, P2, img_size, CV_16SC2, mapx, mapy); - cv::remap(img.right_img, right_dst, mapx, mapy, cv::INTER_LINEAR); - } - else - { - cv::Mat mapx, mapy; - cv::fisheye::initUndistortRectifyMap(K, D, R1, P1, img_size, CV_16SC2, mapx, mapy); - cv::remap(img.left_img, left_dst, mapx, mapy, cv::INTER_LINEAR); - cv::fisheye::initUndistortRectifyMap(K2, D2, R2, P2, img_size, CV_16SC2, mapx, mapy); - cv::remap(img.right_img, right_dst, mapx, mapy, cv::INTER_LINEAR); - } - QString save_name = srcDirPath + "/left/" + img.left_file_name; - std::string str = code->fromUnicode(save_name).data(); - cv::imwrite(str, left_dst); - save_name = srcDirPath + "/right/" + img.left_file_name; - str = code->fromUnicode(save_name).data(); - cv::imwrite(str, right_dst); - for(int i = 1; i < 10; i++) - { - cv::line(left_dst, cv::Point(0, left_dst.rows*i/10), cv::Point(left_dst.cols-1, left_dst.rows*i/10), cv::Scalar(0,0,255), 2); - cv::line(right_dst, cv::Point(0, right_dst.rows*i/10), cv::Point(right_dst.cols-1, right_dst.rows*i/10), cv::Scalar(0,0,255), 2); - } - cv::Mat combian_img = cv::Mat::zeros(cv::Size(img.left_img.cols*2, img.left_img.rows), CV_8UC3); - cv::Mat new_roi = combian_img(cv::Rect(0, 0, img.left_img.cols, img.left_img.rows)); - left_dst.convertTo(new_roi, new_roi.type()); - new_roi = combian_img(cv::Rect(img.left_img.cols, 0, img.left_img.cols, img.left_img.rows)); - right_dst.convertTo(new_roi, new_roi.type()); - save_name = srcDirPath + "/" + img.left_file_name; - str = code->fromUnicode(save_name).data(); - cv::imwrite(str, combian_img); - } - } - else - { - for(unsigned int i = 0; i < imgs.size(); i++) - { - cv::Mat dst; - if(fisheye_flag == false) - cv::undistort(imgs[i].img, dst, cameraMatrix, distCoefficients); - else - cv::fisheye::undistortImage(imgs[i].img, dst, K, D, K, imgs[i].img.size()); - QString save_name = srcDirPath + "/" + imgs[i].file_name; - std::string str = code->fromUnicode(save_name).data(); - cv::imwrite(str, dst); - } - } -} - -// 切换模式时删除所有图片缓存 -void CameraCalibration::reset() -{ - ShowIntro(); - ui->k_2->setEnabled(true); - ui->k_3->setEnabled(true); - ui->tangential->setEnabled(true); - ui->calibrate->setEnabled(true); - ui->export_1->setEnabled(true); - chessboard_size = 0; - calibrate_flag = false; - distort_flag = false; - disconnect(ui->listWidget, SIGNAL(currentItemChanged(QListWidgetItem*, QListWidgetItem*)), this, SLOT(chooseImage(QListWidgetItem*, QListWidgetItem*))); - ui->listWidget->clear(); - connect(ui->listWidget, SIGNAL(currentItemChanged(QListWidgetItem*, QListWidgetItem*)), this, SLOT(chooseImage(QListWidgetItem*, QListWidgetItem*))); - if(imgs.size() > 0) - imgs.clear(); - if(stereo_imgs.size() > 0) - stereo_imgs.clear(); -} - -void CameraCalibration::undistortReset() -{ - ShowIntro(); - ui->k_2->setDisabled(true); - ui->k_3->setDisabled(true); - ui->tangential->setDisabled(true); - ui->calibrate->setDisabled(true); - ui->export_1->setDisabled(true); - chessboard_size = 0; - calibrate_flag = false; - distort_flag = false; - disconnect(ui->listWidget, SIGNAL(currentItemChanged(QListWidgetItem*, QListWidgetItem*)), this, SLOT(chooseImage(QListWidgetItem*, QListWidgetItem*))); - ui->listWidget->clear(); - connect(ui->listWidget, SIGNAL(currentItemChanged(QListWidgetItem*, QListWidgetItem*)), this, SLOT(chooseImage(QListWidgetItem*, QListWidgetItem*))); - if(imgs.size() > 0) - imgs.clear(); - if(stereo_imgs.size() > 0) - stereo_imgs.clear(); -} - -// 显示选择的图像 -void CameraCalibration::chooseImage(QListWidgetItem* item, QListWidgetItem*) -{ - int id = ui->listWidget->row(item); - if(ui->double_camera->isChecked()) - { - // 如果是双目模式 - struct Stereo_Img_t img = stereo_imgs[id]; - cv::Mat left_dst = img.left_img.clone(); - cv::Mat right_dst = img.right_img.clone(); - if(distort_flag == true && calibrate_flag == true) - { - // 对左右图像进行矫正 - if(fisheye_flag == false) - { - cv::Mat mapx, mapy; - cv::initUndistortRectifyMap(cameraMatrix, distCoefficients, R1, P1, img_size, CV_16SC2, mapx, mapy); - cv::remap(img.left_img, left_dst, mapx, mapy, cv::INTER_LINEAR); - cv::initUndistortRectifyMap(cameraMatrix2, distCoefficients2, R2, P2, img_size, CV_16SC2, mapx, mapy); - cv::remap(img.right_img, right_dst, mapx, mapy, cv::INTER_LINEAR); - } - else - { - cv::Mat mapx, mapy; - cv::fisheye::initUndistortRectifyMap(K, D, R1, P1, img_size, CV_16SC2, mapx, mapy); - cv::remap(img.left_img, left_dst, mapx, mapy, cv::INTER_LINEAR); - cv::fisheye::initUndistortRectifyMap(K2, D2, R2, P2, img_size, CV_16SC2, mapx, mapy); - cv::remap(img.right_img, right_dst, mapx, mapy, cv::INTER_LINEAR); - } - // 绘制横线以观察左右图像极线是否对齐 - for(int i = 1; i < 10; i++) - { - cv::line(left_dst, cv::Point(0, left_dst.rows*i/10), cv::Point(left_dst.cols-1, left_dst.rows*i/10), cv::Scalar(0,0,255), 2); - cv::line(right_dst, cv::Point(0, right_dst.rows*i/10), cv::Point(right_dst.cols-1, right_dst.rows*i/10), cv::Scalar(0,0,255), 2); - } - } - else - { - // 非矫正模式下显示角点位置 - for(unsigned int i = 0; i < img.left_img_points.size(); i++) - { - cv::circle(left_dst, img.left_img_points[i], 5, cv::Scalar(0,0,255), 2); - } - for(unsigned int i = 0; i < img.right_img_points.size(); i++) - { - cv::circle(right_dst, img.right_img_points[i], 5, cv::Scalar(0,0,255), 2); - } - if(calibrate_flag == true) - { - // 显示标定后角点的重投影 - if(fisheye_flag == false) - { - std::vector reproject_img_p; - projectPoints(img.world_points, img.left_rvec, img.left_tvec, cameraMatrix, distCoefficients, reproject_img_p); - for(unsigned int i = 0; i < reproject_img_p.size(); i++) - { - cv::circle(left_dst, reproject_img_p[i], 3, cv::Scalar(255,0,0), 2); - } - projectPoints(img.world_points, img.right_rvec, img.right_tvec, cameraMatrix2, distCoefficients2, reproject_img_p); - for(unsigned int i = 0; i < reproject_img_p.size(); i++) - { - cv::circle(right_dst, reproject_img_p[i], 3, cv::Scalar(255,0,0), 2); - } - } - else - { - std::vector reproject_img_p; - std::vector w_p_; - for(unsigned int i = 0; i < img.world_points.size(); i++) - { - w_p_.push_back(img.world_points[i]); - } - cv::fisheye::projectPoints(w_p_, reproject_img_p, img.left_fish_rvec, img.left_fish_tvec, K, D); - for(unsigned int i = 0; i < reproject_img_p.size(); i++) - { - cv::circle(left_dst, reproject_img_p[i], 3, cv::Scalar(255,0,0), 2); - } - cv::fisheye::projectPoints(w_p_, reproject_img_p, img.right_fish_rvec, img.right_fish_tvec, K2, D2); - for(unsigned int i = 0; i < reproject_img_p.size(); i++) - { - cv::circle(right_dst, reproject_img_p[i], 3, cv::Scalar(255,0,0), 2); - } - } - } - } - int height = img.left_img.rows*265/img.left_img.cols; - cv::resize(left_dst, left_dst, cv::Size(265, height)); - cv::resize(right_dst, right_dst, cv::Size(265, height)); - cv::Mat combian_img = cv::Mat::zeros(cv::Size(530, height), CV_8UC3); - cv::Mat new_roi = combian_img(cv::Rect(0, 0, 265, height)); - left_dst.convertTo(new_roi, new_roi.type()); - new_roi = combian_img(cv::Rect(265, 0, 265, height)); - right_dst.convertTo(new_roi, new_roi.type()); - QImage qimage = Mat2QImage(combian_img); - ui->Image->setPixmap(QPixmap::fromImage(qimage)); - } - else if(ui->single_camera->isChecked()) - { - // 单目逻辑同双目 - cv::Mat img; - std::vector corners; - std::vector w_p; - cv::Mat rvecs, tvecs; - cv::Vec3d fish_rvecs, fish_tvecs; - img = imgs[id].img.clone(); - corners = imgs[id].img_points; - w_p = imgs[id].world_points; - tvecs = imgs[id].tvec; - rvecs = imgs[id].rvec; - fish_rvecs = imgs[id].fish_rvec; - fish_tvecs = imgs[id].fish_tvec; - for(unsigned int i = 0; i < corners.size(); i++) - { - cv::circle(img, corners[i], 5, cv::Scalar(0,0,255), 2); - } - if(calibrate_flag == true) - { - if(fisheye_flag == false) - { - std::vector reproject_img_p; - projectPoints(w_p, rvecs, tvecs, cameraMatrix, distCoefficients, reproject_img_p); - for(unsigned int i = 0; i < reproject_img_p.size(); i++) - { - cv::circle(img, reproject_img_p[i], 3, cv::Scalar(255,0,0), 2); - } - } - else - { - std::vector reproject_img_p; - std::vector w_p_; - for(unsigned int i = 0; i < w_p.size(); i++) - { - w_p_.push_back(w_p[i]); - } - cv::fisheye::projectPoints(w_p_, reproject_img_p, fish_rvecs, fish_tvecs, K, D); - for(unsigned int i = 0; i < reproject_img_p.size(); i++) - { - cv::circle(img, reproject_img_p[i], 3, cv::Scalar(255,0,0), 2); - } - } - } - if(distort_flag == true && calibrate_flag == true) - { - cv::Mat dst; - if(fisheye_flag == false) - cv::undistort(img, dst, cameraMatrix, distCoefficients); - else - cv::fisheye::undistortImage(img, dst, K, D, K, img.size()); - img = dst; - } - if(img.cols > img.rows) - cv::resize(img, img, cv::Size(530, img.rows*530/img.cols)); - else - cv::resize(img, img, cv::Size(img.cols*495/img.rows, 495)); - QImage qimage = Mat2QImage(img); - ui->Image->setPixmap(QPixmap::fromImage(qimage)); - } - else if(ui->double_undistort->isChecked()) - { - // 如果是双目模式 - struct Stereo_Img_t img = stereo_imgs[id]; - cv::Mat left_dst = img.left_img.clone(); - cv::Mat right_dst = img.right_img.clone(); - if(distort_flag == true) - { - // 对左右图像进行矫正 - if(fisheye_flag == false) - { - cv::Mat mapx, mapy; - cv::initUndistortRectifyMap(cameraMatrix, distCoefficients, R1, P1, img_size, CV_16SC2, mapx, mapy); - cv::remap(img.left_img, left_dst, mapx, mapy, cv::INTER_LINEAR); - cv::initUndistortRectifyMap(cameraMatrix2, distCoefficients2, R2, P2, img_size, CV_16SC2, mapx, mapy); - cv::remap(img.right_img, right_dst, mapx, mapy, cv::INTER_LINEAR); - } - else - { - cv::Mat mapx, mapy; - cv::fisheye::initUndistortRectifyMap(K, D, R1, P1, img_size, CV_16SC2, mapx, mapy); - cv::remap(img.left_img, left_dst, mapx, mapy, cv::INTER_LINEAR); - cv::fisheye::initUndistortRectifyMap(K2, D2, R2, P2, img_size, CV_16SC2, mapx, mapy); - cv::remap(img.right_img, right_dst, mapx, mapy, cv::INTER_LINEAR); - } - // 绘制横线以观察左右图像极线是否对齐 - for(int i = 1; i < 10; i++) - { - cv::line(left_dst, cv::Point(0, left_dst.rows*i/10), cv::Point(left_dst.cols-1, left_dst.rows*i/10), cv::Scalar(0,0,255), 2); - cv::line(right_dst, cv::Point(0, right_dst.rows*i/10), cv::Point(right_dst.cols-1, right_dst.rows*i/10), cv::Scalar(0,0,255), 2); - } - } - int height = img.left_img.rows*265/img.left_img.cols; - cv::resize(left_dst, left_dst, cv::Size(265, height)); - cv::resize(right_dst, right_dst, cv::Size(265, height)); - cv::Mat combian_img = cv::Mat::zeros(cv::Size(530, height), CV_8UC3); - cv::Mat new_roi = combian_img(cv::Rect(0, 0, 265, height)); - left_dst.convertTo(new_roi, new_roi.type()); - new_roi = combian_img(cv::Rect(265, 0, 265, height)); - right_dst.convertTo(new_roi, new_roi.type()); - QImage qimage = Mat2QImage(combian_img); - ui->Image->setPixmap(QPixmap::fromImage(qimage)); - } - else - { - // 单目逻辑同双目 - cv::Mat img = imgs[id].img.clone(); - if(distort_flag == true) - { - cv::Mat dst; - if(fisheye_flag == false) - cv::undistort(img, dst, cameraMatrix, distCoefficients); - else - cv::fisheye::undistortImage(img, dst, K, D, K, img.size()); - img = dst; - } - if(img.cols > img.rows) - cv::resize(img, img, cv::Size(530, img.rows*530/img.cols)); - else - cv::resize(img, img, cv::Size(img.cols*495/img.rows, 495)); - QImage qimage = Mat2QImage(img); - ui->Image->setPixmap(QPixmap::fromImage(qimage)); - } -} - -// 角点寻找线程结束时的回调函数 -void CameraCalibration::DealThreadDone() -{ - findcorner_thread->quit(); - findcorner_thread->wait(); - thread_done = true; -} - -void CameraCalibration::receiveYamlPath(QString str) -{ - cv::FileStorage fs_read(str.toStdString(), cv::FileStorage::READ); - if(ui->single_undistort->isChecked()) - { - if(fs_read["cameraMatrix"].empty() || fs_read["distCoeffs"].empty()) - { - QMessageBox::critical(this, "错误", "YAML文件格式错误", QMessageBox::Yes, QMessageBox::Yes); - return; - } - if(fisheye_flag == false) - { - fs_read["cameraMatrix"] >> cameraMatrix; - fs_read["distCoeffs"] >> distCoefficients; - calibrate_flag = true; - } - else - { - cv::Mat camera_matrix, Dist; - fs_read["cameraMatrix"] >> camera_matrix; - fs_read["distCoeffs"] >> Dist; - K(0,0) = camera_matrix.at(0,0); - K(0,1) = 0; - K(0,2) = camera_matrix.at(0,2); - K(1,0) = 0; - K(1,1) = camera_matrix.at(1,1); - K(1,2) = camera_matrix.at(1,2); - K(1,0) = 0; - K(2,1) = 0; - K(2,2) = 1; - D[0] = Dist.at(0,0); - D[1] = Dist.at(0,1); - D[2] = Dist.at(0,2); - D[3] = Dist.at(0,3); - calibrate_flag = true; - } - } - else - { - if(fs_read["left_camera_Matrix"].empty() || fs_read["left_camera_distCoeffs"].empty() || fs_read["right_camera_Matrix"].empty() || fs_read["right_camera_distCoeffs"].empty()) - { - QMessageBox::critical(this, "错误", "YAML文件格式错误", QMessageBox::Yes, QMessageBox::Yes); - return; - } - if(fs_read["Rotate_Matrix"].empty() || fs_read["Translate_Matrix"].empty() || fs_read["R1"].empty() || fs_read["R2"].empty() || fs_read["P1"].empty() || - fs_read["P2"].empty() || fs_read["Q"].empty()) - { - QMessageBox::critical(this, "错误", "YAML文件格式错误", QMessageBox::Yes, QMessageBox::Yes); - return; - } - if(fisheye_flag == false) - { - fs_read["left_camera_Matrix"] >> cameraMatrix; - fs_read["left_camera_distCoeffs"] >> distCoefficients; - fs_read["right_camera_Matrix"] >> cameraMatrix2; - fs_read["right_camera_distCoeffs"] >> distCoefficients2; - fs_read["Rotate_Matrix"] >> R; - fs_read["Translate_Matrix"] >> T; - fs_read["R1"] >> R1; - fs_read["R2"] >> R2; - fs_read["P1"] >> P1; - fs_read["P2"] >> P2; - fs_read["Q"] >> Q; - if(cameraMatrix.at(0,0) == 0 || cameraMatrix2.at(0,0) == 0) - { - QMessageBox::critical(this, "错误", "YAML文件格式错误", QMessageBox::Yes, QMessageBox::Yes); - return; - } - calibrate_flag = true; - } - else - { - cv::Mat camera_matrix, Dist; - fs_read["left_camera_Matrix"] >> camera_matrix; - fs_read["left_camera_distCoeffs"] >> Dist; - K(0,0) = camera_matrix.at(0,0); - K(0,1) = 0; - K(0,2) = camera_matrix.at(0,2); - K(1,0) = 0; - K(1,1) = camera_matrix.at(1,1); - K(1,2) = camera_matrix.at(1,2); - K(1,0) = 0; - K(2,1) = 0; - K(2,2) = 1; - D[0] = Dist.at(0,0); - D[1] = Dist.at(0,1); - D[2] = Dist.at(0,2); - D[3] = Dist.at(0,3); - - fs_read["right_camera_Matrix"] >> camera_matrix; - fs_read["right_camera_distCoeffs"] >> Dist; - K2(0,0) = camera_matrix.at(0,0); - K2(0,1) = 0; - K2(0,2) = camera_matrix.at(0,2); - K2(1,0) = 0; - K2(1,1) = camera_matrix.at(1,1); - K2(1,2) = camera_matrix.at(1,2); - K2(1,0) = 0; - K2(2,1) = 0; - K2(2,2) = 1; - D2[0] = Dist.at(0,0); - D2[1] = Dist.at(0,1); - D2[2] = Dist.at(0,2); - D2[3] = Dist.at(0,3); - fs_read["Rotate_Matrix"] >> R; - fs_read["Translate_Matrix"] >> T; - fs_read["R1"] >> R1; - fs_read["R2"] >> R2; - fs_read["P1"] >> P1; - fs_read["P2"] >> P2; - fs_read["Q"] >> Q; - if(cameraMatrix.at(0,0) == 0 || cameraMatrix2.at(0,0) == 0) - { - QMessageBox::critical(this, "错误", "YAML文件格式错误", QMessageBox::Yes, QMessageBox::Yes); - return; - } - calibrate_flag = true; - } - } -} - -// 加载双目图像 -void CameraCalibration::receiveFromDialog(QString str) -{ - HiddenIntro(); - QStringList list = str.split(","); - QString left_src = list[0]; - QString right_src = list[1]; - chessboard_size = list[2].toDouble(); - QDir dir(left_src); - dir.setFilter(QDir::Files | QDir::NoSymLinks); - QStringList filters; - filters << "*.png" << "*.jpg" << "*.jpeg"; - dir.setNameFilters(filters); - QStringList imagesList = dir.entryList(); - if(ui->double_camera->isChecked()) - { - if(imagesList.length() <= 3) - { - QMessageBox::critical(NULL, "错误", "至少需要四组图片", QMessageBox::Yes, QMessageBox::Yes); - return; - } - QProgressDialog *dialog = new QProgressDialog(tr("检测角点..."),tr("取消"),0,imagesList.length(),this); - dialog->setWindowModality(Qt::WindowModal); - dialog->setMinimumDuration(0); - dialog->setWindowTitle("请稍候"); - dialog->setValue(0); - QFont font = dialog->font(); - font.setPixelSize(12); - dialog->setFont(font); - dialog->show(); - QTextCodec *code = QTextCodec::codecForName("GB2312");//解决中文路径问题 - for(int i = 0; i < imagesList.length(); i++) - { - QString left_file_name = left_src + "/" + imagesList[i]; - QString right_file_name = right_src + "/" + imagesList[i]; - std::string str = code->fromUnicode(right_file_name).data(); - cv::Mat right_image = cv::imread(str); - if(right_image.empty()) - { - dialog->setValue(i+1); - continue; - } - str = code->fromUnicode(left_file_name).data(); - cv::Mat left_image = cv::imread(str); - if(left_image.cols != right_image.cols || left_image.rows != right_image.rows) - { - QMessageBox::critical(NULL, "错误", "左右相机图片尺寸不一致", QMessageBox::Yes, QMessageBox::Yes); - delete dialog; - return; - } - find_corner_thread_img = left_image; - thread_done = false; - findcorner_thread->start(); - while(thread_done == false) - { - if(dialog->wasCanceled()) - { - DealThreadDone(); - delete dialog; - return; - } - // 强制Windows消息循环,防止程序出现未响应情况 - QCoreApplication::processEvents(); - } - struct Chessboarder_t left_chess = find_corner_thread_chessboard; - // 如果检测到多个棋盘,则剔除该图片 - if(left_chess.chessboard.size() != 1) - { - dialog->setValue(i+1); - continue; - } - find_corner_thread_img = right_image; - thread_done = false; - findcorner_thread->start(); - while(thread_done == false) - { - if(dialog->wasCanceled()) - { - DealThreadDone(); - delete dialog; - return; - } - QCoreApplication::processEvents(); - } - struct Chessboarder_t right_chess = find_corner_thread_chessboard; - // 如果检测到多个棋盘,则剔除该图片 - if(right_chess.chessboard.size() != 1) - { - dialog->setValue(i+1); - continue; - } - // 如果左右图片检测到的棋盘尺寸不对,则剔除该组图片 - if(left_chess.chessboard[0].rows != right_chess.chessboard[0].rows || - left_chess.chessboard[0].cols != right_chess.chessboard[0].cols) - { - dialog->setValue(i+1); - continue; - } - // 缓存图片及角点 - struct Stereo_Img_t img_; - img_.left_img = left_image; - img_.right_img = right_image; - img_.left_file_name = imagesList[i]; - img_.right_file_name = imagesList[i]; - for (unsigned int j = 0; j < left_chess.chessboard.size(); j++) - { - for (int u = 0; u < left_chess.chessboard[j].rows; u++) - { - for (int v = 0; v < left_chess.chessboard[j].cols; v++) - { - img_.left_img_points.push_back(left_chess.corners.p[left_chess.chessboard[j].at(u, v)]); - img_.world_points.push_back(cv::Point3f(u*chessboard_size, v*chessboard_size, 0)); - img_.right_img_points.push_back(right_chess.corners.p[right_chess.chessboard[j].at(u, v)]); - } - } - } - stereo_imgs.push_back(img_); - img_size = left_image.size(); - int img_height = left_image.rows*90/left_image.cols; - cv::resize(left_image, left_image, cv::Size(90, img_height)); - cv::resize(right_image, right_image, cv::Size(90, img_height)); - cv::Mat combian_img = cv::Mat::zeros(cv::Size(180, img_height), CV_8UC3); - cv::Mat new_roi = combian_img(cv::Rect(0, 0, 90, img_height)); - left_image.convertTo(new_roi, new_roi.type()); - new_roi = combian_img(cv::Rect(90, 0, 90, img_height)); - right_image.convertTo(new_roi, new_roi.type()); - QPixmap pixmap = QPixmap::fromImage(Mat2QImage(combian_img)); - QListWidgetItem* temp = new QListWidgetItem(); - temp->setSizeHint(QSize(220, img_height)); - temp->setIcon(QIcon(pixmap)); - temp->setText(imagesList[i]); - ui->listWidget->addItem(temp); - ui->listWidget->setIconSize(QSize(180, img_height)); - dialog->setValue(i+1); - } - delete dialog; - } - else if(ui->double_undistort->isChecked()) - { - QTextCodec *code = QTextCodec::codecForName("GB2312");//解决中文路径问题 - for(int i = 0; i < imagesList.length(); i++) - { - QString left_file_name = left_src + "/" + imagesList[i]; - QString right_file_name = right_src + "/" + imagesList[i]; - std::string str = code->fromUnicode(right_file_name).data(); - cv::Mat right_image = cv::imread(str); - if(right_image.empty()) - { - continue; - } - str = code->fromUnicode(left_file_name).data(); - cv::Mat left_image = cv::imread(str); - if(left_image.cols != right_image.cols || left_image.rows != right_image.rows) - { - QMessageBox::critical(NULL, "错误", "左右相机图片尺寸不一致", QMessageBox::Yes, QMessageBox::Yes); - return; - } - struct Stereo_Img_t img_; - img_.left_img = left_image; - img_.right_img = right_image; - img_.left_file_name = imagesList[i]; - img_.right_file_name = imagesList[i]; - stereo_imgs.push_back(img_); - img_size = left_image.size(); - int img_height = left_image.rows*90/left_image.cols; - cv::resize(left_image, left_image, cv::Size(90, img_height)); - cv::resize(right_image, right_image, cv::Size(90, img_height)); - cv::Mat combian_img = cv::Mat::zeros(cv::Size(180, img_height), CV_8UC3); - cv::Mat new_roi = combian_img(cv::Rect(0, 0, 90, img_height)); - left_image.convertTo(new_roi, new_roi.type()); - new_roi = combian_img(cv::Rect(90, 0, 90, img_height)); - right_image.convertTo(new_roi, new_roi.type()); - QPixmap pixmap = QPixmap::fromImage(Mat2QImage(combian_img)); - QListWidgetItem* temp = new QListWidgetItem(); - temp->setSizeHint(QSize(220, img_height)); - temp->setIcon(QIcon(pixmap)); - temp->setText(imagesList[i]); - ui->listWidget->addItem(temp); - ui->listWidget->setIconSize(QSize(180, img_height)); - } - } -} - -// 鱼眼相机切换 -void CameraCalibration::fisheyeModeSwitch(int state) -{ - fisheye_flag = state==0 ? false : true; - if(fisheye_flag == true) - { - ui->k_2->setDisabled(true); - ui->k_3->setDisabled(true); - ui->tangential->setDisabled(true); - } - else - { - ui->k_2->setEnabled(true); - ui->k_3->setEnabled(true); - ui->tangential->setEnabled(true); - } -} - -// 畸变矫正切换 -void CameraCalibration::distortModeSwitch() -{ - if(ui->single_undistort->isChecked() || ui->double_undistort->isChecked()) - { - if(calibrate_flag == false) - { - chooseYaml = new choose_yaml(); - chooseYaml->setWindowTitle("选择相机参数文件"); - QFont font = chooseYaml->font(); - font.setPixelSize(12); - chooseYaml->setFont(font); - chooseYaml->show(); - connect(chooseYaml, SIGNAL(SendSignal(QString)), this, SLOT(receiveYamlPath(QString))); - return; - } - } - static int cnt = 0; - cnt++; - if(cnt%2 == 1) - { - distort_flag = true; - } - else - { - distort_flag = false; - } - int id = ui->listWidget->selectionModel()->selectedIndexes()[0].row(); - chooseImage(ui->listWidget->item(id), ui->listWidget->item(id)); -} - -// Mat格式转QImage格式 -QImage CameraCalibration::Mat2QImage(cv::Mat cvImg) -{ - QImage qImg; - if(cvImg.channels()==3) - { - cv::cvtColor(cvImg,cvImg,CV_BGR2RGB); - qImg =QImage((const unsigned char*)(cvImg.data), - cvImg.cols, cvImg.rows, - cvImg.cols*cvImg.channels(), - QImage::Format_RGB888); - } - else if(cvImg.channels()==1) - { - qImg =QImage((const unsigned char*)(cvImg.data), - cvImg.cols,cvImg.rows, - cvImg.cols*cvImg.channels(), - QImage::Format_Indexed8); - } - else - { - qImg =QImage((const unsigned char*)(cvImg.data), - cvImg.cols,cvImg.rows, - cvImg.cols*cvImg.channels(), - QImage::Format_RGB888); - } - return qImg; -} - -// 单目相机图片加载,逻辑同双目相机图片加载 -void CameraCalibration::addImage() -{ - HiddenIntro(); - if(ui->double_camera->isChecked() || ui->double_undistort->isChecked()) - { - d = new choose_two_dir(); - d->setWindowTitle("选择图片文件夹"); - QFont font = d->font(); - font.setPixelSize(12); - d->setFont(font); - d->show(); - connect(d, SIGNAL(SendSignal(QString)), this, SLOT(receiveFromDialog(QString))); - return; - } - else if(ui->single_camera->isChecked()) - { - if(chessboard_size == 0) - { - bool ok; - chessboard_size = QInputDialog::getDouble(this,tr("角点间距"),tr("请输入角点间距(mm)"),20,0,1000,2,&ok); - if(!ok) - { - chessboard_size = 0; - return; - } - } - QStringList path_list = QFileDialog::getOpenFileNames(this, tr("选择图片"), tr("./"), tr("图片文件(*.jpg *.png *.pgm);;所有文件(*.*);")); - QProgressDialog *dialog = new QProgressDialog(tr("检测角点..."),tr("取消"),0,path_list.size(),this); - dialog->setWindowModality(Qt::WindowModal); - dialog->setMinimumDuration(0); - dialog->setWindowTitle("请稍候"); - dialog->setValue(0); - QFont font = dialog->font(); - font.setPixelSize(12); - dialog->setFont(font); - dialog->show(); - QTextCodec *code = QTextCodec::codecForName("GB2312");//解决中文路径问题 - for(int i = 0; i < path_list.size(); i++) - { - QFileInfo file = QFileInfo(path_list[i]); - QString file_name = file.fileName(); - std::string str = code->fromUnicode(path_list[i]).data(); - cv::Mat img = cv::imread(str); - find_corner_thread_img = img; - thread_done = false; - findcorner_thread->start(); - while(thread_done == false) - { - if(dialog->wasCanceled()) - { - DealThreadDone(); - delete dialog; - return; - } - QCoreApplication::processEvents(); - } - struct Chessboarder_t chess = find_corner_thread_chessboard; - if(chess.chessboard.size() != 1) - { - dialog->setValue(i+1); - continue; - } - struct Img_t img_; - img_.img = img; - img_.file_name = file_name; - std::vector img_p; - std::vector world_p; - for (unsigned int j = 0; j < chess.chessboard.size(); j++) - { - for (int u = 0; u < chess.chessboard[j].rows; u++) - { - for (int v = 0; v < chess.chessboard[j].cols; v++) - { - img_p.push_back(chess.corners.p[chess.chessboard[j].at(u, v)]); - world_p.push_back(cv::Point3f(u*chessboard_size, v*chessboard_size, 0)); - } - } - } - img_.img_points = img_p; - img_.world_points = world_p; - imgs.push_back(img_); - img_size = img.size(); - int img_height = img.rows*124/img.cols; - cv::resize(img, img, cv::Size(124, img_height)); - QPixmap pixmap = QPixmap::fromImage(Mat2QImage(img)); - QListWidgetItem* temp = new QListWidgetItem(); - temp->setSizeHint(QSize(200, img_height)); - temp->setIcon(QIcon(pixmap)); - temp->setText(file_name); - ui->listWidget->addItem(temp); - ui->listWidget->setIconSize(QSize(124, img_height)); - dialog->setValue(i+1); - } - delete dialog; - } - else if(ui->single_undistort->isChecked()) - { - QStringList path_list = QFileDialog::getOpenFileNames(this, tr("选择图片"), tr("./"), tr("图片文件(*.jpg *.png *.pgm);;所有文件(*.*);")); - QTextCodec *code = QTextCodec::codecForName("GB2312");//解决中文路径问题 - for(int i = 0; i < path_list.size(); i++) - { - QFileInfo file = QFileInfo(path_list[i]); - QString file_name = file.fileName(); - std::string str = code->fromUnicode(path_list[i]).data(); - cv::Mat img = cv::imread(str); - img_size = img.size(); - Img_t single_img; - single_img.img = img; - single_img.file_name = file_name; - imgs.push_back(single_img); - int img_height = img.rows*124/img.cols; - cv::resize(img, img, cv::Size(124, img_height)); - QPixmap pixmap = QPixmap::fromImage(Mat2QImage(img)); - QListWidgetItem* temp = new QListWidgetItem(); - temp->setSizeHint(QSize(200, img_height)); - temp->setIcon(QIcon(pixmap)); - temp->setText(file_name); - ui->listWidget->addItem(temp); - ui->listWidget->setIconSize(QSize(124, img_height)); - } - } -} - -// 删除选中的图片 -void CameraCalibration::deleteImage() -{ - std::vector delete_idx; - foreach(QModelIndex index,ui->listWidget->selectionModel()->selectedIndexes()){ - delete_idx.push_back(index.row()); - } - if(delete_idx.size() == 0) - return; - std::sort(delete_idx.begin(), delete_idx.end()); - disconnect(ui->listWidget, SIGNAL(currentItemChanged(QListWidgetItem*, QListWidgetItem*)), this, SLOT(chooseImage(QListWidgetItem*, QListWidgetItem*))); - for(int i = delete_idx.size()-1; i >= 0; i--) - { - ui->listWidget->takeItem(delete_idx[i]); - if(ui->double_camera->isChecked()) - stereo_imgs.erase(stereo_imgs.begin()+delete_idx[i]); - else - imgs.erase(imgs.begin()+delete_idx[i]); - } - connect(ui->listWidget, SIGNAL(currentItemChanged(QListWidgetItem*, QListWidgetItem*)), this, SLOT(chooseImage(QListWidgetItem*, QListWidgetItem*))); - // 如果图片全部删除,则重新显示文字引导 - if(ui->listWidget->count() == 0) - ShowIntro(); -} - -// 相机标定 -void CameraCalibration::calibrate() -{ - if(chessboard_size == 0) - { - bool ok; - chessboard_size = QInputDialog::getDouble(this,tr("角点间距"),tr("请输入角点间距(mm)"),20,0,1000,2,&ok); - if(!ok) - { - chessboard_size = 0; - return; - } - } - if(!ui->double_camera->isChecked()) - { - // 单目标定 - if(imgs.size() <= 3) - { - QMessageBox::critical(NULL, "错误", "至少需要四张图片", QMessageBox::Yes, QMessageBox::Yes); - return; - } - std::vector> img_points; - std::vector> world_points; - std::vector tvecsMat; - std::vector rvecsMat; - std::vector > imagePoints; - std::vector > objectPoints; - std::vector rvecs; - std::vector tvecs; - for (unsigned int j = 0; j < imgs.size(); j++) - { - img_points.push_back(imgs[j].img_points); - world_points.push_back(imgs[j].world_points); - std::vector img_p; - std::vector obj_p; - for(unsigned int i = 0; i < imgs[j].img_points.size(); i++) - { - img_p.push_back(imgs[j].img_points[i]); - obj_p.push_back(imgs[j].world_points[i]); - } - imagePoints.push_back(img_p); - objectPoints.push_back(obj_p); - } - cameraMatrix = cv::Mat::zeros(cv::Size(3, 3), CV_32F); - if(fisheye_flag == false) - distCoefficients = cv::Mat::zeros(cv::Size(5, 1), CV_32F); - - if(fisheye_flag == false) - { - int flag = 0; - if(!ui->tangential->isChecked()) - flag |= CV_CALIB_ZERO_TANGENT_DIST; - if(!ui->k_3->isChecked()) - flag |= CV_CALIB_FIX_K3; - cv::calibrateCamera(world_points, img_points, img_size, cameraMatrix, distCoefficients, rvecsMat, tvecsMat, flag); - } - else - { - int flag = 0; - flag |= cv::fisheye::CALIB_RECOMPUTE_EXTRINSIC; - flag |= cv::fisheye::CALIB_FIX_SKEW; - try { - cv::fisheye::calibrate(objectPoints, imagePoints, img_size, K, D, rvecs, tvecs, flag); - } catch (cv::Exception& e) { - QMessageBox::critical(NULL, "错误", "请采集更多方位的图片或者检查角点识别!", QMessageBox::Yes, QMessageBox::Yes); - imgs.clear(); - ui->listWidget->clear(); - return; - } - } - for(unsigned int i = 0; i < imgs.size(); i++) - { - if(fisheye_flag == false) - { - imgs[i].tvec = tvecsMat[i]; - imgs[i].rvec = rvecsMat[i]; - } - else - { - imgs[i].fish_tvec = tvecs[i]; - imgs[i].fish_rvec = rvecs[i]; - } - } - // 评估标定结果 - std::vector error; - for(unsigned int i = 0; i < imgs.size(); i++) - { - std::vector world_p = imgs[i].world_points; - std::vector img_p = imgs[i].img_points, reproject_img_p; - std::vector fisheye_reproject_p; - if(fisheye_flag == false) - projectPoints(world_p, rvecsMat[i], tvecsMat[i], cameraMatrix, distCoefficients, reproject_img_p); - else - cv::fisheye::projectPoints(objectPoints[i], fisheye_reproject_p, rvecs[i], tvecs[i], K, D); - float err = 0; - for (unsigned int j = 0; j < img_p.size(); j++) - { - if(fisheye_flag == false) - err += sqrt((img_p[j].x-reproject_img_p[j].x)*(img_p[j].x-reproject_img_p[j].x)+ - (img_p[j].y-reproject_img_p[j].y)*(img_p[j].y-reproject_img_p[j].y)); - else - err += sqrt((img_p[j].x-fisheye_reproject_p[j].x)*(img_p[j].x-fisheye_reproject_p[j].x)+ - (img_p[j].y-fisheye_reproject_p[j].y)*(img_p[j].y-fisheye_reproject_p[j].y)); - } - error.push_back(err/img_p.size()); - } - int max_idx = max_element(error.begin(), error.end()) - error.begin(); - float max_error = error[max_idx]; - int width = 240 / imgs.size(); - cv::Mat error_plot = cv::Mat(260, 320, CV_32FC3, cv::Scalar(255,255,255)); - cv::rectangle(error_plot, cv::Rect(40, 20, 240, 200), cv::Scalar(0, 0, 0),1, cv::LINE_8,0); - cv::putText(error_plot, "0", cv::Point(20, 220), cv::FONT_HERSHEY_SIMPLEX, 0.3, cv::Scalar(0, 0, 0), 1, 8, 0); - char *chCode; - chCode = new(std::nothrow)char[20]; - sprintf(chCode, "%.2lf", max_error*200/195); - std::string strCode(chCode); - delete []chCode; - cv::putText(error_plot, strCode, cv::Point(10, 20), cv::FONT_HERSHEY_SIMPLEX, 0.3, cv::Scalar(0, 0, 0), 1, 8, 0); - error1 = 0; - for(unsigned int i = 0; i < imgs.size(); i++) - { - error1 += error[i]; - int height = 195*error[i]/max_error; - cv::rectangle(error_plot, cv::Rect(i*width+41, 220-height, width-2, height), cv::Scalar(255,0,0), -1, cv::LINE_8,0); - cv::putText(error_plot, std::to_string(i), cv::Point(i*width+40, 240), cv::FONT_HERSHEY_SIMPLEX, 0.3, cv::Scalar(0, 0, 0), 1, 8, 0); - } - error1 /= imgs.size(); - cv::imshow("error", error_plot); - } - else - { - // 双目标定 - if(stereo_imgs.size() <= 3) - { - QMessageBox::critical(NULL, "错误", "至少需要四组图片", QMessageBox::Yes, QMessageBox::Yes); - return; - } - std::vector> left_img_points; - std::vector> right_img_points; - std::vector> world_points; - std::vector left_tvecsMat; - std::vector left_rvecsMat; - std::vector right_tvecsMat; - std::vector right_rvecsMat; - std::vector > left_imagePoints; - std::vector > right_imagePoints; - std::vector > objectPoints; - std::vector left_rvecs; - std::vector left_tvecs; - std::vector right_rvecs; - std::vector right_tvecs; - for (unsigned int j = 0; j < stereo_imgs.size(); j++) - { - left_img_points.push_back(stereo_imgs[j].left_img_points); - right_img_points.push_back(stereo_imgs[j].right_img_points); - world_points.push_back(stereo_imgs[j].world_points); - std::vector img_p, img_p_; - std::vector obj_p; - for(unsigned int i = 0; i < stereo_imgs[j].left_img_points.size(); i++) - { - img_p.push_back(stereo_imgs[j].left_img_points[i]); - img_p_.push_back(stereo_imgs[j].right_img_points[i]); - obj_p.push_back(stereo_imgs[j].world_points[i]); - } - left_imagePoints.push_back(img_p); - right_imagePoints.push_back(img_p_); - objectPoints.push_back(obj_p); - } - cameraMatrix = cv::Mat::zeros(cv::Size(3, 3), CV_32F); - cameraMatrix2 = cv::Mat::zeros(cv::Size(3, 3), CV_32F); - if(fisheye_flag == false) - { - distCoefficients = cv::Mat::zeros(cv::Size(5, 1), CV_32F); - distCoefficients2 = cv::Mat::zeros(cv::Size(5, 1), CV_32F); - } - // 开始标定 - if(fisheye_flag == false) - { - int flag = 0; - if(!ui->tangential->isChecked()) - flag |= CV_CALIB_ZERO_TANGENT_DIST; - if(!ui->k_3->isChecked()) - flag |= CV_CALIB_FIX_K3; - cv::calibrateCamera(world_points, left_img_points, img_size, cameraMatrix, distCoefficients, left_rvecsMat, left_tvecsMat, flag); - cv::calibrateCamera(world_points, right_img_points, img_size, cameraMatrix2, distCoefficients2, right_rvecsMat, right_tvecsMat, flag); - } - else - { - int flag = 0; - flag |= cv::fisheye::CALIB_RECOMPUTE_EXTRINSIC; - flag |= cv::fisheye::CALIB_FIX_SKEW; - cv::fisheye::calibrate(objectPoints, left_imagePoints, img_size, K, D, left_rvecs, left_tvecs, flag); - cv::fisheye::calibrate(objectPoints, right_imagePoints, img_size, K2, D2, right_rvecs, right_tvecs, flag); - } - for(unsigned int i = 0; i < stereo_imgs.size(); i++) - { - if(fisheye_flag == false) - { - stereo_imgs[i].left_tvec = left_tvecsMat[i]; - stereo_imgs[i].left_rvec = left_rvecsMat[i]; - stereo_imgs[i].right_tvec = right_tvecsMat[i]; - stereo_imgs[i].right_rvec = right_rvecsMat[i]; - } - else - { - stereo_imgs[i].left_fish_tvec = left_tvecs[i]; - stereo_imgs[i].left_fish_rvec = left_rvecs[i]; - stereo_imgs[i].right_fish_tvec = right_tvecs[i]; - stereo_imgs[i].right_fish_rvec = right_rvecs[i]; - } - } - // 评估标定结果 - std::vector left_error, right_error; - for(unsigned int i = 0; i < stereo_imgs.size(); i++) - { - std::vector world_p = stereo_imgs[i].world_points; - std::vector left_img_p = stereo_imgs[i].left_img_points, right_img_p = stereo_imgs[i].right_img_points; - std::vector left_reproject_img_p, right_reproject_img_p; - std::vector left_fisheye_reproject_p, right_fisheye_reproject_p; - if(fisheye_flag == false) - { - projectPoints(world_p, left_rvecsMat[i], left_tvecsMat[i], cameraMatrix, distCoefficients, left_reproject_img_p); - projectPoints(world_p, right_rvecsMat[i], right_tvecsMat[i], cameraMatrix2, distCoefficients2, right_reproject_img_p); - } - else - { - cv::fisheye::projectPoints(objectPoints[i], left_fisheye_reproject_p, left_rvecs[i], left_tvecs[i], K, D); - cv::fisheye::projectPoints(objectPoints[i], right_fisheye_reproject_p, right_rvecs[i], right_tvecs[i], K2, D2); - } - float left_err = 0, right_err = 0; - for (unsigned int j = 0; j < world_p.size(); j++) - { - if(fisheye_flag == false) - { - left_err += sqrt((left_img_p[j].x-left_reproject_img_p[j].x)*(left_img_p[j].x-left_reproject_img_p[j].x)+ - (left_img_p[j].y-left_reproject_img_p[j].y)*(left_img_p[j].y-left_reproject_img_p[j].y)); - right_err += sqrt((right_img_p[j].x-right_reproject_img_p[j].x)*(right_img_p[j].x-right_reproject_img_p[j].x)+ - (right_img_p[j].y-right_reproject_img_p[j].y)*(right_img_p[j].y-right_reproject_img_p[j].y)); - } - else - { - left_err += sqrt((left_img_p[j].x-left_fisheye_reproject_p[j].x)*(left_img_p[j].x-left_fisheye_reproject_p[j].x)+ - (left_img_p[j].y-left_fisheye_reproject_p[j].y)*(left_img_p[j].y-left_fisheye_reproject_p[j].y)); - right_err += sqrt((right_img_p[j].x-right_fisheye_reproject_p[j].x)*(right_img_p[j].x-right_fisheye_reproject_p[j].x)+ - (right_img_p[j].y-right_fisheye_reproject_p[j].y)*(right_img_p[j].y-right_fisheye_reproject_p[j].y)); - } - } - left_error.push_back(left_err/world_p.size()); - right_error.push_back(right_err/world_p.size()); - } - int max_idx = max_element(left_error.begin(), left_error.end()) - left_error.begin(); - float max_error = left_error[max_idx]; - max_idx = max_element(right_error.begin(), right_error.end()) - right_error.begin(); - max_error = std::max(max_error, right_error[max_idx]); - int width = 480 / stereo_imgs.size(); - cv::Mat error_plot = cv::Mat(260, 560, CV_32FC3, cv::Scalar(255,255,255)); - cv::rectangle(error_plot, cv::Rect(40, 20, 480, 200), cv::Scalar(0, 0, 0),1, cv::LINE_8,0); - cv::putText(error_plot, "0", cv::Point(20, 220), cv::FONT_HERSHEY_SIMPLEX, 0.3, cv::Scalar(0, 0, 0), 1, 8, 0); - char *chCode; - chCode = new(std::nothrow)char[20]; - sprintf(chCode, "%.2lf", max_error*200/195); - std::string strCode(chCode); - delete []chCode; - cv::putText(error_plot, strCode, cv::Point(10, 20), cv::FONT_HERSHEY_SIMPLEX, 0.3, cv::Scalar(0, 0, 0), 1, 8, 0); - error1 = 0; error2 = 0; - for(unsigned int i = 0; i < stereo_imgs.size(); i++) - { - error1 += left_error[i]; - error2 += right_error[i]; - int height = 195*left_error[i]/max_error; - cv::rectangle(error_plot, cv::Rect(i*width+43, 220-height, width/2-4, height), cv::Scalar(255,0,0), -1, cv::LINE_8,0); - height = 195*right_error[i]/max_error; - cv::rectangle(error_plot, cv::Rect(i*width+41+width/2, 220-height, width/2-4, height), cv::Scalar(0,255,0), -1, cv::LINE_8,0); - cv::putText(error_plot, std::to_string(i), cv::Point(i*width+40+width/2-2, 240), cv::FONT_HERSHEY_SIMPLEX, 0.3, cv::Scalar(0, 0, 0), 1, 8, 0); - } - error1 /= stereo_imgs.size(); - error2 /= stereo_imgs.size(); - - if(fisheye_flag == false) - { - int flag = 0; - if(!ui->tangential->isChecked()) - flag |= CV_CALIB_ZERO_TANGENT_DIST; - if(!ui->k_3->isChecked()) - flag |= CV_CALIB_FIX_K3; - flag |= CV_CALIB_USE_INTRINSIC_GUESS; - cv::stereoCalibrate(world_points, left_img_points, right_img_points, - cameraMatrix, distCoefficients, - cameraMatrix2, distCoefficients2, - img_size, R, T, cv::noArray(), cv::noArray(), flag); - cv::stereoRectify(cameraMatrix, distCoefficients, - cameraMatrix2, distCoefficients2, - img_size, R, T, - R1, R2, P1, P2, Q,0,0, img_size); - } - else - { - int flag = 0; - flag |= cv::fisheye::CALIB_FIX_SKEW; - flag |= cv::fisheye::CALIB_USE_INTRINSIC_GUESS; - try { - cv::fisheye::stereoCalibrate(objectPoints, left_imagePoints, right_imagePoints, - K, D, - K2, D2, - img_size, R, T, flag); - } catch (cv::Exception& e) { - QMessageBox::critical(NULL, "错误", "请采集更多方位的图片!", QMessageBox::Yes, QMessageBox::Yes); - stereo_imgs.clear(); - ui->listWidget->clear(); - return; - } - - cv::fisheye::stereoRectify(K, D, - K2, D2, - img_size, R, T, - R1, R2, P1, P2, Q, 0, img_size); - } - cv::imshow("error", error_plot); - } - calibrate_flag = true; -} - -// 导出标定参数 -void CameraCalibration::exportParam() -{ - if(calibrate_flag == false) - { - QMessageBox::critical(NULL, "错误", "请先标定相机", QMessageBox::Yes); - return; - } - QString strSaveName = QFileDialog::getSaveFileName(this,tr("保存的文件"),tr("calibration_param.yaml"),tr("yaml files(*.yaml)")); - if(strSaveName.isNull() || strSaveName.isEmpty() || strSaveName.length() == 0) - return; - cv::FileStorage fs_write(strSaveName.toStdString(), cv::FileStorage::WRITE); - if(ui->double_camera->isChecked()) - { - if(fisheye_flag == false) - { - fs_write << "left_camera_Matrix" << cameraMatrix << "left_camera_distCoeffs" << distCoefficients; - fs_write << "right_camera_Matrix" << cameraMatrix2 << "right_camera_distCoeffs" << distCoefficients2; - fs_write << "Rotate_Matrix" << R << "Translate_Matrix" << T; - fs_write << "R1" << R1 << "R2" << R2 << "P1" << P1 << "P2" << P2 << "Q" << Q; - } - else - { - cv::Mat camera_matrix = cv::Mat::zeros(cv::Size(3,3), CV_64F); - camera_matrix.at(0,0) = K(0,0); - camera_matrix.at(0,2) = K(0,2); - camera_matrix.at(1,1) = K(1,1); - camera_matrix.at(1,2) = K(1,2); - camera_matrix.at(2,2) = 1; - cv::Mat Dist = (cv::Mat_(1,4) << D[0], D[1], D[2], D[3]); - fs_write << "left_camera_Matrix" << camera_matrix << "left_camera_distCoeffs" << Dist; - camera_matrix.at(0,0) = K2(0,0); - camera_matrix.at(0,2) = K2(0,2); - camera_matrix.at(1,1) = K2(1,1); - camera_matrix.at(1,2) = K2(1,2); - Dist = (cv::Mat_(1,4) << D2[0], D2[1], D2[2], D2[3]); - fs_write << "right_camera_Matrix" << camera_matrix << "right_camera_distCoeffs" << Dist; - fs_write << "Rotate_Matrix" << R << "Translate_Matrix" << T; - fs_write << "R1" << R1 << "R2" << R2 << "P1" << P1 << "P2" << P2 << "Q" << Q; - } - fs_write << "left_average_reprojection_err" << error1 << "right_average_reprojection_err" << error2; - } - else - { - if(fisheye_flag == false) - fs_write << "cameraMatrix" << cameraMatrix << "distCoeffs" << distCoefficients; - else - { - cv::Mat camera_matrix = cv::Mat::zeros(cv::Size(3,3), CV_64F); - camera_matrix.at(0,0) = K(0,0); - camera_matrix.at(0,2) = K(0,2); - camera_matrix.at(1,1) = K(1,1); - camera_matrix.at(1,2) = K(1,2); - camera_matrix.at(2,2) = 1; - cv::Mat Dist = (cv::Mat_(1,4) << D[0], D[1], D[2], D[3]); - fs_write << "cameraMatrix" << camera_matrix << "distCoeffs" << Dist; - } - fs_write << "average_reprojection_err" << error1; - } - fs_write.release(); -} diff --git a/camera_calibration/CameraCalibration.h b/camera_calibration/CameraCalibration.h deleted file mode 100644 index b94a3f0..0000000 --- a/camera_calibration/CameraCalibration.h +++ /dev/null @@ -1,131 +0,0 @@ -#ifndef CameraCalibration_H -#define CameraCalibration_H - -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include "findCorner.h" -#include "chessboard.h" -#include "choose_two_dir.h" -#ifdef Q_OS_LINUX -#include "single_capture_linux.h" -#elif defined(Q_OS_WIN32) -#include "single_capture.h" -#endif - -#ifdef Q_OS_LINUX -#include "double_capture_linux.h" -#elif defined(Q_OS_WIN32) -#include "double_capture.h" -#endif -#include "choose_yaml.h" - -QT_BEGIN_NAMESPACE -namespace Ui { class CameraCalibration; } -QT_END_NAMESPACE - -struct Img_t -{ - cv::Mat img; - QString file_name; - std::vector img_points; - std::vector world_points; - cv::Mat tvec; - cv::Mat rvec; - cv::Vec3d fish_tvec; - cv::Vec3d fish_rvec; -}; - -struct Stereo_Img_t -{ - cv::Mat left_img; - QString left_file_name; - std::vector left_img_points; - cv::Mat left_tvec; - cv::Mat left_rvec; - cv::Vec3d left_fish_tvec; - cv::Vec3d left_fish_rvec; - - cv::Mat right_img; - QString right_file_name; - std::vector right_img_points; - cv::Mat right_tvec; - cv::Mat right_rvec; - cv::Vec3d right_fish_tvec; - cv::Vec3d right_fish_rvec; - - std::vector world_points; -}; - -extern cv::Mat find_corner_thread_img; -extern struct Chessboarder_t find_corner_thread_chessboard; - -class CameraCalibration : public QMainWindow -{ - Q_OBJECT - -public: - CameraCalibration(QWidget *parent = nullptr); - ~CameraCalibration(); - -private slots: - void addImage(); - void deleteImage(); - void calibrate(); - void fisheyeModeSwitch(int state); - void exportParam(); - void distortModeSwitch(); - void receiveFromDialog(QString str); - void receiveYamlPath(QString str); - void chooseImage(QListWidgetItem* item, QListWidgetItem*); - void reset(); - void undistortReset(); - void saveUndistort(); - void OpenCamera(); - void DealThreadDone(); - -private: - Ui::CameraCalibration *ui; - choose_two_dir *d; - choose_yaml *chooseYaml; -#ifdef Q_OS_LINUX - single_capture_linux *single_c; - double_capture_linux *double_c; -#elif defined(Q_OS_WIN32) - single_capture *single_c; - double_capture *double_c; -#endif - QImage Mat2QImage(cv::Mat cvImg); - void ShowIntro(); - void HiddenIntro(); - QAction *saveImage; - std::vector imgs; - std::vector stereo_imgs; - double chessboard_size; - cv::Mat cameraMatrix; - cv::Mat distCoefficients; - cv::Matx33d K; - cv::Vec4d D; - cv::Mat cameraMatrix2; - cv::Mat distCoefficients2; - double error1, error2; - cv::Matx33d K2; - cv::Vec4d D2; - cv::Size img_size; - bool fisheye_flag = false; - bool calibrate_flag = false; - bool distort_flag = false; - cv::Mat R, T, R1, R2, P1, P2, Q; - FindcornerThread *findcorner_thread; - bool thread_done = false; -}; -#endif // CameraCalibration_H diff --git a/camera_calibration/README.md b/camera_calibration/README.md deleted file mode 100644 index e2ff0de..0000000 --- a/camera_calibration/README.md +++ /dev/null @@ -1,30 +0,0 @@ -# camera_calibration -传统相机和鱼眼相机标定程序 - -基于Qt和OpenCV开发,角点检测部分为MATLAB的角点检测算法的C++实现,并优化实现了多棋盘检测和大畸变鱼眼镜头下棋盘的检测。 - -## 功能列表: -* 角点检测,支持多棋盘检测(代码逻辑见[基于生长的棋盘格角点检测](https://github.com/imuncle/imuncle.github.io/issues/113)) -* 单目传统相机标定,采用张正友标定法,支持模式选择,用户可选择径向畸变参数的个数和是否标定切向畸变 -* 单目鱼眼相机标定 -* 双目传统/鱼眼相机标定,无需预先单独标定单个相机 -* 支持打开摄像头采集图片 -* 支持畸变矫正预览和重投影预览 -* 支持导出畸变矫正后的图片 - -## 文件结构 -|文件名/文件夹名|功能| -|:--|:--| -|CameraCalibration.cpp|程序主界面和主要逻辑的实现| -|findCorner.cpp|棋盘角点检测算法的实现| -|chessboard.cpp|根据检测到的角点进行棋盘生长| -|sigle_capture.cpp|Windows上单个相机采集图像的实现| -|double_capture.cpp|Windows上双目相机采集图片的实现| -|sigle_capture_linux.cpp|Linux上单个相机采集图像的实现| -|double_capture_linux.cpp|Linux上双目相机采集图片的实现| -|choose_two_dir.cpp|立体标定图像文件夹选择对话框| -|res|图标资源文件夹| - -## 软件运行截图: - -![screenshot](screenshot.jpg) \ No newline at end of file diff --git a/camera_calibration/double_capture.cpp b/camera_calibration/double_capture.cpp deleted file mode 100644 index 21c31be..0000000 --- a/camera_calibration/double_capture.cpp +++ /dev/null @@ -1,219 +0,0 @@ -#include "double_capture.h" -#include "ui_double_capture.h" -#include - -double_capture::double_capture(QWidget *parent) : - QMainWindow(parent), - ui(new Ui::double_capture) -{ - ui->setupUi(this); - ui->capture->setFlat(true); - connect(ui->capture, SIGNAL(clicked()), this, SLOT(capture())); - last_left.deviceName() = QString(""); - last_right.deviceName() = QString(""); - camera_list = QCameraInfo::availableCameras(); - if(camera_list.length() > 1) - { - Camera_left = new QCamera(camera_list[0]); - Camera_right = new QCamera(camera_list[1]); - } - else - { - QString str(""); - Camera_left = new QCamera(QCameraInfo(str.toUtf8())); - Camera_right = new QCamera(QCameraInfo(str.toUtf8())); - QMessageBox::warning(this, "警告", "摄像头少于两个,请插上摄像头后再打开该窗口", QMessageBox::Yes); - } - for(int i = 0; i < camera_list.length(); i++) { - QAction *camera = ui->menu_1->addAction(camera_list[i].description()); - camera->setCheckable(true); - connect(camera, &QAction::triggered,[=](){ - if(last_left.deviceName() != camera_list[i].deviceName()) - { - open_left(camera_list[i]); - } - else - { - close_left(); - } - }); - QAction *camera_1 = ui->menu_2->addAction(camera_list[i].description()); - camera_1->setCheckable(true); - connect(camera_1, &QAction::triggered,[=](){ - if(last_right.deviceName() != camera_list[i].deviceName()) - { - open_right(camera_list[i]); - } - else - { - close_right(); - } - }); - } - CameraViewFinder_left = new QCameraViewfinder(ui->left_img); - CameraViewFinder_left->resize(ui->left_img->width(), ui->left_img->height()); - CameraViewFinder_right = new QCameraViewfinder(ui->right_img); - CameraViewFinder_right->resize(ui->right_img->width(), ui->right_img->height()); -} - -double_capture::~double_capture() -{ - delete ui; - Camera_left->stop(); - Camera_right->stop(); -} - -void double_capture::close_left() -{ - last_left = QCameraInfo(QString("").toUtf8()); - Camera_left->stop(); - left_open = false; -} - -void double_capture::open_left(QCameraInfo info) -{ - QList actions = ui->menu_1->actions(); - if(info.deviceName() == last_right.deviceName()) - { - QMessageBox::warning(NULL, "警告", "摄像头"+info.description()+"已经打开!", QMessageBox::Yes); - close_left(); - for(int i = 0; i < actions.length(); i++) - { - actions[i]->setChecked(false); - } - return; - } - for(int i = 0; i < actions.length(); i++) - { - if(camera_list[i].deviceName() == info.deviceName()) - { - actions[i]->setChecked(true); - } - else - actions[i]->setChecked(false); - } - Camera_left->stop(); - Camera_left = new QCamera(info); - Camera_left->setViewfinder(CameraViewFinder_left); - //显示摄像头取景器 - CameraViewFinder_left->show(); - //开启摄像头 - Camera_left->start(); - if(Camera_left->status() != QCamera::ActiveStatus) - { - QMessageBox::warning(this, "警告", "摄像头打开失败!", QMessageBox::Yes); - close_left(); - for(int i = 0; i < actions.length(); i++) - { - actions[i]->setChecked(false); - } - return; - } - //创建获取一帧数据对象 - CameraImageCapture_left = new QCameraImageCapture(Camera_left); - //关联图像获取信号 - connect(CameraImageCapture_left, &QCameraImageCapture::imageCaptured, this, &double_capture::left_take_photo); - left_open = true; - last_left = info; -} - -void double_capture::close_right() -{ - Camera_right->stop(); - left_open = false; - last_right = QCameraInfo(QString("").toUtf8()); -} - -void double_capture::open_right(QCameraInfo info) -{ - QList actions = ui->menu_2->actions(); - if(info.deviceName() == last_left.deviceName()) - { - QMessageBox::warning(NULL, "警告", "摄像头"+info.description()+"已经打开!", QMessageBox::Yes); - close_right(); - for(int i = 0; i < actions.length(); i++) - { - actions[i]->setChecked(false); - } - return; - } - for(int i = 0; i < actions.length(); i++) - { - if(camera_list[i].deviceName() == info.deviceName()) - { - actions[i]->setChecked(true); - } - else - actions[i]->setChecked(false); - } - Camera_right->stop(); - Camera_right = new QCamera(info); - Camera_right->setViewfinder(CameraViewFinder_right); - //显示摄像头取景器 - CameraViewFinder_right->show(); - //开启摄像头 - Camera_right->start(); - if(Camera_right->status() != QCamera::ActiveStatus) - { - QMessageBox::warning(this, "警告", "摄像头打开失败!", QMessageBox::Yes); - close_right(); - for(int i = 0; i < actions.length(); i++) - { - actions[i]->setChecked(false); - } - return; - } - //创建获取一帧数据对象 - CameraImageCapture_right = new QCameraImageCapture(Camera_right); - //关联图像获取信号 - connect(CameraImageCapture_right, &QCameraImageCapture::imageCaptured, this, &double_capture::right_take_photo); - right_open = true; - last_right = info; -} - -void double_capture::capture() -{ - if(left_open == false || right_open == false) - { - QMessageBox::critical(this, "错误", "两个相机都打开后才能拍照!", QMessageBox::Yes); - return; - } - if(save_path == "") - { - save_path = QFileDialog::getExistingDirectory(nullptr, "选择保存路径", "./"); - QDir dir; - dir.mkdir(save_path+"/left/"); - dir.mkdir(save_path+"/right/"); - return; - } - CameraImageCapture_left->capture(); - CameraImageCapture_right->capture(); -} - -void double_capture::left_take_photo(int, const QImage &image) -{ - //使用系统时间来命名图片的名称,时间是唯一的,图片名也是唯一的 - QDateTime dateTime(QDateTime::currentDateTime()); - QString time = dateTime.toString("yyyy-MM-dd-hh-mm-ss"); - //创建图片保存路径名 - QString filename = save_path + "/left/" + QString("./%1.jpg").arg(time); - //保存一帧数据 - image.save(filename); -} - -void double_capture::right_take_photo(int, const QImage &image) -{ - //使用系统时间来命名图片的名称,时间是唯一的,图片名也是唯一的 - QDateTime dateTime(QDateTime::currentDateTime()); - QString time = dateTime.toString("yyyy-MM-dd-hh-mm-ss"); - //创建图片保存路径名 - QString filename = save_path + "/right/" + QString("./%1.jpg").arg(time); - //保存一帧数据 - image.save(filename); -} - -void double_capture::closeEvent ( QCloseEvent *) -{ - Camera_left->stop(); - Camera_right->stop(); -} diff --git a/camera_calibration/double_capture.h b/camera_calibration/double_capture.h deleted file mode 100644 index 516f5d3..0000000 --- a/camera_calibration/double_capture.h +++ /dev/null @@ -1,51 +0,0 @@ -#ifndef DOUBLE_CAPTURE_H -#define DOUBLE_CAPTURE_H - -#include -#include -#include -#include -#include -#include - -namespace Ui { -class double_capture; -} - -class double_capture : public QMainWindow -{ - Q_OBJECT - -public: - explicit double_capture(QWidget *parent = nullptr); - ~double_capture(); - -private slots: - void capture(); - void left_take_photo(int id, const QImage &image); - void right_take_photo(int id, const QImage &image); - -private: - Ui::double_capture *ui; - //摄像头对象指针 - QCamera* Camera_left; - QCamera* Camera_right; - QCameraInfo last_left; - QCameraInfo last_right; - //摄像头的取景器 - QCameraViewfinder* CameraViewFinder_left; - QCameraViewfinder* CameraViewFinder_right; - QList camera_list; - void open_left(QCameraInfo info); - void close_left(); - void open_right(QCameraInfo info); - void close_right(); - void closeEvent(QCloseEvent *event); - //记录摄像头内容 - QCameraImageCapture* CameraImageCapture_left; - QCameraImageCapture* CameraImageCapture_right; - QString save_path; - bool left_open = false, right_open = false; -}; - -#endif // DOUBLE_CAPTURE_H diff --git a/camera_calibration/double_capture.ui b/camera_calibration/double_capture.ui deleted file mode 100644 index 76e08b8..0000000 --- a/camera_calibration/double_capture.ui +++ /dev/null @@ -1,107 +0,0 @@ - - - double_capture - - - - 0 - 0 - 1000 - 480 - - - - MainWindow - - - - :/icon/camera.png:/icon/camera.png - - - - - - 0 - 0 - 500 - 480 - - - - - - - Qt::AlignCenter - - - - - - 500 - 0 - 500 - 480 - - - - - - - Qt::AlignCenter - - - - - - 470 - 350 - 60 - 60 - - - - PointingHandCursor - - - - - - - :/icon/capture.png:/icon/capture.png - - - - 48 - 48 - - - - - - - - 0 - 0 - 1000 - 22 - - - - - 相机1 - - - - - 相机2 - - - - - - - - - - - diff --git a/camera_calibration/double_capture_linux.cpp b/camera_calibration/double_capture_linux.cpp deleted file mode 100644 index 8a7a3c8..0000000 --- a/camera_calibration/double_capture_linux.cpp +++ /dev/null @@ -1,339 +0,0 @@ -#include "double_capture_linux.h" -#include "ui_double_capture.h" -#ifdef Q_OS_LINUX - -V4L2 v4l2_left; -V4L2 v4l2_right; - -double_capture_linux::double_capture_linux(QWidget *parent) : - QMainWindow(parent), - ui(new Ui::double_capture) -{ - ui->setupUi(this); - ui->capture->setFlat(true); - connect(ui->capture, SIGNAL(clicked()), this, SLOT(capture())); - capture_thread_l = new LeftCaptureThread(); - connect(capture_thread_l, SIGNAL(CaptureDone(QImage, int)), this, SLOT(DealLeftCaptureDone(QImage, int))); - capture_thread_r = new RightCaptureThread(); - connect(capture_thread_r, SIGNAL(CaptureDone(QImage, int)), this, SLOT(DealRightCaptureDone(QImage, int))); - err11_l = err19_l = err11_r = err19_r = 0; - int camcount = v4l2_left.GetDeviceCount(); - for(int i = 0; i < camcount; i++) - { - QAction *camera = ui->menu_1->addAction(v4l2_left.GetCameraName(i)); - camera->setCheckable(true); - connect(camera, &QAction::triggered,[=](){ - if(last_left_id != i) - { - open_left(i); - } - else - { - close_left(); - } - }); - QAction *camera_1 = ui->menu_2->addAction(v4l2_left.GetCameraName(i)); - camera_1->setCheckable(true); - connect(camera_1, &QAction::triggered,[=](){ - if(last_right_id != i) - { - open_right(i); - } - else - { - close_right(); - } - }); - } -} - -double_capture_linux::~double_capture_linux() -{ - delete ui; -} - -void double_capture_linux::open_left(int id) -{ - QList actions = ui->menu_1->actions(); - if(id == last_right_id) - { - QMessageBox::warning(NULL, "警告", "摄像头"+actions[id]->text()+"已经打开!", QMessageBox::Yes); - close_left(); - for(int i = 0; i < actions.length(); i++) - { - actions[i]->setChecked(false); - } - return; - } - close_left(); - last_left_id = id; - - for(int i = 0; i < actions.length(); i++) - { - if(id == i) - { - actions[i]->setChecked(true); - } - else - actions[i]->setChecked(false); - } - v4l2_left.StartRun(id); - capture_thread_l->init(id); - capture_thread_l->start(); - open_left_flag = true; -} - -void double_capture_linux::close_left() -{ - last_left_id = -1; - capture_thread_l->stop(); - capture_thread_l->quit(); - capture_thread_l->wait(); - v4l2_left.StopRun(); - open_left_flag = false; -} - -void double_capture_linux::open_right(int id) -{ - QList actions = ui->menu_2->actions(); - if(id == last_left_id) - { - QMessageBox::warning(NULL, "警告", "摄像头"+actions[id]->text()+"已经打开!", QMessageBox::Yes); - close_right(); - for(int i = 0; i < actions.length(); i++) - { - actions[i]->setChecked(false); - } - return; - } - close_right(); - last_right_id = id; - - for(int i = 0; i < actions.length(); i++) - { - if(id == i) - { - actions[i]->setChecked(true); - } - else - actions[i]->setChecked(false); - } - v4l2_right.StartRun(id); - capture_thread_r->init(id); - capture_thread_r->start(); - open_right_flag = true; -} - -void double_capture_linux::close_right() -{ - last_right_id = -1; - capture_thread_r->stop(); - capture_thread_r->quit(); - capture_thread_r->wait(); - v4l2_right.StopRun(); - open_right_flag = false; -} - -void double_capture_linux::DealLeftCaptureDone(QImage image, int result) -{ - //超时后关闭视频 - //超时代表着VIDIOC_DQBUF会阻塞,直接关闭视频即可 - if(result == -1) - { - close_left(); - - ui->left_img->clear(); - ui->left_img->setText("获取设备图像超时!"); - } - - if(!image.isNull()) - { - q_left_image = image; - ui->left_img->clear(); - switch(result) - { - case 0: //Success - err11_l = err19_l = 0; - if(image.isNull()) - ui->left_img->setText("画面丢失!"); - else - ui->left_img->setPixmap(QPixmap::fromImage(image.scaled(ui->left_img->size()))); - - break; - case 11: //Resource temporarily unavailable - err11_l++; - if(err11_l == 10) - { - ui->left_img->clear(); - ui->left_img->setText("设备已打开,但获取视频失败!\n请尝试切换USB端口后断开重试!"); - } - break; - case 19: //No such device - err19_l++; - if(err19_l == 10) - { - ui->left_img->clear(); - ui->left_img->setText("设备丢失!"); - } - break; - } - } -} - -void double_capture_linux::DealRightCaptureDone(QImage image, int result) -{ - //超时后关闭视频 - //超时代表着VIDIOC_DQBUF会阻塞,直接关闭视频即可 - if(result == -1) - { - close_right(); - - ui->right_img->clear(); - ui->right_img->setText("获取设备图像超时!"); - } - - if(!image.isNull()) - { - q_right_image = image; - ui->right_img->clear(); - switch(result) - { - case 0: //Success - err11_r = err19_r = 0; - if(image.isNull()) - ui->right_img->setText("画面丢失!"); - else - ui->right_img->setPixmap(QPixmap::fromImage(image.scaled(ui->right_img->size()))); - - break; - case 11: //Resource temporarily unavailable - err11_r++; - if(err11_r == 10) - { - ui->right_img->clear(); - ui->right_img->setText("设备已打开,但获取视频失败!\n请尝试切换USB端口后断开重试!"); - } - break; - case 19: //No such device - err19_r++; - if(err19_r == 10) - { - ui->right_img->clear(); - ui->right_img->setText("设备丢失!"); - } - break; - } - } -} - -void double_capture_linux::capture() -{ - if(open_left_flag == false || open_right_flag == false) - { - QMessageBox::critical(this, "错误", "两个相机都打开后才能拍照!", QMessageBox::Yes); - return; - } - if(save_path == "") - { - save_path = QFileDialog::getExistingDirectory(nullptr, "选择保存路径", "./"); - QDir dir; - dir.mkdir(save_path+"/left/"); - dir.mkdir(save_path+"/right/"); - return; - } - //使用系统时间来命名图片的名称,时间是唯一的,图片名也是唯一的 - QDateTime dateTime(QDateTime::currentDateTime()); - QString time = dateTime.toString("yyyy-MM-dd-hh-mm-ss"); - //创建图片保存路径名 - QString filename = save_path + "/left/" + QString("%1.jpg").arg(time); - //保存一帧数据 - q_left_image.save(filename); - filename = save_path + "/right/" + QString("%1.jpg").arg(time); - q_right_image.save(filename); -} - -void double_capture_linux::closeEvent ( QCloseEvent *) -{ - close_left(); - close_right(); -} - -LeftCaptureThread::LeftCaptureThread() -{ - stopped = false; - majorindex = -1; -} - -void LeftCaptureThread::stop() -{ - stopped = true; -} - -void LeftCaptureThread::init(int index) -{ - stopped = false; - majorindex = index; -} - -void LeftCaptureThread::run() -{ - if(majorindex != -1) - { - while(!stopped) - { - msleep(1000/30); - - QImage img; - int ret = v4l2_left.GetFrame(); - if(ret == 0) - { - int WV = v4l2_left.GetCurResWidth(); - int HV = v4l2_left.GetCurResHeight(); - img = QImage(v4l2_left.rgb24, WV, HV, QImage::Format_RGB888); - } - - emit CaptureDone(img, ret); - } - } -} - -RightCaptureThread::RightCaptureThread() -{ - stopped = false; - majorindex = -1; -} - -void RightCaptureThread::stop() -{ - stopped = true; -} - -void RightCaptureThread::init(int index) -{ - stopped = false; - majorindex = index; -} - -void RightCaptureThread::run() -{ - if(majorindex != -1) - { - while(!stopped) - { - msleep(1000/30); - - QImage img; - int ret = v4l2_right.GetFrame(); - if(ret == 0) - { - int WV = v4l2_right.GetCurResWidth(); - int HV = v4l2_right.GetCurResHeight(); - img = QImage(v4l2_right.rgb24, WV, HV, QImage::Format_RGB888); - } - - emit CaptureDone(img, ret); - } - } -} - -#endif diff --git a/camera_calibration/double_capture_linux.h b/camera_calibration/double_capture_linux.h deleted file mode 100644 index 4dad5d5..0000000 --- a/camera_calibration/double_capture_linux.h +++ /dev/null @@ -1,97 +0,0 @@ -#ifndef DOUBLE_CAPTURE_LINUX_H -#define DOUBLE_CAPTURE_LINUX_H - -#include - -#ifdef Q_OS_LINUX -#include "v4l2.hpp" -#include -#include -#include -#include - -class LeftCaptureThread : public QThread -{ - Q_OBJECT -public: - LeftCaptureThread(); - - QImage majorImage; - void stop(); - void init(int index); - -protected: - void run(); - -private: - volatile int majorindex; - volatile bool stopped; - -signals: - void CaptureDone(QImage image, int result); -}; - -class RightCaptureThread : public QThread -{ - Q_OBJECT -public: - RightCaptureThread(); - - QImage majorImage; - void stop(); - void init(int index); - -protected: - void run(); - -private: - volatile int majorindex; - volatile bool stopped; - -signals: - void CaptureDone(QImage image, int result); -}; - -extern V4L2 v4l2_left; -extern V4L2 v4l2_right; - -namespace Ui { -class double_capture; -} - -class double_capture_linux : public QMainWindow -{ - Q_OBJECT - -public: - explicit double_capture_linux(QWidget *parent = nullptr); - ~double_capture_linux(); - -private slots: - void capture(); - void DealLeftCaptureDone(QImage image, int result); - void DealRightCaptureDone(QImage image, int result); - -private: - Ui::double_capture *ui; - LeftCaptureThread *capture_thread_l; - RightCaptureThread *capture_thread_r; - int err11_l, err19_l; - int err11_r, err19_r; - void open_left(int id); - void close_left(); - void open_right(int id); - void close_right(); - void closeEvent(QCloseEvent *event); - QString save_path; - bool open_left_flag = false; - int last_left_id; - QImage q_left_image; - bool open_right_flag = false; - int last_right_id; - QImage q_right_image; -}; - -#endif - -#endif // DOUBLE_CAPTURE_LINUX_H diff --git a/camera_calibration/screenshot.jpg b/camera_calibration/screenshot.jpg deleted file mode 100644 index 04f3cbf..0000000 Binary files a/camera_calibration/screenshot.jpg and /dev/null differ diff --git a/camera_calibration/single_capture.cpp b/camera_calibration/single_capture.cpp deleted file mode 100644 index b30c3a2..0000000 --- a/camera_calibration/single_capture.cpp +++ /dev/null @@ -1,122 +0,0 @@ -#include "single_capture.h" -#include "ui_single_capture.h" -#include -#include - -single_capture::single_capture(QWidget *parent) : - QMainWindow(parent), - ui(new Ui::single_capture) -{ - ui->setupUi(this); - ui->capture->setFlat(true); - connect(ui->capture, SIGNAL(clicked()), this, SLOT(capture())); - last_info.deviceName() = QString(""); - camera_list = QCameraInfo::availableCameras(); - if(camera_list.length() > 0) - Camera = new QCamera(camera_list[0]); - else - { - QString str(""); - Camera = new QCamera(QCameraInfo(str.toUtf8())); - QMessageBox::warning(this, "警告", "没有检测到摄像头,请插上摄像头后再打开该窗口", QMessageBox::Yes); - return; - } - for(int i = 0; i < camera_list.length(); i++) { - QAction *camera = ui->menu->addAction(camera_list[i].description()); - camera->setCheckable(true); - connect(camera, &QAction::triggered,[=](){ - if(last_info.deviceName() != camera_list[i].deviceName()) - { - open(camera_list[i]); - } - else - { - close(); - } - }); - } - CameraViewFinder = new QCameraViewfinder(ui->image); - CameraViewFinder->resize(ui->image->width(), ui->image->height()); -} - -single_capture::~single_capture() -{ - Camera->stop(); - delete Camera; - delete ui; -} - -void single_capture::close() -{ - Camera->stop(); - last_info = QCameraInfo(QString("").toUtf8()); - open_flag = false; -} - -void single_capture::open(QCameraInfo info) -{ - QList actions = ui->menu->actions(); - for(int i = 0; i < actions.length(); i++) - { - if(camera_list[i].deviceName() == info.deviceName()) - { - actions[i]->setChecked(true); - } - else - actions[i]->setChecked(false); - } - Camera->stop(); - Camera = new QCamera(info); - Camera->setViewfinder(CameraViewFinder); - //显示摄像头取景器 - CameraViewFinder->show(); - //开启摄像头 - try { - Camera->start(); - } catch (QEvent *e) { - QMessageBox::critical(this, "错误", "摄像头打开失败", QMessageBox::Yes); - } - if(Camera->status() != QCamera::ActiveStatus) - { - QMessageBox::warning(this, "警告", "摄像头打开失败!", QMessageBox::Yes); - close(); - return; - } - //创建获取一帧数据对象 - CameraImageCapture = new QCameraImageCapture(Camera); - //关联图像获取信号 - connect(CameraImageCapture, &QCameraImageCapture::imageCaptured, this, &single_capture::take_photo); - open_flag = true; - last_info = info; -} - -void single_capture::capture() -{ - if(open_flag == false) - { - QMessageBox::critical(this, "错误", "请先选择相机!", QMessageBox::Yes); - return; - } - if(save_path == "") - { - save_path = QFileDialog::getExistingDirectory(nullptr, "选择保存路径", "./"); - return; - } - CameraImageCapture->capture(); -} - -void single_capture::take_photo(int, const QImage &image) -{ - //使用系统时间来命名图片的名称,时间是唯一的,图片名也是唯一的 - QDateTime dateTime(QDateTime::currentDateTime()); - QString time = dateTime.toString("yyyy-MM-dd-hh-mm-ss"); - //创建图片保存路径名 - QString filename = save_path + QString("./%1.jpg").arg(time); - //保存一帧数据 - image.save(filename); -} - -void single_capture::closeEvent ( QCloseEvent *) -{ - Camera->stop(); -} diff --git a/camera_calibration/single_capture.h b/camera_calibration/single_capture.h deleted file mode 100644 index 2ee3b03..0000000 --- a/camera_calibration/single_capture.h +++ /dev/null @@ -1,44 +0,0 @@ -#ifndef SINGLE_CAPTURE_H -#define SINGLE_CAPTURE_H - -#include -#include -#include -#include -#include -#include - -namespace Ui { -class single_capture; -} - -class single_capture : public QMainWindow -{ - Q_OBJECT - -public: - explicit single_capture(QWidget *parent = nullptr); - ~single_capture(); - -private slots: - void capture(); - void take_photo(int id, const QImage &image); - -private: - Ui::single_capture *ui; - //摄像头对象指针 - QCamera* Camera; - QCameraInfo last_info; - //摄像头的取景器 - QCameraViewfinder* CameraViewFinder; - QList camera_list; - void open(QCameraInfo info); - void close(); - void closeEvent(QCloseEvent *event); - //记录摄像头内容 - QCameraImageCapture* CameraImageCapture; - QString save_path; - bool open_flag = false; -}; - -#endif // SINGLE_CAPTURE_H diff --git a/camera_calibration/single_capture.ui b/camera_calibration/single_capture.ui deleted file mode 100644 index 2c3e1a7..0000000 --- a/camera_calibration/single_capture.ui +++ /dev/null @@ -1,85 +0,0 @@ - - - single_capture - - - - 0 - 0 - 640 - 480 - - - - MainWindow - - - - :/icon/camera.png:/icon/camera.png - - - - - - 0 - 0 - 640 - 480 - - - - - - - Qt::AlignCenter - - - - - - 290 - 350 - 60 - 60 - - - - PointingHandCursor - - - - - - - :/icon/capture.png:/icon/capture.png - - - - 48 - 48 - - - - - - - - 0 - 0 - 640 - 22 - - - - - 选择相机 - - - - - - - - - - diff --git a/camera_calibration/single_capture_linux.cpp b/camera_calibration/single_capture_linux.cpp deleted file mode 100644 index 097a366..0000000 --- a/camera_calibration/single_capture_linux.cpp +++ /dev/null @@ -1,181 +0,0 @@ -#include "single_capture_linux.h" -#include "ui_single_capture.h" -#ifdef Q_OS_LINUX - -V4L2 v4l2; - -single_capture_linux::single_capture_linux(QWidget *parent) : - QMainWindow(parent), - ui(new Ui::single_capture) -{ - ui->setupUi(this); - ui->capture->setFlat(true); - connect(ui->capture, SIGNAL(clicked()), this, SLOT(capture())); - capture_thread = new SingleCaptureThread(); - connect(capture_thread, SIGNAL(SingleCaptureDone(QImage, int)), this, SLOT(DealCaptureDone(QImage, int))); - err11 = err19 = 0; - int camcount = v4l2.GetDeviceCount(); - for(int i = 0; i < camcount; i++) - { - QAction *camera = ui->menu->addAction(v4l2.GetCameraName(i)); - camera->setCheckable(true); - connect(camera, &QAction::triggered,[=](){ - if(last_id != i) - { - open_camera(i); - } - else - { - close_camera(); - } - }); - } -} - -single_capture_linux::~single_capture_linux() -{ - delete ui; -} - -void single_capture_linux::open_camera(int id) -{ - close_camera(); - last_id = id; - QList actions = ui->menu->actions(); - for(int i = 0; i < actions.length(); i++) - { - if(id == i) - { - actions[i]->setChecked(true); - } - else - actions[i]->setChecked(false); - } - v4l2.StartRun(id); - capture_thread->init(id); - capture_thread->start(); - open_flag = true; -} - -void single_capture_linux::close_camera() -{ - last_id = -1; - capture_thread->stop(); - capture_thread->quit(); - capture_thread->wait(); - v4l2.StopRun(); - open_flag = false; -} - -void single_capture_linux::DealCaptureDone(QImage image, int result) -{ - //超时后关闭视频 - //超时代表着VIDIOC_DQBUF会阻塞,直接关闭视频即可 - if(result == -1) - { - close_camera(); - - ui->image->clear(); - ui->image->setText("获取设备图像超时!"); - } - - if(!image.isNull()) - { - q_image = image; - ui->image->clear(); - switch(result) - { - case 0: //Success - err11 = err19 = 0; - if(image.isNull()) - ui->image->setText("画面丢失!"); - else - ui->image->setPixmap(QPixmap::fromImage(image.scaled(ui->image->size()))); - - break; - case 11: //Resource temporarily unavailable - err11++; - if(err11 == 10) - { - ui->image->clear(); - ui->image->setText("设备已打开,但获取视频失败!\n请尝试切换USB端口后断开重试!"); - } - break; - case 19: //No such device - err19++; - if(err19 == 10) - { - ui->image->clear(); - ui->image->setText("设备丢失!"); - } - break; - } - } -} - -void single_capture_linux::capture() -{ - if(open_flag == false) - { - QMessageBox::critical(this, "错误", "请先选择相机!", QMessageBox::Yes); - return; - } - if(save_path == "") - { - save_path = QFileDialog::getExistingDirectory(nullptr, "选择保存路径", "./"); - return; - } - //使用系统时间来命名图片的名称,时间是唯一的,图片名也是唯一的 - QDateTime dateTime(QDateTime::currentDateTime()); - QString time = dateTime.toString("yyyy-MM-dd-hh-mm-ss"); - //创建图片保存路径名 - QString filename = save_path +"/" + QString("%1.jpg").arg(time); - //保存一帧数据 - q_image.save(filename); -} - -void single_capture_linux::closeEvent ( QCloseEvent *) -{ - close_camera(); -} - -SingleCaptureThread::SingleCaptureThread() -{ - stopped = false; - majorindex = -1; -} - -void SingleCaptureThread::stop() -{ - stopped = true; -} - -void SingleCaptureThread::init(int index) -{ - stopped = false; - majorindex = index; -} - -void SingleCaptureThread::run() -{ - if(majorindex != -1) - { - while(!stopped) - { - msleep(1000/30); - - QImage img; - int ret = v4l2.GetFrame(); - if(ret == 0) - { - int WV = v4l2.GetCurResWidth(); - int HV = v4l2.GetCurResHeight(); - img = QImage(v4l2.rgb24, WV, HV, QImage::Format_RGB888); - } - - emit SingleCaptureDone(img, ret); - } - } -} - -#endif diff --git a/camera_calibration/single_capture_linux.h b/camera_calibration/single_capture_linux.h deleted file mode 100644 index dd1f19e..0000000 --- a/camera_calibration/single_capture_linux.h +++ /dev/null @@ -1,67 +0,0 @@ -#ifndef SINGLE_CAPTURE_LINUX_H -#define SINGLE_CAPTURE_LINUX_H - -#include - -#ifdef Q_OS_LINUX -#include "v4l2.hpp" -#include -#include -#include -#include - -class SingleCaptureThread : public QThread -{ - Q_OBJECT -public: - SingleCaptureThread(); - - QImage majorImage; - void stop(); - void init(int index); - -protected: - void run(); - -private: - volatile int majorindex; - volatile bool stopped; - -signals: - void SingleCaptureDone(QImage image, int result); -}; - -extern V4L2 v4l2; - -namespace Ui { -class single_capture; -} - -class single_capture_linux : public QMainWindow -{ - Q_OBJECT - -public: - explicit single_capture_linux(QWidget *parent = nullptr); - ~single_capture_linux(); - -private slots: - void capture(); - void DealCaptureDone(QImage image, int result); - -private: - Ui::single_capture *ui; - SingleCaptureThread *capture_thread; - int err11, err19; - void open_camera(int id); - void close_camera(); - void closeEvent(QCloseEvent *event); - QString save_path; - bool open_flag = false; - int last_id; - QImage q_image; -}; - -#endif - -#endif // SINGLE_CAPTURE_LINUX_H diff --git a/camera_calibration/v4l2.hpp b/camera_calibration/v4l2.hpp deleted file mode 100644 index 18c7601..0000000 --- a/camera_calibration/v4l2.hpp +++ /dev/null @@ -1,458 +0,0 @@ -#ifndef V4L2_HPP -#define V4L2_HPP - -#include -#ifdef Q_OS_LINUX - -#include -#include -#include -#include -#include - -#include -#include -#include -#include -#include -#include -#include - -class V4L2 -{ -public: - int GetDeviceCount() - { - char devname[15] = ""; - int count = 0; - int i; - for(i = 0; i < 100; i++) - { - sprintf(devname, "%s%d", "/dev/video", i); - if(test_device_exist(devname) == 0) - count++; - - memset(devname, 0, sizeof(devname)); - } - - return count; - } - char *GetDeviceName(int index) - { - memset(devName, 0, sizeof(devName)); - sprintf(devName, "%s%d", "/dev/video", index); - return devName; - } - char *GetCameraName(int index) - { - if(videoIsRun > 0) - return (char*)""; - - memset(camName, 0, sizeof(camName)); - - char devname[15] = ""; - strcpy(devname, GetDeviceName(index)); - - int fd = open(devname, O_RDWR); - if(ioctl(fd, VIDIOC_QUERYCAP, &cap) != -1) - { - strcpy(camName, (char *)cap.card); - } - close(fd); - - return camName; - } - - int StartRun(int index) - { - if(videoIsRun > 0) - return -1; - - char *devname = GetDeviceName(index); - fd = open(devname, O_RDWR); - if(fd == -1) - return -1; - - deviceIsOpen = 1; - - StartVideoPrePare(); - StartVideoStream(); - - strcpy(runningDev, devname); - videoIsRun = 1; - - return 0; - } - int GetFrame() - { - if(videoIsRun > 0) - { - fd_set fds; - struct timeval tv; - int r; - - FD_ZERO (&fds); - FD_SET (fd, &fds); - - /* Timeout. */ - tv.tv_sec = 7; - tv.tv_usec = 0; - - r = select (fd + 1, &fds, NULL, NULL, &tv); - - if (0 == r) - return -1; - else if(-1 == r) - return errno; - - memset(&buffer, 0, sizeof(buffer)); - buffer.type = V4L2_BUF_TYPE_VIDEO_CAPTURE; - buffer.memory = V4L2_MEMORY_MMAP; - - if (ioctl(fd, VIDIOC_DQBUF, &buffer) == -1) { - perror("GetFrame VIDIOC_DQBUF Failed"); - return errno; - } - else - { - convert_yuv_to_rgb_buffer((unsigned char*)buffers[buffer.index].start, rgb24, WIDTH, HEIGHT); - - if (ioctl(fd, VIDIOC_QBUF, &buffer) < 0) { - perror("GetFrame VIDIOC_QBUF Failed"); - return errno; - } - - return 0; - } - } - - return 0; - } - int StopRun() - { - if(videoIsRun > 0) - { - EndVideoStream(); - EndVideoStreamClear(); - } - - memset(runningDev, 0, sizeof(runningDev)); - videoIsRun = -1; - deviceIsOpen = -1; - - if(close(fd) != 0) - return -1; - - return 0; - } - - char *GetDevFmtDesc(int index) - { - memset(devFmtDesc, 0, sizeof(devFmtDesc)); - - fmtdesc.index=index; - fmtdesc.type=V4L2_BUF_TYPE_VIDEO_CAPTURE; - - if(ioctl(fd, VIDIOC_ENUM_FMT, &fmtdesc) != -1) - { - char fmt[5] = ""; - sprintf(fmt, "%c%c%c%c", - (__u8)(fmtdesc.pixelformat&0XFF), - (__u8)((fmtdesc.pixelformat>>8)&0XFF), - (__u8)((fmtdesc.pixelformat>>16)&0XFF), - (__u8)((fmtdesc.pixelformat>>24)&0XFF)); - - strncpy(devFmtDesc, fmt, 4); - } - - return devFmtDesc; - } - - int GetDevFmtWidth() - { - format.type = V4L2_BUF_TYPE_VIDEO_CAPTURE; - if(ioctl (fd, VIDIOC_G_FMT, &format) == -1) - { - perror("GetDevFmtWidth:"); - return -1; - } - return format.fmt.pix.width; - } - int GetDevFmtHeight() - { - format.type = V4L2_BUF_TYPE_VIDEO_CAPTURE; - if(ioctl (fd, VIDIOC_G_FMT, &format) == -1) - { - perror("GetDevFmtHeight:"); - return -1; - } - return format.fmt.pix.height; - } - int GetDevFmtSize() - { - format.type = V4L2_BUF_TYPE_VIDEO_CAPTURE; - if(ioctl (fd, VIDIOC_G_FMT, &format) == -1) - { - perror("GetDevFmtSize:"); - return -1; - } - return format.fmt.pix.sizeimage; - } - int GetDevFmtBytesLine() - { - format.type = V4L2_BUF_TYPE_VIDEO_CAPTURE; - if(ioctl (fd, VIDIOC_G_FMT, &format) == -1) - { - perror("GetDevFmtBytesLine:"); - return -1; - } - return format.fmt.pix.bytesperline; - } - - int GetResolutinCount() - { - fmtdesc.index = 0; - fmtdesc.type=V4L2_BUF_TYPE_VIDEO_CAPTURE; - - if(ioctl(fd, VIDIOC_ENUM_FMT, &fmtdesc) == -1) - return -1; - - frmsizeenum.pixel_format = fmtdesc.pixelformat; - int i = 0; - for(i = 0; ; i++) - { - frmsizeenum.index = i; - if(ioctl(fd, VIDIOC_ENUM_FRAMESIZES, &frmsizeenum) == -1) - break; - } - return i; - } - int GetResolutionWidth(int index) - { - fmtdesc.index = 0; - fmtdesc.type=V4L2_BUF_TYPE_VIDEO_CAPTURE; - - if(ioctl(fd, VIDIOC_ENUM_FMT, &fmtdesc) == -1) - return -1; - - frmsizeenum.pixel_format = fmtdesc.pixelformat; - - frmsizeenum.index = index; - if(ioctl(fd, VIDIOC_ENUM_FRAMESIZES, &frmsizeenum) != -1) - return frmsizeenum.discrete.width; - else - return -1; - } - int GetResolutionHeight(int index) - { - fmtdesc.index = 0; - fmtdesc.type=V4L2_BUF_TYPE_VIDEO_CAPTURE; - - if(ioctl(fd, VIDIOC_ENUM_FMT, &fmtdesc) == -1) - return -1; - - frmsizeenum.pixel_format = fmtdesc.pixelformat; - - frmsizeenum.index = index; - if(ioctl(fd, VIDIOC_ENUM_FRAMESIZES, &frmsizeenum) != -1) - return frmsizeenum.discrete.height; - else - return -1; - } - int GetCurResWidth() - { - format.type = V4L2_BUF_TYPE_VIDEO_CAPTURE; - if (ioctl(fd, VIDIOC_G_FMT, &format) == -1) - return -1; - return format.fmt.pix.width; - } - int GetCurResHeight() - { - format.type = V4L2_BUF_TYPE_VIDEO_CAPTURE; - if (ioctl(fd, VIDIOC_G_FMT, &format) == -1) - return -1; - return format.fmt.pix.height; - } - int videoIsRun = -1; - unsigned char *rgb24 = NULL; -private: - char runningDev[15] = ""; - char devName[15] = ""; - char camName[32] = ""; - char devFmtDesc[4] = ""; - - int fd = -1; - int deviceIsOpen = -1; - int WIDTH, HEIGHT; - - //V4l2相关结构体 - struct v4l2_capability cap; - struct v4l2_fmtdesc fmtdesc; - struct v4l2_frmsizeenum frmsizeenum; - struct v4l2_format format; - struct v4l2_requestbuffers reqbuf; - struct v4l2_buffer buffer; - - struct buffer{ - void *start; - unsigned int length; - }*buffers; - - void StartVideoPrePare() - { - //申请帧缓存区 - memset (&reqbuf, 0, sizeof (reqbuf)); - reqbuf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE; - reqbuf.memory = V4L2_MEMORY_MMAP; - reqbuf.count = 4; - - if (-1 == ioctl (fd, VIDIOC_REQBUFS, &reqbuf)) { - if (errno == EINVAL) - printf ("Video capturing or mmap-streaming is not supported\n"); - else - perror ("VIDIOC_REQBUFS"); - return; - } - - //分配缓存区 - buffers = (struct buffer*)calloc(reqbuf.count, sizeof (*buffers)); - if(buffers == NULL) - perror("buffers is NULL"); - else - assert (buffers != NULL); - - //mmap内存映射 - int i; - for (i = 0; i < (int)reqbuf.count; i++) { - memset (&buffer, 0, sizeof (buffer)); - buffer.type = reqbuf.type; - buffer.memory = V4L2_MEMORY_MMAP; - buffer.index = i; - - if (-1 == ioctl (fd, VIDIOC_QUERYBUF, &buffer)) { - perror ("VIDIOC_QUERYBUF"); - return; - } - - buffers[i].length = buffer.length; - - buffers[i].start = mmap (NULL, buffer.length, - PROT_READ | PROT_WRITE, - MAP_SHARED, - fd, buffer.m.offset); - - if (MAP_FAILED == buffers[i].start) { - perror ("mmap"); - return; - } - } - - //将缓存帧放到队列中等待视频流到来 - unsigned int ii; - for(ii = 0; ii < reqbuf.count; ii++){ - buffer.type = V4L2_BUF_TYPE_VIDEO_CAPTURE; - buffer.memory = V4L2_MEMORY_MMAP; - buffer.index = ii; - if (ioctl(fd,VIDIOC_QBUF,&buffer)==-1){ - perror("VIDIOC_QBUF failed"); - } - } - - WIDTH = GetCurResWidth(); - HEIGHT = GetCurResHeight(); - rgb24 = (unsigned char*)malloc(WIDTH*HEIGHT*3*sizeof(char)); - assert(rgb24 != NULL); - } - void StartVideoStream() - { - enum v4l2_buf_type type; - type = V4L2_BUF_TYPE_VIDEO_CAPTURE; - if (ioctl(fd,VIDIOC_STREAMON,&type) == -1) { - perror("VIDIOC_STREAMON failed"); - } - } - void EndVideoStream() - { - //关闭视频流 - enum v4l2_buf_type type; - type = V4L2_BUF_TYPE_VIDEO_CAPTURE; - if (ioctl(fd,VIDIOC_STREAMOFF,&type) == -1) { - perror("VIDIOC_STREAMOFF failed"); - } - } - void EndVideoStreamClear() - { - //手动释放分配的内存 - int i; - for (i = 0; i < (int)reqbuf.count; i++) - munmap (buffers[i].start, buffers[i].length); - } - int test_device_exist(char *devName) - { - struct stat st; - if (-1 == stat(devName, &st)) - return -1; - - return 0; - } - int convert_yuv_to_rgb_pixel(int y, int u, int v) - { - unsigned int pixel32 = 0; - unsigned char *pixel = (unsigned char *)&pixel32; - int r, g, b; - r = y + (1.370705 * (v-128)); - g = y - (0.698001 * (v-128)) - (0.337633 * (u-128)); - b = y + (1.732446 * (u-128)); - if(r > 255) r = 255; - if(g > 255) g = 255; - if(b > 255) b = 255; - if(r < 0) r = 0; - if(g < 0) g = 0; - if(b < 0) b = 0; - pixel[0] = r ; - pixel[1] = g ; - pixel[2] = b ; - return pixel32; - } - - int convert_yuv_to_rgb_buffer(unsigned char *yuv, unsigned char *rgb, unsigned int width, unsigned int height) - { - unsigned int in, out = 0; - unsigned int pixel_16; - unsigned char pixel_24[3]; - unsigned int pixel32; - int y0, u, y1, v; - - for(in = 0; in < width * height * 2; in += 4) - { - pixel_16 = - yuv[in + 3] << 24 | - yuv[in + 2] << 16 | - yuv[in + 1] << 8 | - yuv[in + 0]; - y0 = (pixel_16 & 0x000000ff); - u = (pixel_16 & 0x0000ff00) >> 8; - y1 = (pixel_16 & 0x00ff0000) >> 16; - v = (pixel_16 & 0xff000000) >> 24; - pixel32 = convert_yuv_to_rgb_pixel(y0, u, v); - pixel_24[0] = (pixel32 & 0x000000ff); - pixel_24[1] = (pixel32 & 0x0000ff00) >> 8; - pixel_24[2] = (pixel32 & 0x00ff0000) >> 16; - rgb[out++] = pixel_24[0]; - rgb[out++] = pixel_24[1]; - rgb[out++] = pixel_24[2]; - pixel32 = convert_yuv_to_rgb_pixel(y1, u, v); - pixel_24[0] = (pixel32 & 0x000000ff); - pixel_24[1] = (pixel32 & 0x0000ff00) >> 8; - pixel_24[2] = (pixel32 & 0x00ff0000) >> 16; - rgb[out++] = pixel_24[0]; - rgb[out++] = pixel_24[1]; - rgb[out++] = pixel_24[2]; - } - return 0; - } -}; - -#endif - -#endif // V4L2_HPP diff --git a/camera_calibration/choose_two_dir.cpp b/choose_two_dir.cpp similarity index 85% rename from camera_calibration/choose_two_dir.cpp rename to choose_two_dir.cpp index 59a61e3..d312d21 100644 --- a/camera_calibration/choose_two_dir.cpp +++ b/choose_two_dir.cpp @@ -41,7 +41,7 @@ void choose_two_dir::cancel() void choose_two_dir::choose_left_dir() { - QString srcDirPath = QFileDialog::getExistingDirectory(nullptr, "Choose Directory", "./"); + QString srcDirPath = QFileDialog::getExistingDirectory(this, "Choose Directory", "./", QFileDialog::ShowDirsOnly); if(srcDirPath.isEmpty()) { return; @@ -51,7 +51,7 @@ void choose_two_dir::choose_left_dir() void choose_two_dir::choose_right_dir() { - QString srcDirPath = QFileDialog::getExistingDirectory(nullptr, "Choose Directory", "./"); + QString srcDirPath = QFileDialog::getExistingDirectory(this, "Choose Directory", "./", QFileDialog::ShowDirsOnly); if(srcDirPath.isEmpty()) { return; diff --git a/camera_calibration/choose_two_dir.h b/choose_two_dir.h similarity index 100% rename from camera_calibration/choose_two_dir.h rename to choose_two_dir.h diff --git a/camera_calibration/choose_two_dir.ui b/choose_two_dir.ui similarity index 100% rename from camera_calibration/choose_two_dir.ui rename to choose_two_dir.ui diff --git a/camera_calibration/choose_yaml.cpp b/choose_yaml.cpp similarity index 100% rename from camera_calibration/choose_yaml.cpp rename to choose_yaml.cpp diff --git a/camera_calibration/choose_yaml.h b/choose_yaml.h similarity index 100% rename from camera_calibration/choose_yaml.h rename to choose_yaml.h diff --git a/camera_calibration/choose_yaml.ui b/choose_yaml.ui similarity index 100% rename from camera_calibration/choose_yaml.ui rename to choose_yaml.ui diff --git a/guide/new_tool.jpg b/guide/new_tool.jpg deleted file mode 100644 index 4db4684..0000000 Binary files a/guide/new_tool.jpg and /dev/null differ diff --git a/guide/new_ui.jpg b/guide/new_ui.jpg deleted file mode 100644 index e008eff..0000000 Binary files a/guide/new_ui.jpg and /dev/null differ diff --git a/guide/new_ui_3.jpg b/guide/new_ui_3.jpg deleted file mode 100644 index 7abe1b0..0000000 Binary files a/guide/new_ui_3.jpg and /dev/null differ diff --git a/guide/new_ui_dir.jpg b/guide/new_ui_dir.jpg deleted file mode 100644 index 42e0ca5..0000000 Binary files a/guide/new_ui_dir.jpg and /dev/null differ diff --git a/hand_eye_calibration/HandEyeCalibration.cpp b/hand_eye_calibration/HandEyeCalibration.cpp deleted file mode 100644 index 4aa23bb..0000000 --- a/hand_eye_calibration/HandEyeCalibration.cpp +++ /dev/null @@ -1,1495 +0,0 @@ -#include "HandEyeCalibration.h" -#include "ui_HandEyeCalibration.h" -#include "TSAIleastSquareCalibration.h" -#include - -extern cv::Mat find_corner_thread_img; -extern struct Chessboarder_t find_corner_thread_chessboard; - -HandEyeCalibration::HandEyeCalibration(QWidget *parent) - : QMainWindow(parent) - , ui(new Ui::HandEyeCalibration) -{ - ui->setupUi(this); - setFixedSize(this->width(), this->height()); - ui->addRobot->setFlat(true); - ui->addImage->setFlat(true); - ui->calibrate->setFlat(true); - ui->export_1->setFlat(true); - ui->delete_1->setFlat(true); - ui->undistort->setFlat(true); - ui->Camera->setFlat(true); - // ui->Robot->setFlat(true); - findcorner_thread = new FindcornerThread(); - connect(findcorner_thread, SIGNAL(isDone()), this, SLOT(DealThreadDone())); - ShowIntro(); - QMenu *menu = new QMenu(); - QAction *open_camera = menu->addAction("打开相机"); - QFont font = menu->font(); - font.setPixelSize(12); - menu->setFont(font); - connect(open_camera, SIGNAL(triggered()), this,SLOT(OpenCamera())); - ui->Camera->setMenu(menu); - ui->listWidget->setSelectionMode(QAbstractItemView::ExtendedSelection); // 按ctrl多选 - connect(ui->addRobot, SIGNAL(clicked()), this, SLOT(addRobot())); - connect(ui->addImage, SIGNAL(clicked()), this, SLOT(addImage())); - connect(ui->delete_1, SIGNAL(clicked()), this, SLOT(deleteImage())); - chessboard_size = 0; - connect(ui->calibrate, SIGNAL(clicked()), this, SLOT(calibrate())); - connect(ui->fisheye, SIGNAL(stateChanged(int)), this, SLOT(fisheyeModeSwitch(int))); - connect(ui->export_1, SIGNAL(clicked()), this, SLOT(exportParam())); - connect(ui->undistort, SIGNAL(clicked()), this, SLOT(distortModeSwitch())); - connect(ui->listWidget, SIGNAL(currentItemChanged(QListWidgetItem*, QListWidgetItem*)), this, SLOT(chooseImage(QListWidgetItem*, QListWidgetItem*))); - connect(ui->double_camera, SIGNAL(toggled(bool)), this, SLOT(reset())); - connect(ui->single_camera, SIGNAL(toggled(bool)), this, SLOT(reset())); - connect(ui->double_undistort, SIGNAL(toggled(bool)), this, SLOT(undistortReset())); - connect(ui->single_undistort, SIGNAL(toggled(bool)), this, SLOT(undistortReset())); - saveImage = ui->menu->addAction("导出矫正图片"); - font = saveImage->font(); - font.setPixelSize(12); - saveImage->setFont(font); - connect(saveImage, SIGNAL(triggered()), this, SLOT(saveUndistort())); -} - -HandEyeCalibration::~HandEyeCalibration() -{ - delete ui; -} - -// 显示简单的文字引导 -void HandEyeCalibration::ShowIntro() -{ - ui->intro->setFixedHeight(100); - if(ui->double_camera->isChecked() || ui->double_undistort->isChecked()) - { - QString str = "点击左上角添加图片"; - str += "\n"; - str += "左右相机图片分别放于两个文件夹"; - str += "\n"; - str += "对应图片名称需一致。"; - str += "\n"; - str += "\n"; - str += "单/双目纠正模式可纠正无棋盘的图片。"; - ui->intro->setText(str); - } - else if(ui->single_camera->isChecked() || ui->single_undistort->isChecked()) - { - QString str = "点击左上角添加图片"; - str += "\n"; - str += "15至20张较为适宜"; - str += "\n"; - str += "\n"; - str += "单/双目纠正模式可纠正无棋盘的图片。"; - ui->intro->setText(str); - } -} - -// 隐藏文字指导 -void HandEyeCalibration::HiddenIntro() -{ - ui->intro->setText(""); - ui->intro->setFixedHeight(0); -} - -// 使用相机采集图片,根据标定模式选择采集模式 -void HandEyeCalibration::OpenCamera() -{ - if(ui->single_camera->isChecked()) - { -#ifdef Q_OS_LINUX - int camcount = v4l2.GetDeviceCount(); - if(camcount < 1) - { - QMessageBox::warning(this, "警告", "没有检测到摄像头,请插上摄像头后再打开该窗口", QMessageBox::Yes); - return; - } - single_c = new single_capture_linux(); -#elif defined(Q_OS_WIN32) - QList camera_list = QCameraInfo::availableCameras(); - if(camera_list.length() < 1) - { - QMessageBox::warning(this, "警告", "没有检测到摄像头,请插上摄像头后再打开该窗口", QMessageBox::Yes); - return; - } - single_c = new single_capture(); -#endif - single_c->setWindowTitle("单个相机拍照"); - QFont font = single_c->font(); - font.setPixelSize(12); - single_c->setFont(font); - single_c->show(); - } - else - { -#ifdef Q_OS_LINUX - int camcount = v4l2_left.GetDeviceCount(); - if(camcount < 2) - { - QMessageBox::warning(this, "警告", "摄像头少于两个,请插上摄像头后再打开该窗口", QMessageBox::Yes); - return; - } - double_c = new double_capture_linux(); -#elif defined(Q_OS_WIN32) - QList camera_list = QCameraInfo::availableCameras(); - if(camera_list.length() < 2) - { - QMessageBox::warning(this, "警告", "摄像头少于两个,请插上摄像头后再打开该窗口", QMessageBox::Yes); - return; - } - double_c = new double_capture(); -#endif - double_c->setWindowTitle("两个相机拍照"); - QFont font = double_c->font(); - font.setPixelSize(12); - double_c->setFont(font); - double_c->show(); - } -} - -// 导出矫正图片 -void HandEyeCalibration::saveUndistort() -{ - if(calibrate_flag == false) - { - if(ui->single_undistort->isChecked() || ui->double_undistort->isChecked()) - { - QMessageBox::warning(this, "警告", "还未获取到相机参数,请点击UNDISTORT按钮加载yaml文件。", QMessageBox::Yes, QMessageBox::Yes); - return; - } - else - { - QMessageBox::critical(NULL, "错误", "请先标定", QMessageBox::Yes, QMessageBox::Yes); - return; - } - } - QString srcDirPath = QFileDialog::getExistingDirectory(nullptr, "选择保存路径", "./"); - if(srcDirPath.length() <= 0) - return; - QTextCodec *code = QTextCodec::codecForName("GB2312");//解决中文路径问题 - if(ui->double_camera->isChecked() || ui->double_undistort->isChecked()) - { - QDir dir; - dir.mkdir(srcDirPath+"/left/"); - dir.mkdir(srcDirPath+"/right/"); - for(unsigned int i = 0; i < stereo_imgs.size(); i++) - { - struct Stereo_Img_t img = stereo_imgs[i]; - cv::Mat left_dst = img.left_img.clone(); - cv::Mat right_dst = img.right_img.clone(); - if(fisheye_flag == false) - { - cv::Mat mapx, mapy; - cv::initUndistortRectifyMap(cameraMatrix, distCoefficients, R1, P1, img_size, CV_16SC2, mapx, mapy); - cv::remap(img.left_img, left_dst, mapx, mapy, cv::INTER_LINEAR); - cv::initUndistortRectifyMap(cameraMatrix2, distCoefficients2, R2, P2, img_size, CV_16SC2, mapx, mapy); - cv::remap(img.right_img, right_dst, mapx, mapy, cv::INTER_LINEAR); - } - else - { - cv::Mat mapx, mapy; - cv::fisheye::initUndistortRectifyMap(K, D, R1, P1, img_size, CV_16SC2, mapx, mapy); - cv::remap(img.left_img, left_dst, mapx, mapy, cv::INTER_LINEAR); - cv::fisheye::initUndistortRectifyMap(K2, D2, R2, P2, img_size, CV_16SC2, mapx, mapy); - cv::remap(img.right_img, right_dst, mapx, mapy, cv::INTER_LINEAR); - } - QString save_name = srcDirPath + "/left/" + img.left_file_name; - std::string str = code->fromUnicode(save_name).data(); - cv::imwrite(str, left_dst); - save_name = srcDirPath + "/right/" + img.left_file_name; - str = code->fromUnicode(save_name).data(); - cv::imwrite(str, right_dst); - for(int i = 1; i < 10; i++) - { - cv::line(left_dst, cv::Point(0, left_dst.rows*i/10), cv::Point(left_dst.cols-1, left_dst.rows*i/10), cv::Scalar(0,0,255), 2); - cv::line(right_dst, cv::Point(0, right_dst.rows*i/10), cv::Point(right_dst.cols-1, right_dst.rows*i/10), cv::Scalar(0,0,255), 2); - } - cv::Mat combian_img = cv::Mat::zeros(cv::Size(img.left_img.cols*2, img.left_img.rows), CV_8UC3); - cv::Mat new_roi = combian_img(cv::Rect(0, 0, img.left_img.cols, img.left_img.rows)); - left_dst.convertTo(new_roi, new_roi.type()); - new_roi = combian_img(cv::Rect(img.left_img.cols, 0, img.left_img.cols, img.left_img.rows)); - right_dst.convertTo(new_roi, new_roi.type()); - save_name = srcDirPath + "/" + img.left_file_name; - str = code->fromUnicode(save_name).data(); - cv::imwrite(str, combian_img); - } - } - else - { - for(unsigned int i = 0; i < imgs.size(); i++) - { - cv::Mat dst; - if(fisheye_flag == false) - cv::undistort(imgs[i].img, dst, cameraMatrix, distCoefficients); - else - cv::fisheye::undistortImage(imgs[i].img, dst, K, D, K, imgs[i].img.size()); - QString save_name = srcDirPath + "/" + imgs[i].file_name; - std::string str = code->fromUnicode(save_name).data(); - cv::imwrite(str, dst); - } - } -} - -// 切换模式时删除所有图片缓存 -void HandEyeCalibration::reset() -{ - ShowIntro(); - ui->k_2->setEnabled(true); - ui->k_3->setEnabled(true); - ui->tangential->setEnabled(true); - ui->calibrate->setEnabled(true); - ui->export_1->setEnabled(true); - chessboard_size = 0; - calibrate_flag = false; - distort_flag = false; - disconnect(ui->listWidget, SIGNAL(currentItemChanged(QListWidgetItem*, QListWidgetItem*)), this, SLOT(chooseImage(QListWidgetItem*, QListWidgetItem*))); - ui->listWidget->clear(); - connect(ui->listWidget, SIGNAL(currentItemChanged(QListWidgetItem*, QListWidgetItem*)), this, SLOT(chooseImage(QListWidgetItem*, QListWidgetItem*))); - if(imgs.size() > 0) - imgs.clear(); - if(stereo_imgs.size() > 0) - stereo_imgs.clear(); -} - -void HandEyeCalibration::undistortReset() -{ - ShowIntro(); - ui->k_2->setDisabled(true); - ui->k_3->setDisabled(true); - ui->tangential->setDisabled(true); - ui->calibrate->setDisabled(true); - ui->export_1->setDisabled(true); - chessboard_size = 0; - calibrate_flag = false; - distort_flag = false; - disconnect(ui->listWidget, SIGNAL(currentItemChanged(QListWidgetItem*, QListWidgetItem*)), this, SLOT(chooseImage(QListWidgetItem*, QListWidgetItem*))); - ui->listWidget->clear(); - connect(ui->listWidget, SIGNAL(currentItemChanged(QListWidgetItem*, QListWidgetItem*)), this, SLOT(chooseImage(QListWidgetItem*, QListWidgetItem*))); - if(imgs.size() > 0) - imgs.clear(); - if(stereo_imgs.size() > 0) - stereo_imgs.clear(); -} - -// 显示选择的图像 -void HandEyeCalibration::chooseImage(QListWidgetItem* item, QListWidgetItem*) -{ - int id = ui->listWidget->row(item); - if(ui->double_camera->isChecked()) - { - // 如果是双目模式 - struct Stereo_Img_t img = stereo_imgs[id]; - cv::Mat left_dst = img.left_img.clone(); - cv::Mat right_dst = img.right_img.clone(); - if(distort_flag == true && calibrate_flag == true) - { - // 对左右图像进行矫正 - if(fisheye_flag == false) - { - cv::Mat mapx, mapy; - cv::initUndistortRectifyMap(cameraMatrix, distCoefficients, R1, P1, img_size, CV_16SC2, mapx, mapy); - cv::remap(img.left_img, left_dst, mapx, mapy, cv::INTER_LINEAR); - cv::initUndistortRectifyMap(cameraMatrix2, distCoefficients2, R2, P2, img_size, CV_16SC2, mapx, mapy); - cv::remap(img.right_img, right_dst, mapx, mapy, cv::INTER_LINEAR); - } - else - { - cv::Mat mapx, mapy; - cv::fisheye::initUndistortRectifyMap(K, D, R1, P1, img_size, CV_16SC2, mapx, mapy); - cv::remap(img.left_img, left_dst, mapx, mapy, cv::INTER_LINEAR); - cv::fisheye::initUndistortRectifyMap(K2, D2, R2, P2, img_size, CV_16SC2, mapx, mapy); - cv::remap(img.right_img, right_dst, mapx, mapy, cv::INTER_LINEAR); - } - // 绘制横线以观察左右图像极线是否对齐 - for(int i = 1; i < 10; i++) - { - cv::line(left_dst, cv::Point(0, left_dst.rows*i/10), cv::Point(left_dst.cols-1, left_dst.rows*i/10), cv::Scalar(0,0,255), 2); - cv::line(right_dst, cv::Point(0, right_dst.rows*i/10), cv::Point(right_dst.cols-1, right_dst.rows*i/10), cv::Scalar(0,0,255), 2); - } - } - else - { - // 非矫正模式下显示角点位置 - for(unsigned int i = 0; i < img.left_img_points.size(); i++) - { - cv::circle(left_dst, img.left_img_points[i], 10, cv::Scalar(0,0,255), 2); - } - for(unsigned int i = 0; i < img.right_img_points.size(); i++) - { - cv::circle(right_dst, img.right_img_points[i], 10, cv::Scalar(0,0,255), 2); - } - if(calibrate_flag == true) - { - // 显示标定后角点的重投影 - if(fisheye_flag == false) - { - std::vector reproject_img_p; - projectPoints(img.world_points, img.left_rvec, img.left_tvec, cameraMatrix, distCoefficients, reproject_img_p); - for(unsigned int i = 0; i < reproject_img_p.size(); i++) - { - cv::circle(left_dst, reproject_img_p[i], 3, cv::Scalar(255,0,0), 2); - } - projectPoints(img.world_points, img.right_rvec, img.right_tvec, cameraMatrix2, distCoefficients2, reproject_img_p); - for(unsigned int i = 0; i < reproject_img_p.size(); i++) - { - cv::circle(right_dst, reproject_img_p[i], 3, cv::Scalar(255,0,0), 2); - } - } - else - { - std::vector reproject_img_p; - std::vector w_p_; - for(unsigned int i = 0; i < img.world_points.size(); i++) - { - w_p_.push_back(img.world_points[i]); - } - cv::fisheye::projectPoints(w_p_, reproject_img_p, img.left_fish_rvec, img.left_fish_tvec, K, D); - for(unsigned int i = 0; i < reproject_img_p.size(); i++) - { - cv::circle(left_dst, reproject_img_p[i], 3, cv::Scalar(255,0,0), 2); - } - cv::fisheye::projectPoints(w_p_, reproject_img_p, img.right_fish_rvec, img.right_fish_tvec, K2, D2); - for(unsigned int i = 0; i < reproject_img_p.size(); i++) - { - cv::circle(right_dst, reproject_img_p[i], 3, cv::Scalar(255,0,0), 2); - } - } - } - } - int height = img.left_img.rows*265/img.left_img.cols; - cv::resize(left_dst, left_dst, cv::Size(265, height)); - cv::resize(right_dst, right_dst, cv::Size(265, height)); - cv::Mat combian_img = cv::Mat::zeros(cv::Size(530, height), CV_8UC3); - cv::Mat new_roi = combian_img(cv::Rect(0, 0, 265, height)); - left_dst.convertTo(new_roi, new_roi.type()); - new_roi = combian_img(cv::Rect(265, 0, 265, height)); - right_dst.convertTo(new_roi, new_roi.type()); - QImage qimage = Mat2QImage(combian_img); - ui->Image->setPixmap(QPixmap::fromImage(qimage)); - } - else if(ui->single_camera->isChecked()) - { - // 单目逻辑同双目 - cv::Mat img; - std::vector corners; - std::vector w_p; - cv::Mat rvecs, tvecs; - cv::Vec3d fish_rvecs, fish_tvecs; - img = imgs[id].img.clone(); - corners = imgs[id].img_points; - w_p = imgs[id].world_points; - tvecs = imgs[id].tvec; - rvecs = imgs[id].rvec; - fish_rvecs = imgs[id].fish_rvec; - fish_tvecs = imgs[id].fish_tvec; - for(unsigned int i = 0; i < corners.size(); i++) - { - cv::circle(img, corners[i], 10, cv::Scalar(0,0,255), 2); - } - if(calibrate_flag == true) - { - if(fisheye_flag == false) - { - std::vector reproject_img_p; - projectPoints(w_p, rvecs, tvecs, cameraMatrix, distCoefficients, reproject_img_p); - for(unsigned int i = 0; i < reproject_img_p.size(); i++) - { - cv::circle(img, reproject_img_p[i], 3, cv::Scalar(255,0,0), 2); - } - } - else - { - std::vector reproject_img_p; - std::vector w_p_; - for(unsigned int i = 0; i < w_p.size(); i++) - { - w_p_.push_back(w_p[i]); - } - cv::fisheye::projectPoints(w_p_, reproject_img_p, fish_rvecs, fish_tvecs, K, D); - for(unsigned int i = 0; i < reproject_img_p.size(); i++) - { - cv::circle(img, reproject_img_p[i], 3, cv::Scalar(255,0,0), 2); - } - } - } - if(distort_flag == true && calibrate_flag == true) - { - cv::Mat dst; - if(fisheye_flag == false) - cv::undistort(img, dst, cameraMatrix, distCoefficients); - else - cv::fisheye::undistortImage(img, dst, K, D, K, img.size()); - img = dst; - } - if(img.cols > img.rows) - cv::resize(img, img, cv::Size(530, img.rows*530/img.cols)); - else - cv::resize(img, img, cv::Size(img.cols*495/img.rows, 495)); - QImage qimage = Mat2QImage(img); - ui->Image->setPixmap(QPixmap::fromImage(qimage)); - } - else if(ui->double_undistort->isChecked()) - { - // 如果是双目模式 - struct Stereo_Img_t img = stereo_imgs[id]; - cv::Mat left_dst = img.left_img.clone(); - cv::Mat right_dst = img.right_img.clone(); - if(distort_flag == true) - { - // 对左右图像进行矫正 - if(fisheye_flag == false) - { - cv::Mat mapx, mapy; - cv::initUndistortRectifyMap(cameraMatrix, distCoefficients, R1, P1, img_size, CV_16SC2, mapx, mapy); - cv::remap(img.left_img, left_dst, mapx, mapy, cv::INTER_LINEAR); - cv::initUndistortRectifyMap(cameraMatrix2, distCoefficients2, R2, P2, img_size, CV_16SC2, mapx, mapy); - cv::remap(img.right_img, right_dst, mapx, mapy, cv::INTER_LINEAR); - } - else - { - cv::Mat mapx, mapy; - cv::fisheye::initUndistortRectifyMap(K, D, R1, P1, img_size, CV_16SC2, mapx, mapy); - cv::remap(img.left_img, left_dst, mapx, mapy, cv::INTER_LINEAR); - cv::fisheye::initUndistortRectifyMap(K2, D2, R2, P2, img_size, CV_16SC2, mapx, mapy); - cv::remap(img.right_img, right_dst, mapx, mapy, cv::INTER_LINEAR); - } - // 绘制横线以观察左右图像极线是否对齐 - for(int i = 1; i < 10; i++) - { - cv::line(left_dst, cv::Point(0, left_dst.rows*i/10), cv::Point(left_dst.cols-1, left_dst.rows*i/10), cv::Scalar(0,0,255), 2); - cv::line(right_dst, cv::Point(0, right_dst.rows*i/10), cv::Point(right_dst.cols-1, right_dst.rows*i/10), cv::Scalar(0,0,255), 2); - } - } - int height = img.left_img.rows*265/img.left_img.cols; - cv::resize(left_dst, left_dst, cv::Size(265, height)); - cv::resize(right_dst, right_dst, cv::Size(265, height)); - cv::Mat combian_img = cv::Mat::zeros(cv::Size(530, height), CV_8UC3); - cv::Mat new_roi = combian_img(cv::Rect(0, 0, 265, height)); - left_dst.convertTo(new_roi, new_roi.type()); - new_roi = combian_img(cv::Rect(265, 0, 265, height)); - right_dst.convertTo(new_roi, new_roi.type()); - QImage qimage = Mat2QImage(combian_img); - ui->Image->setPixmap(QPixmap::fromImage(qimage)); - } - else - { - // 单目逻辑同双目 - cv::Mat img = imgs[id].img.clone(); - if(distort_flag == true) - { - cv::Mat dst; - if(fisheye_flag == false) - cv::undistort(img, dst, cameraMatrix, distCoefficients); - else - cv::fisheye::undistortImage(img, dst, K, D, K, img.size()); - img = dst; - } - if(img.cols > img.rows) - cv::resize(img, img, cv::Size(530, img.rows*530/img.cols)); - else - cv::resize(img, img, cv::Size(img.cols*495/img.rows, 495)); - QImage qimage = Mat2QImage(img); - ui->Image->setPixmap(QPixmap::fromImage(qimage)); - } -} - -// 角点寻找线程结束时的回调函数 -void HandEyeCalibration::DealThreadDone() -{ - findcorner_thread->quit(); - findcorner_thread->wait(); - thread_done = true; -} - -void HandEyeCalibration::receiveYamlPath(QString str) -{ - cv::FileStorage fs_read(str.toStdString(), cv::FileStorage::READ); - if(ui->single_undistort->isChecked()) - { - if(fs_read["cameraMatrix"].empty() || fs_read["distCoeffs"].empty()) - { - QMessageBox::critical(this, "错误", "YAML文件格式错误", QMessageBox::Yes, QMessageBox::Yes); - return; - } - if(fisheye_flag == false) - { - fs_read["cameraMatrix"] >> cameraMatrix; - fs_read["distCoeffs"] >> distCoefficients; - calibrate_flag = true; - } - else - { - cv::Mat camera_matrix, Dist; - fs_read["cameraMatrix"] >> camera_matrix; - fs_read["distCoeffs"] >> Dist; - K(0,0) = camera_matrix.at(0,0); - K(0,1) = 0; - K(0,2) = camera_matrix.at(0,2); - K(1,0) = 0; - K(1,1) = camera_matrix.at(1,1); - K(1,2) = camera_matrix.at(1,2); - K(1,0) = 0; - K(2,1) = 0; - K(2,2) = 1; - D[0] = Dist.at(0,0); - D[1] = Dist.at(0,1); - D[2] = Dist.at(0,2); - D[3] = Dist.at(0,3); - calibrate_flag = true; - } - } - else - { - if(fs_read["left_camera_Matrix"].empty() || fs_read["left_camera_distCoeffs"].empty() || fs_read["right_camera_Matrix"].empty() || fs_read["right_camera_distCoeffs"].empty()) - { - QMessageBox::critical(this, "错误", "YAML文件格式错误", QMessageBox::Yes, QMessageBox::Yes); - return; - } - if(fs_read["Rotate_Matrix"].empty() || fs_read["Translate_Matrix"].empty() || fs_read["R1"].empty() || fs_read["R2"].empty() || fs_read["P1"].empty() || - fs_read["P2"].empty() || fs_read["Q"].empty()) - { - QMessageBox::critical(this, "错误", "YAML文件格式错误", QMessageBox::Yes, QMessageBox::Yes); - return; - } - if(fisheye_flag == false) - { - fs_read["left_camera_Matrix"] >> cameraMatrix; - fs_read["left_camera_distCoeffs"] >> distCoefficients; - fs_read["right_camera_Matrix"] >> cameraMatrix2; - fs_read["right_camera_distCoeffs"] >> distCoefficients2; - fs_read["Rotate_Matrix"] >> R; - fs_read["Translate_Matrix"] >> T; - fs_read["R1"] >> R1; - fs_read["R2"] >> R2; - fs_read["P1"] >> P1; - fs_read["P2"] >> P2; - fs_read["Q"] >> Q; - if(cameraMatrix.at(0,0) == 0 || cameraMatrix2.at(0,0) == 0) - { - QMessageBox::critical(this, "错误", "YAML文件格式错误", QMessageBox::Yes, QMessageBox::Yes); - return; - } - calibrate_flag = true; - } - else - { - cv::Mat camera_matrix, Dist; - fs_read["left_camera_Matrix"] >> camera_matrix; - fs_read["left_camera_distCoeffs"] >> Dist; - K(0,0) = camera_matrix.at(0,0); - K(0,1) = 0; - K(0,2) = camera_matrix.at(0,2); - K(1,0) = 0; - K(1,1) = camera_matrix.at(1,1); - K(1,2) = camera_matrix.at(1,2); - K(1,0) = 0; - K(2,1) = 0; - K(2,2) = 1; - D[0] = Dist.at(0,0); - D[1] = Dist.at(0,1); - D[2] = Dist.at(0,2); - D[3] = Dist.at(0,3); - - fs_read["right_camera_Matrix"] >> camera_matrix; - fs_read["right_camera_distCoeffs"] >> Dist; - K2(0,0) = camera_matrix.at(0,0); - K2(0,1) = 0; - K2(0,2) = camera_matrix.at(0,2); - K2(1,0) = 0; - K2(1,1) = camera_matrix.at(1,1); - K2(1,2) = camera_matrix.at(1,2); - K2(1,0) = 0; - K2(2,1) = 0; - K2(2,2) = 1; - D2[0] = Dist.at(0,0); - D2[1] = Dist.at(0,1); - D2[2] = Dist.at(0,2); - D2[3] = Dist.at(0,3); - fs_read["Rotate_Matrix"] >> R; - fs_read["Translate_Matrix"] >> T; - fs_read["R1"] >> R1; - fs_read["R2"] >> R2; - fs_read["P1"] >> P1; - fs_read["P2"] >> P2; - fs_read["Q"] >> Q; - if(cameraMatrix.at(0,0) == 0 || cameraMatrix2.at(0,0) == 0) - { - QMessageBox::critical(this, "错误", "YAML文件格式错误", QMessageBox::Yes, QMessageBox::Yes); - return; - } - calibrate_flag = true; - } - } -} - -// 加载双目图像 -void HandEyeCalibration::receiveFromDialog(QString str) -{ - HiddenIntro(); - QStringList list = str.split(","); - QString left_src = list[0]; - QString right_src = list[1]; - chessboard_size = list[2].toDouble(); - QDir dir(left_src); - dir.setFilter(QDir::Files | QDir::NoSymLinks); - QStringList filters; - filters << "*.png" << "*.jpg" << "*.jpeg"; - dir.setNameFilters(filters); - QStringList imagesList = dir.entryList(); - if(ui->double_camera->isChecked()) - { - if(imagesList.length() <= 3) - { - QMessageBox::critical(NULL, "错误", "至少需要四组图片", QMessageBox::Yes, QMessageBox::Yes); - return; - } - QProgressDialog *dialog = new QProgressDialog(tr("检测角点..."),tr("取消"),0,imagesList.length(),this); - dialog->setWindowModality(Qt::WindowModal); - dialog->setMinimumDuration(0); - dialog->setWindowTitle("请稍候"); - dialog->setValue(0); - QFont font = dialog->font(); - font.setPixelSize(12); - dialog->setFont(font); - dialog->show(); - for(int i = 0; i < imagesList.length(); i++) - { - QString left_file_name = left_src + "/" + imagesList[i]; - QString right_file_name = right_src + "/" + imagesList[i]; - cv::Mat right_image = cv::imread(right_file_name.toStdString()); - if(right_image.empty()) - { - dialog->setValue(i+1); - continue; - } - cv::Mat left_image = cv::imread(left_file_name.toStdString()); - if(left_image.cols != right_image.cols || left_image.rows != right_image.rows) - { - QMessageBox::critical(NULL, "错误", "左右相机图片尺寸不一致", QMessageBox::Yes, QMessageBox::Yes); - delete dialog; - return; - } - find_corner_thread_img = left_image; - thread_done = false; - findcorner_thread->start(); - while(thread_done == false) - { - if(dialog->wasCanceled()) - { - DealThreadDone(); - delete dialog; - return; - } - // 强制Windows消息循环,防止程序出现未响应情况 - QCoreApplication::processEvents(); - } - struct Chessboarder_t left_chess = find_corner_thread_chessboard; - // 如果检测到多个棋盘,则剔除该图片 - if(left_chess.chessboard.size() != 1) - { - dialog->setValue(i+1); - continue; - } - find_corner_thread_img = right_image; - thread_done = false; - findcorner_thread->start(); - while(thread_done == false) - { - if(dialog->wasCanceled()) - { - DealThreadDone(); - delete dialog; - return; - } - QCoreApplication::processEvents(); - } - struct Chessboarder_t right_chess = find_corner_thread_chessboard; - // 如果检测到多个棋盘,则剔除该图片 - if(right_chess.chessboard.size() != 1) - { - dialog->setValue(i+1); - continue; - } - // 如果左右图片检测到的棋盘尺寸不对,则剔除该组图片 - if(left_chess.chessboard[0].rows != right_chess.chessboard[0].rows || - left_chess.chessboard[0].cols != right_chess.chessboard[0].cols) - { - dialog->setValue(i+1); - continue; - } - // 缓存图片及角点 - struct Stereo_Img_t img_; - img_.left_img = left_image; - img_.right_img = right_image; - img_.left_file_name = imagesList[i]; - img_.right_file_name = imagesList[i]; - for (unsigned int j = 0; j < left_chess.chessboard.size(); j++) - { - for (int u = 0; u < left_chess.chessboard[j].rows; u++) - { - for (int v = 0; v < left_chess.chessboard[j].cols; v++) - { - img_.left_img_points.push_back(left_chess.corners.p[left_chess.chessboard[j].at(u, v)]); - img_.world_points.push_back(cv::Point3f(u*chessboard_size, v*chessboard_size, 0)); - img_.right_img_points.push_back(right_chess.corners.p[right_chess.chessboard[j].at(u, v)]); - } - } - } - stereo_imgs.push_back(img_); - img_size = left_image.size(); - int img_height = left_image.rows*90/left_image.cols; - cv::resize(left_image, left_image, cv::Size(90, img_height)); - cv::resize(right_image, right_image, cv::Size(90, img_height)); - cv::Mat combian_img = cv::Mat::zeros(cv::Size(180, img_height), CV_8UC3); - cv::Mat new_roi = combian_img(cv::Rect(0, 0, 90, img_height)); - left_image.convertTo(new_roi, new_roi.type()); - new_roi = combian_img(cv::Rect(90, 0, 90, img_height)); - right_image.convertTo(new_roi, new_roi.type()); - QPixmap pixmap = QPixmap::fromImage(Mat2QImage(combian_img)); - QListWidgetItem* temp = new QListWidgetItem(); - temp->setSizeHint(QSize(220, img_height)); - temp->setIcon(QIcon(pixmap)); - temp->setText(imagesList[i]); - ui->listWidget->addItem(temp); - ui->listWidget->setIconSize(QSize(180, img_height)); - dialog->setValue(i+1); - } - delete dialog; - } - else if(ui->double_undistort->isChecked()) - { - for(int i = 0; i < imagesList.length(); i++) - { - QString left_file_name = left_src + "/" + imagesList[i]; - QString right_file_name = right_src + "/" + imagesList[i]; - cv::Mat right_image = cv::imread(right_file_name.toStdString()); - if(right_image.empty()) - { - continue; - } - cv::Mat left_image = cv::imread(left_file_name.toStdString()); - if(left_image.cols != right_image.cols || left_image.rows != right_image.rows) - { - QMessageBox::critical(NULL, "错误", "左右相机图片尺寸不一致", QMessageBox::Yes, QMessageBox::Yes); - return; - } - struct Stereo_Img_t img_; - img_.left_img = left_image; - img_.right_img = right_image; - img_.left_file_name = imagesList[i]; - img_.right_file_name = imagesList[i]; - stereo_imgs.push_back(img_); - img_size = left_image.size(); - int img_height = left_image.rows*90/left_image.cols; - cv::resize(left_image, left_image, cv::Size(90, img_height)); - cv::resize(right_image, right_image, cv::Size(90, img_height)); - cv::Mat combian_img = cv::Mat::zeros(cv::Size(180, img_height), CV_8UC3); - cv::Mat new_roi = combian_img(cv::Rect(0, 0, 90, img_height)); - left_image.convertTo(new_roi, new_roi.type()); - new_roi = combian_img(cv::Rect(90, 0, 90, img_height)); - right_image.convertTo(new_roi, new_roi.type()); - QPixmap pixmap = QPixmap::fromImage(Mat2QImage(combian_img)); - QListWidgetItem* temp = new QListWidgetItem(); - temp->setSizeHint(QSize(220, img_height)); - temp->setIcon(QIcon(pixmap)); - temp->setText(imagesList[i]); - ui->listWidget->addItem(temp); - ui->listWidget->setIconSize(QSize(180, img_height)); - } - } -} - -// 鱼眼相机切换 -void HandEyeCalibration::fisheyeModeSwitch(int state) -{ - fisheye_flag = state==0 ? false : true; - if(fisheye_flag == true) - { - ui->k_2->setDisabled(true); - ui->k_3->setDisabled(true); - ui->tangential->setDisabled(true); - } - else - { - ui->k_2->setEnabled(true); - ui->k_3->setEnabled(true); - ui->tangential->setEnabled(true); - } -} - -// 畸变矫正切换 -void HandEyeCalibration::distortModeSwitch() -{ - if(ui->single_undistort->isChecked() || ui->double_undistort->isChecked()) - { - if(calibrate_flag == false) - { - chooseYaml = new choose_yaml(); - chooseYaml->setWindowTitle("选择相机参数文件"); - QFont font = chooseYaml->font(); - font.setPixelSize(12); - chooseYaml->setFont(font); - chooseYaml->show(); - connect(chooseYaml, SIGNAL(SendSignal(QString)), this, SLOT(receiveYamlPath(QString))); - return; - } - } - static int cnt = 0; - cnt++; - if(cnt%2 == 1) - { - distort_flag = true; - } - else - { - distort_flag = false; - } - int id = ui->listWidget->selectionModel()->selectedIndexes()[0].row(); - chooseImage(ui->listWidget->item(id), ui->listWidget->item(id)); -} - -// Mat格式转QImage格式 -QImage HandEyeCalibration::Mat2QImage(cv::Mat cvImg) -{ - QImage qImg; - if(cvImg.channels()==3) - { - cv::cvtColor(cvImg,cvImg,CV_BGR2RGB); - qImg =QImage((const unsigned char*)(cvImg.data), - cvImg.cols, cvImg.rows, - cvImg.cols*cvImg.channels(), - QImage::Format_RGB888); - } - else if(cvImg.channels()==1) - { - qImg =QImage((const unsigned char*)(cvImg.data), - cvImg.cols,cvImg.rows, - cvImg.cols*cvImg.channels(), - QImage::Format_Indexed8); - } - else - { - qImg =QImage((const unsigned char*)(cvImg.data), - cvImg.cols,cvImg.rows, - cvImg.cols*cvImg.channels(), - QImage::Format_RGB888); - } - return qImg; -} - -// 单目相机图片加载,逻辑同双目相机图片加载 -void HandEyeCalibration::addImage() -{ - HiddenIntro(); - if(ui->double_camera->isChecked() || ui->double_undistort->isChecked()) - { - d = new choose_two_dir(); - d->setWindowTitle("选择图片文件夹"); - QFont font = d->font(); - font.setPixelSize(12); - d->setFont(font); - d->show(); - connect(d, SIGNAL(SendSignal(QString)), this, SLOT(receiveFromDialog(QString))); - return; - } - else if(ui->single_camera->isChecked()) - { - if(chessboard_size == 0) - { - bool ok; - chessboard_size = QInputDialog::getDouble(this,tr("角点间距"),tr("请输入角点间距(mm)"),20,0,1000,2,&ok); - if(!ok) - { - chessboard_size = 0; - return; - } - } - QStringList path_list = QFileDialog::getOpenFileNames(this, tr("选择图片"), tr("./"), tr("图片文件(*.jpg *.png *.pgm);;所有文件(*.*);")); - QProgressDialog *dialog = new QProgressDialog(tr("检测角点..."),tr("取消"),0,path_list.size(),this); - dialog->setWindowModality(Qt::WindowModal); - dialog->setMinimumDuration(0); - dialog->setWindowTitle("请稍候"); - dialog->setValue(0); - QFont font = dialog->font(); - font.setPixelSize(12); - dialog->setFont(font); - dialog->show(); - for(int i = 0; i < path_list.size(); i++) - { - QFileInfo file = QFileInfo(path_list[i]); - QString file_name = file.fileName(); - cv::Mat img = cv::imread(path_list[i].toStdString()); - find_corner_thread_img = img; - thread_done = false; - findcorner_thread->start(); - while(thread_done == false) - { - if(dialog->wasCanceled()) - { - DealThreadDone(); - delete dialog; - return; - } - QCoreApplication::processEvents(); - } - struct Chessboarder_t chess = find_corner_thread_chessboard; - if(chess.chessboard.size() != 1) - { - dialog->setValue(i+1); - continue; - } - struct Img_t img_; - img_.img = img; - img_.file_name = file_name; - std::vector img_p; - std::vector world_p; - for (unsigned int j = 0; j < chess.chessboard.size(); j++) - { - for (int u = 0; u < chess.chessboard[j].rows; u++) - { - for (int v = 0; v < chess.chessboard[j].cols; v++) - { - img_p.push_back(chess.corners.p[chess.chessboard[j].at(u, v)]); - world_p.push_back(cv::Point3f(u*chessboard_size, v*chessboard_size, 0)); - } - } - } - img_.img_points = img_p; - img_.world_points = world_p; - imgs.push_back(img_); - img_size = img.size(); - int img_height = img.rows*124/img.cols; - cv::resize(img, img, cv::Size(124, img_height)); - QPixmap pixmap = QPixmap::fromImage(Mat2QImage(img)); - QListWidgetItem* temp = new QListWidgetItem(); - temp->setSizeHint(QSize(200, img_height)); - temp->setIcon(QIcon(pixmap)); - temp->setText(file_name); - ui->listWidget->addItem(temp); - ui->listWidget->setIconSize(QSize(124, img_height)); - dialog->setValue(i+1); - } - delete dialog; - } - else if(ui->single_undistort->isChecked()) - { - QStringList path_list = QFileDialog::getOpenFileNames(this, tr("选择图片"), tr("./"), tr("图片文件(*.jpg *.png *.pgm);;所有文件(*.*);")); - for(int i = 0; i < path_list.size(); i++) - { - QFileInfo file = QFileInfo(path_list[i]); - QString file_name = file.fileName(); - cv::Mat img = cv::imread(path_list[i].toStdString()); - img_size = img.size(); - Img_t single_img; - single_img.img = img; - single_img.file_name = file_name; - imgs.push_back(single_img); - int img_height = img.rows*124/img.cols; - cv::resize(img, img, cv::Size(124, img_height)); - QPixmap pixmap = QPixmap::fromImage(Mat2QImage(img)); - QListWidgetItem* temp = new QListWidgetItem(); - temp->setSizeHint(QSize(200, img_height)); - temp->setIcon(QIcon(pixmap)); - temp->setText(file_name); - ui->listWidget->addItem(temp); - ui->listWidget->setIconSize(QSize(124, img_height)); - } - } -} - -// 删除选中的图片 -void HandEyeCalibration::deleteImage() -{ - std::vector delete_idx; - foreach(QModelIndex index,ui->listWidget->selectionModel()->selectedIndexes()){ - delete_idx.push_back(index.row()); - } - if(delete_idx.size() == 0) - return; - std::sort(delete_idx.begin(), delete_idx.end()); - disconnect(ui->listWidget, SIGNAL(currentItemChanged(QListWidgetItem*, QListWidgetItem*)), this, SLOT(chooseImage(QListWidgetItem*, QListWidgetItem*))); - for(int i = delete_idx.size()-1; i >= 0; i--) - { - ui->listWidget->takeItem(delete_idx[i]); - if(ui->double_camera->isChecked()) - stereo_imgs.erase(stereo_imgs.begin()+delete_idx[i]); - else - imgs.erase(imgs.begin()+delete_idx[i]); - } - connect(ui->listWidget, SIGNAL(currentItemChanged(QListWidgetItem*, QListWidgetItem*)), this, SLOT(chooseImage(QListWidgetItem*, QListWidgetItem*))); - // 如果图片全部删除,则重新显示文字引导 - if(ui->listWidget->count() == 0) - ShowIntro(); -} - -// 相机标定 -void HandEyeCalibration::calibrate() -{ - if(chessboard_size == 0) - { - bool ok; - chessboard_size = QInputDialog::getDouble(this,tr("角点间距"),tr("请输入角点间距(mm)"),20,0,1000,2,&ok); - if(!ok) - { - chessboard_size = 0; - return; - } - } - if(!ui->double_camera->isChecked()) - { - // 单目标定 - if(imgs.size() <= 3) - { - QMessageBox::critical(NULL, "错误", "至少需要四张图片", QMessageBox::Yes, QMessageBox::Yes); - return; - } - std::vector> img_points; - std::vector> world_points; - std::vector tvecsMat; - std::vector rvecsMat; - std::vector > imagePoints; - std::vector > objectPoints; - std::vector rvecs; - std::vector tvecs; - for (unsigned int j = 0; j < imgs.size(); j++) - { - img_points.push_back(imgs[j].img_points); - world_points.push_back(imgs[j].world_points); - std::vector img_p; - std::vector obj_p; - for(unsigned int i = 0; i < imgs[j].img_points.size(); i++) - { - img_p.push_back(imgs[j].img_points[i]); - obj_p.push_back(imgs[j].world_points[i]); - } - imagePoints.push_back(img_p); - objectPoints.push_back(obj_p); - } - cameraMatrix = cv::Mat::zeros(cv::Size(3, 3), CV_32F); - if(fisheye_flag == false) - distCoefficients = cv::Mat::zeros(cv::Size(5, 1), CV_32F); - - if(fisheye_flag == false) - { - int flag = 0; - if(!ui->tangential->isChecked()) - flag |= CV_CALIB_ZERO_TANGENT_DIST; - if(!ui->k_3->isChecked()) - flag |= CV_CALIB_FIX_K3; - cv::calibrateCamera(world_points, img_points, img_size, cameraMatrix, distCoefficients, rvecsMat, tvecsMat, flag); - } - else - { - int flag = 0; - flag |= cv::fisheye::CALIB_RECOMPUTE_EXTRINSIC; - flag |= cv::fisheye::CALIB_FIX_SKEW; - try { - cv::fisheye::calibrate(objectPoints, imagePoints, img_size, K, D, rvecs, tvecs, flag); - } catch (cv::Exception& e) { - QMessageBox::critical(NULL, "错误", "请采集更多方位的图片或者检查角点识别!", QMessageBox::Yes, QMessageBox::Yes); - imgs.clear(); - ui->listWidget->clear(); - return; - } - } - for(unsigned int i = 0; i < imgs.size(); i++) - { - if(fisheye_flag == false) - { - imgs[i].tvec = tvecsMat[i]; - imgs[i].rvec = rvecsMat[i]; - } - else - { - imgs[i].fish_tvec = tvecs[i]; - imgs[i].fish_rvec = rvecs[i]; - } - } - // 评估标定结果 - std::vector error; - for(unsigned int i = 0; i < imgs.size(); i++) - { - std::vector world_p = imgs[i].world_points; - std::vector img_p = imgs[i].img_points, reproject_img_p; - std::vector fisheye_reproject_p; - if(fisheye_flag == false) - projectPoints(world_p, rvecsMat[i], tvecsMat[i], cameraMatrix, distCoefficients, reproject_img_p); - else - cv::fisheye::projectPoints(objectPoints[i], fisheye_reproject_p, rvecs[i], tvecs[i], K, D); - float err = 0; - for (unsigned int j = 0; j < img_p.size(); j++) - { - if(fisheye_flag == false) - err += sqrt((img_p[j].x-reproject_img_p[j].x)*(img_p[j].x-reproject_img_p[j].x)+ - (img_p[j].y-reproject_img_p[j].y)*(img_p[j].y-reproject_img_p[j].y)); - else - err += sqrt((img_p[j].x-fisheye_reproject_p[j].x)*(img_p[j].x-fisheye_reproject_p[j].x)+ - (img_p[j].y-fisheye_reproject_p[j].y)*(img_p[j].y-fisheye_reproject_p[j].y)); - } - error.push_back(err/img_p.size()); - } - int max_idx = max_element(error.begin(), error.end()) - error.begin(); - float max_error = error[max_idx]; - int width = 240 / imgs.size(); - cv::Mat error_plot = cv::Mat(260, 320, CV_32FC3, cv::Scalar(255,255,255)); - cv::rectangle(error_plot, cv::Rect(40, 20, 240, 200), cv::Scalar(0, 0, 0),1, cv::LINE_8,0); - cv::putText(error_plot, "0", cv::Point(20, 220), cv::FONT_HERSHEY_SIMPLEX, 0.3, cv::Scalar(0, 0, 0), 1, 8, 0); - char *chCode; - chCode = new(std::nothrow)char[20]; - sprintf(chCode, "%.2lf", max_error*200/195); - std::string strCode(chCode); - delete []chCode; - cv::putText(error_plot, strCode, cv::Point(10, 20), cv::FONT_HERSHEY_SIMPLEX, 0.3, cv::Scalar(0, 0, 0), 1, 8, 0); - error1 = 0; - for(unsigned int i = 0; i < imgs.size(); i++) - { - error1 += error[i]; - int height = 195*error[i]/max_error; - cv::rectangle(error_plot, cv::Rect(i*width+41, 220-height, width-2, height), cv::Scalar(255,0,0), -1, cv::LINE_8,0); - cv::putText(error_plot, std::to_string(i), cv::Point(i*width+40, 240), cv::FONT_HERSHEY_SIMPLEX, 0.3, cv::Scalar(0, 0, 0), 1, 8, 0); - } - error1 /= imgs.size(); - cv::imshow("error", error_plot); - } - else - { - // 双目标定 - if(stereo_imgs.size() <= 3) - { - QMessageBox::critical(NULL, "错误", "至少需要四组图片", QMessageBox::Yes, QMessageBox::Yes); - return; - } - std::vector> left_img_points; - std::vector> right_img_points; - std::vector> world_points; - std::vector left_tvecsMat; - std::vector left_rvecsMat; - std::vector right_tvecsMat; - std::vector right_rvecsMat; - std::vector > left_imagePoints; - std::vector > right_imagePoints; - std::vector > objectPoints; - std::vector left_rvecs; - std::vector left_tvecs; - std::vector right_rvecs; - std::vector right_tvecs; - for (unsigned int j = 0; j < stereo_imgs.size(); j++) - { - left_img_points.push_back(stereo_imgs[j].left_img_points); - right_img_points.push_back(stereo_imgs[j].right_img_points); - world_points.push_back(stereo_imgs[j].world_points); - std::vector img_p, img_p_; - std::vector obj_p; - for(unsigned int i = 0; i < stereo_imgs[j].left_img_points.size(); i++) - { - img_p.push_back(stereo_imgs[j].left_img_points[i]); - img_p_.push_back(stereo_imgs[j].right_img_points[i]); - obj_p.push_back(stereo_imgs[j].world_points[i]); - } - left_imagePoints.push_back(img_p); - right_imagePoints.push_back(img_p_); - objectPoints.push_back(obj_p); - } - cameraMatrix = cv::Mat::zeros(cv::Size(3, 3), CV_32F); - cameraMatrix2 = cv::Mat::zeros(cv::Size(3, 3), CV_32F); - if(fisheye_flag == false) - { - distCoefficients = cv::Mat::zeros(cv::Size(5, 1), CV_32F); - distCoefficients2 = cv::Mat::zeros(cv::Size(5, 1), CV_32F); - } - // 开始标定 - if(fisheye_flag == false) - { - int flag = 0; - if(!ui->tangential->isChecked()) - flag |= CV_CALIB_ZERO_TANGENT_DIST; - if(!ui->k_3->isChecked()) - flag |= CV_CALIB_FIX_K3; - cv::calibrateCamera(world_points, left_img_points, img_size, cameraMatrix, distCoefficients, left_rvecsMat, left_tvecsMat, flag); - cv::calibrateCamera(world_points, right_img_points, img_size, cameraMatrix2, distCoefficients2, right_rvecsMat, right_tvecsMat, flag); - } - else - { - int flag = 0; - flag |= cv::fisheye::CALIB_RECOMPUTE_EXTRINSIC; - flag |= cv::fisheye::CALIB_FIX_SKEW; - cv::fisheye::calibrate(objectPoints, left_imagePoints, img_size, K, D, left_rvecs, left_tvecs, flag); - cv::fisheye::calibrate(objectPoints, right_imagePoints, img_size, K2, D2, right_rvecs, right_tvecs, flag); - } - for(unsigned int i = 0; i < stereo_imgs.size(); i++) - { - if(fisheye_flag == false) - { - stereo_imgs[i].left_tvec = left_tvecsMat[i]; - stereo_imgs[i].left_rvec = left_rvecsMat[i]; - stereo_imgs[i].right_tvec = right_tvecsMat[i]; - stereo_imgs[i].right_rvec = right_rvecsMat[i]; - } - else - { - stereo_imgs[i].left_fish_tvec = left_tvecs[i]; - stereo_imgs[i].left_fish_rvec = left_rvecs[i]; - stereo_imgs[i].right_fish_tvec = right_tvecs[i]; - stereo_imgs[i].right_fish_rvec = right_rvecs[i]; - } - } - // 评估标定结果 - std::vector left_error, right_error; - for(unsigned int i = 0; i < stereo_imgs.size(); i++) - { - std::vector world_p = stereo_imgs[i].world_points; - std::vector left_img_p = stereo_imgs[i].left_img_points, right_img_p = stereo_imgs[i].right_img_points; - std::vector left_reproject_img_p, right_reproject_img_p; - std::vector left_fisheye_reproject_p, right_fisheye_reproject_p; - if(fisheye_flag == false) - { - projectPoints(world_p, left_rvecsMat[i], left_tvecsMat[i], cameraMatrix, distCoefficients, left_reproject_img_p); - projectPoints(world_p, right_rvecsMat[i], right_tvecsMat[i], cameraMatrix2, distCoefficients2, right_reproject_img_p); - } - else - { - cv::fisheye::projectPoints(objectPoints[i], left_fisheye_reproject_p, left_rvecs[i], left_tvecs[i], K, D); - cv::fisheye::projectPoints(objectPoints[i], right_fisheye_reproject_p, right_rvecs[i], right_tvecs[i], K2, D2); - } - float left_err = 0, right_err = 0; - for (unsigned int j = 0; j < world_p.size(); j++) - { - if(fisheye_flag == false) - { - left_err += sqrt((left_img_p[j].x-left_reproject_img_p[j].x)*(left_img_p[j].x-left_reproject_img_p[j].x)+ - (left_img_p[j].y-left_reproject_img_p[j].y)*(left_img_p[j].y-left_reproject_img_p[j].y)); - right_err += sqrt((right_img_p[j].x-right_reproject_img_p[j].x)*(right_img_p[j].x-right_reproject_img_p[j].x)+ - (right_img_p[j].y-right_reproject_img_p[j].y)*(right_img_p[j].y-right_reproject_img_p[j].y)); - } - else - { - left_err += sqrt((left_img_p[j].x-left_fisheye_reproject_p[j].x)*(left_img_p[j].x-left_fisheye_reproject_p[j].x)+ - (left_img_p[j].y-left_fisheye_reproject_p[j].y)*(left_img_p[j].y-left_fisheye_reproject_p[j].y)); - right_err += sqrt((right_img_p[j].x-right_fisheye_reproject_p[j].x)*(right_img_p[j].x-right_fisheye_reproject_p[j].x)+ - (right_img_p[j].y-right_fisheye_reproject_p[j].y)*(right_img_p[j].y-right_fisheye_reproject_p[j].y)); - } - } - left_error.push_back(left_err/world_p.size()); - right_error.push_back(right_err/world_p.size()); - } - int max_idx = max_element(left_error.begin(), left_error.end()) - left_error.begin(); - float max_error = left_error[max_idx]; - max_idx = max_element(right_error.begin(), right_error.end()) - right_error.begin(); - max_error = std::max(max_error, right_error[max_idx]); - int width = 480 / stereo_imgs.size(); - cv::Mat error_plot = cv::Mat(260, 560, CV_32FC3, cv::Scalar(255,255,255)); - cv::rectangle(error_plot, cv::Rect(40, 20, 480, 200), cv::Scalar(0, 0, 0),1, cv::LINE_8,0); - cv::putText(error_plot, "0", cv::Point(20, 220), cv::FONT_HERSHEY_SIMPLEX, 0.3, cv::Scalar(0, 0, 0), 1, 8, 0); - char *chCode; - chCode = new(std::nothrow)char[20]; - sprintf(chCode, "%.2lf", max_error*200/195); - std::string strCode(chCode); - delete []chCode; - cv::putText(error_plot, strCode, cv::Point(10, 20), cv::FONT_HERSHEY_SIMPLEX, 0.3, cv::Scalar(0, 0, 0), 1, 8, 0); - error1 = 0; error2 = 0; - for(unsigned int i = 0; i < stereo_imgs.size(); i++) - { - error1 += left_error[i]; - error2 += right_error[i]; - int height = 195*left_error[i]/max_error; - cv::rectangle(error_plot, cv::Rect(i*width+43, 220-height, width/2-4, height), cv::Scalar(255,0,0), -1, cv::LINE_8,0); - height = 195*right_error[i]/max_error; - cv::rectangle(error_plot, cv::Rect(i*width+41+width/2, 220-height, width/2-4, height), cv::Scalar(0,255,0), -1, cv::LINE_8,0); - cv::putText(error_plot, std::to_string(i), cv::Point(i*width+40+width/2-2, 240), cv::FONT_HERSHEY_SIMPLEX, 0.3, cv::Scalar(0, 0, 0), 1, 8, 0); - } - error1 /= stereo_imgs.size(); - error2 /= stereo_imgs.size(); - - if(fisheye_flag == false) - { - int flag = 0; - if(!ui->tangential->isChecked()) - flag |= CV_CALIB_ZERO_TANGENT_DIST; - if(!ui->k_3->isChecked()) - flag |= CV_CALIB_FIX_K3; - flag |= CV_CALIB_USE_INTRINSIC_GUESS; - cv::stereoCalibrate(world_points, left_img_points, right_img_points, - cameraMatrix, distCoefficients, - cameraMatrix2, distCoefficients2, - img_size, R, T, cv::noArray(), cv::noArray(), flag); - cv::stereoRectify(cameraMatrix, distCoefficients, - cameraMatrix2, distCoefficients2, - img_size, R, T, - R1, R2, P1, P2, Q,0,0, img_size); - } - else - { - int flag = 0; - flag |= cv::fisheye::CALIB_FIX_SKEW; - flag |= cv::fisheye::CALIB_USE_INTRINSIC_GUESS; - try { - cv::fisheye::stereoCalibrate(objectPoints, left_imagePoints, right_imagePoints, - K, D, - K2, D2, - img_size, R, T, flag); - } catch (cv::Exception& e) { - QMessageBox::critical(NULL, "错误", "请采集更多方位的图片!", QMessageBox::Yes, QMessageBox::Yes); - stereo_imgs.clear(); - ui->listWidget->clear(); - return; - } - - cv::fisheye::stereoRectify(K, D, - K2, D2, - img_size, R, T, - R1, R2, P1, P2, Q, 0, img_size); - } - D = D2; - cv::imshow("error", error_plot); - } - - // hand-eye calibration - std::vector cTo; - cv::Mat cRo_i, cPo_i; - cv::Mat cTc = cv::Mat::zeros(3, 3, CV_64F); - cTc.at(0, 1) = +1; - cTc.at(1, 0) = +1; - cTc.at(2, 2) = -1; - if (!ui->double_camera->isChecked()) // - { - for(unsigned int i = 0; i < imgs.size(); i++) - { - if(fisheye_flag == false) - { - cPo_i = imgs[i].tvec; - cv::Rodrigues(imgs[i].rvec, cRo_i); - } - else - { - cPo_i = imgs[i].fish_tvec; - cv::Rodrigues(imgs[i].fish_rvec, cRo_i); - } - - cv::Mat cTo_i = cv::Mat::zeros(cv::Size(4, 4), CV_64F); - for (int r = 0; r < 3; ++ r) { - for (int c = 0; c < 3; ++ c) { - cTo_i.at(r, c) = cRo_i.at(r, c); - } - cTo_i.at(r, 3) = cPo_i.at(r, 0) / 1000.0; - } - cTo_i.at(3, 3) = 1; - cTo.push_back(cTo_i); - } - } else { - for(unsigned int i = 0; i < stereo_imgs.size(); i++) - { - if(fisheye_flag == false) - { - cPo_i = stereo_imgs[i].left_tvec; - cv::Rodrigues(stereo_imgs[i].left_rvec, cRo_i); - } - else - { - cPo_i = stereo_imgs[i].left_fish_tvec; - cv::Rodrigues(stereo_imgs[i].left_fish_rvec, cRo_i); - } - - cv::Mat cTo_i = cv::Mat::zeros(cv::Size(4, 4), CV_64F); - for (int r = 0; r < 3; ++ r) { - for (int c = 0; c < 3; ++ c) { - cTo_i.at(r, c) = cRo_i.at(r, c); - } - cTo_i.at(r, 3) = cPo_i.at(r, 0) / 1000.0; - } - cTo_i.at(3, 3) = 1; - cTo.push_back(cTo_i); - } - } - - TSAIleastSquareCalibration::runCalibration(robot_poses, cTo, ui->k_eye_in_hand->isChecked(), extrinsicMatrix); - - calibrate_flag = true; -} - -// 导出标定参数 -void HandEyeCalibration::exportParam() -{ - if(calibrate_flag == false) - { - QMessageBox::critical(NULL, "错误", "请先标定相机", QMessageBox::Yes); - return; - } - QString strSaveName = QFileDialog::getSaveFileName(this,tr("保存的文件"),tr("calibration_param.yaml"),tr("yaml files(*.yaml)")); - if(strSaveName.isNull() || strSaveName.isEmpty() || strSaveName.length() == 0) - return; - cv::FileStorage fs_write(strSaveName.toStdString(), cv::FileStorage::WRITE); - if(ui->double_camera->isChecked()) - { - if(fisheye_flag == false) - { - fs_write << "left_camera_Matrix" << cameraMatrix << "left_camera_distCoeffs" << distCoefficients; - fs_write << "right_camera_Matrix" << cameraMatrix2 << "right_camera_distCoeffs" << distCoefficients2; - fs_write << "Rotate_Matrix" << R << "Translate_Matrix" << T; - fs_write << "R1" << R1 << "R2" << R2 << "P1" << P1 << "P2" << P2 << "Q" << Q; - } - else - { - cv::Mat camera_matrix = cv::Mat::zeros(cv::Size(3,3), CV_64F); - camera_matrix.at(0,0) = K(0,0); - camera_matrix.at(0,2) = K(0,2); - camera_matrix.at(1,1) = K(1,1); - camera_matrix.at(1,2) = K(1,2); - camera_matrix.at(2,2) = 1; - cv::Mat Dist = (cv::Mat_(1,4) << D[0], D[1], D[2], D[3]); - fs_write << "left_camera_Matrix" << camera_matrix << "left_camera_distCoeffs" << Dist; - camera_matrix.at(0,0) = K2(0,0); - camera_matrix.at(0,2) = K2(0,2); - camera_matrix.at(1,1) = K2(1,1); - camera_matrix.at(1,2) = K2(1,2); - Dist = (cv::Mat_(1,4) << D2[0], D2[1], D2[2], D2[3]); - fs_write << "right_camera_Matrix" << camera_matrix << "right_camera_distCoeffs" << Dist; - fs_write << "Rotate_Matrix" << R << "Translate_Matrix" << T; - fs_write << "R1" << R1 << "R2" << R2 << "P1" << P1 << "P2" << P2 << "Q" << Q; - } - fs_write << "left_average_reprojection_err" << error1 << "right_average_reprojection_err" << error2; - } - else - { - if(fisheye_flag == false) - fs_write << "cameraMatrix" << cameraMatrix << "distCoeffs" << distCoefficients; - else - { - cv::Mat camera_matrix = cv::Mat::zeros(cv::Size(3,3), CV_64F); - camera_matrix.at(0,0) = K(0,0); - camera_matrix.at(0,2) = K(0,2); - camera_matrix.at(1,1) = K(1,1); - camera_matrix.at(1,2) = K(1,2); - camera_matrix.at(2,2) = 1; - cv::Mat Dist = (cv::Mat_(1,4) << D[0], D[1], D[2], D[3]); - fs_write << "cameraMatrix" << camera_matrix << "disCoeffs" << Dist; - } - fs_write << "average_reprojection_err" << error1; - } - - fs_write << "extrinsicMatrix" << extrinsicMatrix; - - fs_write.release(); -} - - -// 单目相机图片加载,逻辑同双目相机图片加载 -void HandEyeCalibration::addRobot() -{ - robot_poses.clear(); - QStringList path_list = QFileDialog::getOpenFileNames(this, tr("选择文件"), tr("./"), tr("文本文件(*.txt);;所有文件(*.*);")); - for(int i = 0; i < path_list.size(); i++) - { - cv::Mat robot_pose = cv::Mat::zeros(cv::Size(4, 4), CV_64F); - QFileInfo file = QFileInfo(path_list[i]); - QString file_name = file.fileName(); - std::ifstream in(path_list[i].toStdString()); - if (in.is_open()) - { - in >> robot_pose.at(0, 0) >> robot_pose.at(0, 1) >> robot_pose.at(0, 2) >> robot_pose.at(0, 3) \ - >> robot_pose.at(1, 0) >> robot_pose.at(1, 1) >> robot_pose.at(1, 2) >> robot_pose.at(1, 3) \ - >> robot_pose.at(2, 0) >> robot_pose.at(2, 1) >> robot_pose.at(2, 2) >> robot_pose.at(2, 3) \ - >> robot_pose.at(3, 0) >> robot_pose.at(3, 1) >> robot_pose.at(3, 2) >> robot_pose.at(3, 3); - } - in.close(); - robot_poses.push_back(robot_pose); - } -} diff --git a/hand_eye_calibration/HandEyeCalibration.h b/hand_eye_calibration/HandEyeCalibration.h deleted file mode 100644 index 0d538aa..0000000 --- a/hand_eye_calibration/HandEyeCalibration.h +++ /dev/null @@ -1,103 +0,0 @@ -#ifndef HandEyeCalibration_H -#define HandEyeCalibration_H - -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include "../camera_calibration/CameraCalibration.h" -#include "../camera_calibration/findCorner.h" -#include "../camera_calibration/chessboard.h" -#include "../camera_calibration/choose_two_dir.h" -#ifdef Q_OS_LINUX -#include "../camera_calibration/single_capture_linux.h" -#elif defined(Q_OS_WIN32) -#include "../camera_calibration/single_capture.h" -#endif - -#ifdef Q_OS_LINUX -#include "../camera_calibration/double_capture_linux.h" -#elif defined(Q_OS_WIN32) -#include "../camera_calibration/double_capture.h" -#endif -#include "../camera_calibration/choose_yaml.h" - -QT_BEGIN_NAMESPACE -namespace Ui { class HandEyeCalibration; } -QT_END_NAMESPACE - -extern cv::Mat find_corner_thread_img; -extern struct Chessboarder_t find_corner_thread_chessboard; - -class HandEyeCalibration : public QMainWindow -{ - Q_OBJECT - -public: - HandEyeCalibration(QWidget *parent = nullptr); - ~HandEyeCalibration(); - -private slots: - void addRobot(); - void addImage(); - void deleteImage(); - void calibrate(); - void fisheyeModeSwitch(int state); - void exportParam(); - void distortModeSwitch(); - void receiveFromDialog(QString str); - void receiveYamlPath(QString str); - void chooseImage(QListWidgetItem* item, QListWidgetItem*); - void reset(); - void undistortReset(); - void saveUndistort(); - void OpenCamera(); - void DealThreadDone(); - -private: - Ui::HandEyeCalibration *ui; - choose_two_dir *d; - //choose_robot_dir *d_robot; - choose_yaml *chooseYaml; -#ifdef Q_OS_LINUX - single_capture_linux *single_c; - double_capture_linux *double_c; -#elif defined(Q_OS_WIN32) - single_capture *single_c; - double_capture *double_c; -#endif - QImage Mat2QImage(cv::Mat cvImg); - void ShowIntro(); - void HiddenIntro(); - QAction *saveImage; - std::vector robot_poses; - std::vector imgs; - std::vector stereo_imgs; - double chessboard_size; - cv::Mat extrinsicMatrix; - cv::Mat cameraMatrix; - cv::Mat distCoefficients; - cv::Matx33d K; - cv::Vec4d D; - cv::Mat cameraMatrix2; - cv::Mat distCoefficients2; - double error1, error2; - cv::Matx33d K2; - cv::Vec4d D2; - cv::Size img_size; - bool fisheye_flag = false; - bool calibrate_flag = false; - bool distort_flag = false; - cv::Mat R, T, R1, R2, P1, P2, Q; - FindcornerThread *findcorner_thread; - bool thread_done = false; -}; -#endif // HandEyeCalibration_H diff --git a/hand_eye_calibration/HandEyeCalibration.ui b/hand_eye_calibration/HandEyeCalibration.ui deleted file mode 100644 index 6391193..0000000 --- a/hand_eye_calibration/HandEyeCalibration.ui +++ /dev/null @@ -1,633 +0,0 @@ - - - HandEyeCalibration - - - - 0 - 0 - 1087 - 635 - - - - 相机标定 - - - - :/icon/camera.png:/icon/camera.png - - - - - - 0 - 0 - 221 - 80 - - - - - 微软雅黑 - - - - FILE - - - Qt::AlignCenter - - - - - 10 - 15 - 60 - 40 - - - - PointingHandCursor - - - - res/AddImage.png - :/icon/AddImage.pngres/AddImage.png - - - - 50 - 50 - - - - - - - 80 - 15 - 60 - 60 - - - - PointingHandCursor - - - - - - - :/icon/delete.png - :/icon/delete.png:/icon/delete.png - - - - 35 - 35 - - - - - - - 10 - 50 - 60 - 25 - - - - - 8 - - - - PointingHandCursor - - - 添加 - - - - - - 150 - 15 - 60 - 40 - - - - PointingHandCursor - - - - :/icon/robot.png - :/icon/robot.png:/icon/robot.png - - - - - - 150 - 50 - 60 - 25 - - - - - 8 - - - - PointingHandCursor - - - 添加 - - - - - - - 230 - 0 - 361 - 80 - - - - - 微软雅黑 - - - - OPTIONS - - - Qt::AlignCenter - - - - - 10 - 20 - 100 - 16 - - - - 2 Coefficients - - - - - true - - - - 10 - 50 - 100 - 16 - - - - 3 Coefficients - - - false - - - - - - 130 - 20 - 100 - 16 - - - - Tangential - - - true - - - - - - 130 - 50 - 101 - 16 - - - - Fisheye - - - - - - 240 - 20 - 121 - 21 - - - - eye-in-hand - - - - - true - - - - 240 - 50 - 121 - 21 - - - - eye-to-hand - - - true - - - - - - - 620 - 0 - 80 - 80 - - - - - 微软雅黑 - - - - CALIBRATE - - - Qt::AlignCenter - - - - - 10 - 15 - 60 - 60 - - - - PointingHandCursor - - - - - - - :/icon/play.png - :/icon/play.png:/icon/play.png - - - - 48 - 48 - - - - - - - - 700 - 0 - 80 - 80 - - - - - 微软雅黑 - - - - EXPORT - - - Qt::AlignCenter - - - - - 10 - 15 - 60 - 60 - - - - PointingHandCursor - - - - - - - :/icon/yes.png - res/yes.png:/icon/yes.png - - - - 48 - 48 - - - - - - - - 0 - 80 - 250 - 520 - - - - - 130 - 100 - - - - - 500000 - 500000 - - - - true - - - - - 0 - 0 - 248 - 518 - - - - - 130 - 150 - - - - - 248 - 16777215 - - - - - - 0 - 0 - 248 - 518 - - - - - 248 - 518 - - - - - 248 - 16777215 - - - - - - - 0 - 0 - 248 - 100 - - - - - 微软雅黑 - - - - - - - Qt::AlignLeading|Qt::AlignLeft|Qt::AlignTop - - - - - - - - 260 - 80 - 811 - 520 - - - - - 微软雅黑 - - - - Image - - - - - 90 - 15 - 651 - 495 - - - - - - - Qt::AlignCenter - - - - - - - 780 - 0 - 80 - 80 - - - - - 微软雅黑 - - - - UNDISTORT - - - Qt::AlignCenter - - - - - 10 - 15 - 60 - 60 - - - - PointingHandCursor - - - - - - - :/icon/undistort.png - :/icon/undistort.png:/icon/undistort.png - - - - 40 - 40 - - - - - - - - 860 - 0 - 211 - 80 - - - - - 微软雅黑 - - - - MODE - - - Qt::AlignCenter - - - - - 10 - 20 - 85 - 16 - - - - 单目标定 - - - true - - - - - - 10 - 50 - 85 - 16 - - - - 双目标定 - - - - - - 110 - 50 - 89 - 16 - - - - 双目纠正 - - - - - - 110 - 20 - 89 - 16 - - - - 单目纠正 - - - - - - - - 0 - 0 - 1087 - 26 - - - - - 菜单 - - - - - - - - - - diff --git a/hand_eye_calibration/README.md b/hand_eye_calibration/README.md deleted file mode 100644 index fea3819..0000000 --- a/hand_eye_calibration/README.md +++ /dev/null @@ -1,23 +0,0 @@ -# hand_eye_calibration -机器人和相机的手眼机标定程序 - -基于Qt和OpenCV开发,标定算法来源"A New Technique for Fully Autonomous and Efficient 3D Robotics Hand-Eye Calibration, Tsai, R.Y. and Lenz, R.K" - -## 功能列表: -* 支持手眼在眼标定(Eye-in-Hand) -* 支持手眼到眼标定(Eye-to-Hand) - -## 文件结构 -|文件名/文件夹名|功能| -|:--|:--| -|HandEyeCalibration.cpp|程序主界面和主要逻辑的实现| -|TSAIleastSquareCalibration.cpp棋盘角点检测算法的实现| -|res|图标资源文件夹| - -## 软件运行截图: - -![screenshot](screenshot.jpg) - -## 说明 -* 暂时只支持导入标定,需要导入图片和机器人位姿文件 -* 先导入照片,然后选择与列表中对应编号的机器人位姿文件(有些图片会检测角点失败,不在列表中) diff --git a/hand_eye_calibration/TSAIleastSquareCalibration.cpp b/hand_eye_calibration/TSAIleastSquareCalibration.cpp deleted file mode 100644 index 31c8237..0000000 --- a/hand_eye_calibration/TSAIleastSquareCalibration.cpp +++ /dev/null @@ -1,114 +0,0 @@ -#include "TSAIleastSquareCalibration.h" - -TSAIleastSquareCalibration::TSAIleastSquareCalibration() -{ - -} - -void TSAIleastSquareCalibration::crossprod(cv::Mat a, cv::Mat &ax) -{ - ax = cv::Mat::zeros(cv::Size(3, 3), CV_64F); - if (a.rows == 3 && a.cols == 1) { - ax.at(0, 1) = -a.at(2, 0); - ax.at(0, 2) = a.at(1, 0); - - ax.at(1, 0) = a.at(2, 0); - ax.at(1, 2) = -a.at(0, 0); - - ax.at(2, 0) = -a.at(1, 0); - ax.at(2, 1) = a.at(0, 0); - } -} - -void TSAIleastSquareCalibration::runCalibration( - std::vector eHa, - std::vector cHb, - int mode, // 1 for eye in hand, 0 for eye to hand - cv::Mat &extrinsicMatrix) -{ - cv::Mat invM, rgij, rcij, rngij, rncij, Pgij, Pcij, Pskew, invA; - std::vector Hgij; - std::vector Hcij; - int n = cHb.size(); - cv::Mat A = cv::Mat::zeros(cv::Size(3, 3 * (n - 1)), CV_64F); - cv::Mat b = cv::Mat::zeros(cv::Size(1, 3 * (n - 1)), CV_64F); - // we have n-1 linearly independent relations between the views - for (int i = 1; i < n; ++ i) { - // Transformations between views - if (mode == 1) { - invert(eHa[i], invM); - Hgij.push_back(invM * eHa[i-1]); - } else { - invert(eHa[i-1], invM); - Hgij.push_back(eHa[i] * invM); - } - invert(cHb[i-1], invM); - Hcij.push_back(cHb[i] * invM); - // turn it into angle axis representation (rodrigues formula: P is - // the eigenvector of the rotation matrix with eigenvalue 1 - cv::Rodrigues(Hgij[i-1](cv::Range(0, 3), cv::Range(0, 3)), rgij); - cv::Rodrigues(Hcij[i-1](cv::Range(0, 3), cv::Range(0, 3)), rcij); - double theta_gij = norm(rgij); - double theta_cij = norm(rcij); - rngij = rgij / theta_gij; - rncij = rcij / theta_cij; - // Tsai uses a modified version of this - Pgij = 2 * sin(theta_gij / 2) * rngij; - Pcij = 2 * sin(theta_cij / 2) * rncij; - // Now we know that - // skew(Pgij+Pcij)*x = Pcij-Pgij which is equivalent to Ax = b - // So we need to construct vector b and matrix A to solve this - // overdetermined system. (Note we need >=3 Views to have at least 2 - // linearly independent inter-view relations. - crossprod(Pgij + Pcij, Pskew); - for (int r = 0; r < 3; ++ r) { - for (int c = 0; c < 3; ++ c) { - A.at(3 * i - 3 + r, c) = Pskew.at(r, c); - } - b.at(3 * i - 3 + r, 0) = Pcij.at(r, 0) - Pgij.at(r, 0); - } - } - - // Computing Rotation - invert(A, invA, cv::DECOMP_SVD); - cv::Mat Pcg_prime = invA * b; - // Computing residus - cv::Mat err = A * Pcg_prime - b; - double residus_TSAI_rotation = sqrt(norm(err) / (n - 1)); - cv::Mat Pcg = 2 * Pcg_prime / (sqrt(1 + norm(Pcg_prime) * norm(Pcg_prime))); - double norm_Pcg = norm(Pcg); - crossprod(Pcg, Pskew); - cv::Mat Rcg = (1 - norm_Pcg * norm_Pcg / 2) * cv::Mat::eye(3, 3, CV_64F) + 0.5 * (Pcg * Pcg.t() + sqrt(4 - norm_Pcg * norm_Pcg) * Pskew); - - // Computing Translation - cv::Mat I3 = cv::Mat::eye(3, 3, CV_64F); - for (int i = 1; i < n; ++ i) - { - for (int r = 0; r < 3; ++ r) { - for (int c = 0; c < 3; ++ c) { - A.at(3 * i - 3 + r, c) = Hgij[i-1].at(r, c) - I3.at(r, c); - } - Pcij.at(r, 0) = Hcij[i-1].at(r, 3); - Pgij.at(r, 0) = Hgij[i-1].at(r, 3); - } - cv::Mat Pp = Rcg * Pcij - Pgij; - for (int r = 0; r < 3; ++ r) { - b.at(3 * i - 3 + r, 0) = Pp.at(r, 0); - } - } - invert(A, invA, cv::DECOMP_SVD); - cv::Mat Tcg = invA * b; - // Computing residus - err = A * Tcg - b; - double residus_TSAI_translation = sqrt(norm(err)/(n-1)); - // Estimated transformation - - extrinsicMatrix = cv::Mat::zeros(cv::Size(4, 4), CV_64F); - for (int r = 0; r < 3; ++ r) { - for (int c = 0; c < 3; ++ c) { - extrinsicMatrix.at(r, c) = Rcg.at(r, c); - } - extrinsicMatrix.at(r, 3) = Tcg.at(r, 0); - } - extrinsicMatrix.at(3, 3) = 1; -} diff --git a/hand_eye_calibration/TSAIleastSquareCalibration.h b/hand_eye_calibration/TSAIleastSquareCalibration.h deleted file mode 100644 index 7902faa..0000000 --- a/hand_eye_calibration/TSAIleastSquareCalibration.h +++ /dev/null @@ -1,19 +0,0 @@ -#ifndef TSAILEASTSQUARECALIBRATION_H -#define TSAILEASTSQUARECALIBRATION_H - -#include -#include "../camera_calibration/CameraCalibration.h" - -QT_BEGIN_NAMESPACE -namespace Ui { class TSAIleastSquareCalibration; } -QT_END_NAMESPACE - -class TSAIleastSquareCalibration -{ -public: - TSAIleastSquareCalibration(); - static void runCalibration(std::vector Hg, std::vector Hc, int mode, cv::Mat &extrinsicMatrix); - static void crossprod(cv::Mat a, cv::Mat &ax); -}; - -#endif // TSAILEASTSQUARECALIBRATION_H diff --git a/hand_eye_calibration/res/AddImage.png b/hand_eye_calibration/res/AddImage.png deleted file mode 100644 index 9e10392..0000000 Binary files a/hand_eye_calibration/res/AddImage.png and /dev/null differ diff --git a/hand_eye_calibration/res/camera.png b/hand_eye_calibration/res/camera.png deleted file mode 100644 index 92e2040..0000000 Binary files a/hand_eye_calibration/res/camera.png and /dev/null differ diff --git a/hand_eye_calibration/res/capture.png b/hand_eye_calibration/res/capture.png deleted file mode 100644 index bc69cc0..0000000 Binary files a/hand_eye_calibration/res/capture.png and /dev/null differ diff --git a/hand_eye_calibration/res/delete.png b/hand_eye_calibration/res/delete.png deleted file mode 100644 index 238b4d0..0000000 Binary files a/hand_eye_calibration/res/delete.png and /dev/null differ diff --git a/hand_eye_calibration/res/hand_eye_image.qrc b/hand_eye_calibration/res/hand_eye_image.qrc deleted file mode 100644 index 4218634..0000000 --- a/hand_eye_calibration/res/hand_eye_image.qrc +++ /dev/null @@ -1,13 +0,0 @@ - - - AddImage.png - play.png - yes.png - delete.png - undistort.png - capture.png - camera.png - ../../icon.ico - robot.png - - diff --git a/hand_eye_calibration/res/play.png b/hand_eye_calibration/res/play.png deleted file mode 100644 index ed78fcf..0000000 Binary files a/hand_eye_calibration/res/play.png and /dev/null differ diff --git a/hand_eye_calibration/res/robot.png b/hand_eye_calibration/res/robot.png deleted file mode 100644 index 9f8fba2..0000000 Binary files a/hand_eye_calibration/res/robot.png and /dev/null differ diff --git a/hand_eye_calibration/res/undistort.png b/hand_eye_calibration/res/undistort.png deleted file mode 100644 index ee31e2d..0000000 Binary files a/hand_eye_calibration/res/undistort.png and /dev/null differ diff --git a/hand_eye_calibration/res/yes.png b/hand_eye_calibration/res/yes.png deleted file mode 100644 index 29d0859..0000000 Binary files a/hand_eye_calibration/res/yes.png and /dev/null differ diff --git a/hand_eye_calibration/screenshot.jpg b/hand_eye_calibration/screenshot.jpg deleted file mode 100644 index e71a101..0000000 Binary files a/hand_eye_calibration/screenshot.jpg and /dev/null differ diff --git a/icon.ico b/icon.ico deleted file mode 100644 index de1d2c9..0000000 Binary files a/icon.ico and /dev/null differ diff --git a/main.cpp b/main.cpp index 821ece1..ae8bc25 100644 --- a/main.cpp +++ b/main.cpp @@ -1,10 +1,10 @@ -#include "mainwindow.h" +#include "CameraCalibration.h" #include int main(int argc, char *argv[]) { QApplication a(argc, argv); - MainWindow w; + CameraCalibration w; QFont font = w.font(); font.setPixelSize(12); w.setFont(font); diff --git a/mainwindow.cpp b/mainwindow.cpp deleted file mode 100644 index cefd3d0..0000000 --- a/mainwindow.cpp +++ /dev/null @@ -1,55 +0,0 @@ -#include "mainwindow.h" -#include "ui_mainwindow.h" - -MainWindow::MainWindow(QWidget *parent) : - QMainWindow(parent), - ui(new Ui::MainWindow) -{ - ui->setupUi(this); - setFixedSize(this->width(), this->height()); - ui->CameraCalib->setFlat(true); - connect(ui->CameraCalib, SIGNAL(clicked()), this, SLOT(startCameraCalib())); - ui->HandEyeCalib->setFlat(true); - connect(ui->HandEyeCalib, SIGNAL(clicked()), this, SLOT(startHandEyeCalib())); - about = ui->menu->addAction("关于"); - QFont font = about->font(); - font.setPixelSize(12); - about->setFont(font); - connect(about, SIGNAL(triggered()), this, SLOT(showIntro())); - -} - -MainWindow::~MainWindow() -{ - delete ui; -} - -void MainWindow::showIntro() -{ - a = new AboutUs(); - QFont font = a->font(); - font.setPixelSize(12); - a->setFont(font); - a->setWindowTitle("关于TStoneCalibration"); - a->show(); -} - -void MainWindow::startCameraCalib() -{ - camera_calibration = new CameraCalibration(); - QFont font = camera_calibration->font(); - font.setPixelSize(12); - camera_calibration->setFont(font); - camera_calibration->setWindowTitle("相机标定"); - camera_calibration->show(); -} - -void MainWindow::startHandEyeCalib() -{ - hand_eye_calibration = new HandEyeCalibration(); - QFont font = hand_eye_calibration->font(); - font.setPixelSize(12); - hand_eye_calibration->setFont(font); - hand_eye_calibration->setWindowTitle("手眼标定"); - hand_eye_calibration->show(); -} diff --git a/mainwindow.h b/mainwindow.h deleted file mode 100644 index 40650a2..0000000 --- a/mainwindow.h +++ /dev/null @@ -1,34 +0,0 @@ -#ifndef MAINWINDOW_H -#define MAINWINDOW_H - -#include -#include "camera_calibration/CameraCalibration.h" -#include "hand_eye_calibration/HandEyeCalibration.h" -#include "AboutUs.h" - -namespace Ui { -class MainWindow; -} - -class MainWindow : public QMainWindow -{ - Q_OBJECT - -public: - explicit MainWindow(QWidget *parent = nullptr); - ~MainWindow(); - -private slots: - void startCameraCalib(); - void startHandEyeCalib(); - void showIntro(); - -private: - Ui::MainWindow *ui; - CameraCalibration* camera_calibration; - HandEyeCalibration* hand_eye_calibration; - QAction *about; - AboutUs *a; -}; - -#endif // MAINWINDOW_H diff --git a/mainwindow.ui b/mainwindow.ui deleted file mode 100644 index eca84a3..0000000 --- a/mainwindow.ui +++ /dev/null @@ -1,137 +0,0 @@ - - - MainWindow - - - - 0 - 0 - 320 - 240 - - - - ArrowCursor - - - MainWindow - - - - - - 10 - 10 - 80 - 50 - - - - PointingHandCursor - - - - - - - :/icon/camera.png:/icon/camera.png - - - - 48 - 48 - - - - - - - 10 - 60 - 80 - 20 - - - - - 微软雅黑 - - - - 相机标定 - - - Qt::AlignCenter - - - - - - 120 - 10 - 80 - 50 - - - - PointingHandCursor - - - - - - - :/icon/camera.png:/icon/camera.png - - - - 48 - 48 - - - - - - - 120 - 60 - 80 - 20 - - - - - 微软雅黑 - - - - 手眼标定 - - - Qt::AlignCenter - - - - - - - 0 - 0 - 320 - 26 - - - - - 帮助 - - - - - - - - - - - diff --git a/camera_calibration/res/AddImage.png b/res/AddImage.png similarity index 100% rename from camera_calibration/res/AddImage.png rename to res/AddImage.png diff --git a/camera_calibration/res/camera.png b/res/camera.png similarity index 100% rename from camera_calibration/res/camera.png rename to res/camera.png diff --git a/camera_calibration/res/capture.png b/res/capture.png similarity index 100% rename from camera_calibration/res/capture.png rename to res/capture.png diff --git a/camera_calibration/res/delete.png b/res/delete.png similarity index 100% rename from camera_calibration/res/delete.png rename to res/delete.png diff --git a/camera_calibration/res/image.qrc b/res/image.qrc similarity index 88% rename from camera_calibration/res/image.qrc rename to res/image.qrc index 7b58f52..67f4df1 100644 --- a/camera_calibration/res/image.qrc +++ b/res/image.qrc @@ -7,6 +7,5 @@ undistort.png capture.png camera.png - ../../icon.ico diff --git a/camera_calibration/res/play.png b/res/play.png similarity index 100% rename from camera_calibration/res/play.png rename to res/play.png diff --git a/camera_calibration/res/undistort.png b/res/undistort.png similarity index 100% rename from camera_calibration/res/undistort.png rename to res/undistort.png diff --git a/camera_calibration/res/yes.png b/res/yes.png similarity index 100% rename from camera_calibration/res/yes.png rename to res/yes.png diff --git a/types.h b/types.h new file mode 100644 index 0000000..dd1427e --- /dev/null +++ b/types.h @@ -0,0 +1,40 @@ +#ifndef TYPES_H +#define TYPES_H + +#include +#include + +struct Img_t +{ + std::string file_path; + QString file_name; + std::vector img_points; + std::vector world_points; + cv::Mat tvec; + cv::Mat rvec; + cv::Vec3d fish_tvec; + cv::Vec3d fish_rvec; +}; + +struct Stereo_Img_t +{ + std::string left_path; + QString left_file_name; + std::vector left_img_points; + cv::Mat left_tvec; + cv::Mat left_rvec; + cv::Vec3d left_fish_tvec; + cv::Vec3d left_fish_rvec; + + std::string right_path; + QString right_file_name; + std::vector right_img_points; + cv::Mat right_tvec; + cv::Mat right_rvec; + cv::Vec3d right_fish_tvec; + cv::Vec3d right_fish_rvec; + + std::vector world_points; +}; + +#endif \ No newline at end of file