Chapter 1: Getting Started with OpenCV
OpenCV is an open source computer vision library started in 1999 by Gary Bradski of Intel. The OpenCV library is written in C and C++, covers more than 500 functions in various fields of computer vision, and can run on multiple operating systems. It aims to provide a concise and efficient interface to help developers quickly build vision systems.
This chapter mainly introduces the simple use of OpenCV
Basic operations of image processing:
The most basic operations of image processing include:
- read image
- display image
- save image
1. Read the image:
Use the cv2.imread() function in OpenCV to read the image, which supports various static image formats.
The syntax is: retval = cv2.imread( filename[, flags] )
- retval: return value, the read image. Returns None if no image was read
- filename: Indicates the full path of the image to read.
- flags: is the read flag. This flag is used to control the type of read file, as shown in the following table.
Note: The parameters in the first column and the third column in the table are equivalent. That is: cv2.IMREAD_UNCHANGED = -1
value | meaning | value |
---|---|---|
cv2.IMREAD_UNCHANGED | Keep the original format unchanged | -1 |
cv2.IMREAD_GRAYSCALE | Resize the image to a single-channel grayscale image | 0 |
cv2.IMREAD_COLOR | Resize the image to a 3-channel BGR image. This value is the default | 1 |
cv2.IMREAD_ANYDEPTH | When the depth of the loaded image is 16-bit or 32-bit, return the image of its corresponding depth; otherwise, convert it to an 8-bit image | 2 |
cv2.IMREAD_ANYCOLOR | Read an image in any possible color format | 4 |
cv2.IMREAD_LOAD_GDAL | Load the image using the gdal driver | 8 |
cv2.IMREAD_REDUCED_GRAYSCALE_2 | Convert the image to a single-channel grayscale image and reduce the image size by 1/2 | |
cv2.IMREAD_REDUCED_COLOR_2 | Convert the image to a 3-channel BGR color image and reduce the image size by 1/2 | |
cv2.IMREAD_REDUCED_GRAYSCALE_4 | Convert the image to a single-channel grayscale image and reduce the image size by 1/4 | |
cv2.IMREAD_REDUCED_COLOR_4 | Convert image to 3-channel BGR color image and reduce image size by 1/4 | |
cv2.IMREAD_REDUCED_GRAYSCALE_8 | Convert the image to a single-channel grayscale image and reduce the image size by 1/8 | |
cv2.IMREAD_REDUCED_COLOR_8 | Convert the image to a 3-channel BGR color image and reduce the image size by 1/8 | |
cv2.IMREAD_IGNORE_ORIENTATION | Rotate image without EXIF orientation as marker |
The function cv2.imread() supports reading many different types of images:
For example: read an image using the cv2.imread() function
import cv2
lena = cv2.imread("sky.png")
print(lena)
2. Display the image:
Several display-related functions are provided in OpenCV:
-
namedWidow function:
-
used to create a window with the specified name
-
Grammar format: None = cv2.namedWindow( winname ) where winname refers to the name of the created window
For example, create a window named lessonCV2.namedWindow("lesson")
-
imshow function:
- used to display images
- Syntax format: None = cv2.imshow( winname, mat ) where winname is the name of the window, and mat is the image to be displayed.
For example:
import cv2 lena = cv2.imread("lena.bmp") cv2.namedWindow("lesson") cv2.imshow("lesson", lena)
In the above code, first read the image lena.bmp through the cv2.imread() function, then create a window named lesson through the cv2.namedWindow() function, and finally display the image in the lesson window through the cv2.imshow() function lena.bmp.
Note : In actual use, it is also possible not to create a window, directly use the function cv2.imshow() to refer to a non-existing creation space, and display the specified image in it, such as:
import cv2 lena = cv2.imread("lena.bmp") cv2.imshow("demo", lena)
-
waitKey function:
- Used to wait for the key, when the user presses the keyboard, the statement will be executed and return a value
- Syntax: retval = cv2.waitKey([delay])
- retval: Indicates the return value. If no key is pressed, return -1; if there is a key registered, return the ASCII code of the key
- delay: Indicates the time to wait for the keyboard trigger, the unit is ms. When the value is negative or 0, it means waiting infinitely. Default is 0
In actual use, the pressed key can be obtained through the function cv2.waitKey(), and different responses can be made to different keys to realize the interactive function
import cv2 lenam = cv2.imread("lena.png") cv2.imshow("demo", lena) key = cv2.waitKey() if key == ord("A"): CV2.imshow("PressA", lena) elif key == ord("B"): cv2.imshow("PressB", lena)
-
destroyWindow function:
- Used to release (destroy) the specified window
- Syntax: None = cv2.destroyWindow( winname ) winname is the window name
import cv2 lena = cv2.imread("lena.png") cv2.imshow("demo", lena) cv2.waitKey() cv2.destroyWindow("demo")
-
destroyAllWindows function
- Used to release (destroy) all windows
- Syntax: None = cv2.destroyAllWindows()
import cv2 lena = cv2.imread("lena.png") cv2.imshow("demo1", lena) cv2.imshow("demo2", lena) cv2.waitKey() cv2.destroyAllWindows()
3. Save the image
Use the function cv2.imwrite() in OpenCV to save the image, the syntax of this function is:
- retval = cv2.imwrite( filename , img [, params ])
parameter:
- retval: return value. Returns True if the save was successful, False if it failed
- filename: The full path of the target file to save.
- img: the image to save
- params: Save type parameters, optional.
import cv2
lena = cv2.imread("lena.png")
r = cv2.imwrite("result.png", lena)
OpenCV contribution library introduction
Currently, the OpenCV library consists of the following two parts:
- OpenCV main library: the commonly installed OpenCV library, which is mature and stable and maintained by the core OpenCV team
- OpenCV contribution library: The name of the extension library is opencv_contrib, which is mainly developed and maintained by the community. It contains more comprehensive visual applications than the main OpenCV library. It should be noted that the OpenCV Contributed Library contains parts not licensed by OpenCV and contains patent-protected algorithms. Therefore, special attention should be paid before using this module.
- The OpenCV Contribution Library contains many extension modules:
- bioinspired: biological vision module
- datasets: dataset reading module
- dnn: Deep Neural Network Module
- face: face recognition module
- matlab: MATLAB interface module
- stereo: binocular stereo matching module
- text: visual text matching module
- tracking: Vision-based target tracking module
- ximgpro: image processing extension module
- xobjdetect: Enhanced 2D object detection module
- datasets: dataset reading module
- dnn: Deep Neural Network Module
- face: face recognition module
- matlab: MATLAB interface module
- stereo: binocular stereo matching module
- text: visual text matching module
- tracking: Vision-based target tracking module
- ximgpro: image processing extension module
- xobjdetect: Enhanced 2D object detection module
- xphoto: Computational Photography Extension Module