图像旋转是非常常见的图像变换,通常应用于图像矫正,在OpenCV可以使用密集仿射变换函数cv::warpAffine()实现图像旋转。为了理解图像旋转的原理,本文实现了一个图像旋转算法。

图像旋转是指将图像绕某个中心点旋转一定角度后,得到一幅新的图像。图像旋转的示意图如图1所示。其中,四边形ABCD表示需要旋转的图像区域,它经过旋转角度后得到的图像区域为四边形 A'B'C'D'。点p(x,y)为图像内任意一点,它经过旋转角度后对应的点为p'(x',y')。

pytorch旋转图像 opencv python 图像旋转_插值

图1 图像旋转示意图

图像是如何进行旋转的?通常这个过程有三个步骤。

第一步,把图像内的坐标点绕旋转中心点旋转到对应的坐标上。由于图像是通过二维数组进行保存的,所以图像的坐标点一定要落在坐标系的第一象限内,并且要保证它们是整数坐标点。通常情况下,进行旋转后得到的坐标点不是整数点也不一定在第一象限内,因此需要对旋转后得到的点进行平移取整,使得它们都是落在第一象限内的整数点。

图像内任意一点p(x,y)绕某个旋转点(X,Y)逆时针旋转角度后得到点p'(x',y')的计算公式如下:

pytorch旋转图像 opencv python 图像旋转_插值_02

 点旋转的C++实现代码如下。(OpenCV3.45+VS2019)

//将点point2绕点point1逆时针旋转angle度后得到新的点newPoint
void rotatePoint(cv::Point& point1, cv::Point& point2, cv::Point& newPoint, double angle)
{
	int dx, dy;
	double dx1, dy1;
	dy1 = -((double)point2.x - point1.x) * sin(angle) + ((double)point2.y - point1.y) * cos(angle);
	dx1 = ((double)point2.x - point1.x) * cos(angle) + ((double)point2.y - point1.y) * sin(angle);
	if (dx1 - (int)dx1 > 0.5)    //做一个四舍五入取整
		dx = (int)dx1 + 1;
	else
	{
		if (dx1 - (int)dx1 < -0.5)
			dx = (int)dx1 - 1;
		else
			dx = (int)(dx1);
	}
	if (dy1 - (int)dy1 > 0.5)   //做一个四舍五入取整
		dy = (int)dy1 + 1;
	else
	{
		if (dy1 - (int)dy1 < -0.5)
			dy = (int)dy1 - 1;
		else
			dy = (int)(dy1);
	}
	newPoint.x = point1.x + dx;
	newPoint.y = point1.y + dy;
}

用来平移坐标点的代码如下。

void translationPoint(cv::Point& point, int x, int y) //平移运算
{
	point.x = point.x + x;
	point.y = point.y + y;
}

注:平移量x与y的大小,可以根据旋转后图像的四个顶点A'、B'、C'、D'获得。

第二步,把图像对应的坐标像素大小赋给旋转后的坐标。即,图像内任意点p(x,y)对应的像素值为I(x,y),那它旋转后得到的点p'(x',y')的像素值I(x',y')=I(x,y)。

如下图2所示。当我们需要旋转的图像区域在图片内时(这区域也可以是整张图片),如何确定旋转区域ABCD是很重要的,只有这样才能判断整张图片内的哪些点是四边形ABCD区域内的。

pytorch旋转图像 opencv python 图像旋转_插值_03

 图2 图片中要旋转的区域

我们以图片的左上顶点为原点建立如图2所示的坐标系,其中四边形ABCD的四个顶点是已知的,分别为A(x0,y0)、B(x1,y1)、C(x2,y2)、D(x3,y3)。这时根据两点式可得到四条边的直线方程如下:

pytorch旋转图像 opencv python 图像旋转_计算机视觉_04

根据线性规划的知识,可以通过直线方程来表示四边形ABCD的区域。

pytorch旋转图像 opencv python 图像旋转_opencv_05

注:因为四边形ABCD内的任意点p在直线AB上方,所以直线AB方程大于等于0;点p在直线BC左侧,所以直线BC方程小于等于0。同理可得,直线CD方程小于等于0、直线AD方程大于等于0。

实现代码块如下:

std::vector<cv::Point> newPoints;
	cv::Point newP;
	for (int i = 0; i < 4; ++i)
	{
		if (points[i] != point)  //判断输入的4个顶点是否与旋转点point相同
		{
			rotatePoint(point, points[i], newP, angle);  //顶点points[i]与旋转点point不同,则进行旋转计算
			newPoints.push_back(newP);
		}
		else
		{
			newPoints.push_back(points[i]);
		}
	}
	//获取经旋转后,新图像的大小,其中w表示图像宽长,h表示图像高长。
	int w = 0, h = 0;
	int suw[4] = { newPoints[1].x - newPoints[0].x,newPoints[1].x - newPoints[3].x,
		newPoints[2].x - newPoints[0].x,newPoints[2].x - newPoints[3].x };
	int suh[4] = { newPoints[2].y - newPoints[0].y ,newPoints[2].y - newPoints[1].y,
		newPoints[3].y - newPoints[0].y,newPoints[3].y - newPoints[1].y };
	w = absMax4(suw);
	h = absMax4(suh);
	//获取需要旋转的四边形区域的外接矩形表示区域范围(x_min,y_min)、(x_max,y_max)
	int y_max, y_min, x_max, x_min;
	int points_x[4] = { points[0].x,points[1].x,points[2].x,points[3].x };
	int points_y[4] = { points[0].y,points[1].y,points[2].y,points[3].y };
	y_max = Max4(points_y);
	y_min = Min4(points_y);
	x_max = Max4(points_x);
	x_min = Min4(points_x);
	//计算向x轴的平移量dx,向y轴的平移量dy
	int dx, dy;
	int a[4] = { newPoints[0].x,newPoints[1].x,newPoints[2].x,newPoints[3].x };
	int b[4] = { newPoints[0].y,newPoints[1].y,newPoints[2].y,newPoints[3].y };
	dx = Min4(a);
	dy = Min4(b);
	//初始化输出矩阵
	if(inputMat.type() == CV_8UC1)
		cv::Mat(h, w, CV_8UC1, cv::Scalar::all(255)).copyTo(outputMat);
	if(inputMat.type() == CV_8UC3)
		cv::Mat(h, w, CV_8UC3, cv::Scalar(255, 255, 255)).copyTo(outputMat);
  //实现I(x',y')=I(x,y)
	double z1, z2, z3, z4;
	for (int i = y_min; i < y_max; ++i)
	{
		for (int j = x_min; j < x_max; ++j)
		{
				//四边形顶点A为points[0],顶点B为points[1],顶点C为points[2],顶点D为points[3].
			z1 = i - (double)points[0].y -
				(j - (double)points[0].x) * ((double)points[0].y - points[1].y) / ((double)points[0].x - points[1].x);
			z2 = j - (double)points[1].x -
				(i - (double)points[1].y) * ((double)points[1].x - points[2].x) / ((double)points[1].y - points[2].y);
			z3 = i - (double)points[2].y -
				(j - (double)points[2].x) * ((double)points[2].y - points[3].y) / ((double)points[2].x - points[3].x);
			z4 = j - (double)points[0].x -
				(i - (double)points[0].y) * ((double)points[0].x - points[3].x) / ((double)points[0].y - points[3].y);
			if (z1 >= 0 && z2 <= 0 && z3 <= 0 && z4 >= 0)
			{
				cv::Point point0(j, i);
				rotatePoint(point, point0, point0, angle);  //将点point0绕点point旋转angle度得到新的点point0
				translationPoint(point0, -dx, -dy);     //平移
				if (point0.x >= 0 && point0.x < w && point0.y >= 0 && point0.y < h)
				{
					if (inputMat.type() == CV_8UC1)
					{
						uchar* str = inputMat.ptr<uchar>(i);
						outputMat.at<uchar>(point0.y, point0.x) = str[j];
					}
					if (inputMat.type() == CV_8UC3)
					{
						cv::Vec3b* str = inputMat.ptr<cv::Vec3b>(i);
						outputMat.at<cv::Vec3b>(point0.y, point0.x) = str[j];
					}
				}
			}
		}
	}

 第三步,对旋转后的图像进行插值。由于在第一步中对旋转后的点进行了取整,这难免会使得新图像存在间隙,所以需要对这些间隙进行填充。在OpenCV中常用的插值方法有以下5种:

pytorch旋转图像 opencv python 图像旋转_插值_06

图3 常见的插值方法 

在本文中采用的插值方法与最近邻插值类似,即把最近四个方向(上下左右)的平均值作为插值。

//灰度图(CV_8UC1)的插值代码
for (int i = 1; i < outputMat.rows - 1; ++i)
		{
			for (int j = 1; j < outputMat.cols - 1; ++j)
			{
				if (outputMat.at<uchar>(i, j) == 255)
				{
					int sum = 0;
					uchar* str1 = outputMat.ptr<uchar>(i - 1);
					sum = str1[j - 1] + str1[j] + str1[j + 1];
					uchar* str2 = outputMat.ptr<uchar>(i);
					sum = sum + str2[j - 1] + str2[j + 1];
					uchar* str3 = outputMat.ptr<uchar>(i + 1);
					sum = sum + str3[j - 1] + str3[j] + str3[j + 1];
					sum = sum / 8;
					outputMat.at<uchar>(i, j) = (uchar)sum;
				}
			}
		}
///彩色图(CV_8UC3)的插值代码
for (int i = 1; i < outputMat.rows - 1; ++i)
		{
			for (int j = 1; j < outputMat.cols - 1; ++j)
			{
				if (outputMat.at<cv::Vec3b>(i, j) == cv::Vec3b(255, 255, 255))
				{
					int sum[3] = { 0,0,0 };
					uchar r, g, b;
					for (int k = 0; k < 3; k++)
					{
						cv::Vec3b* str1 = outputMat.ptr<cv::Vec3b>(i - 1);
						sum[k] = str1[j - 1][k] + str1[j][k] + str1[j + 1][k];
						cv::Vec3b* str2 = outputMat.ptr<cv::Vec3b>(i);
						sum[k] = sum[k] + str2[j - 1][k] + str2[j + 1][k];
						cv::Vec3b* str3 = outputMat.ptr<cv::Vec3b>(i + 1);
						sum[k] = sum[k] + str3[j - 1][k] + str3[j][k] + str3[j + 1][k];
						sum[k] = sum[k] / 8;
					}
					r = (uchar)sum[0];
					g = (uchar)sum[1];
					b = (uchar)sum[2];
					outputMat.at<cv::Vec3b>(i, j) = cv::Vec3b(r, g, b);
				}
			}
		}

整个算法的完整代码如下:

#include<iostream>
#include<opencv2/opencv.hpp>
  
  //计算点point2绕点point1逆时针旋转angle度后得到新的点newPoint
void rotatePoint(cv::Point& point1, cv::Point& point2, cv::Point& newPoint, double angle)
{
	int dx, dy;
	double dx1, dy1;
	dy1 = -((double)point2.x - point1.x) * sin(angle) + ((double)point2.y - point1.y) * cos(angle);
	dx1 = ((double)point2.x - point1.x) * cos(angle) + ((double)point2.y - point1.y) * sin(angle);
	if (dx1 - (int)dx1 > 0.5)    //做一个四舍五入
		dx = (int)dx1 + 1;
	else
	{
		if (dx1 - (int)dx1 < -0.5)
			dx = (int)dx1 - 1;
		else
			dx = (int)(dx1);
	}
	if (dy1 - (int)dy1 > 0.5)   //做一个四舍五入
		dy = (int)dy1 + 1;
	else
	{
		if (dy1 - (int)dy1 < -0.5)
			dy = (int)dy1 - 1;
		else
			dy = (int)(dy1);
	}
	newPoint.x = point1.x + dx;
	newPoint.y = point1.y + dy;
}

void translationPoint(cv::Point& point, int x, int y) //平移运算
{
	point.x = point.x + x;
	point.y = point.y + y;
}

int Max4(int a[4])    //获取四个数中的最大值
{
	int max = a[0];
	for (int i = 1; i < 4; i++)
	{
		if (max < a[i])
			max = a[i];
	}
	return max;
}

int Min4(int a[4])    //获取四个数中的最小值
{
	int min = a[0];
	for (int i = 1; i < 4; i++)
	{
		if (min > a[i])
			min = a[i];
	}
	return min;
}

int absMax4(int a[4])
{
	int max = 0, m;
	for (int i = 0; i < 4; i++)
	{
		if (a[i] < 0)
			m = -a[i];
		else m = a[i];
		if (max < m)
			max = m;
	}
	return max;
}

void rotateImage(cv::Mat inputMat, cv::Mat& outputMat, std::vector<cv::Point> points, cv::Point point, double angle)
{
	std::vector<cv::Point> newPoints;
	cv::Point newP;
	for (int i = 0; i < 4; ++i)
	{
		if (points[i] != point)  //判断输入的4个顶点是否与旋转点point相同
		{
			rotatePoint(point, points[i], newP, angle);  //顶点points[i]与旋转点point不同,则进行旋转计算
			newPoints.push_back(newP);
		}
		else
		{
			newPoints.push_back(points[i]);
		}
	}
	//获取经旋转后,新图像的大小,其中w表示图像宽长,h表示图像高长。
	int w = 0, h = 0;
	int suw[4] = { newPoints[1].x - newPoints[0].x,newPoints[1].x - newPoints[3].x,
		newPoints[2].x - newPoints[0].x,newPoints[2].x - newPoints[3].x };
	int suh[4] = { newPoints[2].y - newPoints[0].y ,newPoints[2].y - newPoints[1].y,
		newPoints[3].y - newPoints[0].y,newPoints[3].y - newPoints[1].y };
	w = absMax4(suw);
	h = absMax4(suh);
	//获取需要旋转的四边形区域的外接矩形表示区域范围(x_min,y_min)、(x_max,y_max)
	int y_max, y_min, x_max, x_min;
	int points_x[4] = { points[0].x,points[1].x,points[2].x,points[3].x };
	int points_y[4] = { points[0].y,points[1].y,points[2].y,points[3].y };
	y_max = Max4(points_y);
	y_min = Min4(points_y);
	x_max = Max4(points_x);
	x_min = Min4(points_x);
	//计算向x轴的平移量dx,向y轴的平移量dy
	int dx, dy;
	int a[4] = { newPoints[0].x,newPoints[1].x,newPoints[2].x,newPoints[3].x };
	int b[4] = { newPoints[0].y,newPoints[1].y,newPoints[2].y,newPoints[3].y };
	dx = Min4(a);
	dy = Min4(b);
	//初始化输出矩阵
	if(inputMat.type() == CV_8UC1)
		cv::Mat(h, w, CV_8UC1, cv::Scalar::all(255)).copyTo(outputMat);
	if(inputMat.type() == CV_8UC3)
		cv::Mat(h, w, CV_8UC3, cv::Scalar(255, 255, 255)).copyTo(outputMat);
 //实现I(x',y')=I(x,y)
	double z1, z2, z3, z4;
	for (int i = y_min; i < y_max; ++i)
	{
		for (int j = x_min; j < x_max; ++j)
		{
				//四边形顶点A为points[0],顶点B为points[1],顶点C为points[2],顶点D为points[3].
      //直线AB
			z1 = i - (double)points[0].y -
				(j - (double)points[0].x) * ((double)points[0].y - points[1].y) / ((double)points[0].x - points[1].x);
			//直线BC
			z2 = j - (double)points[1].x -
				(i - (double)points[1].y) * ((double)points[1].x - points[2].x) / ((double)points[1].y - points[2].y);
		  //直线CD
			z3 = i - (double)points[2].y -
				(j - (double)points[2].x) * ((double)points[2].y - points[3].y) / ((double)points[2].x - points[3].x);
			//直线AD
			z4 = j - (double)points[0].x -
				(i - (double)points[0].y) * ((double)points[0].x - points[3].x) / ((double)points[0].y - points[3].y);
			if (z1 >= 0 && z2 <= 0 && z3 <= 0 && z4 >= 0)
			{
				cv::Point point0(j, i);
				rotatePoint(point, point0, point0, angle);  //将点point0绕点point旋转angle度得到新的点point0
				translationPoint(point0, -dx, -dy);
				if (point0.x >= 0 && point0.x < w && point0.y >= 0 && point0.y < h)
				{
					if (inputMat.type() == CV_8UC1)
					{
						uchar* str = inputMat.ptr<uchar>(i);
						outputMat.at<uchar>(point0.y, point0.x) = str[j];
					}
					if (inputMat.type() == CV_8UC3)
					{
						cv::Vec3b* str = inputMat.ptr<cv::Vec3b>(i);
						outputMat.at<cv::Vec3b>(point0.y, point0.x) = str[j];
					}
				}
			}
		}
	}
	if (inputMat.type() == CV_8UC1)
	{   //插值
		for (int i = 1; i < outputMat.rows - 1; ++i)
		{
			for (int j = 1; j < outputMat.cols - 1; ++j)
			{
				if (outputMat.at<uchar>(i, j) == 255)
				{
					int sum = 0;
					uchar* str1 = outputMat.ptr<uchar>(i - 1);
					sum = str1[j - 1] + str1[j] + str1[j + 1];
					uchar* str2 = outputMat.ptr<uchar>(i);
					sum = sum + str2[j - 1] + str2[j + 1];
					uchar* str3 = outputMat.ptr<uchar>(i + 1);
					sum = sum + str3[j - 1] + str3[j] + str3[j + 1];
					sum = sum / 8;
					outputMat.at<uchar>(i, j) = (uchar)sum;
				}
			}
		}
	}
	if (inputMat.type() == CV_8UC3)
	{   //插值
		for (int i = 1; i < outputMat.rows - 1; ++i)
		{
			for (int j = 1; j < outputMat.cols - 1; ++j)
			{
				if (outputMat.at<cv::Vec3b>(i, j) == cv::Vec3b(255, 255, 255))
				{
					int sum[3] = { 0,0,0 };
					uchar r, g, b;
					for (int k = 0; k < 3; k++)
					{
						cv::Vec3b* str1 = outputMat.ptr<cv::Vec3b>(i - 1);
						sum[k] = str1[j - 1][k] + str1[j][k] + str1[j + 1][k];
						cv::Vec3b* str2 = outputMat.ptr<cv::Vec3b>(i);
						sum[k] = sum[k] + str2[j - 1][k] + str2[j + 1][k];
						cv::Vec3b* str3 = outputMat.ptr<cv::Vec3b>(i + 1);
						sum[k] = sum[k] + str3[j - 1][k] + str3[j][k] + str3[j + 1][k];
						sum[k] = sum[k] / 8;
					}
					r = (uchar)sum[0];
					g = (uchar)sum[1];
					b = (uchar)sum[2];
					outputMat.at<cv::Vec3b>(i, j) = cv::Vec3b(r, g, b);
				}
			}
		}
	}
}
int main()
{
	cv::Mat srcImage = cv::imread("F:/图像处理/图片一/thee2.jpg");
	//cv::Mat srcImage = cv::imread("F:/图像处理/图片一/thee2.jpg", 0);
	if (srcImage.empty())
	{
		printf("图片读取失败!\n");
		return -1;
	}
  //需要旋转的图像区域四个顶点
	std::vector<cv::Point> points;  
	points.push_back(cv::Point(0, 0));
	points.push_back(cv::Point(srcImage.cols, 0));
	points.push_back(cv::Point(srcImage.cols, srcImage.rows));
	points.push_back(cv::Point(0, srcImage.rows));
  
	cv::Mat outputImage;
	rotateImage(srcImage, outputImage, points, cv::Point(40, 70), 0.4);
	cv::imshow("原图", srcImage);
	cv::imshow("旋转得到的图像", outputImage);
	cv::waitKey(0);
	return 0;
}

算法测试:

输入的图像(大小460X613):

pytorch旋转图像 opencv python 图像旋转_opencv_07

 

//旋转区域1,其中srcImage为输入图像
std::vector<cv::Point> points;
	points.push_back(cv::Point(0, 0));    //顶点A
	points.push_back(cv::Point(srcImage.cols, 0));  //顶点B
	points.push_back(cv::Point(srcImage.cols, srcImage.rows));  //顶点C
	points.push_back(cv::Point(0, srcImage.rows));
//旋转点为Point(40, 70),旋转角度0.4

旋转得到的结果如下:

pytorch旋转图像 opencv python 图像旋转_学习_08

 

//旋转区域2,其中srcImage为输入图像
std::vector<cv::Point> points;
	points.push_back(cv::Point(70, 0));    //顶点A
	points.push_back(cv::Point(srcImage.cols-50, 0));
	points.push_back(cv::Point(srcImage.cols-100, srcImage.rows-100));
	points.push_back(cv::Point(0, srcImage.rows-50));
//旋转点为Point(30, 50),旋转角度0.2

旋转得到的结果如下:

pytorch旋转图像 opencv python 图像旋转_插值_09