opencv-矩阵与点相乘
小例程
cv::Mat rot_mat(2, 2, CV_64FC1);
double theta = 90 * M_PI / 180.;
double sin_theta = sin(theta);
double cos_theta = cos(theta);
rot_mat.at<double>(0, 0) = cos_theta;
rot_mat.at<double>(0, 1) = -sin_theta;
rot_mat.at<double>(1, 0) = sin_theta;
rot_mat.at<double>(1, 1) = cos_theta;
cv::Point2d pt(1, 0);
cv::Mat pt_trans;
//第一种方法
long long int start = cv::getTickCount();
for (int i = 0; i < 1000; i++)
pt_trans = rot_mat * cv::Mat(cv::Vec2d(pt.x, pt.y));
long long int end1 = cv::getTickCount();
//第二种方法
for (int i = 0; i < 1000; i++) {
cv::Mat pt_mat = cv::Mat::ones(2, 1, CV_64FC1);
pt_mat.at<double>(0, 0) = pt.x;
pt_mat.at<double>(1, 0) = pt.y;
pt_trans = rot_mat * pt_mat;
}
long long int end2 = cv::getTickCount();
std::cout << end1 - start << std::endl;
std::cout << end2 - end1 << std::endl;
std::getchar();
从代码简洁度看第一种方法优于第二种方法,从效率而上看第一种方法也占有较小优势,欢迎大家提出其他方法评论区讨论。