![]() Rotated = cv2.warpAffine(image, M, (w, h))Ĭv2.imshow("Rotated by 45 Degrees", rotated) # rotate our image by 45 degrees around the center of the image # grab the dimensions of the image and calculate the center of the Next, let’s load our input image from disk and do some basic resizing: # load the image and show it We only have a single command line argument, -image, which is the path to the input image we want to rotate (which we default to opencv_logo.png). We’ll use argparse for command line arguments, imutils for my set of OpenCV convenience functions (namely the imutils.rotate and imutils.rotate_bound methods), and cv2 for our OpenCV bindings. Lines 2-4 import our required Python packages. # construct the argument parser and parse the argumentsĪp.add_argument("-i", "-image", type=str, default="opencv_logo.png", Open the opencv_rotate.py file in your project directory structure and insert the following code: # import the necessary packages We are now ready to implement image rotation with OpenCV. This script will load opencv_logo.png (or any other image you choose) and then apply a series of rotations to it, thereby demonstrating how to perform rotation with OpenCV. Gain access to Jupyter Notebooks for this tutorial and other PyImageSearch guides that are pre-configured to run on Google Colab’s ecosystem right in your web browser! No installation required.Īnd best of all, these Jupyter Notebooks will run on Windows, macOS, and Linux! Project structureīefore we can implement rotation with OpenCV, let’s first review our project directory structure.īe sure you access the “Downloads” section of this tutorial to retrieve the source code and example images, and from there, take a peek inside: $ tree. Ready to run the code right now on your Windows, macOS, or Linux systems?.Wanting to skip the hassle of fighting with the command line, package managers, and virtual environments?.Learning on your employer’s administratively locked system?.Having problems configuring your development environment?įigure 1: Having trouble configuring your development environment? Want access to pre-configured Jupyter Notebooks running on Google Colab? Be sure to join PyImageSearch Plus - you will be up and running with this tutorial in a matter of minutes. If you need help configuring your development environment for OpenCV, I highly recommend that you read my pip install OpenCV guide - it will have you up and running in a matter of minutes. Luckily, OpenCV is pip-installable: $ pip install opencv-contrib-python To follow this guide, you need to have the OpenCV library installed on your system. If the mathematics is starting to get a bit overwhelming, no worries - we’ll jump into some code that will make these concepts much clearer. Where and and and are the respective (x, y)-coordinates around which the rotation is performed. Our modified rotation matrix, M, is thus: However, OpenCV also provides the ability to (1) scale (i.e., resize) an image and (2) provide an arbitrary rotation center around which to perform the rotation. In this case, the origin is normally the center of the image however, in practice, we can define any arbitrary (x, y)-coordinate as our rotation center.įrom the original image, I, the rotated image, R, is then obtained by simple matrix multiplication: ![]() Given an (x, y)-Cartesian plane, this matrix can be used to rotate a vector degrees (counterclockwise) about the origin. Similar to translation, and perhaps unsurprisingly, rotation by an angle can be defined by constructing a matrix, M, in the form: We’ll wrap up this tutorial by reviewing our OpenCV rotation results. Ensures that no part of the image is cut off during rotation. Use the imutils.rotate_bound function: Also part of my imutils library.Makes it possible to rotate an image with OpenCV in a single function call. Use the imutils.rotate function: Part of my imutils library.Use the cv2.rotate function: Built into OpenCV, but requires constructing a rotation matrix and explicitly applying an affine warp, making the code more verbose.I’ll then show you three ways to rotate an image with OpenCV: In the first part of this tutorial, we’ll discuss how OpenCV rotates images and the functions available for rotation.įrom there, we’ll configure our development environment and review our project directory structure. Looking for the source code to this post? Jump Right To The Downloads Section OpenCV Rotate Image
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |