OpenCV의 내장 기능을 사용하여 이미지를 회전하는 것은 쉬운 작업입니다. 이미지를 회전하려면 'highgui.hpp' 및 'imgproc.hpp' 헤더 파일을 사용해야 하며 이 프로그램에서 이미지 회전을 처리하는 더 많은 기능을 소개할 것입니다.
다음 프로그램은 C++를 사용하여 OpenCV에서 이미지를 회전하는 방법입니다.
예시
#include<iostream>
#include<opencv2/highgui/highgui.hpp>
#include<opencv2/imgproc/imgproc.hpp>
using namespace std;
using namespace cv;
int main(int argc, char** argv) {
Mat before_rotation = imread("bright.jpg");//loading image to a matrix
namedWindow("BeforeRotation");//Declaring window to show the original image//
imshow("BeforeRotation", before_rotation);//showing the image before rotation//
namedWindow("AfterRotation");//declaring window to show rotated image//
int Rotation = 180;//initialization rotation angle//
createTrackbar("Rotation", "AfterRotation", &Rotation, 360);//creating trackbar//
int Height = before_rotation.rows / 2;//getting middle point of rows//
int Width = before_rotation.cols / 2;//getting middle point of height//
while (true) {
Mat for_Rotation = getRotationMatrix2D(Point(Width, Height), (Rotation - 180), 1);//affine transformation matrix for 2D rotation//
Mat for_Rotated;//declaring a matrix for rotated image
warpAffine(before_rotation, for_Rotated, for_Rotation, before_rotation.size());//applying affine transformation//
imshow("AfterRotation", for_Rotated);//show rotated image//
int termination = waitKey(30);//allow system 30 millisecond time to create the rottion effect//
if (termination == 27){ //terminate if Esc button is pressed//
break;
}
}
return 0;
} 출력
