4

everyone I just referenced a rotating image sample from: Rotate an image without cropping in OpenCV in C++

#include "opencv2/opencv.hpp"

int main()
{
    cv::Mat src = cv::imread("im.png", CV_LOAD_IMAGE_UNCHANGED);
    double angle = -45;

    // get rotation matrix for rotating the image around its center
    cv::Point2f center(src.cols/2.0, src.rows/2.0);
    cv::Mat rot = cv::getRotationMatrix2D(center, angle, 1.0);
    // determine bounding rectangle
    cv::Rect bbox = cv::RotatedRect(center,src.size(), angle).boundingRect();
    // adjust transformation matrix
    rot.at<double>(0,2) += bbox.width/2.0 - center.x;
    rot.at<double>(1,2) += bbox.height/2.0 - center.y;

    cv::Mat dst;
    cv::warpAffine(src, dst, rot, bbox.size());
    cv::imwrite("rotated_im.png", dst);

    return 0;
}

Code tested with opencv 2.4.9 (visual c++ 2010)

For me, I want to let the angle be alternative, that means I want to change it from 0 to 360. And import each images out. Like this:

#include "opencv2/opencv.hpp"

int main()
{	for (double i=0;i<361;i++)
{
    cv::Mat src = cv::imread("refshape.bmp", CV_LOAD_IMAGE_UNCHANGED);
    double angle = -i;

    // get rotation matrix for rotating the image around its center
    cv::Point2f center(src.cols/2.0, src.rows/2.0);
    cv::Mat rot = cv::getRotationMatrix2D(center, angle, 1.0);
    // determine bounding rectangle
    cv::Rect bbox = cv::RotatedRect(center,src.size(), angle).boundingRect();
    // adjust transformation matrix
    rot.at<double>(0,2) += bbox.width/2.0 - center.x;
    rot.at<double>(1,2) += bbox.height/2.0 - center.y;

    cv::Mat dst;
    cv::warpAffine(src, dst, rot, bbox.size());
    cv::imwrite("rotated_im.png", dst);
}
    return 0;
}

========================================= It's easy to change the angle, but the import part is the challenge for me. I wants have 360 results in my file, how can I do for it? I tried to modify this line: cv::imwrite("rotated_im.png", dst); to cv::imwrite("rotated_im_%d.png", i , dst);

It doesn't work for it.

4
  • Your question should be sth like "how to dynamically add a number to a string" Commented Dec 6, 2014 at 0:43
  • 1
    Use cv::format() like imwrite(format(%d.png,i),img); Commented Dec 6, 2014 at 5:18
  • Please format the question better and kindly remove options to run code. Commented Dec 8, 2014 at 11:50
  • 1
    @Haris Not %d.png,i but "%d.png,i". And a more convenient way to use is with zero padding imwrite(format("%05d.png, i), img); Commented Jul 2, 2019 at 3:51

1 Answer 1

9

You will need to create a string each time through the loop with your desired file name and pass that to cv::imwrite. The easiest way to do that in C++ is with a std::ostringstream. Replace the last line (the cv::imwrite) of your loop body with:

std::ostringstream name;
name << "rotated_im_" << i << ".png";
cv::imwrite(name.str(), dst);

You will need to add #include <sstream> at the top with your other includes. String streams work just like other iostreams (such as std::cout and fstreams if you are familiar with those).

Also, I would not use a double in a for loop like that. If you want to count by one, use an integer and then convert it to a double if you need to use it as a double. Since you are doing that (with double angle = -i;), just change your for loop header to be:

for (int i =0; i <= 360; i++)

This will also make your output filenames look more like I suspect you want them. If i is a double, you may get file names like rotated_im_5.000000.png.

Sign up to request clarification or add additional context in comments.

4 Comments

Must be name.str().c_str() i guess
@Micka only if you need a C string (const char*). In this case, cv::write takes a std::string for the filename.
Thanks for @plasmoidia suggestion. It works. I revised the double to int.
@HsuYu-WeiAuston glad it worked for you. Would you mind accepting my answer if it was helpful?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.