【Opencv】cvCreateTrackbar Error "void (*)(int)"类型的实参与"int"类型的形参不兼容

在使用《学习OpenCV》书中关于轮廓的例子时,遇到cvCreateTrackbar错误,提示类型不兼容。原因是OpenCV2中cvCreateTrackbar函数的参数变化。解决方案是调整函数调用,将滚动条数值上限作为第四个参数传入,如:cvCreateTrackbar("Threshold","Contours",&g_thresh,100,on_trackbar)。" 87109404,7464803,MySQL 8.0.11 在Win10上的详细安装指南,"['数据库', 'MySQL', '安装教程', 'Windows 10']

在《学习Opencv》一书( 2009年10月第一版)轮廓一章(p269.)中,按实例代码运行会产生类型错误: Error "void (*)(int)"类型的实参与"int"类型的形参不兼容。


原因可能是我们现在常用的opencv2库函数对opencv中的cvCreateTrackbar函数做了小的修改,因此我们将回调函数放在了一个错误的参数位置上。


cvCreateTrackbar("Threshold","Contours",&g_thresh,on_trackbar);

修改为

cvCreateTrackbar("Threshold","Contours",&g_thresh,100,on_trackbar);即可

其中的100也就是插入的第四个参数是滚动条数值上限


仔细看cvCreateTrackar的参数说明即可:int _cdecl cvCreateTrackbar(const char *trackbar _name,const char *window_name,int *value,int count,CvTrackbarCallback on_change = (CvTrackbarCallback)0)

#include <iostream> #include <vector> #include <string> #include <opencv2/opencv.hpp> #include <opencv2/core/utils/filesystem.hpp> using namespace std; using namespace cv; // 优化标定函数 bool refineCalibration(const vector<vector<Point3f>>& objectPoints, const vector<vector<Point2f>>& imagePoints, Mat& cameraMatrix, Mat& distCoeffs, Size imageSize, double targetError = 0.2, int maxIterations = 30, bool useExtendedModel = false) { vector<Mat> rvecs, tvecs; TermCriteria criteria(TermCriteria::EPS + TermCriteria::COUNT, maxIterations, DBL_EPSILON); int flags = CALIB_USE_INTRINSIC_GUESS | CALIB_FIX_ASPECT_RATIO | CALIB_FIX_PRINCIPAL_POINT | CALIB_ZERO_TANGENT_DIST; // 动态控制畸变模型 if (!useExtendedModel) { flags |= CALIB_FIX_K3 | CALIB_FIX_K4 | CALIB_FIX_K5 | CALIB_FIX_K6; } else { flags |= CALIB_RATIONAL_MODEL; // 启用8参数模型 } double error = calibrateCamera(objectPoints, imagePoints, imageSize, cameraMatrix, distCoeffs, rvecs, tvecs, flags, criteria); cout << "Optimized error: " << error << " | Target: " << targetError << endl; return error <= targetError; } // 标定流程控制 bool calibrationPipeline(const string& imagePath, Size boardSize, float squareSize, Mat& cameraMatrix, Mat& distCoeffs, bool useInitialGuess = false, bool useExtendedModel = false, Size& outputImageSize) { vector<String> imgPaths; glob(imagePath + "/*.bmp", imgPaths, false); if (imgPaths.empty()) { cerr << "No images found in " << imagePath << endl; return false; } // 生成物体坐标系点 vector<vector<Point3f>> objectPoints; vector<Point3f> objPattern; for (int i = 0; i < boardSize.height; ++i) for (int j = 0; j < boardSize.width; ++j) objPattern.emplace_back(j * squareSize, i * squareSize, 0); // 检测角点 vector<vector<Point2f>> imagePoints; Size imageSize; for (const auto& path : imgPaths) { Mat img = imread(path, IMREAD_GRAYSCALE); if (img.empty()) continue; imageSize = img.size(); vector<Point2f> corners; bool found = findChessboardCorners(img, boardSize, corners, CALIB_CB_ADAPTIVE_THRESH + CALIB_CB_NORMALIZE_IMAGE); if (found) { cornerSubPix(img, corners, { 11,11 }, { -1,-1 }, TermCriteria(TermCriteria::EPS + TermCriteria::COUNT, 30, 0.01)); imagePoints.push_back(corners); objectPoints.push_back(objPattern); } } if (imagePoints.size() < 3) { cerr << "Insufficient valid images: " << imagePoints.size() << endl; return false; } // 初始化参数 if (!useInitialGuess) { cameraMatrix = Mat::eye(3, 3, CV_64F); distCoeffs = Mat::zeros(useExtendedModel ? 8 : 5, 1, CV_64F); } // 首次标定 vector<Mat> rvecs, tvecs; int baseFlags = useInitialGuess ? CALIB_USE_INTRINSIC_GUESS : 0; baseFlags |= CALIB_FIX_ASPECT_RATIO | CALIB_FIX_PRINCIPAL_POINT; double rms = calibrateCamera(objectPoints, imagePoints, imageSize, cameraMatrix, distCoeffs, rvecs, tvecs, baseFlags); cout << "Initial calibration error: " << rms << endl; // 优化阶段 if (rms > 0.5) { cout << "Starting optimization..." << endl; if (!refineCalibration(objectPoints, imagePoints, cameraMatrix, distCoeffs, imageSize, 0.2, 50, useExtendedModel)) { cerr << "Optimization failed to reach target" << endl; return false; } } outputImageSize = imageSize; return true; } int main() { // 路径配置 const string calibPaths[2] = { "C://ceshitupian//ceshi", "C://ceshitupian//distance2m_G200_M0.8s_D_0.2s_No//shenrs4_33697" }; // 标定参数 const Size boardSize(20, 14); // 内角点数量 const float squareSize = 20.0f; // 棋盘格物理尺寸(mm) Size imageSize; try { // 第一阶段:扩展标定(8参数) Mat cameraMatrix, distCoeffs; cout << "===== Phase 1: Extended Calibration =====" << endl; if (!calibrationPipeline(calibPaths[0], boardSize, squareSize, cameraMatrix, distCoeffs, false, true, imageSize)) { throw runtime_error("Phase 1 calibration failed"); } cout << "\nPhase 1 Results:\n" << "Camera Matrix:\n" << cameraMatrix << "\n" << "Distortion Coeffs:\n" << distCoeffs << endl; // 第二阶段:扩展标定(8参数) cout << "\n===== Phase 2: Extended Calibration =====" << endl; Mat refinedCamera = cameraMatrix.clone(); Mat extendedDist = Mat::zeros(8, 1, CV_64F); distCoeffs.copyTo(extendedDist.rowRange(0, 5)); if (!calibrationPipeline(calibPaths[1], boardSize, squareSize, refinedCamera, extendedDist, true, true, imageSize)) { throw runtime_error("Phase 2 calibration failed"); } cout << "\nFinal Results:\n" << "Refined Camera Matrix:\n" << refinedCamera << "\n" << "Extended Distortion Coeffs:\n" << extendedDist << endl; // 保存结果 FileStorage fs("calibration_result.yml", FileStorage::WRITE); fs << "camera_matrix" << refinedCamera << "distortion_coefficients" << extendedDist << "image_width" << imageSize.width << "image_height" << imageSize.height; cout << "\nCalibration data saved to calibration_result.yml" << endl; } catch (const exception& e) { cerr << "Error: " << e.what() << endl; return EXIT_FAILURE; } return EXIT_SUCCESS; } 严重性 代码 说明 项目 文件 行 禁止显示状态 详细信息 错误 C2548 “calibrationPipeline”: 缺少形参 8 的默认实参 二次标定法 C:\Users\Lenovo\source\repos\二次标定法\二次标定法\test.cpp 52 解决上述问题,并提供完整的优化后的代码
05-22
#include <iostream> #include <vector> #include <string> #include <opencv2/opencv.hpp> #include <opencv2/core/utils/filesystem.hpp> using namespace std; using namespace cv; // 优化标定函数 bool refineCalibration(const vector<vector<Point3f>>& objectPoints, const vector<vector<Point2f>>& imagePoints, Mat& cameraMatrix, Mat& distCoeffs, Size imageSize, double targetError = 0.2, int maxIterations = 30, bool useExtendedModel = false) { vector<Mat> rvecs, tvecs; TermCriteria criteria(TermCriteria::EPS + TermCriteria::COUNT, maxIterations, DBL_EPSILON); int flags = CALIB_USE_INTRINSIC_GUESS | CALIB_FIX_ASPECT_RATIO | CALIB_FIX_PRINCIPAL_POINT | CALIB_ZERO_TANGENT_DIST; // 动态控制畸变模型 if (!useExtendedModel) { flags |= CALIB_FIX_K3 | CALIB_FIX_K4 | CALIB_FIX_K5 | CALIB_FIX_K6; } else { flags |= CALIB_RATIONAL_MODEL; // 启用8参数模型 } double error = calibrateCamera(objectPoints, imagePoints, imageSize, cameraMatrix, distCoeffs, rvecs, tvecs, flags, criteria); cout << "Optimized error: " << error << " | Target: " << targetError << endl; return error <= targetError; } // 标定流程控制 bool calibrationPipeline(const string& imagePath, Size boardSize, float squareSize, Mat& cameraMatrix, Mat& distCoeffs, bool useInitialGuess = false, bool useExtendedModel = false, Size& outputImageSize) { vector<String> imgPaths; glob(imagePath + "/*.bmp", imgPaths, false); if (imgPaths.empty()) { cerr << "No images found in " << imagePath << endl; return false; } // 生成物体坐标系点 vector<vector<Point3f>> objectPoints; vector<Point3f> objPattern; for (int i = 0; i < boardSize.height; ++i) for (int j = 0; j < boardSize.width; ++j) objPattern.emplace_back(j * squareSize, i * squareSize, 0); // 检测角点 vector<vector<Point2f>> imagePoints; Size imageSize; for (const auto& path : imgPaths) { Mat img = imread(path, IMREAD_GRAYSCALE); if (img.empty()) continue; imageSize = img.size(); vector<Point2f> corners; bool found = findChessboardCorners(img, boardSize, corners, CALIB_CB_ADAPTIVE_THRESH + CALIB_CB_NORMALIZE_IMAGE); if (found) { cornerSubPix(img, corners, { 11,11 }, { -1,-1 }, TermCriteria(TermCriteria::EPS + TermCriteria::COUNT, 30, 0.01)); imagePoints.push_back(corners); objectPoints.push_back(objPattern); } } if (imagePoints.size() < 3) { cerr << "Insufficient valid images: " << imagePoints.size() << endl; return false; } // 初始化参数 if (!useInitialGuess) { cameraMatrix = Mat::eye(3, 3, CV_64F); distCoeffs = Mat::zeros(useExtendedModel ? 8 : 5, 1, CV_64F); } // 首次标定 vector<Mat> rvecs, tvecs; int baseFlags = useInitialGuess ? CALIB_USE_INTRINSIC_GUESS : 0; baseFlags |= CALIB_FIX_ASPECT_RATIO | CALIB_FIX_PRINCIPAL_POINT; double rms = calibrateCamera(objectPoints, imagePoints, imageSize, cameraMatrix, distCoeffs, rvecs, tvecs, baseFlags); cout << "Initial calibration error: " << rms << endl; // 优化阶段 if (rms > 0.5) { cout << "Starting optimization..." << endl; if (!refineCalibration(objectPoints, imagePoints, cameraMatrix, distCoeffs, imageSize, 0.2, 50, useExtendedModel)) { cerr << "Optimization failed to reach target" << endl; return false; } } outputImageSize = imageSize; return true; } int main() { // 路径配置 const string calibPaths[2] = { "C://ceshitupian//ceshi", "C://ceshitupian//distance2m_G200_M0.8s_D_0.2s_No//shenrs4_33697" }; // 标定参数 const Size boardSize(20, 14); // 内角点数量 const float squareSize = 20.0f; // 棋盘格物理尺寸(mm) Size imageSize; try { // 第一阶段:扩展标定(8参数) Mat cameraMatrix, distCoeffs; cout << "===== Phase 1: Extended Calibration =====" << endl; if (!calibrationPipeline(calibPaths[0], boardSize, squareSize, cameraMatrix, distCoeffs, false, true, imageSize)) { throw runtime_error("Phase 1 calibration failed"); } cout << "\nPhase 1 Results:\n" << "Camera Matrix:\n" << cameraMatrix << "\n" << "Distortion Coeffs:\n" << distCoeffs << endl; // 第二阶段:扩展标定(8参数) cout << "\n===== Phase 2: Extended Calibration =====" << endl; Mat refinedCamera = cameraMatrix.clone(); Mat extendedDist = Mat::zeros(8, 1, CV_64F); distCoeffs.copyTo(extendedDist.rowRange(0, 5)); if (!calibrationPipeline(calibPaths[1], boardSize, squareSize, refinedCamera, extendedDist, true, true, imageSize)) { throw runtime_error("Phase 2 calibration failed"); } cout << "\nFinal Results:\n" << "Refined Camera Matrix:\n" << refinedCamera << "\n" << "Extended Distortion Coeffs:\n" << extendedDist << endl; // 保存结果 FileStorage fs("calibration_result.yml", FileStorage::WRITE); fs << "camera_matrix" << refinedCamera << "distortion_coefficients" << extendedDist << "image_width" << imageSize.width << "image_height" << imageSize.height; cout << "\nCalibration data saved to calibration_result.yml" << endl; } catch (const exception& e) { cerr << "Error: " << e.what() << endl; return EXIT_FAILURE; } return EXIT_SUCCESS; }
最新发布
05-22
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值