C++的OpenCV版本为4.1.0,python的OpenCV版本为4.4.0;
问题表现为,getAffineTransform函数,两个版本的输出不一致;但发现与warpAffine配套使用后,表达的效果和数值一致。
更新1
有点问题没弄清,直接cout << warp_mat;可以得到对的结果,但按元素就是会出错。
更新2
是由于输出的type出现了错误,并不是定义了CV_32FC1就是这个格式的,实际上返回的是CV_64FC1,取值的时候使用double即可。
测试代码
bool test_getAffineTransform()
{
Mat img;
img = imread("E:\\test.jpg");
int height, width;
height = img.rows;
width = img.cols;
Point2f srcTri[3] = { cv::Point2f(100, 50), cv::Point2f(100, 390), cv::Point2f(600, 50) };
Point2f dstTri[3] = { cv::Point2f(200, 100), cv::Point2f(200, 330), cv::Point2f(500, 50) };
Mat warp_mat(2, 3, CV_32FC1);
warp_mat = getAffineTransform(srcTri, dstTri);
cout << warp_mat.at<Vec3f>(0,0) << endl;
cout << warp_mat.at<Vec3f>(0,0) << endl;
Mat res = Mat::zeros(img.rows, img.cols, img.type());
warpAffine(img, res, warp_mat, res.size());
//ofstream outfile("E:\\out.txt", ios::app);
//for (int i = 0; i < res.rows; i++)
//{
// for (int j = 0; j < res.cols; j++)
// {
// Vec3i bgr = res.at<Vec3b>(i, j);
// outfile << bgr.val[0] << " " << bgr.val[1] << " " << bgr.val[2];
// outfile << endl;
// }
//}
//outfile.close();
//imshow("affine", res);
//waitKey(0);
}
输出为
[4.17233e-08, 1.775, 0]
[-1.58819e-23, -1.45, -2.87352e-16]
import numpy as np
import cv2
img = cv2.imread("E:\\test.jpg")
src = np.array([[100, 50], [100, 390], [600, 50]])
dst = np.array([[200, 100], [200, 330], [500, 50]])
trans = cv2.getAffineTransform(np.float32(src), np.float32(dst))
res = cv2.warpAffine(img, trans, (img.shape[1], image.shape[0]))
cv2.imshow('affine', res)
cv2.waitKey(0)
trans的输出为
[[ 6.00000000e-01 0.00000000e+00 1.40000000e+02]
[-1.00000000e-01 6.76470588e-01 7.61764706e+01]]