Digital Image Processing: Read, Write, Show.
OpenCV and Numpy, using python to do image recognition must know how to use it.
What is OpenCV? Why learn it?
OpenCV, the full name of Open Source Computer Vision Library, is an image processing suite for python, written in C++ and packaged in NumPy, so it is faster than other suites. Therefore, it has become the basis for learning image processing. If you want to learn how to use python to change your photos or videos, or if you want to use python to import a large number of images to train AI, just read on.
Contents
What is OpenCV? Why learn it?
Why Numpy?
Install
how to use
●Read pictures
●Save pictures
●Display pictures
Why Numpy?
OpenCV has been introduced above, and here we mainly introduce why it should be used with Numpy. As mentioned earlier, openCV is encapsulated by NumPy on python; that is to say, the formats of OpenCV and NumPy are common, and the images read by openCV are stored in NumPy arrays. You can use type
command to confirm it.
Install
Before starting to use it, it is necessary to install it properly.
- Open your terminal and enter the following commands.
pip install numpy opencv-python
or
pip install opencv-python
There is no need to additionally install numpy
, because it is already included when installing opencv
.
General python is pre-installed with pip3, so you can use it directly. If you are a user who has installed the
pipenv
mentioned in the previous article, you can enter the virtual environment and change the previous pip topipenv
input.
More details can be found in my previous article.
2. Before start using them, you also need to import them into the program.
Type these commands at the top of your code:
import numpy as np
import cv2
Usually, numpy is abbreviated to
np
for use, and some people will abbreviatedcv2
tocv
.
Then you can start using them in the program! As for how to use them, keep reading!
How to use it?
After it is finally installed, let’s introduce the various functions of OpenCV!
Read image
Use the imread
function to read images, which can be thought of as a shorthand for image read, which is easier to remember.
img = cv2.imread(path, flag)
path is the image path variable you want to read. Note that you should enclose the path with
‘ ‘
when declaring it, and remember to add the filename extension, such as'input.jpg'
.flag is to determine the format of the image to be read, or it can be omitted. There are the following three types:
cv2.IMREAD_UNCHANGED
or-1
: read all channels in the image, including transparency.
cv2.IMREAD_GRAYSCALE
or0
: read in grayscale format.
cv2.IMREAD_COLOR
or1
: (default) read in BGRformat.img is a variable that stores an image. It has been said before that the type will be a numpy array; if the input image format is preset, three colors of BGR will be stored in the variable, and the dimension is three-dimensional (note that the order cannot be changed, if you want to use other software to display the order); if the format of image is gray, it will using grayscale format, also mean that it’s dimension will be two-dimensional.
Save image
Use the imwrite
function to save images, like imread
, which can be thought of as a shorthand for image write/save.
cv2.imwrite(path, img, params)
path is a variable that stores the path of the archive. Note that when declaring it, enclose the path with
‘ ‘
, and remember to add the filename extension. This important so I say it again.img is a variable that holds the image to be output.
params is the parameter of the storage type, and it can be added or not. There are 17 kinds in total, See the following website for details.
Show image
Use the imshow
function to display the picture. If you want to remember it, I don’t need to say more.
cv2.imshow(“windows”, img)
“windows” is the displayed window name.
img is a variable that holds the image to be output.
Usually, these two lines of instructions are followed after the image is displayed.
cv2.waitKey(delay)
cv2.destroyAllWindows()
waitKey is to wait for the user to input after displaying the picture, or you can use the returned value to get the ASCII code value
key = cv2.waitKey(delay)
.destroyAllWindows will close the window after receiving input. You can also use
cv2.destroyWindow(“windows”)
to close the specified window.
Done!
Now you know how to use OpenCV to deal with the image, so just go try to do it by yourself. Thanks for your watching, If you like my article don’t forget to leave your clap or share it with your friend. Your appreciation is my motivation. —Jeffrey
Co-Author: Y. S. Huang, a master’s student studying AIVC, likes open-source.
If you are interested, go to check my Github!