OpenCV
OpenCV (Open Source Computer Vision Library) is an open-source computer vision and machine learning library. It allows us to process images and videos, detect objects, faces and even handwriting.
Why Learn OpenCV?�
Install OpenCV
To check if OpenCV is correctly installed
Reading Images�
# Python code to read image
import cv2
#To read image from disk, we use
# cv2.imread function, in below method,
img = cv2.imread("geeks.png", cv2.IMREAD_COLOR)
print(img)
Extracting height and width of image
# Importing the OpenCV library
import cv2
# Reading the image using imread() function
image = cv2.imread('image.jpg')
# Extracting the height and width of an image
h, w = image.shape[:2]
# Displaying the height and width
print("Height = {}, Width = {}".format(h, w))
Extracting the RGB Values of a Pixel�
import cv2
# ---------- Load Image ----------
# Replace 'image.jpg' with your file path
image = cv2.imread("image.jpg")
# OpenCV loads images in BGR format, so we convert to RGB
image_rgb = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
# ---------- Extract RGB Values ----------
# Example: get RGB values of a single pixel at (x=50, y=100)
x, y = 50, 100
(b, g, r) = image[y, x] # Note: OpenCV uses (row, col) => (y, x)
print(f"Pixel at ({x}, {y}) - R: {r}, G: {g}, B: {b}")
# ---------- Extract All RGB Values ----------
# Convert to list of tuples [(R,G,B), (R,G,B), ...]
rgb_values = image_rgb.reshape(-1, 3)
print("First 10 RGB values:", rgb_values[:10])
Resizing Image�
Color Spaces�