OpenCV代码提取:rotate函数的实现
生活随笔
收集整理的這篇文章主要介紹了
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.hppmodules/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 imagecv::Point center_ = cv::Point(matSrc.cols / 2, matSrc.rows / 2);// Get the rotation matrix with the specifications abovecv::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 imagecv::Point2f center_ = cv::Point2f(matSrc.cols / 2.0, matSrc.rows / 2.0);// Get the rotation matrix with the specifications abovecv::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 imagecv::Point2f center_ = cv::Point2f(matSrc.cols / 2.0, matSrc.rows / 2.0);// Get the rotation matrix with the specifications abovecv::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的結果圖像:
GitHub:https://github.com/fengbingchun/OpenCV_Test
總結
以上是生活随笔為你收集整理的OpenCV代码提取:rotate函数的实现的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: C++11中auto的使用
- 下一篇: C++中struct的使用