Ask Your Question

Revision history [back]

This is the code that I am using for rotating images around their center point

// Return the rotation matrices for each rotation
void rotate(Mat& src, double angle, Mat& dst)
{
    Point2f pt(src.cols/2., src.rows/2.);
    Mat r = getRotationMatrix2D(pt, angle, 1.0);
    warpAffine(src, dst, r, cv::Size(src.cols, src.rows));
}

And then in code you simply do something like

int number_of_steps = degree_total / degree_step;
vector<Mat> rotated_matrices;
vector<Mat> all_rotation_matrices;
for ( int i = 0; i < number_of_steps; i ++ ){
     // Rotate the image
     Mat rotated = frame.clone();
     rotate(frame, (i+1)*degree_step, rotated);
     // Add to the collection of rotated images
     rotated_matrices.push_back(rotated);
 }

Simply keep in mind that this will cut off information of your image (which wasn't bad for my case). To avoid this you should perform these preprocessing steps

  • Read in the original image
  • Define the largest dimension, either rows or cols
  • Now create a new black image with dimensions [largestDimension x largestDimension]
  • Then rotate using the code above, no information will be lost
  • Once you reach the rotation, crop again your region of interest around the center point to loose the extra black space!

EDIT: adapted my own code a bit to get the desired result and make it more clear!

This is the code that I am using for rotating images around their center point

// Return the rotation matrices for each rotation
void rotate(Mat& src, double angle, Mat& dst)
{
    Point2f pt(src.cols/2., src.rows/2.);
    Mat r = getRotationMatrix2D(pt, angle, 1.0);
    warpAffine(src, dst, r, cv::Size(src.cols, src.rows));
}

And then in code you simply do something likelike this code snippet (tested with latest 2.4 OpenCV branch)

#include <iostream>
#include "opencv2/opencv.hpp"

using namespace std;
using namespace cv;

// Return the rotation matrices for each rotation
void rotate(Mat& src, double angle, Mat& dst)
{
    Point2f pt(src.cols/2., src.rows/2.);
    Mat r = getRotationMatrix2D(pt, angle, 1.0);
    warpAffine(src, dst, r, cv::Size(src.cols, src.rows));
}

int number_of_steps = degree_total / degree_step;
vector<Mat> rotated_matrices;
vector<Mat> all_rotation_matrices;
for main()
{
    // Read in the image
    Mat input = imread("/data/test/image.jpg");
    imshow("input", input);

    // Make larger image
    int rows = input.rows;
    int cols = input.cols;
    int largest = 0;
    if ( int i = 0; i < number_of_steps; i ++ rows > cols ){
        largest = rows;
    }else{
        largest = cols;
    }
    Mat temp = Mat::zeros(largest, largest, CV_8UC3);

    // Copy your original image
    // First define the roi in the large image --> draw this on a paper to make it clear
    // There are two possible cases
    Rect roi;
    if (input.rows > input.cols){
        roi = Rect((temp.cols - input.cols)/2, 0, input.cols, input.rows);
    }
    if (input.cols > input.rows){
        roi = Rect(0, (temp.rows - input.rows)/2, input.cols, input.rows);
    }

    // Copy the original to the black large temp image
    input.copyTo(temp(roi));

    // Rotate the image
     Mat rotated = frame.clone();
     rotate(frame, (i+1)*degree_step, temp.clone();
    rotate(temp, 90, rotated);
     // Add to the collection of rotated images
     rotated_matrices.push_back(rotated);
 
    imshow("rotated", rotated);

    // Now cut it out again
    Mat result = rotated(Rect(roi.y, roi.x, roi.height, roi.width)).clone();

    imshow("result", result);

    waitKey(0);
    return 0;
}

Simply keep Which results into the following images in mind that this will cut off information of your image (which wasn't bad for my case). To avoid this you should perform these preprocessing steps

  • Read in the original image
  • Define the largest dimension, either rows or cols
  • Now create a new black image with dimensions [largestDimension x largestDimension]
  • Then rotate using the code above, no information will be lost
  • Once you reach the rotation, crop again your region of interest around the center point to loose the extra black space!
order [original - rotated - cropped result]

image description

image description

image description