#include <opencv2\core\core.hpp>
#include <opencv2\imgproc\imgproc.hpp>
#include <opencv2\highgui\highgui.hpp>
#include <iostream>
using namespace cv;
using namespace std;int main(int argc, char** argv)
{return 0;}
**************************************************************************************************************
#include <opencv2\opencv.hpp>
#include <iostream>
#include <string>
using namespace cv;
using namespace std;
#pragma comment(linker, "/subsystem:\"windows\" /entry:\"mainCRTStartup\"")int main(int argc, char*argv[])
{int width = 140;int height = 140;IplImage *src = cvCreateImage(cvSize(1120, 1120), IPL_DEPTH_8U, 1);cvZero(src);for (int i = 0; i<src->width; i++){for (int j = 0; j<src->height; j++){if ((i / width + j / height) % 2 == 0){src->imageData[i*src->widthStep + j*src->nChannels] = 255;}}}cvNamedWindow("src");cvShowImage("src", src);cvSaveImage("ChessBoard.bmp", src, 0);cvWaitKey(0);return 0;}
#include <opencv2\opencv.hpp>
#include <iostream>
#include <string>
#include <sstream>
#include <vector>
using namespace cv;
using namespace std;#include "videoprocessor.h"void draw(const cv::Mat& img, cv::Mat& out) {img.copyTo(out);cv::circle(out, cv::Point(100, 100), 5, cv::Scalar(255, 0, 0), 2);
}
void canny(cv::Mat& img, cv::Mat& out) {if (img.channels() == 3)cv::cvtColor(img, out, CV_BGR2GRAY);cv::Canny(out, out, 100, 200);cv::threshold(out, out, 128, 255, cv::THRESH_BINARY_INV);
}int main()
{cv::VideoCapture capture("D:\\workplace\\opencv_training\\bike.avi");if (!capture.isOpened())return 1;double rate = capture.get(CV_CAP_PROP_FPS);std::cout << "Frame rate: " << rate << "fps" << std::endl;bool stop(false);cv::Mat frame; cv::namedWindow("Extracted Frame");int delay = 1000 / rate;long long i = 0;std::string b = "bike";std::string ext = ".bmp";while (!stop) {if (!capture.read(frame))break;cv::imshow("Extra cted Frame", frame);std::string name(b);std::ostringstream ss; ss << i; name += ss.str(); i++;name += ext;std::cout << name << std::endl;cv::imwrite(name, frame);if (cv::waitKey(delay) >= 0)stop = true;}capture.release();cv::waitKey();VideoProcessor processor;processor.setInput("D:\\workplace\\opencv_training\\bike.avi");processor.displayInput("Input Video");processor.displayOutput("Output Video");processor.setDelay(1000. / processor.getFrameRate());processor.setFrameProcessor(canny);processor.setOutput("D:\\workplace\\opencv_training\\bike1.avi", -1, 15);processor.stopAtFrameNo(51);processor.run();cv::waitKey();return 0;
}
#include <opencv2\opencv.hpp>
#include <iostream>
#include <string>
using namespace cv;
using namespace std;
int main()
{int dd = 80; int dx = 3; int dy = 4; Mat img(Size(2 * dy * dd,2 * dx * dd), CV_8UC1, Scalar(0));imshow("sss", img);int flag = 0;for (int i = 0; i < 2 * dx; i++)for (int j = 0; j < 2 * dy; j++){flag = (i + j) % 2;if (flag == 0){for (int m = i*dd; m < (i + 1)*dd; m++)for (int n = j*dd; n < (j + 1)*dd; n++)(*(img.data + m * img.step+n * img.elemSize())) = 255;}}imwrite("棋盘格标定图.bmp", img);cvNamedWindow("棋盘格", 1);imshow("棋盘格", img);cvWaitKey(0);cvDestroyWindow("棋盘格");
}
#include <opencv2\opencv.hpp>
#include <iostream>
#include <string>
using namespace cv;
using namespace std;
int main()
{int dx = 80; int dy = 5; Mat img(Size(2 * dx*dy, 2 * dx*dy), CV_8UC1, Scalar(0));int flag = 0;for (int i = 0; i<2 * dy; i++)for (int j = 0; j<2 * dy; j++){flag = (i + j) % 2;if (flag == 0){for (int m = i*dx; m<(i + 1)*dx; m++)for (int n = j*dx; n<(j + 1)*dx; n++)((uchar *)(img.data + m * img.step))[n] = 255;}}imwrite("棋盘格标定图.bmp", img);cvNamedWindow("棋盘格", 1);imshow("棋盘格", img);cvWaitKey(0);cvDestroyWindow("棋盘格");
}
#include <opencv2\opencv.hpp>
#include <iostream>
#include <string>
#include <vector>
#include <iomanip>
#include <fstream>
using namespace cv;
using namespace std;
int main()
{double time0 = static_cast<double>(getTickCount());ofstream fout("caliberation_result.txt"); cout << "开始提取角点………………" << endl;int image_count = 21; Size image_size; Size board_size = Size(9, 6); vector<Point2f> corners; vector<vector<Point2f>> corners_Seq; vector<Mat> image_Seq;int count = 0;for (int i = 0; i != image_count; i++){cout << "Frame #" << i + 1 << "..." << endl;std::stringstream str;str << "D:\\workplace\\opencv_training\\mytrainings\\mytest5\\color3\\img" << std::setw(2) << std::setfill('0') << i + 1 << ".jpg";std::cout << str.str() << std::endl;Mat image = cv::imread(str.str());image_size = image.size();Mat imageGray;cvtColor(image, imageGray, CV_RGB2GRAY);bool patternfound = findChessboardCorners(image, board_size, corners, CALIB_CB_ADAPTIVE_THRESH + CALIB_CB_NORMALIZE_IMAGE +CALIB_CB_FAST_CHECK);if (!patternfound){cout << "can not find chessboard corners!\n";continue;exit(1);}else{cornerSubPix(imageGray, corners, Size(11, 11), Size(-1, -1), TermCriteria(CV_TERMCRIT_EPS + CV_TERMCRIT_ITER, 30, 0.1));Mat imageTemp = image.clone();for (int j = 0; j < corners.size(); j++){circle(imageTemp, corners[j], 10, Scalar(0, 0, 255), 2, 8, 0);}string imageFileName;std::stringstream StrStm;StrStm << i + 1;StrStm >> imageFileName;imageFileName += "_corner.jpg";imwrite(imageFileName, imageTemp);cout << "Frame corner#" << i + 1 << "...end" << endl;count = count + corners.size();corners_Seq.push_back(corners);}image_Seq.push_back(image);}cout << "角点提取完成!\n";cout << "开始定标………………" << endl;Size square_size = Size(20, 20); vector<vector<Point3f>> object_Points; Mat image_points = Mat(1, count, CV_32FC2, Scalar::all(0)); vector<int> point_counts; Mat intrinsic_matrix = Mat(3, 3, CV_32FC1, Scalar::all(0)); Mat distortion_coeffs = Mat(1, 4, CV_32FC1, Scalar::all(0)); vector<cv::Mat> rotation_vectors; vector<cv::Mat> translation_vectors; for (int t = 0; t<image_count; t++){vector<Point3f> tempPointSet;for (int i = 0; i<board_size.height; i++){for (int j = 0; j<board_size.width; j++){Point3f tempPoint;tempPoint.x = i*square_size.width;tempPoint.y = j*square_size.height;tempPoint.z = 0;tempPointSet.push_back(tempPoint);}}object_Points.push_back(tempPointSet);}for (int i = 0; i< image_count; i++){point_counts.push_back(board_size.width*board_size.height);}calibrateCamera(object_Points, corners_Seq, image_size, intrinsic_matrix, distortion_coeffs, rotation_vectors, translation_vectors, 0);cout << "定标完成!\n";cout << "开始评价定标结果………………" << endl;double total_err = 0.0; double err = 0.0; vector<Point2f> image_points2; cout << "每幅图像的定标误差:" << endl;fout << "每幅图像的定标误差:" << endl << endl;for (int i = 0; i<image_count; i++){vector<Point3f> tempPointSet = object_Points[i];projectPoints(tempPointSet, rotation_vectors[i], translation_vectors[i], intrinsic_matrix, distortion_coeffs, image_points2);vector<Point2f> tempImagePoint = corners_Seq[i];Mat tempImagePointMat = Mat(1, tempImagePoint.size(), CV_32FC2);Mat image_points2Mat = Mat(1, image_points2.size(), CV_32FC2);for (size_t i = 0; i != tempImagePoint.size(); i++){image_points2Mat.at<Vec2f>(0, i) = Vec2f(image_points2[i].x, image_points2[i].y);tempImagePointMat.at<Vec2f>(0, i) = Vec2f(tempImagePoint[i].x, tempImagePoint[i].y);}err = norm(image_points2Mat, tempImagePointMat, NORM_L2);total_err += err /= point_counts[i];cout << "第" << i + 1 << "幅图像的平均误差:" << err << "像素" << endl;fout << "第" << i + 1 << "幅图像的平均误差:" << err << "像素" << endl;}cout << "总体平均误差:" << total_err / image_count << "像素" << endl;fout << "总体平均误差:" << total_err / image_count << "像素" << endl << endl;cout << "评价完成!" << endl;cout << "开始保存定标结果………………" << endl;Mat rotation_matrix = Mat(3, 3, CV_32FC1, Scalar::all(0)); fout << "相机内参数矩阵:" << endl;cout << "相机内参数矩阵:" << endl;fout << intrinsic_matrix << endl;cout << intrinsic_matrix << endl;fout << "畸变系数:\n";cout << "畸变系数:\n";fout << distortion_coeffs << endl;cout << distortion_coeffs << endl;for (int i = 0; i<image_count; i++){fout << "第" << i + 1 << "幅图像的旋转向量:" << endl;fout << rotation_vectors[i] << endl;Rodrigues(rotation_vectors[i], rotation_matrix);fout << "第" << i + 1 << "幅图像的旋转矩阵:" << endl;fout << rotation_matrix << endl;fout << "第" << i + 1 << "幅图像的平移向量:" << endl;fout << translation_vectors[i] << endl;}cout << "完成保存" << endl;fout << endl;Mat mapx = Mat(image_size, CV_32FC1);Mat mapy = Mat(image_size, CV_32FC1);Mat R = Mat::eye(3, 3, CV_32F);cout << "保存矫正图像" << endl;for (int i = 0; i != image_count; i++){cout << "Frame #" << i + 1 << "..." << endl;Mat newCameraMatrix = Mat(3, 3, CV_32FC1, Scalar::all(0));initUndistortRectifyMap(intrinsic_matrix, distortion_coeffs, R, intrinsic_matrix, image_size, CV_32FC1, mapx, mapy);Mat t = image_Seq[i].clone();cv::remap(image_Seq[i], t, mapx, mapy, INTER_LINEAR);string imageFileName;std::stringstream StrStm;StrStm << i + 1;StrStm >> imageFileName;imageFileName += "_校正图像.jpg";imwrite(imageFileName, t);}cout << "保存结束" << endl;time0 = ((double)getTickCount() - time0) / getTickFrequency();cout << "标定用时:" << time0 << "秒" << endl;waitKey(0);double time1 = static_cast<double>(getTickCount());if (1){cout << "TestImage ..." << endl;Mat newCameraMatrix = Mat(3, 3, CV_32FC1, Scalar::all(0));Mat testImage = imread("D:\\workplace\\opencv_training\\mytrainings\\mytest5\\color3\\img03.jpg", 1);initUndistortRectifyMap(intrinsic_matrix, distortion_coeffs, R, intrinsic_matrix, image_size, CV_32FC1, mapx, mapy);Mat t = testImage.clone();cv::remap(testImage, t, mapx, mapy, INTER_LINEAR);imwrite("img03_TestOutput.jpg", t);cout << "保存结束" << endl;}time1 = ((double)getTickCount() - time1) / getTickFrequency();cout << "一张图片的畸变校正用时:" << time1 << "秒" << endl;waitKey(0);return 0;
}
#include <opencv2\opencv.hpp>
#include <opencv2\opencv.hpp>
#include <iostream>
#include <string>
using namespace cv;
using namespace std;
#include <vector>
#include<opencv2/legacy/legacy.hpp>
int main()
{Mat img_1 = imread("D:\\workplace\\opencv_training\\mytrainings\\mytest7\\test1.png");Mat img_2 = imread("D:\\workplace\\opencv_training\\mytrainings\\mytest7\\test2.png");if (!img_1.data || !img_2.data){cout << "error reading images " << endl;return -1;}ORB orb;vector<KeyPoint> keyPoints_1, keyPoints_2;Mat descriptors_1, descriptors_2;orb(img_1, Mat(), keyPoints_1, descriptors_1);orb(img_2, Mat(), keyPoints_2, descriptors_2);cout << "img1特征点个数:" << keyPoints_1.size() << " points img2特征点个数:" << keyPoints_2.size()<< " points" << endl << ">" << endl;Mat img_keypoints1, img_keypoints2;drawKeypoints(img_1, keyPoints_1, img_keypoints1, Scalar::all(-1), 0);drawKeypoints(img_2, keyPoints_2, img_keypoints2, Scalar::all(-1), 0);imshow("test1", img_keypoints1);imshow("test2", img_keypoints2);BruteForceMatcher<L2<float> > matcher;vector<DMatch> matches;matcher.match(descriptors_1, descriptors_2, matches);double max_dist = 0; double min_dist = 100;for (int i = 0; i < descriptors_1.rows; i++){double dist = matches[i].distance;if (dist < min_dist) min_dist = dist;if (dist > max_dist) max_dist = dist;}printf("-- Max dist : %f \n", max_dist);printf("-- Min dist : %f \n", min_dist);std::vector< DMatch > good_matches;for (int i = 0; i < descriptors_1.rows; i++){if (matches[i].distance < 0.6*max_dist){good_matches.push_back(matches[i]);}}Mat img_matches;drawMatches(img_1, keyPoints_1, img_2, keyPoints_2,good_matches, img_matches, Scalar::all(-1), Scalar::all(-1),vector<char>(), DrawMatchesFlags::NOT_DRAW_SINGLE_POINTS);imshow("Match1", img_matches);waitKey();std::vector<Point2f> obj;std::vector<Point2f> scene;for (size_t i = 0; i < good_matches.size(); ++i){obj.push_back(keyPoints_1[good_matches[i].queryIdx].pt);scene.push_back(keyPoints_2[good_matches[i].trainIdx].pt);}Mat H = findHomography(obj, scene, CV_RANSAC);std::vector<Point2f> obj_corners(4);obj_corners[0] = cvPoint(0, 0);obj_corners[1] = cvPoint(img_1.cols, 0);obj_corners[2] = cvPoint(img_1.cols, img_1.rows);obj_corners[3] = cvPoint(0, img_1.rows);std::vector<Point2f> scene_corners(4);perspectiveTransform(obj_corners, scene_corners, H);line(img_matches, scene_corners[0] + Point2f(img_1.cols, 0), scene_corners[1] + Point2f(img_1.cols, 0), Scalar(0, 255, 0));line(img_matches, scene_corners[1] + Point2f(img_1.cols, 0), scene_corners[2] + Point2f(img_1.cols, 0), Scalar(0, 255, 0));line(img_matches, scene_corners[2] + Point2f(img_1.cols, 0), scene_corners[3] + Point2f(img_1.cols, 0), Scalar(0, 255, 0));line(img_matches, scene_corners[3] + Point2f(img_1.cols, 0), scene_corners[0] + Point2f(img_1.cols, 0), Scalar(0, 255, 0));imshow("被标记的Match2", img_matches);waitKey(0);}
#include <opencv2\opencv.hpp>
#include <opencv2\opencv.hpp>
#include <iostream>
#include <string>
#include <stdio.h>
using namespace cv;
using namespace std;int main(int argc, char *argv[])
{CvCapture* capture = cvCaptureFromAVI("D:\\workplace\\opencv_training\\bike.avi");int i = 0;IplImage* img = 0;char image_name[100];cvNamedWindow("抽取视频窗口");while (1){img = cvQueryFrame(capture); if (img == NULL)break;cvShowImage("抽取视频窗口", img); char key = cvWaitKey(25);if (i == 10)break;sprintf(image_name, "%s%d%s", "D:..\\mytest", ++i, ".jpg");cvSaveImage(image_name, img); }cvReleaseCapture(&capture);cvDestroyWindow("抽取视频窗口");return 0;
}
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <iostream> using namespace std;
using namespace cv;int main()
{VideoCapture capture("D:\\workplace\\opencv_training\\1.avi");if (!capture.isOpened())cout << "fail to open!" << endl;long totalFrameNumber = capture.get(CV_CAP_PROP_FRAME_COUNT);cout << "整个视频共" << totalFrameNumber << "帧" << endl;long frameToStart = 300;capture.set(CV_CAP_PROP_POS_FRAMES, frameToStart);cout << "从第" << frameToStart << "帧开始读" << endl;int frameToStop = 400;if (frameToStop < frameToStart){cout << "结束帧小于开始帧,程序错误,即将退出!" << endl;return -1;}else{cout << "结束帧为:第" << frameToStop << "帧" << endl;}double rate = capture.get(CV_CAP_PROP_FPS);cout << "帧率为:" << rate << endl;bool stop = false;Mat frame;namedWindow("Extracted frame");int delay = 1000 / rate;long currentFrame = frameToStart;int kernel_size = 3;Mat kernel = Mat::ones(kernel_size, kernel_size, CV_32F) / (float)(kernel_size*kernel_size);while (!stop){if (!capture.read(frame)){cout << "读取视频失败" << endl;return -1;}imshow("Extracted frame", frame);filter2D(frame, frame, -1, kernel);imshow("after filter", frame);cout << "正在读取第" << currentFrame << "帧" << endl;int c = waitKey(delay);if ((char)c == 27 || currentFrame > frameToStop){stop = true;}if (c >= 0){waitKey(0);}currentFrame++;}capture.release();waitKey(0);return 0;
}
#include <opencv2\opencv.hpp>
#include <iostream>
#include <string> #include "videoInput.h"using namespace std;
using namespace cv;
void main()
{videoInput VI;int numID = VI.listDevices();int device1 = 0;VI.setupDevice(device1, 640, 480, 1);int width = VI.getWidth(device1);int height = VI.getHeight(device1);int size = VI.getSize(device1);cout << "width=" << width << "\t" << "height=" << height << endl;cout << "framesize=" << size << endl;Mat image;Mat frame;image.create(Size(width, height), CV_8UC3);frame.create(Size(width, height), CV_8UC3);uchar* yourBuffer = (uchar*)malloc(size);while (1){VI.getPixels(device1, yourBuffer, false, false);image.data = (uchar*)yourBuffer;flip(image, image, 0);waitKey(50);imshow("采集的图像", image);}}
#include <opencv2\opencv.hpp>
#include <iostream>
#include <string>
#include <vector>
#include <iomanip>
#include <fstream>#include "videoInput.h"
using namespace cv;
using namespace std;
int main()
{videoInput VI;int numID = VI.listDevices();int device1 = 0;VI.setupDevice(device1, 640, 480, 1);int width = VI.getWidth(device1);int height = VI.getHeight(device1);int size = VI.getSize(device1);cout << "width=" << width << "\t" << "height=" << height << endl;cout << "framesize=" << size << endl;Mat frame;frame.create(Size(width, height), CV_8UC3);uchar* yourBuffer = (uchar*)malloc(size);cout << "开始提取21张标定板图像………………" << endl;int imageCount = 21;int key = 0;int count1 = 0;for (int i = 0; i != imageCount; i++){cout << "Frame#" << i + 1 << "..." << endl;std::stringstream StrStm;string imageFileName;StrStm << i + 1;StrStm >> imageFileName;imageFileName += ".jpg";cout << "按Enter开始抽取图像,进入后可按q或者ESC键重新抽取图像,若按Enter键表明这帧图像被存入文件夹中" << endl;int flag = 1;while (flag){VI.getPixels(device1, yourBuffer, false, false);frame.data = (uchar*)yourBuffer;waitKey(50);flip(frame, frame, 0);Mat image0 = frame;imshow("显示抓取图像", image0);int key2;key2 = waitKey();if (key2 == 13){cout << "提取标定板图像成功!………………" << endl;std::stringstream str;str << "F:\\biaoding\\img" << std::setw(2) << std::setfill('0') << i + 1 << ".jpg";std::cout << "提取的图像保存路径及文件名" << str.str() << endl;imwrite(str.str(), image0);flag = 0;count1 += 1;}elseif (key2 == 113 || key2 == 27)cout << "这次提取的标定板图像不成功!重新提取!!!!………………" << endl;};}if (count1 == imageCount){cout << "***********************………………" << endl;cout << "***********************………………" << endl;cout << "下面开始标定图像...................." << endl;count1 = 0;}system("pause");cout << "开始提取角点………………" << endl;cout << "开始提取角点………………" << endl;double time0 = static_cast<double>(getTickCount());ofstream fout("F:\\biaoding\\biaoding_result.txt"); Size image_size; Size board_size(9, 9); vector<Point2f> corners; vector<vector<Point2f>> corners_Seq; vector<Mat> image_Seq;int count = 0;int image_count = imageCount;for (int i = 0; i != image_count; i++){cout << "Frame #" << i + 1 << "..." << endl;std::stringstream str;str << "F:\\biaoding\\img" << std::setw(2) << std::setfill('0') << i + 1 << ".jpg";std::cout << str.str() << std::endl;Mat image = imread(str.str());image_size = image.size();Mat imageGray;cvtColor(image, imageGray, CV_RGB2GRAY);bool patternfound = findChessboardCorners(image, board_size, corners, CALIB_CB_ADAPTIVE_THRESH + CALIB_CB_NORMALIZE_IMAGE +CALIB_CB_FAST_CHECK);if (!patternfound){cout << "can not find chessboard corners!\n";continue;exit(1);}else{cornerSubPix(imageGray, corners, Size(11, 11), Size(-1, -1), TermCriteria(CV_TERMCRIT_EPS + CV_TERMCRIT_ITER, 30, 0.1));Mat imageTemp = image.clone();for (int j = 0; j < corners.size(); j++){circle(imageTemp, corners[j], 10, Scalar(0, 0, 255), 2, 8, 0);}string imageFileName;std::stringstream StrStm;StrStm << i + 1;StrStm >> imageFileName;imageFileName += "_corner.jpg";imwrite("F:\\biaoding\\" + imageFileName, imageTemp);cout << "Frame corner#" << i + 1 << "...end" << endl;count = count + corners.size();corners_Seq.push_back(corners);}image_Seq.push_back(image);}cout << "角点提取完成!\n";cout << "角点提取完成!下一步摄像机定标\n";system("pause");cout << "开始定标………………" << endl;Size square_size = Size(80, 80); vector<vector<Point3f>> object_Points; Mat image_points = Mat(1, count, CV_32FC2, Scalar::all(0)); vector<int> point_counts; Mat intrinsic_matrix = Mat(3, 3, CV_32FC1, Scalar::all(0)); Mat distortion_coeffs = Mat(1, 4, CV_32FC1, Scalar::all(0)); vector<cv::Mat> rotation_vectors; vector<cv::Mat> translation_vectors; for (int t = 0; t<image_count; t++){vector<Point3f> tempPointSet;for (int i = 0; i<board_size.height; i++){for (int j = 0; j<board_size.width; j++){Point3f tempPoint;tempPoint.x = i*square_size.width;tempPoint.y = j*square_size.height;tempPoint.z = 0;tempPointSet.push_back(tempPoint);}}object_Points.push_back(tempPointSet);}for (int i = 0; i< image_count; i++){point_counts.push_back(board_size.width*board_size.height);}calibrateCamera(object_Points, corners_Seq, image_size, intrinsic_matrix, distortion_coeffs, rotation_vectors, translation_vectors, 0);cout << "定标完成!\n";cout << "开始评价定标结果………………" << endl;double total_err = 0.0; double err = 0.0; vector<Point2f> image_points2; cout << "每幅图像的定标误差:" << endl;fout << "每幅图像的定标误差:" << endl << endl;for (int i = 0; i<image_count; i++){vector<Point3f> tempPointSet = object_Points[i];projectPoints(tempPointSet, rotation_vectors[i], translation_vectors[i], intrinsic_matrix, distortion_coeffs, image_points2);vector<Point2f> tempImagePoint = corners_Seq[i];Mat tempImagePointMat = Mat(1, tempImagePoint.size(), CV_32FC2);Mat image_points2Mat = Mat(1, image_points2.size(), CV_32FC2);for (size_t i = 0; i != tempImagePoint.size(); i++){image_points2Mat.at<Vec2f>(0, i) = Vec2f(image_points2[i].x, image_points2[i].y);tempImagePointMat.at<Vec2f>(0, i) = Vec2f(tempImagePoint[i].x, tempImagePoint[i].y);}err = norm(image_points2Mat, tempImagePointMat, NORM_L2);total_err += err /= point_counts[i];cout << "第" << i + 1 << "幅图像的平均误差:" << err << "像素" << endl;fout << "第" << i + 1 << "幅图像的平均误差:" << err << "像素" << endl;}cout << "总体平均误差:" << total_err / image_count << "像素" << endl;fout << "总体平均误差:" << total_err / image_count << "像素" << endl << endl;cout << "评价完成!" << endl;cout << "开始保存定标结果………………" << endl;Mat rotation_matrix = Mat(3, 3, CV_32FC1, Scalar::all(0)); fout << "相机内参数矩阵:" << endl;cout << "相机内参数矩阵:" << endl;fout << intrinsic_matrix << endl;cout << intrinsic_matrix << endl;fout << "畸变系数:\n";cout << "畸变系数:\n";fout << distortion_coeffs << endl;cout << distortion_coeffs << endl;for (int i = 0; i<image_count; i++){fout << "第" << i + 1 << "幅图像的旋转向量:" << endl;fout << rotation_vectors[i] << endl;Rodrigues(rotation_vectors[i], rotation_matrix);fout << "第" << i + 1 << "幅图像的旋转矩阵:" << endl;fout << rotation_matrix << endl;fout << "第" << i + 1 << "幅图像的平移向量:" << endl;fout << translation_vectors[i] << endl;}cout << "完成保存" << endl;fout << endl;Mat mapx = Mat(image_size, CV_32FC1);Mat mapy = Mat(image_size, CV_32FC1);Mat R = Mat::eye(3, 3, CV_32F);cout << "保存矫正图像" << endl;for (int i = 0; i != image_count; i++){cout << "Frame #" << i + 1 << "..." << endl;Mat newCameraMatrix = Mat(3, 3, CV_32FC1, Scalar::all(0));initUndistortRectifyMap(intrinsic_matrix, distortion_coeffs, R, intrinsic_matrix, image_size, CV_32FC1, mapx, mapy);Mat t = image_Seq[i].clone();cv::remap(image_Seq[i], t, mapx, mapy, INTER_LINEAR);string imageFileName;std::stringstream StrStm;StrStm << i + 1;StrStm >> imageFileName;imageFileName += "_校正图像.jpg";imwrite("F:\\biaoding\\" + imageFileName, t);}cout << "保存结束" << endl;time0 = ((double)getTickCount() - time0) / getTickFrequency();cout << "标定用时:" << time0 << "秒" << endl;system("pause");return 0;
}