Environment Setup and Basics
Environment Setup and Basics¶
Overview¶
OpenCV (Open Source Computer Vision Library) is an open-source library for real-time computer vision. This document covers OpenCV installation, running your first program, and understanding the basic structure of image data.
Difficulty: β (Beginner)
Learning Objectives: - Install OpenCV and configure development environment - Verify version and write first program - Understand the relationship between OpenCV and NumPy - Understand how images are represented as ndarrays
Table of Contents¶
- Introduction to OpenCV
- Installation Methods
- Development Environment Setup
- Version Check and First Program
- OpenCV and NumPy Relationship
- Images are ndarrays
- Practice Problems
- Next Steps
- References
1. Introduction to OpenCV¶
What is OpenCV?¶
OpenCV is a computer vision library originally started by Intel and now maintained as open source.
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β OpenCV Application Areas β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ€
β β
β βββββββββββββββ βββββββββββββββ βββββββββββββββ β
β β Image β β Object β β Face β β
β β Processing β β Detection β β Recognitionβ β
β β Filtering β β YOLO/SSD β β Auth Systemsβ β
β β Transform β β Tracking β β Emotion β β
β βββββββββββββββ βββββββββββββββ βββββββββββββββ β
β β
β βββββββββββββββ βββββββββββββββ βββββββββββββββ β
β β Medical β β Autonomous β β AR/VR β β
β β Imaging β β Driving β β Marker β β
β β CT/MRI β β Lane Detectβ β Recognitionβ β
β β Diagnosis β β Obstacles β β 3D Recon β β
β βββββββββββββββ βββββββββββββββ βββββββββββββββ β
β β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
OpenCV Features¶
| Feature | Description |
|---|---|
| Cross-platform | Supports Windows, macOS, Linux, Android, iOS |
| Multi-language | Bindings for C++, Python, Java, and more |
| Real-time Processing | Optimized algorithms for real-time video processing |
| Rich Features | Over 2500 optimized algorithms |
| Active Community | Extensive documentation, examples, and active development |
2. Installation Methods¶
opencv-python vs opencv-contrib-python¶
ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β OpenCV Python Packages β
ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ€
β β
β opencv-python opencv-contrib-python β
β ββββββββββββββββββββ ββββββββββββββββββββββββββββ β
β β Main modules β β Main modules β β
β β - core β β - core β β
β β - imgproc β β - imgproc β β
β β - video β β - video β β
β β - highgui β β β + Extra modules β β
β β - calib3d β β - SIFT, SURF β β
β β - features2d β β - xfeatures2d β β
β β - objdetect β β - tracking β β
β β - dnn β β - aruco β β
β β - ml β β - face β β
β ββββββββββββββββββββ ββββββββββββββββββββββββββββ β
β β
β β Covers most features β For additional algorithms β
β β Quick installation β Includes patented/research β
β β
ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
Installation via pip¶
# Basic installation (sufficient for most cases)
pip install opencv-python
# Installation with extra features (SIFT, SURF, etc.)
pip install opencv-contrib-python
# Install with NumPy and matplotlib (recommended)
pip install opencv-python numpy matplotlib
# Install specific version
pip install opencv-python==4.8.0.76
# Upgrade
pip install --upgrade opencv-python
Warning: Do not install both opencv-python and opencv-contrib-python simultaneously. This can cause conflicts.
# Wrong (causes conflicts)
pip install opencv-python opencv-contrib-python # β
# Correct (choose one)
pip install opencv-contrib-python # β (contrib includes basic features)
Using Virtual Environment (Recommended)¶
# Create virtual environment
python -m venv opencv_env
# Activate (Windows)
opencv_env\Scripts\activate
# Activate (macOS/Linux)
source opencv_env/bin/activate
# Install packages
pip install opencv-contrib-python numpy matplotlib
# Deactivate
deactivate
3. Development Environment Setup¶
VSCode Setup¶
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β VSCode Recommended Settings β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ€
β β
β Essential Extensions: β
β βββββββββββββββββββββββββββββββββββββββββββββββββββββββ β
β β 1. Python (Microsoft) - Python support β β
β β 2. Pylance - Code analysis β β
β β 3. Jupyter - Notebook support β β
β β 4. Python Image Preview - Image preview β β
β βββββββββββββββββββββββββββββββββββββββββββββββββββββββ β
β β
β Recommended Extensions: β
β βββββββββββββββββββββββββββββββββββββββββββββββββββββββ β
β β 5. Image Preview - Image file preview β β
β β 6. Rainbow CSV - CSV readability β β
β β 7. GitLens - Git history β β
β βββββββββββββββββββββββββββββββββββββββββββββββββββββββ β
β β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
Recommended settings.json:
{
"python.analysis.typeCheckingMode": "basic",
"python.analysis.autoImportCompletions": true,
"[python]": {
"editor.formatOnSave": true
}
}
PyCharm Setup¶
- Create Project: File β New Project β Pure Python
- Configure Interpreter: Settings β Project β Python Interpreter
- Install Packages: + button β search opencv-contrib-python β Install
Jupyter Notebook¶
# Install Jupyter
pip install jupyter
# Run
jupyter notebook
# Or JupyterLab
pip install jupyterlab
jupyter lab
Displaying images in Jupyter:
import cv2
import matplotlib.pyplot as plt
img = cv2.imread('image.jpg')
# BGR β RGB conversion (matplotlib uses RGB)
img_rgb = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
plt.imshow(img_rgb)
plt.axis('off')
plt.show()
4. Version Check and First Program¶
Installation Verification¶
import cv2
import numpy as np
# Check OpenCV version
print(f"OpenCV version: {cv2.__version__}")
# Output example: OpenCV version: 4.8.0
# Check NumPy version
print(f"NumPy version: {np.__version__}")
# Output example: NumPy version: 1.24.3
# Check build information (detailed)
print(cv2.getBuildInformation())
First Program: Reading and Displaying an Image¶
import cv2
# Read image
img = cv2.imread('sample.jpg')
# Check if image was read successfully
if img is None:
print("Cannot read image!")
else:
print(f"Image size: {img.shape}")
# Display image in window
cv2.imshow('My First OpenCV', img)
# Wait for key press (0 = wait indefinitely)
cv2.waitKey(0)
# Close all windows
cv2.destroyAllWindows()
Testing Without an Image¶
import cv2
import numpy as np
# Create black image (300x400, 3 channels)
img = np.zeros((300, 400, 3), dtype=np.uint8)
# Add text
cv2.putText(img, 'Hello OpenCV!', (50, 150),
cv2.FONT_HERSHEY_SIMPLEX, 1, (255, 255, 255), 2)
# Draw circle (center, radius, color, thickness)
cv2.circle(img, (200, 200), 50, (0, 255, 0), 2)
# Display
cv2.imshow('Test Image', img)
cv2.waitKey(0)
cv2.destroyAllWindows()
5. OpenCV and NumPy Relationship¶
NumPy-based Structure¶
In OpenCV-Python, images are represented as NumPy arrays (ndarray).
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β Relationship between OpenCV and NumPy β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ€
β β
β cv2.imread() β
β β β
β βΌ β
β βββββββββββββββββββββββββββββββββββββββββββββββββββ β
β β numpy.ndarray β β
β β βββββββββββββββββββββββββββββββββββββββββββ β β
β β β shape: (height, width, channels) β β β
β β β dtype: uint8 (0-255) β β β
β β β data: actual pixel values β β β
β β βββββββββββββββββββββββββββββββββββββββββββ β β
β βββββββββββββββββββββββββββββββββββββββββββββββββββ β
β β β
β βΌ β
β NumPy operations available: β
β - Slicing: img[100:200, 50:150] β
β - Operations: img + 50, img * 1.5 β
β - Functions: np.mean(img), np.max(img) β
β β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
Example Using NumPy Operations¶
import cv2
import numpy as np
img = cv2.imread('sample.jpg')
# Use NumPy functions
print(f"Average brightness: {np.mean(img):.2f}")
print(f"Maximum: {np.max(img)}")
print(f"Minimum: {np.min(img)}")
# Adjust brightness with array operations
brighter = np.clip(img + 50, 0, 255).astype(np.uint8)
darker = np.clip(img - 50, 0, 255).astype(np.uint8)
# Comparison operations
bright_pixels = img > 200 # Boolean array
# Statistics
print(f"Standard deviation: {np.std(img):.2f}")
OpenCV Functions vs NumPy Operations¶
import cv2
import numpy as np
img = cv2.imread('sample.jpg')
# Method 1: Using OpenCV functions
mean_cv = cv2.mean(img)
print(f"OpenCV mean: {mean_cv}") # (B_avg, G_avg, R_avg, 0)
# Method 2: Using NumPy
mean_np = np.mean(img, axis=(0, 1))
print(f"NumPy mean: {mean_np}") # [B_avg, G_avg, R_avg]
# Performance comparison (OpenCV is usually faster)
import time
# Gaussian blur comparison
img_large = np.random.randint(0, 256, (1000, 1000, 3), dtype=np.uint8)
start = time.time()
blur_cv = cv2.GaussianBlur(img_large, (5, 5), 0)
print(f"OpenCV: {time.time() - start:.4f}s")
6. Images are ndarrays¶
Image Data Structure¶
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β Image = 3D Array β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ€
β β
β img.shape = (height, width, channels) β
β β
β e.g., (480, 640, 3) β 480 rows Γ 640 cols Γ 3 channels (BGR) β
β β
β width (columns, x-axis) β
β βββββββββββββββββ β
β βββββββββββββββββββ β β
β β B G R β B G R β β β β
β β pixel β pixel β β β height β
β βββββββββΌββββββββ€ β β (rows, y-axis) β
β β B G R β B G R β β β β
β β pixel β pixel β β β β
β βββββββββββββββββββ β β
β β
β Access: img[y, x] or img[y, x, channel] β
β β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
Data Types (dtype)¶
import cv2
import numpy as np
img = cv2.imread('sample.jpg')
# Basic data type
print(f"Data type: {img.dtype}") # uint8
# Common data types
# uint8: 0 ~ 255 (most common)
# float32: 0.0 ~ 1.0 (deep learning, precision calculations)
# float64: 0.0 ~ 1.0 (scientific computing)
# Type conversion
img_float = img.astype(np.float32) / 255.0
print(f"After conversion: {img_float.dtype}, range: {img_float.min():.2f} ~ {img_float.max():.2f}")
# Back to uint8 (for saving/display)
img_back = (img_float * 255).astype(np.uint8)
Various Image Forms¶
import cv2
import numpy as np
# Color image (3 channels)
color_img = cv2.imread('sample.jpg', cv2.IMREAD_COLOR)
print(f"Color: {color_img.shape}") # (H, W, 3)
# Grayscale (1 channel, 2D)
gray_img = cv2.imread('sample.jpg', cv2.IMREAD_GRAYSCALE)
print(f"Gray: {gray_img.shape}") # (H, W)
# With alpha channel (4 channels)
alpha_img = cv2.imread('sample.png', cv2.IMREAD_UNCHANGED)
if alpha_img is not None and alpha_img.shape[2] == 4:
print(f"With alpha: {alpha_img.shape}") # (H, W, 4)
# Create new images
blank_color = np.zeros((300, 400, 3), dtype=np.uint8) # Black color
blank_gray = np.zeros((300, 400), dtype=np.uint8) # Black gray
white_img = np.ones((300, 400, 3), dtype=np.uint8) * 255 # White
Checking Image Properties¶
import cv2
img = cv2.imread('sample.jpg')
if img is not None:
# Basic properties
print(f"Shape (H, W, C): {img.shape}")
print(f"Height: {img.shape[0]}px")
print(f"Width: {img.shape[1]}px")
print(f"Channels: {img.shape[2]}")
# Data properties
print(f"Data type: {img.dtype}")
print(f"Total pixels: {img.size}") # H * W * C
print(f"Memory size: {img.nbytes} bytes")
# Dimensions
print(f"Dimensions: {img.ndim}") # Color=3, Gray=2
7. Practice Problems¶
Exercise 1: Environment Check Script¶
Write a script that outputs the following information:
- OpenCV version
- NumPy version
- Python version
- GPU acceleration availability (cv2.cuda.getCudaEnabledDeviceCount())
# Hint
import cv2
import numpy as np
import sys
# Write your code here
Exercise 2: Image Info Printer¶
Write a function that outputs all properties of a given image file:
def print_image_info(filepath):
"""
Prints detailed information about an image file.
Output items:
- File path
- Load success status
- Image size (width x height)
- Number of channels
- Data type
- Memory usage
- Pixel value range (min, max)
- Average brightness
"""
# Write your code here
pass
Exercise 3: Creating Blank Canvas¶
Create and save images with the following conditions:
- 800x600 black image
- 800x600 white image
- 800x600 red image (what is red in BGR?)
- 400x400 checkerboard pattern (50px units)
Exercise 4: NumPy Operations Practice¶
After loading an image, perform the following operations:
# 1. Increase brightness by 50 (apply clipping)
# 2. Decrease brightness by 50 (apply clipping)
# 3. Increase contrast by 1.5x
# 4. Invert image (255 - img)
Exercise 5: Channel Separation Preview¶
Write code to separate a color image into BGR channels and display each as grayscale. Use NumPy indexing.
8. Next Steps¶
In 02_Image_Basics.md, you'll learn basic image operations such as reading/writing images, pixel access, and ROI settings!
Topics to Learn Next:
- Details of cv2.imread(), cv2.imshow(), cv2.imwrite()
- Pixel-level access and modification
- Region of Interest (ROI) settings
- Image copying vs referencing
9. References¶
Official Documentation¶
Useful Links¶
- PyImageSearch - Many practical examples
- Learn OpenCV - Advanced tutorials
- OpenCV GitHub
Related Learning Materials¶
| Folder | Related Content |
|---|---|
| Python/ | NumPy array operations, type hints |
| Linux/ | Development environment, terminal usage |