ภาพรวม
สาธิตการ หมุนภาพ (Image Rotation) แบบพื้นฐาน ด้วย getRotationMatrix2D และ warpAffine พร้อม Trackbar ปรับมุมแบบ Real-time
โค้ดตัวอย่าง C++
#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");
namedWindow("BeforeRotation"); imshow("BeforeRotation", before_rotation);
namedWindow("AfterRotation");
int Rotation = 180;
createTrackbar("Rotation", "AfterRotation", &Rotation, 360);
int Height = before_rotation.rows / 2;
int Width = before_rotation.cols / 2;
while (true) {
Mat for_Rotation = getRotationMatrix2D(Point(Width, Height), Rotation - 180, 1);
Mat for_Rotated;
warpAffine(before_rotation, for_Rotated, for_Rotation, before_rotation.size());
imshow("AfterRotation", for_Rotated);
int termination = waitKey(30);
if (termination == 27) break;
}
return 0;
}
คำอธิบาย
getRotationMatrix2D(center, angle, scale): สร้าง Matrix 2x3 สำหรับ Affine Transformcenter: จุดหมุน (จุดกึ่งกลางภาพ)angle: มุมหมุน (องศา, บวก = ทวนเข็มนาฬิกา)scale: ค่าปรับขนาด (1.0 = เดิม)warpAffine(): นำ Matrix ไป Transform ภาพ- Trackbar 0-360 → Angle -180 ถึง +180
ผลลัพธ์
แสดง 2 หน้าต่าง: ภาพต้นฉบับ และ ภาพที่หมุนแล้ว (ปรับมุมได้จาก Trackbar)
