0
点赞
收藏
分享

微信扫一扫

opencv 旋转图片

在觉 2023-05-09 阅读 29


旋转图像



有几种方法旋转OpenCV的图像。旋转图像中的主要问题是它会留下一些空白图像的四个角。有几种方法来应对的空白区域



下面是一个例子,旋转图像,同时保留它的大小。 




#include <opencv/cv.h>
#include <opencv/highgui.h>

IplImage *rotateImage(const IplImage *src, int angleDegrees)
{    
    IplImage *imageRotated = cvCloneImage(src);

    if(angleDegrees!=0){
        CvMat* rot_mat = cvCreateMat(2,3,CV_32FC1);
    
        // Compute rotation matrix
        CvPoint2D32f center = cvPoint2D32f( cvGetSize(imageRotated).width/2, cvGetSize(imageRotated).height/2 );
        cv2DRotationMatrix( center, angleDegrees, 1, rot_mat );

        // Do the transformation
        cvWarpAffine( src, imageRotated, rot_mat );
    }

    return imageRotated;
}


int main()
{
          
          IplImage* img;
          IplImage* rotated_img;

          int angle=0;
          
          //creating the window with a track bar
          cvNamedWindow("MyWindow");
          cvCreateTrackbar("Angle", "MyWindow", &angle, 360, 0);
          
          while(true){
              //load the original image
              img = cvLoadImage("d:/1.jpg");
              
              //rotate the image
              rotated_img=rotateImage(img,angle);

              //display the rotated image
              cvShowImage("MyWindow", rotated_img); 

              //clean up
              cvReleaseImage(&img);
              cvReleaseImage(&rotated_img);
              
              //if user press 'ESC' button, program quit the while loop
              int c=cvWaitKey(50);               
              if(c==27) break;
          }
          
          
          cvDestroyWindow("MyWindow");
          
          return 0;
}



opencv 旋转图片_ide







Explanation



Here are the new OpenCV functions , found in the above example.



  • cvCloneImage(const IplImage* src)

Makes a identical(cloned) copy of the 'src' image including header and region of interrest(ROI) 


Returns IplImage* points to the cloned image



Arguements -


  • IplImage* src - source image to be cloned


  • cvCreateMat(int rows, int columns, int types)

Creates matrix header and allocates matrix data

Returns 

CvMat*  points to the matrix


Arguements -


  • int rows - number of rows of the matrix
  • int columns - number of columns of the matrix
  • int types - CV_<no. of bits of the image data><data type of image data>C<no. of channel>

                             <no. of bits of the image data>  = 8,16,32, ....

                             <data type of image data> = 'S' for signed data, 'U' for unsigned data and  'F' for float data


                             <no. of channel> = 1,2,3


                             


                             eg. : CV_8UC1 = 8 bit unsigned image with a single channel


                                     CV_32FC3 = 32 bit float image with 3 channels





  • cvPoint2D32f (float x, float y)

It is a basic structure for 2D point with floating point cordinates

            

typedef 
   
 struct 
   
 CvPoint2D32f 
 
            { 
 
                 
 float 
   
 x 
 ; 
 
                 
 float 
   
 y 
 ; 
 
            } 
 
            CvPoint2D32f 
 ; 
 

/* Constructor */
inline CvPoint2D32f cvPoint2D32f( double x, double y );





  • cv2DRotationMatrix(CvPoint2D32f center, double angle, double scale, CvMat* mapMatrix)

Computes affine matrix of 2D rotation


Arguements - 


  • CvPoint2D32f center - specifies the ceter of rotation
  • double angle - rotation angle in degrees (Positive value means counter clockwise rotation and negative value means clockwise rotation)
  • double scale -Isotrophic scale factor
  • CvMat* mapMatrix - the pointer to the resultant matrix which should be 2x3 matrix




  • cvWarpAffine(const CvArr* src, CvArr* dst, const CvMat*

Applies an affine transformation to the source image





Arguements -



  • const CvArr* src - source image
  • CvArr* dst - destination image which is the affine transformed image of the source image
  •  const CvMat* mapMatrix - 2x3 transformation matrix



#include <opencv/cv.h>
#include <opencv/highgui.h>


IplImage *rotateImage(const IplImage *src, int angleDegrees, double zoom)
{    
    IplImage *imageRotated = cvCloneImage(src);

    
    CvMat* rot_mat = cvCreateMat(2,3,CV_32FC1);
    
    // Compute rotation matrix
    CvPoint2D32f center = cvPoint2D32f( cvGetSize(imageRotated).width/2, cvGetSize(imageRotated).height/2 );
    cv2DRotationMatrix( center, angleDegrees, zoom, rot_mat );

    // Do the transformation
    cvWarpAffine( src, imageRotated, rot_mat );
    
    return imageRotated;
}


int main()
{
          
          IplImage* img;
          IplImage* rotated_img;

          int angle=0;
          int zoom=24;
          
          //creating the window with 2 track bars
          cvNamedWindow("MyWindow");
          cvCreateTrackbar("Angle", "MyWindow", &angle, 360, 0);
          cvCreateTrackbar("Zoom", "MyWindow", &zoom, 99, 0);
          
          while(true){
              //load the original image
              img = cvLoadImage("d:/lena.jpg");
              
              //rotate the image
              rotated_img=rotateImage( img, angle, (zoom+1)/25.0 );

              //display the rotated image
              cvShowImage("MyWindow", rotated_img); 

              //clean up
              cvReleaseImage(&img);
              cvReleaseImage(&rotated_img);
              
              //if user press 'ESC' button, program quit the while loop
              int c=cvWaitKey(50);               
              if(c==27) break;
          }
          
          
          cvDestroyWindow("MyWindow");
          
          return 0;
}



opencv 旋转图片_ide_02







旋转填充式:




#include <opencv/cv.h>
#include <opencv/highgui.h>


IplImage* rotateImage(const IplImage* src, int angleDegrees)
{
    //take the dimention of original image
    int w = src->width;
    int h = src->height; 

    // Make a new image for the result
    CvSize newSize;
    newSize.width = cvRound(w);
    newSize.height = cvRound(h);
    IplImage *imageRotated = cvCreateImage( newSize, src->depth, src->nChannels );
    
    
    // Create a map_matrix, where the left 2x2 matrix is the transform and the right 2x1 is the dimensions.
    float m[6];
    CvMat M = cvMat(2, 3, CV_32F, m);
    
    float angleRadians = angleDegrees * ((float)CV_PI / 180.0f);
    m[0] = (float)( cos(angleRadians) );
    m[1] = (float)( sin(angleRadians) );
    m[3] = -m[1];
    m[4] = m[0];
    m[2] = w*0.5f;  
    m[5] = h*0.5f;  


    // Transform the image
    cvGetQuadrangleSubPix( src, imageRotated, &M);

    return imageRotated;
}


int main()
{
          
          IplImage* img;
          IplImage* rotated_img;
          int angle=0; 
       
          //creating the window with a track bar
          cvNamedWindow("MyWindow");
          cvCreateTrackbar("Angle", "MyWindow", &angle, 360, 0);
          
          while(true){
              //load the original image
              img = cvLoadImage("d:/lena.jpg");
              
              //rotate the image
              rotated_img=rotateImage(img,angle);

              //display the rotated image
              cvShowImage("MyWindow", rotated_img); 

              //clean up
              cvReleaseImage(&img);
              cvReleaseImage(&rotated_img);
              
              //if user press 'ESC' button, program quit the while loop
              int c=cvWaitKey(50);               
              if(c==27) break;
          }
          
          
          cvDestroyWindow("MyWindow");
          
          return 0;
}



opencv 旋转图片_ide_03





举报

相关推荐

0 条评论