OpenCV与图像旋转有关的函数:
(1)warpAffine函数
void cv::warpAffine ( InputArray src, OutputArray dst, InputArray M, Size dsize, int flags = INTER_LINEAR
,int borderMode = BORDER_CONSTANT
,const Scalar & borderValue = Scalar()
) Applies an affine transformation to an image.
The function warpAffine transforms the source image using the specified matrix:
dst(x,y)=src(M11x+M12y+M13,M21x+M22y+M23)when the flag WARP_INVERSE_MAP is set. Otherwise, the transformation is first inverted with cv::invertAffineTransform and then put in the formula above instead of M. The function cannot operate in-place.
- Parameters
src input image. dst output image that has the size dsize and the same type as src . M 2×3 transformation matrix. dsize size of the output image. flags combination of interpolation methods (see cv::InterpolationFlags) and the optional flag WARP_INVERSE_MAP that means that M is the inverse transformation ( dst→src ). borderMode pixel extrapolation method (see cv::BorderTypes); when borderMode=BORDER_TRANSPARENT, it means that the pixels in the destination image corresponding to the "outliers" in the source image are not modified by the function. borderValue value used in case of a constant border; by default, it is 0.
中文解释:
C++: void warpAffine(InputArray src, OutputArray dst, InputArray M, Size dsize, int flags=INTER_LINEAR, intborderMode=BORDER_CONSTANT, const Scalar& borderValue=Scalar())
第5个参数:int borderMode:图像边界的处理方式,默认是BORDER_CONSTANT(即指定常数值填充) ,实质上,边界处理类型,该枚举型还有:
Enumerator | |
---|---|
BORDER_CONSTANT |
|
BORDER_REPLICATE |
|
BORDER_REFLECT |
|
BORDER_WRAP |
|
BORDER_REFLECT_101 |
|
BORDER_TRANSPARENT |
|
BORDER_REFLECT101 | same as BORDER_REFLECT_101 |
BORDER_DEFAULT | same as BORDER_REFLECT_101 |
BORDER_ISOLATED | do not look outside of ROI |
(2)getRotationMatrix2D函数 Mat getRotationMatrix2D(Point2f center, double angle, double scale) 参数详解: Point2f center:表示旋转的中心点 double angle:表示旋转的角度 double scale:图像缩放因子
例子:
int main() { Mat src = imread("D:\\OpencvTest\\test1.jpg"); cv::Mat dst; //float scale = 200.0/ src.rows;//缩放因子 //cv::resize(src, src, cv::Size(), scale, scale, cv::INTER_LINEAR); //旋转角度-20度 double angle = -20; //输出图像的尺寸与原图一样 cv::Size dst_sz(src.cols, src.rows); //指定旋转中心 cv::Point2f center(src.cols / 2., src.rows / 2.); //获取旋转矩阵(2x3矩阵) cv::Mat rot_mat = cv::getRotationMatrix2D(center, angle, 1.0); //设置选择背景边界颜色:绿色 cv::Scalar borderColor = Scalar(0, 238, 0); cv::warpAffine(src, dst, rot_mat, src.size(), INTER_LINEAR, BORDER_CONSTANT, borderColor); //cv::warpAffine(src, dst, rot_mat, dst_sz, INTER_LINEAR, BORDER_REPLICATE); //显示旋转效果 cv::imshow("src image ", src); cv::imshow("Rotation Image", dst); waitKey(0); return 0; return 0; }
运行效果:改为BORDER_REPLICATE:复制边缘填充,其效果如下:cv::warpAffine(src, dst, rot_mat, dst_sz, INTER_LINEAR, BORDER_REPLICATE);
【尊重原创,转载请注明出处】 http://blog.csdn.net/guyuealian/article/details/77993410