1061549 作業1
1112 Digital Image Processing Assignment #1
主題: 圖像旋轉 (Image Rotation) 撰寫一個程式將一張圖像的(a)整張圖像,(b)中心內切圓區域,旋轉一個角度(逆時針旋 轉 0 度至 359 度):利用一個滑動條(trackbar)控制旋轉角度。
開發環境: Windows 10 Visual Studio
2022 C++ OpenCV-4.7.0
程式說明:
void rotateImage(int, void*) {
Point2f center(img.cols / 2.0, img.rows / 2.0);
Mat rotationMatrix = getRotationMatrix2D(center, angle, 1.0);
if (choice == 'A' || choice == 'a') {
warpAffine(img, resultImg, rotationMatrix, img.size());
}
else if (choice == 'B' || choice == 'b') {
Mat rotated;
warpAffine(img, rotated, rotationMatrix, img.size());
int radius = min(img.cols, img.rows) / 4;
Mat mask = Mat::zeros(img.size(), CV_8UC1);
circle(mask, center, radius, Scalar(255), -1);
計算內切圓的半徑,創建一個與圖像大小相同的掩碼。使用circle函數在掩碼上繪製一個實心圓。
img.copyTo(resultImg);
rotated.copyTo(resultImg, mask);
}
imshow("Result Image", resultImg);
}
定義rotateImage回調函數,該函數根據輸入的選擇執行圖像旋轉。rotateImage函數首先計算圖像的中心點。接下來,根據當前的旋轉角度和中心點創建一個旋轉矩陣,然後,根據輸入的選擇(A或B),執行相應的操作來旋轉整個圖像或只旋轉中心的內切圓區域。
namedWindow("Result Image",
WINDOW_NORMAL);
createTrackbar("Angle", "Result Image", &angle,
359, rotateImage);
rotateImage(angle, nullptr);
創建"Result Image",並為其創建一個滑動條以調整旋轉角度。設置滑動條的回調函數為rotateImage。
以下是實作影片和相片:
https://www.youtube.com/watch?v=kT4HRmJfQ3s
留言
張貼留言