![]() ![]() By default, we will set the -image argument to be opencv_logo.png. We only need a single argument, -image, which points to the input image we want to load from disk and apply OpenCV translation operations to. Let’s now parse our command line arguments: # construct the argument parser and parse the argumentsĪp.add_argument("-i", "-image", type=str, default="opencv_logo.png", If you don’t already have imutils installed on your machine, you can install it with pip: $ pip install imutils Rather, it’s a library that I personally wrote containing a handful of “convenience” methods to more easily perform common tasks like translation, rotation, and resizing (and with less code). This isn’t a package included in NumPy or OpenCV. However, I am introducing a new package here: imutils. At this point, using NumPy, argparse, and cv2 should feel commonplace. On Lines 2-5, we simply import the packages we will make use of. This concept is better explained through some code: # import the necessary packages Mathematically, we define a translation matrix, M, that we can use to translate an image:įigure 3: Defining an image translation matrix with OpenCV. Using translation, we can shift an image up, down, left, or right, along with any combination of the above. ![]() Translation is the shifting of an image along the x- and y-axis. This script will load the opencv_logo.png image from disk and then translate/shift it using the OpenCV library. We have a single Python script, opencv_translate.py, which we will be reviewing in detail. 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 perform image translation with OpenCV, let’s first review our project directory structure: $ tree. Ready to run the code right now on your Windows, macOS, or Linux system?.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 2: 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 along with this guide, you need to have the OpenCV library installed on your system. We will see a complete example of defining our image translation matrix and applying the cv2.warpAffine function later in this guide. Now, if we want to shift an image 7 pixels to the left and 23 pixels up, our translation matrix would look like the following: M = np.float32([Īnd as a final example, let’s suppose we want to translate our image 30 pixels to the left and 12 pixels down: M = np.float32([Īs you can see, defining our affine transformation matrix for image translation is quite easy!Īnd once our transformation matrix is defined, we can simply perform the image translation using the cv2.warpAffine function, like so: shifted = cv2.warpAffine(image, M, (image.shape, image.shape)) Our translation matrix would look like the following (implemented as a NumPy array): M = np.float32([ ![]() ![]() Positive values for will shift the image downįor example, let’s suppose we want to shift an image 25 pixels to the right and 50 pixels down.Negative values for shifts the image up.Positive values for shifts the image to the right.Negative values for the value will shift the image to the left.Figure 1: To translate an image with OpenCV, we must first construct an affine transformation matrix.įor the purposes of translation, all we care about are the and values: ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |