How to Get Started with Computer Vision in Python
To get started with computer vision in Python, install the
opencv-python library and use it to read, process, and display images. Begin by loading an image with cv2.imread(), then apply basic operations like resizing or converting to grayscale.Syntax
Here is the basic syntax to load and display an image using OpenCV in Python:
import cv2: Imports the OpenCV library.img = cv2.imread('path'): Loads an image from the given file path.cv2.imshow('title', img): Opens a window to display the image.cv2.waitKey(0): Waits for a key press to close the window.cv2.destroyAllWindows(): Closes all OpenCV windows.
python
import cv2 # Load an image from file img = cv2.imread('image.jpg') # Show the image in a window cv2.imshow('Image Window', img) # Wait until a key is pressed cv2.waitKey(0) # Close the window cv2.destroyAllWindows()
Example
This example loads an image, converts it to grayscale, resizes it, and then displays both the original and processed images. It shows how to perform simple computer vision tasks with OpenCV.
python
import cv2 # Load the image img = cv2.imread('image.jpg') # Convert to grayscale gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) # Resize image to half its size resized = cv2.resize(img, (img.shape[1]//2, img.shape[0]//2)) # Display original image cv2.imshow('Original Image', img) # Display grayscale image cv2.imshow('Grayscale Image', gray) # Display resized image cv2.imshow('Resized Image', resized) cv2.waitKey(0) cv2.destroyAllWindows()
Common Pitfalls
Common mistakes when starting with computer vision in Python include:
- Not installing
opencv-pythonbefore importingcv2. - Using incorrect file paths causing
cv2.imread()to returnNone. - Forgetting to call
cv2.waitKey()which prevents image windows from showing properly. - Not handling color channels correctly; OpenCV uses BGR format, not RGB.
python
import cv2 # Wrong: File path typo leads to None img = cv2.imread('wrong_path.jpg') print(img) # Output: None # Right: Correct file path img = cv2.imread('image.jpg') print(type(img)) # Output: <class 'numpy.ndarray'>
Output
None
<class 'numpy.ndarray'>
Quick Reference
Here is a quick cheat sheet for common OpenCV functions to get started:
| Function | Purpose |
|---|---|
| cv2.imread(path) | Load image from file |
| cv2.imshow(title, img) | Display image in window |
| cv2.waitKey(delay) | Wait for key press (0 = infinite) |
| cv2.destroyAllWindows() | Close all image windows |
| cv2.cvtColor(img, flag) | Convert image color space (e.g., BGR to Gray) |
| cv2.resize(img, size) | Resize image to given size |
Key Takeaways
Install the opencv-python package to start computer vision in Python.
Use cv2.imread() to load images and cv2.imshow() to display them.
Always check that image paths are correct to avoid loading errors.
Remember to call cv2.waitKey() to show images properly.
OpenCV uses BGR color format, so convert if needed for other libraries.