ภาพรวม
สาธิตการใช้ Trackbar หลายตัวในหน้าต่างเดียวกัน ควบคุม: มุมหมุน (Angle), ขนาด (Scale), และ โหมดขอบ (Border Mode) พร้อมกัน
โค้ดตัวอย่าง C++
#include <iostream>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
using namespace std;
using namespace cv;
int Rotate = 180, Scale = 50, Border = 0;
Mat before_Rotate;
int vertical = 0, horizontal = 0;
void rotator(int, void*) {
Mat Rotation = getRotationMatrix2D(Point(horizontal, vertical), Rotate - 180, Scale / 50.0);
Mat Rotated;
warpAffine(before_Rotate, Rotated, Rotation, before_Rotate.size(), INTER_LINEAR, Border, Scalar());
imshow("RotatedImage", Rotated);
}
int main(int argc, char** argv) {
before_Rotate = imread("sky.jpg");
vertical = before_Rotate.rows / 2;
horizontal = before_Rotate.cols / 2;
namedWindow("BeforeRotate"); imshow("BeforeRotate", before_Rotate);
namedWindow("AfterRotate");
createTrackbar("Angle", "AfterRotate", &Rotate, 360, rotator);
createTrackbar("Scale", "AfterRotate", &Scale, 100, rotator);
createTrackbar("Border Mode", "AfterRotate", &Border, 5, rotator);
int cb = 0; rotator(cb, &cb); // เรียกครั้งแรกเพื่อแสดงผลเริ่มต้น
waitKey(0);
return 0;
}
คำอธิบาย Trackbar
- Angle (0-360): มุมหมุน (180 = 0 องศา)
- Scale (0-100): ขนาดภาพ (50 = 1.0x, 100 = 2.0x)
- Border Mode (0-5): โหมดการจัดการขอบภาพเมื่อหมุนเกินขอบ:
- 0 = BORDER_CONSTANT (สีคงที่)
- 1 = BORDER_REPLICATE (ซ้ำขอบ)
- 2 = BORDER_REFLECT (กระจก)
- 3 = BORDER_WRAP (วนรอบ)
- 4 = BORDER_REFLECT_101
- 5 = BORDER_TRANSPARENT
ผลลัพธ์
หน้าต่าง "AfterRotate" มี Trackbar 3 ตัว ควบคุมการหมุน ขนาด และขอบภาพแบบ Real-time
