圖像旋轉的實現

本文轉載地址:http://blog.csdn.net/carson2005/article/details/36900401

上一篇轉載的文章(http://blog.csdn.net/carson2005/article/details/36900161)介紹了圖像旋轉的原理,這裏給出代碼實現,具體原理請參考上面的鏈接;


實現代碼:

  1. void ImgRotate(cv::Mat imgIn, float theta, cv::Mat& imgOut)  
  2. {  
  3.     int oldWidth = imgIn.cols;  
  4.     int oldHeight = imgIn.rows;  
  5.   
  6.     // 源圖四個角的座標(以圖像中心爲座標系原點)  
  7.     float fSrcX1,fSrcY1,fSrcX2,fSrcY2,fSrcX3,fSrcY3,fSrcX4,fSrcY4;  
  8.     fSrcX1 = (float) (- (oldWidth  - 1) / 2);  
  9.     fSrcY1 = (float) (  (oldHeight - 1) / 2);  
  10.     fSrcX2 = (float) (  (oldWidth  - 1) / 2);  
  11.     fSrcY2 = (float) (  (oldHeight - 1) / 2);  
  12.     fSrcX3 = (float) (- (oldWidth  - 1) / 2);  
  13.     fSrcY3 = (float) (- (oldHeight - 1) / 2);  
  14.     fSrcX4 = (float) (  (oldWidth  - 1) / 2);  
  15.     fSrcY4 = (float) (- (oldHeight - 1) / 2);  
  16.   
  17.     // 旋轉後四個角的座標(以圖像中心爲座標系原點)  
  18.     float fDstX1,fDstY1,fDstX2,fDstY2,fDstX3,fDstY3,fDstX4,fDstY4;  
  19.     fDstX1 =  cos(theta) * fSrcX1 + sin(theta) * fSrcY1;  
  20.     fDstY1 = -sin(theta) * fSrcX1 + cos(theta) * fSrcY1;  
  21.     fDstX2 =  cos(theta) * fSrcX2 + sin(theta) * fSrcY2;  
  22.     fDstY2 = -sin(theta) * fSrcX2 + cos(theta) * fSrcY2;  
  23.     fDstX3 =  cos(theta) * fSrcX3 + sin(theta) * fSrcY3;  
  24.     fDstY3 = -sin(theta) * fSrcX3 + cos(theta) * fSrcY3;  
  25.     fDstX4 =  cos(theta) * fSrcX4 + sin(theta) * fSrcY4;  
  26.     fDstY4 = -sin(theta) * fSrcX4 + cos(theta) * fSrcY4;  
  27.   
  28.     int newWidth  = ( max( fabs(fDstX4 - fDstX1), fabs(fDstX3 - fDstX2) ) + 0.5);  
  29.     int newHeight = ( max( fabs(fDstY4 - fDstY1), fabs(fDstY3 - fDstY2) )  + 0.5);  
  30.   
  31.     imgOut.create(newHeight, newWidth, imgIn.type());  
  32.   
  33.     float dx = -0.5*newWidth*cos(theta) - 0.5*newHeight*sin(theta) + 0.5*oldWidth;  
  34.     float dy = 0.5*newWidth*sin(theta) - 0.5*newHeight*cos(theta) + 0.5*oldHeight;  
  35.   
  36.     int x,y;  
  37.     for (int i=0; i<newHeight; i++)  
  38.     {  
  39.         for (int j=0; j<newWidth; j++)  
  40.         {  
  41.             x = float(j)*cos(theta) + float(i)*sin(theta) + dx;  
  42.             y = float(-j)*sin(theta) + float(i)*cos(theta) + dy;  
  43.   
  44.             if ((x<0) || (x>=oldWidth) || (y<0) || (y>=oldHeight))  
  45.             {  
  46.                 if (imgIn.channels() == 3)  
  47.                 {  
  48.                     imgOut.at<cv::Vec3b>(i,j) = cv::Vec3b(0,0,0);  
  49.                 }  
  50.                 else if (imgIn.channels() == 1)  
  51.                 {  
  52.                     imgOut.at<uchar>(i,j) = 0;  
  53.                 }  
  54.             }  
  55.             else  
  56.             {  
  57.                 if (imgIn.channels() == 3)  
  58.                 {  
  59.                     imgOut.at<cv::Vec3b>(i,j) = imgIn.at<cv::Vec3b>(y,x);  
  60.                 }  
  61.                 else if (imgIn.channels() == 1)  
  62.                 {  
  63.                     imgOut.at<uchar>(i,j) = imgIn.at<uchar>(y,x);  
  64.                 }  
  65.             }  
  66.         }  
  67.     }  
  68. }  

測試代碼:

  1. void ImgRotateTest()  
  2. {  
  3.     cv::Mat srcImg = cv::imread("test.jpg");  
  4.     if (srcImg.empty())  
  5.     {  
  6.         printf("srcImg load error \n");  
  7.         system("pause");  
  8.         exit(-1);  
  9.     }  
  10.   
  11.     float angle = 30.0f*3.1415926/180.0f;  
  12.   
  13.     cv::Mat dstImg;  
  14.     ImgRotate(srcImg, angle, dstImg);  
  15.     cv::imwrite("result.jpg", dstImg);  
  16.     cv::imshow("src", srcImg);  
  17.     cv::imshow("dst", dstImg);  
  18.     cv::waitKey(0);  
  19.   
  20. }  
發佈了33 篇原創文章 · 獲贊 6 · 訪問量 2萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章