Easiest way to rotate by 90 degrees an image using OpenCV?

后端 未结 7 1080
轮回少年
轮回少年 2020-11-29 05:52

What is the best way (in c/c++) to rotate an IplImage/cv::Mat by 90 degrees? I would assume that there must be something better than transforming it using a matrix, but I ca

7条回答
  •  春和景丽
    2020-11-29 06:07

    Well I was looking for some details and didn't find any example. So I am posting a transposeImage function which, I hope, will help others who are looking for a direct way to rotate 90° without losing data:

    IplImage* transposeImage(IplImage* image) {
    
        IplImage *rotated = cvCreateImage(cvSize(image->height,image->width),   
            IPL_DEPTH_8U,image->nChannels);
        CvPoint2D32f center;
        float center_val = (float)((image->width)-1) / 2;
        center.x = center_val;
        center.y = center_val;
        CvMat *mapMatrix = cvCreateMat( 2, 3, CV_32FC1 );        
        cv2DRotationMatrix(center, 90, 1.0, mapMatrix);
        cvWarpAffine(image, rotated, mapMatrix, 
            CV_INTER_LINEAR + CV_WARP_FILL_OUTLIERS, 
            cvScalarAll(0));      
        cvReleaseMat(&mapMatrix);
    
        return rotated;
    }
    

    Question : Why this?

    float center_val = (float)((image->width)-1) / 2; 
    

    Answer : Because it works :) The only center I found that doesn't translate image. Though if somebody has an explanation I would be interested.

提交回复
热议问题