From the course: OpenCV for Python Developers

Get started with OpenCV and Python

From the course: OpenCV for Python Developers

Get started with OpenCV and Python

- [Instructor] This chapter will focus on the core concepts in image processing. These areas will act as the building blocks for more intricate image manipulations in later chapters. Becoming familiar with these characteristics of using Python and OpenCV, viewers will then be able to jump around to the different concepts more easily. This video will show how to open images, view them using the built-in Python and OpenCV tools, and then save them back out to disk. If you are following this course using Google Collab or Jupyter Notebooks, I suggest you skip to the next video, which covers the same content. I've now switched over to my terminal window where you will note that I'm already in my directory for the exercise files for this video, specifically in the Ch02_01 Begin folder. Using ls or der on Windows, you will note that I already have an image in this folder called opencv-logo.png dot. At this point, I will type python3 to enter into my environment. If you're using Windows, the command may just be Python. First, we'll import our libraries. The first one is going to be the NumPy Library, sometimes pronounced NumPy, which we can import using import numpy as np which is the conventional alias. Next, we can import our OpenCV library using import cv2. Now we're ready to use our first OpenCV command. Namely, we'll read in the image opencv-logo.png. We'll create a variable name ing = and then we'll type cv2.IMREAD and then open parentheses, double quotes, We'll type in the file name openCV-logo.png close quote, close parenthesis. Upon pressing enter, we have now loaded in our image. Next, we actually want to display our image. If you're working with a local installed OpenCV, we can do this using imshow and name windows. Once again, if you're using Google Collab or Jupyter Notebooks, please move on to the next video because this command will not work. We can start off by initializing name the windows for a runtime environment. This can be done using cv2.namedWindow, noting the W is capitalize, and then open parentheses. And then in quotes, we'll give a name for this window which I will just call Image here, close quote, and then we're going to pass in the form of the window. This is going to be cv2. and then in all caps for the global variable defined by cvt, we'll type in WINDOW_NORMAL close parenthesis. That has then registered a named window, but note nothing has actually popped up yet. This command initializes the window that'll be used to show the actual image loaded. Next, we'll type cv2.imshow, all lowercase, and then we'll pass in image which is just the name we're showing for this image. And then we're going to pass in the variable that we loaded beforehand, img. Upon pressing enter, at this point, Python is ready to display the image. We simply have to tell the runtime environment to pause or hang until the user decides to let it continue. For this, we'll use cv2.waitKey noting that there is no space or underscore and the K is capitalized, and then we'll pass in a value of zero. This is telling Python to hold indefinitely, whereas if you pass in a non-zero number, it will wait that number of milliseconds. Upon pressing enter, our image will load and we can see the OpenCV logo displayed in front of us. At this point, we can press any key, such as the escape key, and that'll give us control back over our environment terminal. Switching back to the terminal, you'll notice that the value 27 was printed out. This is because the wait key function will actually capture the key pressed on the keyboard. In this case, the escape key has the value of 27. The last thing we're going to do is write the image back out to file. We can do this using cv2.imwrite and then in open parentheses and in quotes we'll give the name of the output image we want to write. In this case, it can be output.jpg for jpeg and then we're passing in the image data that is already loaded that we want to use for writing out file in this case, img. And then close parenthesis. Upon pressing enter, we are given the True value which indicates that it was successful in saving out this file. And then if we exit out of our Python environment using the exit command, and then using ls, or dur if you are on Windows, we can then see that we successfully saved out our output.jpg. As an interesting point to recognize, we can take a look at the file sizes using a du space -a command on Linux, or OS X, or just use the dur command on Windows. Upon running this, you'll see the two sizes of these images are actually slightly different. This is because OpenCV actually changed the encoding of the image that saved out to disk and therefore it's different in size. Having used our first OpenCV command, the rest of this chapter will dive into more principles in the basics of image processing.

Contents