└── ImageUndistort ├── data ├── 0.png ├── 1.png ├── 2.png ├── 3.png ├── 4.png ├── 0_pts.png ├── 0_un2.png ├── 0_Bbox.png ├── 0_pts_un.png ├── 0_un2_K.png ├── 0_un_0.png ├── 0_un_1.png └── 0_Bbox_un.png ├── CMakeLists.txt ├── undistort2_2.cpp ├── undistort2_1.cpp ├── undistort2_3_1.cpp ├── undistort2_3_2.cpp └── undistort2_4.cpp /ImageUndistort/data/0.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/JiangZhengok/WeChatCode/HEAD/ImageUndistort/data/0.png -------------------------------------------------------------------------------- /ImageUndistort/data/1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/JiangZhengok/WeChatCode/HEAD/ImageUndistort/data/1.png -------------------------------------------------------------------------------- /ImageUndistort/data/2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/JiangZhengok/WeChatCode/HEAD/ImageUndistort/data/2.png -------------------------------------------------------------------------------- /ImageUndistort/data/3.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/JiangZhengok/WeChatCode/HEAD/ImageUndistort/data/3.png -------------------------------------------------------------------------------- /ImageUndistort/data/4.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/JiangZhengok/WeChatCode/HEAD/ImageUndistort/data/4.png -------------------------------------------------------------------------------- /ImageUndistort/data/0_pts.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/JiangZhengok/WeChatCode/HEAD/ImageUndistort/data/0_pts.png -------------------------------------------------------------------------------- /ImageUndistort/data/0_un2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/JiangZhengok/WeChatCode/HEAD/ImageUndistort/data/0_un2.png -------------------------------------------------------------------------------- /ImageUndistort/data/0_Bbox.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/JiangZhengok/WeChatCode/HEAD/ImageUndistort/data/0_Bbox.png -------------------------------------------------------------------------------- /ImageUndistort/data/0_pts_un.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/JiangZhengok/WeChatCode/HEAD/ImageUndistort/data/0_pts_un.png -------------------------------------------------------------------------------- /ImageUndistort/data/0_un2_K.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/JiangZhengok/WeChatCode/HEAD/ImageUndistort/data/0_un2_K.png -------------------------------------------------------------------------------- /ImageUndistort/data/0_un_0.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/JiangZhengok/WeChatCode/HEAD/ImageUndistort/data/0_un_0.png -------------------------------------------------------------------------------- /ImageUndistort/data/0_un_1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/JiangZhengok/WeChatCode/HEAD/ImageUndistort/data/0_un_1.png -------------------------------------------------------------------------------- /ImageUndistort/data/0_Bbox_un.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/JiangZhengok/WeChatCode/HEAD/ImageUndistort/data/0_Bbox_un.png -------------------------------------------------------------------------------- /ImageUndistort/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.14) 2 | project(ImageUndistort) 3 | 4 | set(CMAKE_CXX_STANDARD 11) 5 | 6 | set(OpenCV_DIR "/home/jiang/6_lib/install/opencv3.3.0/share/OpenCV") 7 | 8 | find_package(OpenCV REQUIRED) 9 | 10 | add_executable(undistort2_1 undistort2_1.cpp) 11 | target_link_libraries(undistort2_1 12 | ${OpenCV_LIBS} 13 | ) 14 | 15 | add_executable(undistort2_2 undistort2_2.cpp) 16 | target_link_libraries(undistort2_2 17 | ${OpenCV_LIBS} 18 | ) 19 | 20 | add_executable(undistort2_3_1 undistort2_3_1.cpp) 21 | target_link_libraries(undistort2_3_1 22 | ${OpenCV_LIBS} 23 | ) 24 | 25 | add_executable(undistort2_3_2 undistort2_3_2.cpp) 26 | target_link_libraries(undistort2_3_2 27 | ${OpenCV_LIBS} 28 | ) 29 | 30 | add_executable(undistort2_4 undistort2_4.cpp) 31 | target_link_libraries(undistort2_4 32 | ${OpenCV_LIBS} 33 | ) -------------------------------------------------------------------------------- /ImageUndistort/undistort2_2.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // Created by jiang on 2020/4/29. 3 | // 4 | #include 5 | #include 6 | 7 | using namespace std; 8 | 9 | int main() 10 | { 11 | const cv::Mat K = ( cv::Mat_ ( 3,3 ) << 931.73, 0.0, 480.0, 0.0, 933.16, 302.0, 0.0, 0.0, 1.0 ); 12 | const cv::Mat D = ( cv::Mat_ ( 5,1 ) << -1.7165e-1, 1.968259e-1, 0.0, 0.0, -3.639514e-1 ); 13 | 14 | const string str = "/home/jiang/4_learn/WeChatCode/ImageUndistort/data/"; 15 | const int nImage = 5; 16 | const int ImgWidth = 960; 17 | const int ImgHeight = 640; 18 | 19 | cv::Mat map1, map2; 20 | cv::Size imageSize(ImgWidth, ImgHeight); 21 | const double alpha = 1; 22 | cv::Mat NewCameraMatrix = getOptimalNewCameraMatrix(K, D, imageSize, alpha, imageSize, 0); 23 | 24 | for(int i=0; i 5 | #include 6 | 7 | using namespace std; 8 | 9 | int main() 10 | { 11 | const cv::Mat K = ( cv::Mat_ ( 3,3 ) << 931.73, 0.0, 480.0, 0.0, 933.16, 302.0, 0.0, 0.0, 1.0 ); 12 | const cv::Mat D = ( cv::Mat_ ( 5,1 ) << -1.7165e-1, 1.968259e-1, 0.0, 0.0, -3.639514e-1 ); 13 | 14 | const string str = "/home/jiang/4_learn/WeChatCode/ImageUndistort/data/"; 15 | const int nImage = 5; 16 | const int ImgWidth = 960; 17 | const int ImgHeight = 640; 18 | 19 | cv::Mat map1, map2; 20 | cv::Size imageSize(ImgWidth, ImgHeight); 21 | const double alpha = 1; 22 | cv::Mat NewCameraMatrix = getOptimalNewCameraMatrix(K, D, imageSize, alpha, imageSize, 0); 23 | initUndistortRectifyMap(K, D, cv::Mat(), NewCameraMatrix, imageSize, CV_16SC2, map1, map2); 24 | 25 | for(int i=0; i 5 | #include 6 | 7 | using namespace std; 8 | 9 | const cv::Mat K = ( cv::Mat_ ( 3,3 ) << 931.73, 0.0, 480.0, 0.0, 933.16, 302.0, 0.0, 0.0, 1.0 ); 10 | const cv::Mat D = ( cv::Mat_ ( 5,1 ) << -1.7165e-1, 1.968259e-1, 0.0, 0.0, -3.639514e-1 ); 11 | 12 | void UndistortKeyPoints(vector &points); 13 | 14 | int main() 15 | { 16 | const string str = "/home/jiang/4_learn/WeChatCode/ImageUndistort/data/"; 17 | const int nImage = 5; 18 | const int MAX_CNT = 150; 19 | const int MIN_DIST = 30; 20 | 21 | for(int i=0; i pts; 27 | cv::Mat RawImage_Gray; 28 | cv::cvtColor(RawImage, RawImage_Gray, CV_RGB2GRAY); 29 | 30 | cv::goodFeaturesToTrack(RawImage_Gray, pts, MAX_CNT, 0.01, MIN_DIST); 31 | 32 | for(auto& pt:pts) 33 | circle(RawImage, pt, 2, cv::Scalar(255, 0, 0), 2); 34 | cv::imshow("pts", RawImage); 35 | 36 | UndistortKeyPoints(pts); 37 | 38 | cv::Mat UndistortImage; 39 | cv::undistort(RawImage, UndistortImage, K, D, K); 40 | 41 | for(auto& pt:pts) 42 | circle(UndistortImage, pt, 2, cv::Scalar(0, 0, 255), 2); 43 | cv::imshow("pts_un", UndistortImage); 44 | 45 | string OutputPath = str + to_string(i) + "_pts_un" + ".png"; 46 | cv::imwrite(OutputPath, UndistortImage); 47 | cv::waitKey(0); 48 | } 49 | 50 | return 0; 51 | } 52 | 53 | void UndistortKeyPoints(vector &points) 54 | { 55 | if(D.at(0)==0.0) // 图像矫正过 56 | return; 57 | 58 | // N为提取的特征点数量,将N个特征点保存在N*2的mat中 59 | uint N = points.size(); 60 | cv::Mat mat(N,2,CV_32F); 61 | for(int i=0; i(i,0)=points[i].x; 64 | mat.at(i,1)=points[i].y; 65 | } 66 | 67 | // 调整mat的通道为2,矩阵的行列形状不变 68 | mat=mat.reshape(2); 69 | cv::undistortPoints(mat, mat, K, D, cv::Mat(), K); 70 | mat=mat.reshape(1); 71 | 72 | // 存储校正后的特征点 73 | for(int i=0; i(i,0); 77 | kp.y=mat.at(i,1); 78 | points[i] = kp; 79 | } 80 | } -------------------------------------------------------------------------------- /ImageUndistort/undistort2_3_2.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // Created by jiang on 2020/4/29. 3 | // 4 | #include 5 | #include 6 | 7 | using namespace std; 8 | 9 | const cv::Mat K = ( cv::Mat_ ( 3,3 ) << 931.73, 0.0, 480.0, 0.0, 933.16, 302.0, 0.0, 0.0, 1.0 ); 10 | const cv::Mat D = ( cv::Mat_ ( 5,1 ) << -1.7165e-1, 1.968259e-1, 0.0, 0.0, -3.639514e-1 ); 11 | 12 | void UndistortKeyPoints(vector &points, cv::Mat &newCamMatrix); 13 | 14 | int main() 15 | { 16 | const string str = "/home/jiang/4_learn/WeChatCode/ImageUndistort/data/"; 17 | const int nImage = 5; 18 | const int ImgWidth = 960; 19 | const int ImgHeight = 640; 20 | const int MAX_CNT = 150; 21 | const int MIN_DIST = 30; 22 | 23 | cv::Mat map1, map2; 24 | cv::Size imageSize(ImgWidth, ImgHeight); 25 | const double alpha = 1; 26 | cv::Mat NewCameraMatrix = getOptimalNewCameraMatrix(K, D, imageSize, alpha, imageSize, 0); 27 | initUndistortRectifyMap(K, D, cv::Mat(), NewCameraMatrix, imageSize, CV_16SC2, map1, map2); 28 | 29 | for(int i=0; i pts; 38 | cv::Mat RawImage_Gray; 39 | cv::cvtColor(RawImage, RawImage_Gray, CV_RGB2GRAY); 40 | 41 | cv::goodFeaturesToTrack(RawImage_Gray, pts, MAX_CNT, 0.01, MIN_DIST); 42 | 43 | for(auto& pt:pts) 44 | circle(RawImage, pt, 2, cv::Scalar(255, 0, 0), 2); 45 | cv::imshow("pts", RawImage); 46 | 47 | UndistortKeyPoints(pts, NewCameraMatrix); 48 | 49 | for(auto& pt:pts) 50 | circle(UndistortImage, pt, 2, cv::Scalar(0, 0, 255), 2); 51 | cv::imshow("pts_un", UndistortImage); 52 | 53 | string OutputPath = str + to_string(i) + "_pts_un" + ".png"; 54 | cv::imwrite(OutputPath, UndistortImage); 55 | cv::waitKey(0); 56 | } 57 | 58 | return 0; 59 | } 60 | 61 | void UndistortKeyPoints(vector &points, cv::Mat &newCamMatrix) 62 | { 63 | if(D.at(0)==0.0) // 图像矫正过 64 | return; 65 | 66 | // N为提取的特征点数量,将N个特征点保存在N*2的mat中 67 | uint N = points.size(); 68 | cv::Mat mat(N,2,CV_32F); 69 | for(int i=0; i(i,0)=points[i].x; 72 | mat.at(i,1)=points[i].y; 73 | } 74 | 75 | // 调整mat的通道为2,矩阵的行列形状不变 76 | mat=mat.reshape(2); 77 | cv::undistortPoints(mat, mat, K, D, cv::Mat(), newCamMatrix); 78 | mat=mat.reshape(1); 79 | 80 | // 存储校正后的特征点 81 | for(int i=0; i(i,0); 85 | kp.y=mat.at(i,1); 86 | points[i] = kp; 87 | } 88 | } -------------------------------------------------------------------------------- /ImageUndistort/undistort2_4.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // Created by jiang on 2020/4/29. 3 | // 4 | #include 5 | #include 6 | 7 | using namespace std; 8 | 9 | const cv::Mat K = ( cv::Mat_ ( 3,3 ) << 931.73, 0.0, 480.0, 0.0, 933.16, 302.0, 0.0, 0.0, 1.0 ); 10 | const cv::Mat D = ( cv::Mat_ ( 5,1 ) << -1.7165e-1, 1.968259e-1, 0.0, 0.0, -3.639514e-1 ); 11 | 12 | void UndistortBbox(cv::Rect &rect, cv::Mat &newCamMatrix); 13 | 14 | int main() 15 | { 16 | const string str = "/home/jiang/4_learn/WeChatCode/ImageUndistort/data/"; 17 | const int nImage = 5; 18 | const int ImgWidth = 960; 19 | const int ImgHeight = 640; 20 | 21 | cv::Mat map1, map2; 22 | cv::Size imageSize(ImgWidth, ImgHeight); 23 | const double alpha = 1; 24 | cv::Mat NewCameraMatrix = getOptimalNewCameraMatrix(K, D, imageSize, alpha, imageSize, 0); 25 | initUndistortRectifyMap(K, D, cv::Mat(), NewCameraMatrix, imageSize, CV_16SC2, map1, map2); 26 | 27 | cv::Rect Bbox{338, 141, 23, 57}; 28 | 29 | for(int i=0; i(0, 0) = rect.x; 59 | mat.at(0, 1) = rect.y; 60 | 61 | mat.at(1, 0) = rect.x + rect.width; 62 | mat.at(1, 1) = rect.y; 63 | 64 | mat.at(2, 0) = rect.x; 65 | mat.at(2, 1) = rect.y + rect.height; 66 | 67 | mat.at(3, 0) = rect.x + rect.width; 68 | mat.at(3, 1) = rect.y + rect.height; 69 | 70 | mat = mat.reshape(2); // 2通道,行列不变 71 | cv::undistortPoints(mat, mat, K, D, cv::Mat(), newCamMatrix); 72 | mat = mat.reshape(1); // 单通道,行列不变 73 | 74 | double MaxX, MaxY; 75 | rect.x = min(mat.at(0, 0), mat.at(2, 0)); 76 | MaxX = max(mat.at(1, 0), mat.at(3, 0)); 77 | rect.y = min(mat.at(0, 1), mat.at(1, 1)); 78 | MaxY = max(mat.at(2, 1), mat.at(3, 1)); 79 | rect.width = MaxX - rect.x; 80 | rect.height = MaxY - rect.y; 81 | } --------------------------------------------------------------------------------