In this post I’m sharing a couple of very simple tricks to perform an efficient Rotate or Flip (Mirror) operation on OpenCV Mat images.
Obviously you’ll start by reading an image from disk, or grab it from a video frame and so on, similar to this:
imread("/Users/amin/Test.jpg");
After that depending on the required transformation use any of the following codes.
Flip (Mirror) Vertically
flip(image, image, 0);
Flip (Mirror) Horizontally
flip(image, image, +1);
Rotate 90 Degrees Clockwise
transpose(image, image);
flip(image, image, +1);
Rotate 90 Degrees Counter Clockwise (Same as -90 Degrees and 270 Degrees)
transpose(image, image);
flip(image, image, 0);
Rotate 180 Degrees (Same as Flipping vertically and horizontally at the same time)
flip(image, image, -1);
Post your comments and questions below.
Would it be possible to flip about a specific line using opencv?
Not with the functions provided in this tutorial, but you can potentially use remap to achieve what you’re looking for.
See this one as a starting point:
https://docs.opencv.org/4.5.3/d1/da0/tutorial_remap.html