Overview
Image I/O is the foundation of computer vision applications. OpenCV provides simple functions to:- Read images from various formats (JPEG, PNG, BMP, etc.)
- Display images in GUI windows
- Save processed images to disk
- Handle errors when files are not found
Basic Image Loading and Display
Read the image
Use
imread() to load an image file. Always check if the image was loaded successfully.Display the image
Use
imshow() to display the image in a named window, followed by waitKey() to keep the window open.Complete Example
- Python
- C++
Window Operations
OpenCV provides several functions to manage display windows:Creating and Managing Windows
- Python
- C++
Image Reading Flags
Theimread() function accepts flags to control how images are loaded:
- Python
- C++
Always check if an image was loaded successfully before processing it. An empty/null image will cause your program to crash.
Key Functions
| Function | Description |
|---|---|
imread() | Loads an image from a file |
imshow() | Displays an image in a window |
imwrite() | Saves an image to a file |
waitKey() | Waits for a key press (0 = wait indefinitely) |
namedWindow() | Creates a window with a specific name |
destroyWindow() | Closes a specific window |
destroyAllWindows() | Closes all OpenCV windows |
