图像旋转原理+代码:opencv+C++

原理:图像旋转是指以图像中的某一点为原点,以逆时针或顺时针旋转。通常是绕图像的起始点以逆时针旋转。

i^{^{'}} =icos\theta - jsin\theta

j^{'}=isin\theta + jcos\theta

C++代码:

#include 
#include 
#include 

using namespace cv;
using namespace std;

#define pi 3.1415926

int main()
{
	Mat img = imread("C:\\Users\\Yang\\Desktop\\0.bmp", 0);
	imshow("ԭͼ", img);

	 
	int rows = img.rows, cols = img.cols;
	printf("rows=%d cols=%d \n", rows, cols);
	Mat row_Mat = Mat::zeros(img.size(), CV_64F);
	Mat col_Mat = Mat::zeros(img.size(), CV_64F);

	ofstream fout("C:\\Users\\Yang\\Desktop\\data.txt");
	double teta = 30 * pi / 180;
	for (int i = 0; i < rows; i++)
	{
		for (int j = 0; j < cols; j++)
		{
			row_Mat.at(i, j) = round(i*cos(teta) - j*sin(teta));
			col_Mat.at(i, j) = round(i*sin(teta) + j*cos(teta));
			fout << "rows= " << row_Mat.at(i, j) << " " << "cols= " << col_Mat.at(i, j) << endl;
			//fOut << "rows=" << row_Mat.at(i, j) << " " << "cols=" << col_Mat.at(i, j)<(i, j) = row_Mat.at(i, j) + add_value;
				fout_row << "row=" << row_Mat.at(i, j) << endl;
			}	
		}
	}
	fout_row.close();
	ofstream fout_col("C:\\Users\\Yang\\Desktop\\col.txt");
	if (minv_col <= 0)
	{
		double add_value = 1 - minv_col;
		for (int i = 0; i < rows; i++)
		{
			for (int j = 0; j < cols; j++)
			{
				col_Mat.at(i, j) = col_Mat.at(i, j) + add_value;
				fout_col << "col=" << col_Mat.at(i, j) << endl;
			}
		}
	}
	fout_col.close();

	int nmaxv_row = (int)maxv_row;
	int nmaxv_col = (int)maxv_col;
	Mat newImg = Mat::zeros(nmaxv_row, nmaxv_col, CV_8U);

	row_Mat.convertTo(row_Mat, CV_8U);
	col_Mat.convertTo(col_Mat, CV_8U);
	for (int i = 0; i < rows; i++)
	{
		for (int j = 0; j < cols; j++)
		{
			newImg.at(row_Mat.at(i, j), col_Mat.at(i, j)) = img.at(i, j);
		}
	}

	imshow("xiaoguotu", newImg);
	waitKey();
	return 0;
}

 

你可能感兴趣的:(图像旋转原理+代码:opencv+C++)