This chapter focuses on the core image manipulation concepts and operations. By becoming familiar with these characteristics of using Python and OpenCV, you will be able to jump around to different concepts more easily. This video shows you how to open images, view them using the built-in Python and OpenCV tools, and then save the images back to a disk.
- [Man] 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. I've now switched over to my terminal window, where you will note that I have already changed my directory into the exercise file found with the videos.
Specifically, into Chapter two, video zero, two, underscore, one begin. Using ls, you will note that I already have an image in this folder called OpenCV dash logo dot PNG. At this point, I will type Python 3 to enter into my runtime command. If you're in Windows, it may be Python. First, we will import our libaries. The first one is the NumPy library, or sometimes called num-pie. This is done by using import NumPy, and then typically, people will import it as a np signifier.
Next, we will import the CV2 library. Now, we're ready to use our first OpenCV command. Namely, we will read in the image OpenCv dash logo dot PNG. We can create a variable called img, and then using the equal symbol, we can then type in CV2 dot imread open parentheses, then, since we are already in the same folder as the OpenCV logo, we can simply type the file name out. OpenCV dash logo dot PNG.
End quote and close parentheses. At this point, we have loaded in our image. Next, we want to actually display our image. We can do that using imshow and creating named windows. We can start off by initializing a named window for our runtime environment. This can be done using CV2 dot named window. And then we can specify the name for this window, such as Image, and then, we can describe how this window will actually behave.
We can type in zero, for example, to use the default behavior, or, we can use the CV variable, CV2 dot window normal. This initializes the window that will be used to show the actual image loaded. Next, we will type CV2 dot imshow. Again, use the same image name we already used above, image, and then pass in our variable, img. At this point, Python is ready to display the image. We just have to tell the runtime environment to pause or hang for the user element to show up.
For this, we will use CV2 dot waitKey. Note that the capitalized K is important in this case. And then, we're going to pass in a value of zero. This is essentially a command that tells the interface to wait for a specified number of milliseconds. Or, in this case, by passing in zero, we're telling it to wait indefinitely until a user has interact with the environment. 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 will give us control back over our environment terminal.
Switching back to the terminal, you'll note that the value 27 was printed out. This is because the waitKey function will actually capture the key pressed on the keyboard. In this case, the escape key has a value of 27. The last thing we are going to do is write this image back out to file. We can do this using CV2 dot imwrite. We can also specify an output name, and even a change in the extension variable. For example, output dot J-P-G for J-PEG. And then, we pass in the variable.
Upon pressing enter, we get the value true, indicating that it was successful in saving out this file, and then if we exit our of our Python environment using exit, and then using ls to list our directory, we can see that we have both of the images now in this folder. As an interesting point to recognize, if I use the command du minus a, which is specific to OS X, and for Linux, you can see that the sizes of these two images created are actually slightly different. This is because the OpenCV environment actually changed the encoding of the image that it saved out to disk, and therefore, it's a slightly different size.
Having used our first OpenCV command, the rest of this chapter will dive into more principles in the basics of image processing.
- Installing and configuring OpenCV
- Data types and structures
- Image types
- Manipulating pixels
- Scaling and rotating images
- Using video inputs
- Creating custom interfaces
- Object detection
- Face and feature detection
- Template matching