ภาพรวม
การหมุนวิดีโอคล้ายการหมุนภาพ แต่แทนที่จะโหลดภาพนิ่ง ให้อ่านเฟรมจาก VideoCapture (กล้องหรือไฟล์วิดีโอ) แล้วหมุนทีละเฟรม
ฟังก์ชันสำคัญ
getRotationMatrix2D(center, angle, scale)→ สร้าง Affine Transformation Matrix สำหรับหมุน 2DwarpAffine(src, dst, M, size)→ นำ Transformation Matrix ไปประยุกต์กับภาพ
โค้ดตัวอย่าง 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[]) {
VideoCapture loadvideo(0);
namedWindow("OriginalVideo");
namedWindow("RotatedVideo");
int rotating_angle = 180;
createTrackbar("Rotation", "RotatedVideo", &rotating_angle, 360);
while (true) {
Mat before_Rotating; loadvideo.read(before_Rotating);
imshow("OriginalVideo", before_Rotating);
// จุดหมุน = จุดกึ่งกลางภาพ, Angle = rotating_angle - 180 (ให้ 0 = ไม่หมุน)
Mat for_Rotation = getRotationMatrix2D(Point(before_Rotating.cols/2, before_Rotating.rows/2), rotating_angle - 180, 1);
Mat after_Rotating;
warpAffine(before_Rotating, after_Rotating, for_Rotation, before_Rotating.size());
imshow("RotatedVideo", after_Rotating);
if (waitKey(30) == 27) break;
}
return 0;
}
คำอธิบาย
- Trackbar "Rotation" ค่า 0-360 แปลงเป็น Angle -180 ถึง +180
- จุดหมุน = จุดกึ่งกลางภาพ
(cols/2, rows/2) - Scale = 1 (ขนาดเดิม)
- กด ESC เพื่อออก
ผลลัพธ์
แสดง 2 หน้าต่าง: วิดีโอต้นฉบับ และ วิดีโอที่หมุนแล้ว (ปรับมุมได้จาก Trackbar)
