背景
想执行一个平移变换
try
{
cv::Mat original_points = (cv::Mat_<int>(3, 2, CV_8UC1) << 1, 1, 1, 2, 3, 1);
// Define the transformed coordinates
cv::Mat transformed_points = (cv::Mat_<int>(3, 2) << 2, 2, 2, 3, 4, 2);
// Calculate the transformation matrix
cv::Mat T = cv::getAffineTransform(original_points, transformed_points);
// Print the transformation matrix
/*qDebug() << "Transformation matrix:";
qDebug() << cv::format(original_points, cv::Formatter::FMT_C);*/
std::cout << "T value is: \r\n" << original_points << "\r\n channels:" <<
original_points.channels() << std::endl;
std::cout << "T value is: \r\n" << T << std::endl;
//qDebug() << T ;
//Use the transformation matrix to transform a new point
cv::Mat new_point = (cv::Mat_<float>(1, 1) << 4);
cv::Mat transformed_point;
cv::transform(new_point, transformed_point, T);
// Print the transformed point
std::cout << "Transformed point:" << std::endl;
std::cout << transformed_point << std::endl;
}
catch (const cv::Exception& e)
{
std::cout << e.what();
}
结果报错:
Error: Assertion failed (src.checkVector(2, CV_32F) == 3 && dst.checkVector(2, CV_32F) == 3) in cv::getAffineTransform, file D:\opencv\opencv-3.4.16\modules\imgproc\src\imgwarp.cpp, line 3407
将矩阵的类型修改如下:
cv::Mat transformed_points = (cv::Mat_<float>(3, 2) << 2, 2, 2, 3, 4, 2);
此问题解决,但是报错:
Assertion failed (scn == m.cols || scn + 1 == m.cols) in cv::transform, file D:\opencv\opencv-3.4.16\modules\core\src\matmul.dispatch.cpp
error: (-215:Assertion failed) scn == m.cols || scn + 1 == m.cols in function 'cv::transform'
这个问题在于transform()用错了,gpt有毒啊,这里应该用warpAffine();