(原)使用opencv的warpAffine函数对图像进行旋转

时间:2023-12-24 20:21:13

转载请注明出处:

http://www.cnblogs.com/darkknightzh/p/5070576.html

参考网址:

http://*.com/questions/7813376/rotate-cvmat-using-cvwarpaffine-offsets-destination-image  的plhn的回复

http://blog.csdn.net/xiaowei_cqu/article/details/7616044

http://docs.opencv.org/2.4/modules/imgproc/doc/geometric_transformations.html?highlight=warpaffine#void%20warpAffine%28InputArray%20src,%20OutputArray%20dst,%20InputArray%20M,%20Size%20dsize,%20int%20flags,%20int%20borderMode,%20const%20Scalar&%20borderValue%29

http://docs.opencv.org/2.4/doc/tutorials/imgproc/imgtrans/warp_affine/warp_affine.html

使用opencv的warpAffine函数对图像进行旋转

 Mat ImgRotate(const Mat& ucmatImg, double dDegree)
{
Mat ucImgRotate; double a = sin(dDegree * CV_PI / );
double b = cos(dDegree * CV_PI / );
int width = ucmatImg.cols;
int height = ucmatImg.rows;
int width_rotate = int(height * fabs(a) + width * fabs(b));
int height_rotate = int(width * fabs(a) + height * fabs(b)); Point center = Point(ucmatImg.cols / , ucmatImg.rows / ); Mat map_matrix = getRotationMatrix2D(center, dDegree, 1.0);
map_matrix.at<double>(, ) += (width_rotate - width) / ; // 修改坐标偏移
map_matrix.at<double>(, ) += (height_rotate - height) / ; // 修改坐标偏移 warpAffine(ucmatImg, ucImgRotate, map_matrix, { width_rotate, height_rotate },
CV_INTER_CUBIC | CV_WARP_FILL_OUTLIERS, BORDER_CONSTANT, cvScalarAll()); return ucImgRotate;
}