背景:今天需要對程序生成的圖像進行旋轉90度和下采樣操作,當然還有改變圖像類型的操作,就是把原來.png的圖像轉換為.jpg的圖像,主要是我目前使用libharu庫,無法成功從本地加載png圖像到pdf中去,不得不使用jpg圖像。我的圖像是橫向的,為了能夠更大的呈現在pdf中,我需要將圖像旋轉90度,得到豎向的圖像。
我最初使用的方法是這樣的
?
cv::Mat temp, dest;
cv::Mat img1 = cv::imread("dancer.png");cv::imshow("org", img1);
對讀入的圖像進行旋轉90度
cv::Point2f center(img1.cols / 2, img1.rows / 2);
cv::Mat M = getRotationMatrix2D(center, 90, -1);
warpAffine(img1, dest, M, cv::Size(img1.cols, img1.rows));cv::imshow("dest", dest);
//將旋轉后的圖像降分辨率cv::imwrite("img1.png", dest);
cv::waitKey(0);
經過上面的方式對圖像旋轉90度后,得到的圖像如下圖,硬生生被截掉一截,
?
后來找到了這種方法,直接可以旋轉90度,180度
cv::Mat temp, dest;
cv::Mat cover = cv::imread("dancer.png");
cv::imshow("org", cover);
transpose(cover, temp);
flip(temp, dest, 1);
cv::imshow("temp", temp);
cv::imshow("dest", dest);
cv::imwrite("temp.png", temp);
cv::imwrite("dest.png", dest);
cv::waitKey(0);
原始圖像
?
經過tranpose進行變換的圖像,達到的效果是對原圖像順時針旋轉90度,且進行鏡面變換。
?
既然進行了鏡面變換,那我再給他鏡面回來不久好了。使用flip函數進行鏡面變換,我們可以看到,下面的圖像就是將原始圖像順時針旋轉90度的結果了。
?
下面實現將圖像旋轉180度。
cv::Mat temp, dest;
cv::Mat cover = cv::imread("dancer.png");
cv::imshow("org", cover);
flip(cover, dest, -1);
cv::imshow("dest", dest);
cv::imwrite("dest.png", dest);
cv::waitKey(0);
?