OpenCV 的AffineTransform(传说中的仿射变换)
Mat affineTransform(Mat src, std::vector<float>& v)
{
Mat rot_mat(2, 3, CV_32FC1);
Mat dst = Mat::zeros(src.rows, src.cols, src.type());
/** Rotating the image after Warp */
/// Compute a rotation matrix with respect to the center of the image
Point center = Point(dst.cols / 2, dst.rows / 2);
double scale = 0.7;
double angle = rand()%720;
/// Get the rotation matrix with the specifications above
rot_mat = getRotationMatrix2D(center, angle, scale);
double a11 = rot_mat.at<double>(0,0);
double a12 = rot_mat.at<double>(0,1);
double a21 = rot_mat.at<double>(1,0);
double a22 = rot_mat.at<double>(1,1);
double b11 = rot_mat.at<double>(0,2);
double b12 = rot_mat.at<double>(1,2);
/// Rotate the warped image
warpAffine(src, dst, rot_mat, dst.size());
int ftx = v[0]*src.cols;;
int fty = v[1]*src.rows;
int bnx = v[2]*src.cols;
int bny = v[3]*src.rows;
//calculation of new label
float cftx = (ftx*a11+fty*a12+b11)/src.cols;
float cfty = (ftx*a21+fty*a22+b12)/src.rows;
float cbnx = (bnx*a11+bny*a12+b11)/src.cols;
float cbny = (bnx*a21+bny*a22+b12)/src.rows;
//write vector
v.erase(v.begin(),v.end());
v.push_back(cftx);v.push_back(cfty);
v.push_back(cbnx);v.push_back(cbny);
return dst;
}
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。