OpenCV中并没有直接提供实现rotate的函数,这里通过getRotationMatrix2D和warpAffine函数实现rotate,并增加了一个crop参数,用来判断是否进行crop。目前支持uchar和float两种类型,经测试,与OpenCV3.1结果完全一致。
公式的推导可以参考:http://blog.csdn.net/fengbingchun/article/details/17713429
实现代码rotate.hpp:
// fbc_cv is free software and uses the same licence as OpenCV
// Email: [email protected]
#ifndef FBC_CV_ROTATE_HPP_
#define FBC_CV_ROTATE_HPP_
/* reference: include/opencv2/imgproc.hpp
modules/imgproc/src/imgwarp.cpp
*/
#include "core/mat.hpp"
#include "warpAffine.hpp"
namespace fbc {
// Calculates an affine matrix of 2D rotation
// Positive values mean counter-clockwise rotation (the coordinate origin is assumed to be the top - left corner)
FBC_EXPORTS int getRotationMatrix2D(Point2f center, double angle, double scale, Mat_& dst);
// Applies an rotate to an image
// The function cannot operate in - place
// support type: uchar/float
template
int rotate(const Mat_<_Tp, chs>& src, Mat_<_Tp, chs>& dst, Point2f center, double angle,
bool crop = true, int flags = INTER_LINEAR, int borderMode = BORDER_CONSTANT, const Scalar& borderValue = Scalar())
{
FBC_Assert(typeid(float).name() == typeid(_Tp).name() || typeid(uchar).name() == typeid(_Tp).name());
FBC_Assert(src.data != NULL && src.rows > 0 && src.cols > 0);
Mat_ rot_matrix(2, 3);
getRotationMatrix2D(center, angle, 1.0, rot_matrix);
if (crop) {
if (dst.data == NULL) {
dst = Mat_<_Tp, chs>(src.rows, src.cols);
}
} else {
Rect bbox = RotatedRect(center, Size2f(src.cols, src.rows), angle).boundingRect();
double* p = (double*)rot_matrix.data;
p[2] += bbox.width / 2.0 - center.x;
p[5] += bbox.height / 2.0 - center.y;
if (dst.rows != bbox.height || dst.cols != bbox.width) {
dst = Mat_<_Tp, chs>(bbox.height, bbox.width);
}
}
warpAffine(src, dst, rot_matrix, flags, borderMode, borderValue);
return 0;
}
} // namespace fbc
#endif // FBC_CV_ROTATE_HPP_
测试代码test_rotate.cpp:
#include "test_rotate.hpp"
#include
#include
#include
int test_getRotationMatrix2D()
{
cv::Mat matSrc = cv::imread("E:/GitCode/OpenCV_Test/test_images/lena.png", 1);
if (!matSrc.data) {
std::cout << "read image fail" << std::endl;
return -1;
}
double angle = -50.0;
double scale = 0.6;
fbc::Point2f center = fbc::Point2f(matSrc.cols / 2, matSrc.rows / 2);
fbc::Mat_ mat_rot(2, 3);
fbc::getRotationMatrix2D(center, angle, scale, mat_rot);
// Compute a rotation matrix with respect to the center of the image
cv::Point center_ = cv::Point(matSrc.cols / 2, matSrc.rows / 2);
// Get the rotation matrix with the specifications above
cv::Mat mat_rot_ = cv::getRotationMatrix2D(center_, angle, scale);
assert(mat_rot.cols == mat_rot_.cols && mat_rot.rows == mat_rot_.rows);
assert(mat_rot.step == mat_rot_.step);
for (int y = 0; y < mat_rot.rows; y++) {
const fbc::uchar* p = mat_rot.ptr(y);
const uchar* p_ = mat_rot_.ptr(y);
for (int x = 0; x < mat_rot.step; x++) {
assert(p[x] == p_[x]);
}
}
return 0;
}
int test_rotate_uchar()
{
cv::Mat matSrc = cv::imread("E:/GitCode/OpenCV_Test/test_images/lena.png", 1);
if (!matSrc.data) {
std::cout << "read image fail" << std::endl;
return -1;
}
double angle = -50.0;
for (int interpolation = 0; interpolation < 5; interpolation++) {
fbc::Point2f center = fbc::Point2f(matSrc.cols / 2.0, matSrc.rows / 2.0);
fbc::Mat_ mat(matSrc.rows, matSrc.cols, matSrc.data);
fbc::Mat_ rotate_dst;
fbc::rotate(mat, rotate_dst, center, angle, true, interpolation);
// Compute a rotation matrix with respect to the center of the image
cv::Point2f center_ = cv::Point2f(matSrc.cols / 2.0, matSrc.rows / 2.0);
// Get the rotation matrix with the specifications above
cv::Mat mat_rot_ = getRotationMatrix2D(center_, angle, 1.0);
cv::Mat rotate_dst_;
cv::warpAffine(matSrc, rotate_dst_, mat_rot_, matSrc.size(), interpolation);
assert(rotate_dst.step == rotate_dst_.step && rotate_dst.rows == rotate_dst_.rows);
for (int y = 0; y < rotate_dst.rows; y++) {
const fbc::uchar* p = rotate_dst.ptr(y);
const uchar* p_ = rotate_dst_.ptr(y);
for (int x = 0; x < rotate_dst.step; x++) {
assert(p[x] == p_[x]);
}
}
}
return 0;
}
int test_rotate_float()
{
cv::Mat matSrc = cv::imread("E:/GitCode/OpenCV_Test/test_images/lena.png", 1);
if (!matSrc.data) {
std::cout << "read image fail" << std::endl;
return -1;
}
cv::cvtColor(matSrc, matSrc, CV_BGR2GRAY);
matSrc.convertTo(matSrc, CV_32FC1);
double angle = -50.0;
for (int interpolation = 0; interpolation < 5; interpolation++) {
fbc::Point2f center = fbc::Point2f(matSrc.cols / 2.0, matSrc.rows / 2.0);
fbc::Mat_ mat(matSrc.rows, matSrc.cols, matSrc.data);
fbc::Mat_ rotate_dst;
fbc::rotate(mat, rotate_dst, center, angle, true, interpolation);
// Compute a rotation matrix with respect to the center of the image
cv::Point2f center_ = cv::Point2f(matSrc.cols / 2.0, matSrc.rows / 2.0);
// Get the rotation matrix with the specifications above
cv::Mat mat_rot_ = getRotationMatrix2D(center_, angle, 1.0);
cv::Mat rotate_dst_;
cv::warpAffine(matSrc, rotate_dst_, mat_rot_, matSrc.size(), interpolation);
assert(rotate_dst.step == rotate_dst_.step && rotate_dst.rows == rotate_dst_.rows);
for (int y = 0; y < rotate_dst.rows; y++) {
const fbc::uchar* p = rotate_dst.ptr(y);
const uchar* p_ = rotate_dst_.ptr(y);
for (int x = 0; x < rotate_dst.step; x++) {
assert(p[x] == p_[x]);
}
}
}
return 0;
}
int test_rotate_without_crop()
{
cv::Mat matSrc = cv::imread("E:/GitCode/OpenCV_Test/test_images/1.jpg", 1);
if (!matSrc.data) {
std::cout << "read image fail" << std::endl;
return -1;
}
double angle = -50.0;
double scale = 0.6;
fbc::Point2f center = fbc::Point2f(matSrc.cols / 2.0, matSrc.rows / 2.0);
fbc::Mat_ mat(matSrc.rows, matSrc.cols, matSrc.data);
fbc::Mat_ rotate_dst;
fbc::rotate(mat, rotate_dst, center, angle, true/*false*/, 2, 0, fbc::Scalar(128, 255, 0));
cv::Mat mat_save(rotate_dst.rows, rotate_dst.cols, CV_8UC3, rotate_dst.data);
cv::imwrite("E:/GitCode/OpenCV_Test/test_images/1_rotate2.jpg", mat_save);
return 0;
}
以下分别是源图像,调用rotate函数,生成的crop和非crop的结果图像:
GitHub:https://github.com/fengbingchun/OpenCV_Test