1 of 32

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. 

2 of 32

3 of 32

Why Learn OpenCV?�

  • Comprehensive Image Processing: OpenCV has a range of functions to manipulate and analyze images helps in making it ideal for various applications.
  • Real-Time Video Processing: It supports video capture and real-time video processing.
  • Cross-Platform: Works on multiple platforms like Windows, Linux, macOS and Android.
  • Open-Source: It is free to use and has a large community support.
  • Integration with Deep Learning: It integrates with popular deep learning libraries like TensorFlow and PyTorch.

4 of 32

Install OpenCV

  • Check for python installation.

  • PIP is a package management system used to install and manage software packages/libraries written in Python. 

5 of 32

6 of 32

To check if OpenCV is correctly installed

7 of 32

Reading Images�

  • To read the images cv2.imread() method is used. This method loads an image from the specified file.
  • Code

# 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)

8 of 32

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))

9 of 32

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])

10 of 32

Resizing Image�

  • Image resizing refers to the scaling of images. It helps in reducing the number of pixels from an image and that has several advantages e.g. It can reduce the time of training of a neural network as more is the number of pixels in an image more is the number of input nodes that in turn increases the complexity of the model. 

11 of 32

  • import cv2
  • import numpy as np
  • import matplotlib.pyplot as plt

  • image = cv2.imread("geeks.png", 1)
  • # Loading the image
  • half = cv2.resize(image, (0, 0), fx = 0.1, fy = 0.1)
  • bigger = cv2.resize(image, (1050, 1610))

  • stretch_near = cv2.resize(image, (780, 540),
  • interpolation = cv2.INTER_NEAREST)
  • Titles =["Original", "Half", "Bigger", "Interpolation Nearest"]
  • images =[image, half, bigger, stretch_near]
  • count = 4
  • for i in range(count):
  • plt.subplot(2, 3, i + 1)
  • plt.title(Titles[i])
  • plt.imshow(images[i])
  • plt.show()

12 of 32

Color Spaces�

13 of 32

14 of 32

15 of 32

16 of 32

17 of 32

18 of 32

19 of 32

20 of 32

21 of 32

22 of 32

23 of 32

24 of 32

25 of 32

26 of 32

27 of 32

28 of 32

29 of 32

30 of 32

31 of 32

32 of 32