Question
I need to rotate an image 45degrees, I can not use the cv.warpAffine or cv.warpPerspective functions. This is what I have so far. The function
I need to rotate an image 45degrees, I can not use the cv.warpAffine or cv.warpPerspective functions. This is what I have so far.
The function rotate needs to have parameters Mat rotate (Mat image, int transform[][]) and return a Mat rotatedImage that will be saved and displayed
image is the given .jpg image and transform[][] is the rotate matrix.
#include
using namespace cv; using namespace std;
Mat rotate(Mat image, int transform[][3]);
int main(int argv, char** argc) { Mat image = imread("lenna.jpg", IMREAD_COLOR); int theta = 45; double PI = 3.1415926535897932;
//rotate image matrix int rotate[3][3] = { {cos(theta * PI / 180) , -sin(theta * PI / 180) , 0}, {sin(theta * PI / 180) , cos(theta * PI / 180) , 0}, {0 , 0 , 1} };
Mat rotatedImg = rotate(image, rotate); imshow("Display window with Scaled image", rotatedImg); imwrite("scaledimage.JPG", rotatedImg);
int c = waitKey(0);
}
Mat rotate(Mat image, int transform[][3]) { Mat scaledImage = Mat::zeros( image.rows + transform[2][0], image.cols + transform[2][1] , CV_8UC3);
for (int x = 0; x < image.cols; x++) { for (int y = 0; y < image.rows; y++) { Vec3b pixel = image.at
int nx = x * transform[0][0] + y * transform[1][0] + transform[2][0]; int ny = x * transform[0][1] + y * transform[1][1] + transform[2][1]; scaledImage.at
} }
return scaledImage; }
**************DONT GIVE THIS ANSWER***************
PROGRAM CODE:
#include
#include
using namespace std;
// Using macros to convert degree to radian
// and call sin() and cos() as these functions
// take input in radians
#define SIN(x) sin(x * 3.141592653589 / 180)
#define COS(x) cos(x * 3.141592653589 / 180)
// To rotate an object given as order set of points in a[]
// (x_pivot, y_pivot)
void rotate(float a[][2], int n, int x_pivot, int y_pivot,
int angle)
{
int i = 0;
while (i < n) {
// Shifting the pivot point to the origin
// and the given points accordingly
int x_shifted = a[i][0] - x_pivot;
int y_shifted = a[i][1] - y_pivot;
// Calculating the rotated point co-ordinates
// and shifting it back
a[i][0] = x_pivot
+ (x_shifted * COS(angle)
- y_shifted * SIN(angle));
a[i][1] = y_pivot
+ (x_shifted * SIN(angle)
+ y_shifted * COS(angle));
cout << "(" << a[i][0] << ", " << a[i][1] << ") ";
i++;
}
}
// Driver Code
int main()
{
// 1st Example
// The following figure is to be
// rotated about (0, 0) by 90 degrees
int size1 = 4; // No. of vertices
// Vertex co-ordinates must be in order
float points_list1[][2] = { { 100, 100 },
{ 150, 200 },
{ 200, 200 },
{ 200, 150 } };
rotate(points_list1, size1, 0, 0, 90);
// 2nd Example
// The following figure is to be
// rotated about (50, -50) by -45 degrees
/*int size2 = 3;//No. of vertices
float points_list2[][2] = {{100, 100}, {100, 200},
{200, 200}};
rotate(points_list2, size2, 50, -50, -45);*/
return 0;
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started