OpenCV图像旋转,指定填充背景颜色边界颜色

来源:互联网 发布:百度一下淘宝男裤 编辑:程序博客网 时间:2024/06/08 04:09

OpenCV图像旋转,指定填充背景颜色边界颜色

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
srcinput image.dstoutput image that has the size dsize and the same type as src .M2×3 transformation matrix.dsizesize of the output image.flagscombination of interpolation methods (see cv::InterpolationFlags) and the optional flag WARP_INVERSE_MAP that means that M is the inverse transformation ( dstsrc ).borderModepixel 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.borderValuevalue 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())
  InputArray src:输入的图像C++: void warpAffine(InputArray src, OutputArray dst, InputArray M, Size dsize, int flags=INTER_LINEAR, intborderMode=BORDER_CONSTANT, const Scalar& borderValue=Scalar())
  第1个参数:OutputArray dst:输出的图像 
  第2个参数:InputArray M:透视变换的矩阵
  第3个参数:Size dsize:输出图像的大小
  第4个参数:int flags=INTER_LINEAR:输出图像的插值方法,可以为
            INTER_LINEAR 线性插值;
            INTER_NEAREST 最近邻插值;
            INTER_AREA 区域插值
            INTER_CUBIC 三次条样插值
            CV_WARP_INVERSE_MAP:指定 matrix 是输出图像到输入图像的反变换,因此可以直接用来做象素插值。否则, 函数从 map_matrix 得到反变换。
            CV_WARP_FILL_OUTLIERS:填充所有缩小图像的象素。如果部分象素落在输入图像的边界外,那么它们的值设定为 fillval(fillval
用来填充边界外面的值).

  第5个参数:int borderMode:图像边界的处理方式,默认是BORDER_CONSTANT(即指定常数值填充) ,实质上,边界处理类型,该枚举型还有:

EnumeratorBORDER_CONSTANT 

iiiiii|abcdefgh|iiiiiii with some specified i(指定常数填充)

BORDER_REPLICATE 

aaaaaa|abcdefgh|hhhhhhh(复制边缘像素填充)

BORDER_REFLECT 

fedcba|abcdefgh|hgfedcb(反射复制边界像素)

BORDER_WRAP 

cdefgh|abcdefgh|abcdefg

BORDER_REFLECT_101 

gfedcb|abcdefgh|gfedcba(对称填充,也就是以最边缘像素为轴)

BORDER_TRANSPARENT 

uvwxyz|absdefgh|ijklmno

BORDER_REFLECT101 

same as BORDER_REFLECT_101

BORDER_DEFAULT 

same as BORDER_REFLECT_101

BORDER_ISOLATED 

do not look outside of ROI

  第6个参数:const Scalar& borderValue=Scalar():边界的颜色设置,一般默认是0。
(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