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

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 .
M 2×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(即指定常数值填充) ,实质上,边界处理类型,该枚举型还有:

Enumerator
BORDER_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
  • 6
    点赞
  • 34
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

AI吃大瓜

尊重原创,感谢支持

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值