- OpenCV with Python By Example
- Prateek Joshi
- 446字
- 2021-07-16 19:57:01
Reading, displaying, and saving images
Let's see how we can load an image in OpenCV-Python. Create a file named first_program.py
and open it in your favorite code editor. Create a folder named images
in the current folder and make sure that you have an image named input.jpg
in that folder.
Once you do that, add the following lines to that Python file:
import cv2 img = cv2.imread('./images/input.jpg') cv2.imshow('Input image', img) cv2.waitKey()
If you run the preceding program, you will see an image being displayed in a new window.
What just happened?
Let's understand the previous piece of code, line by line. In the first line, we are importing the OpenCV library. We need this for all the functions we will be using in the code. In the second line, we are reading the image and storing it in a variable. OpenCV uses NumPy data structures to store the images. You can learn more about NumPy at http://www.numpy.org
So if you open up the Python shell and type the following, you will see the datatype printed on the terminal:
>>> import cv2 >>> img = cv2.imread('./images/input.jpg') >>> type(img) <type 'numpy.ndarray'>
In the next line, we display the image in a new window. The first argument in cv2.imshow
is the name of the window. The second argument is the image you want to display.
You must be wondering why we have the last line here. The function, cv2.waitKey()
, is used in OpenCV for keyboard binding. It takes a number as an argument, and that number indicates the time in milliseconds. Basically, we use this function to wait for a specified duration, until we encounter a keyboard event. The program stops at this point, and waits for you to press any key to continue. If we don't pass any argument or if we pass 0
as the argument, this function will wait for a keyboard event indefinitely.
Loading and saving an image
OpenCV provides multiple ways of loading an image. Let's say we want to load a color image in grayscale mode. We can do that using the following piece of code:
import cv2 gray_img = cv2.imread('images/input.jpg', cv2.IMREAD_GRAYSCALE) cv2.imshow('Grayscale', gray_img) cv2.waitKey()
Here, we are using the flag cv2.IMREAD_GRAYSCALE
to load the image in grayscale mode. You can see that from the image being displayed in the new window. Next, is the input image:

Following is the corresponding grayscale image:

We can save this image into a file as well:
cv2.imwrite('images/output.jpg', gray_img)
This will save the grayscale image into an output file named output.jpg
. Make sure you get comfortable with reading, displaying, and saving images in OpenCV, because we will be doing this quite a bit during the course of this book.
- ASP.NET Web API:Build RESTful web applications and services on the .NET framework
- 深度學習經典案例解析:基于MATLAB
- Vue.js 3.x從入門到精通(視頻教學版)
- Getting Started with CreateJS
- 區塊鏈:以太坊DApp開發實戰
- Full-Stack Vue.js 2 and Laravel 5
- Learning Selenium Testing Tools(Third Edition)
- Tableau 10 Bootcamp
- Scala Data Analysis Cookbook
- ScratchJr趣味編程動手玩:讓孩子用編程講故事
- Python:Deeper Insights into Machine Learning
- Bootstrap for Rails
- Mastering JavaScript
- Java程序設計教程
- 少兒編程輕松學(全2冊)