OpenCV代码提取:rotate函数的实现

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: fengbingchun@163.com

#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_<double, 1>& dst);

// Applies an rotate to an image
// The function cannot operate in - place
// support type: uchar/float
template<typename _Tp, int chs>
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_<double, 1> 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 <assert.h>
#include <opencv2/opencv.hpp>
#include <rotate.hpp>

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_<double, 1> 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_<uchar, 3> mat(matSrc.rows, matSrc.cols, matSrc.data);
		fbc::Mat_<uchar, 3> 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_<float, 1> mat(matSrc.rows, matSrc.cols, matSrc.data);
		fbc::Mat_<float, 1> 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_<uchar, 3> mat(matSrc.rows, matSrc.cols, matSrc.data);
	fbc::Mat_<uchar, 3> 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的结果图像:

GitHubhttps://github.com/fengbingchun/OpenCV_Test

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值