通过transpose和flip实现图像旋转90/180/270度

在fbc_cv库中,提供了对图像进行任意角度旋转的函数rotate,其实内部也是调用了仿射变换函数warpAffine。如果图像仅是进行90度倍数的旋转,是没有必要用warpAffine函数的。这里通过transpose和flip函数实现对图像进行顺时针90度、180度、270度的旋转。

用fbc::transpose、fbc::flip和cv::transpose、cv::flip实现的结果是完全一致的。

通过fbc_cv库实现代码如下:

#include "test_rotate90.hpp"
#include <opencv2/opencv.hpp>
#include <transpose.hpp>
#include <flip.hpp>

int test_rotate90()
{
	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;
	}

	int width = matSrc.cols;
	int height = matSrc.rows;

	fbc::Mat_<uchar, 3> mat1(height, width, matSrc.data);
	fbc::Mat_<uchar, 3> matTranspose(width, height);
	fbc::transpose(mat1, matTranspose);

	// clockwise rotation  90
	fbc::Mat_<uchar, 3> matRotate90(width, height);
	fbc::flip(matTranspose, matRotate90, 1);
	cv::Mat tmp2(width, height, CV_8UC3, matRotate90.data);
	cv::imwrite("E:/GitCode/OpenCV_Test/test_images/rotate_90.jpg", tmp2);

	// clockwise rotation 180
	fbc::Mat_<uchar, 3> matRotate180(height, width);
	fbc::flip(mat1, matRotate180, -1);
	cv::Mat tmp3(height, width, CV_8UC3, matRotate180.data);
	cv::imwrite("E:/GitCode/OpenCV_Test/test_images/rotate_180.jpg", tmp3);

	// clockwise rotation 270
	fbc::Mat_<uchar, 3> matRotate270(width, height);
	fbc::flip(matTranspose, matRotate270, 0);
	cv::Mat tmp4(matTranspose.rows, matTranspose.cols, CV_8UC3, matRotate270.data);
	cv::imwrite("E:/GitCode/OpenCV_Test/test_images/rotate_270.jpg", tmp4);

	return 0;
}

结果图像如下:

原图

顺时针旋转90度 

 顺时针旋转180度


                                                             顺时针旋转270度                                                                


GitHubhttps://github.com/fengbingchun/OpenCV_Test


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值