Skip to content

Instantly share code, notes, and snippets.

@gcpantazis
Last active December 20, 2015 07:19
Show Gist options
  • Save gcpantazis/6092838 to your computer and use it in GitHub Desktop.
Save gcpantazis/6092838 to your computer and use it in GitHub Desktop.
OpenCV, rotate an image 180 degrees.
#include <opencv2/gpu/gpu.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/highgui/highgui.hpp>
using namespace cv;
int main(int argc, char* argv[])
{
Mat src_img, dst_img;
gpu::GpuMat src_gpu, dst_gpu;
src_img = imread("my_image.jpg");
if (src_img.empty()) {
printf("error: can not load image...\n");
return -1;
}
// Create a destination to paint the source into.
dst_img.create(src_img.size(), src_img.type());
// Push the images into the GPU
src_gpu.upload(src_img);
dst_gpu.upload(dst_img);
// Rotate in the GPU!
gpu::rotate(src_gpu, dst_gpu, src_gpu.size(), 180, src_gpu.size().width, src_gpu.size().height);
// Download the rendered GPU data back to CPU
dst_gpu.download(dst_img);
// ... so that it can be written with imwrite.
imwrite("./COOL.png", dst_img);
return 0;
}
@TimZaman
Copy link

TimZaman commented Jun 2, 2014

For 180 degree rotations you should just use a transpose and flip command. It's 10x faster than the arbitrary rotation you are using now.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment