0
0
Computer Visionml~5 mins

Python CV ecosystem (OpenCV, PIL, torchvision) in Computer Vision

Choose your learning style9 modes available
Introduction

Python CV ecosystem helps you work with images and videos easily. It lets you read, edit, and analyze pictures for projects like face detection or photo filters.

You want to open and show images in your program.
You need to apply filters or resize photos for a project.
You want to prepare images for a machine learning model.
You want to detect objects or faces in pictures or videos.
You want to convert images between different formats.
Syntax
Computer Vision
import cv2
from PIL import Image
import torchvision.transforms as transforms

OpenCV (cv2) is great for fast image and video processing.

PIL (Pillow) is simple for opening and editing images.

torchvision helps prepare images for deep learning models.

Examples
OpenCV reads and shows an image in a window.
Computer Vision
import cv2
img = cv2.imread('photo.jpg')
cv2.imshow('Image', img)
cv2.waitKey(0)
cv2.destroyAllWindows()
PIL opens an image, resizes it, and displays it.
Computer Vision
from PIL import Image
img = Image.open('photo.jpg')
img = img.resize((100, 100))
img.show()
torchvision transforms resize an image and convert it to a tensor for ML models.
Computer Vision
from PIL import Image
import torchvision.transforms as transforms
img = Image.open('photo.jpg')
transform = transforms.Compose([
    transforms.Resize((128, 128)),
    transforms.ToTensor()
])
tensor_img = transform(img)
Sample Model

This program shows how to open an image with PIL, convert it to OpenCV format, resize it, convert back to PIL, and finally transform it to a tensor using torchvision. It prints the size and shape at each step.

Computer Vision
import cv2
import numpy as np
from PIL import Image
import torchvision.transforms as transforms

# Open image with PIL
img_pil = Image.open('sample.jpg')
print(f'PIL image size: {img_pil.size}')

# Convert PIL image to OpenCV format
img_cv = cv2.cvtColor(np.array(img_pil), cv2.COLOR_RGB2BGR)
print(f'OpenCV image shape: {img_cv.shape}')

# Resize image using OpenCV
img_cv_resized = cv2.resize(img_cv, (64, 64))
print(f'Resized OpenCV image shape: {img_cv_resized.shape}')

# Convert back to PIL
img_pil_resized = Image.fromarray(cv2.cvtColor(img_cv_resized, cv2.COLOR_BGR2RGB))

# Use torchvision to transform image to tensor
transform = transforms.ToTensor()
tensor_img = transform(img_pil_resized)
print(f'Tensor shape: {tensor_img.shape}')
OutputSuccess
Important Notes

OpenCV uses BGR color order, while PIL uses RGB. Remember to convert colors when switching.

torchvision transforms are useful to prepare images for deep learning models.

Always check image shapes and sizes after transformations to avoid errors.

Summary

OpenCV, PIL, and torchvision are key tools for working with images in Python.

Use OpenCV for fast image/video processing, PIL for easy image editing, and torchvision for ML image preparation.

Converting between these libraries is common and requires attention to color formats and shapes.