OpenCV-Python Installation and Image Read, Display, Save
OpenCV is an open-source computer vision library widely used in image processing, machine learning, and real-time computer vision applications. It supports operations like image and video filtering, denoising, object detection, face recognition, ID number recognition, and license plate recognition. While there are other tools available for these domains, this article focuses on OpenCV.
OpenCV provides many functions for digital images. An image consists of pixels, each containing information such as color, brightness, and saturation. These are stored as data (typically various numeric types) in computers, and OpenCV excels at processing such data.
Before processing, we need the data. This article serves as an entry point to the OpenCV-Python series, covering installation, reading, displaying, and saving images.
OpenCV supports multiple programming languages like Python, C++, and even JavaScript.
- Official website: https://opencv.org/
- OpenCV Python documentation: https://docs.opencv.org/4.1.2/d6/d00/tutorial_py_root.html
Installation and Import
Installation
First, ensure a Python environment is set up. Then install OpenCV:
pip install opencv-python
Verify installation with:
python -c "import cv2; print(cv2.__version__)"
If the version number is printed, installation succeeded.
Import
Create a project directory and a file main.py. Open it and import OpenCV:
import cv2
Reading Images
Reading an image is the first step. Use cv2.imread() to load an image file.
import cv2
# Read an image
image = cv2.imread('example.png')
cv2.imread() accepts two parameters:
- Parameter 1: local file path. Direct reading from URLs is not supported; use libraries like
requeststo download and convert to OpenCV format. - Parameter 2: read mode (optional). Options include:
cv2.IMREAD_COLOR: load color image, ignoring transparency (default).cv2.IMREAD_GRAYSCALE: load image in grayscale.cv2.IMREAD_UNCHANGED: load image including alpha channel.
Displaying Images
Unlike JavaScript, Python cannot direct display in a browser. Use cv2.imshow() to create a window and show the image. Use cv2.waitKey() to keep the window open.
# Display the image
cv2.imshow('Image Window', image)
# Wait for a key press to close the window
cv2.waitKey(0)
# Close all windows and release resources
cv2.destroyAllWindows()
cv2.imshow() takes two arguments:
- Window name (string).
- Image data to display.
cv2.waitKey(0) waits indefinitely until a key is pressed. A non-zero value waits that many milliseconds. Finally, cv2.destroyAllWindows() closes all OpenCV windows.
This functon is essential for visualizing results during development.
Saving Images
After processing (e.g., resizing, filtering), use cv2.imwrite() to save the result.
# Save the image
cv2.imwrite('output.jpg', image)
cv2.imwrite() takes two arguments:
- File path to save.
- Image data to save.
These basic operations will be frequently used in future topics.