#!/usr/bin/env python # coding: utf-8 # # Digital Image and Processing Example # # This notebook provides a simple example of digital image representation and simple processing using Open CV. # In[1]: # Import necessary libraries import cv2 import numpy as np import matplotlib.pyplot as plt # ##### Step 1: Load an image from a file # In[2]: image_path = 'images/chess_board.bmp' # Source: https://commons.wikimedia.org/wiki/File:Affine_Transformation_Original_Checkerboard.jpg # image_path = 'images/Lena.png' # Image of Lena Forsén used in many image processing experiments # Source: https://en.wikipedia.org/wiki/Lenna original_image = cv2.imread(image_path) # ##### Step 2: Display the loaded image in its original format # In[3]: plt.figure(figsize=(5, 5)) plt.imshow(cv2.cvtColor(original_image, cv2.COLOR_BGR2RGB)) plt.title('Original Image') plt.axis('off') plt.show() # A digital image is represented as a matrix that contains the intensity values of its corresponding pixels. In our case, the image is stored as a NumPy array. Color images have 3 channels (Red, Green and Blue), so the array has dimensions (image_height, image_width, 3): # In[4]: image_shape = original_image.shape # Get the image size print(f"Image dimensions = {image_shape}") # ##### Step 3: Display the Red, Green, and Blue channels as 3 independent images # # In[5]: r_channel, g_channel, b_channel = cv2.split(original_image) plt.figure(figsize=(15, 5)) plt.subplot(1, 3, 1) plt.imshow(r_channel, cmap='gray') # plt.imshow(r_channel, cmap='Reds_r') plt.title('Red Channel') plt.axis('off') plt.subplot(1, 3, 2) plt.imshow(g_channel, cmap='gray') # plt.imshow(g_channel, cmap='Greens_r') plt.title('Green Channel') plt.axis('off') plt.subplot(1, 3, 3) plt.imshow(b_channel, cmap='gray') # plt.imshow(b_channel, cmap='Blues_r') plt.title('Blue Channel') plt.axis('off') plt.show() # ##### Step 4: Convert the original image to grayscale and display it # # In[6]: gray_image = cv2.cvtColor(original_image, cv2.COLOR_BGR2GRAY) plt.figure(figsize=(5, 5)) plt.imshow(gray_image, cmap='gray') plt.title('Grayscale Image') plt.axis('off') plt.show() # The gray scale image is also an array, but it only has one chanel (gray). Therefore, the dimensions of the array are now (image_height, image_width): # In[7]: gray_image.shape # Our image array is composed by 8-bit integer numbers: # - 0 = lowest intensity (black) # - 255 = highest intensity (white - for a gray scale image) # # Check the content around the center of the array: # In[8]: y1 = int(image_shape[0]/2 - 5) y2 = int(image_shape[0]/2 + 5) x1 = int(image_shape[1]/2 - 5) x2 = int(image_shape[1]/2 + 5) print(f'Pixels: ({y1}:{y2}, {x1}:{x2})') print(gray_image[y1:y2,x1:x2]) # ##### Step 5: Apply a smoothing filter (convolution with a mask) to the grayscale image # # In[9]: kernel_size = 9 smoothed_image = cv2.GaussianBlur(gray_image, (kernel_size, kernel_size), 0) plt.figure(figsize=(5, 5)) plt.imshow(smoothed_image, cmap='gray') plt.title('Smoothed Image') plt.axis('off') plt.show() # Check the values of the same pixels around the center of the image: # In[10]: print(smoothed_image[y1:y2,x1:x2]) # ##### Step 6: Apply Sobel filter to get the contours # In[11]: sobel_x = cv2.Sobel(gray_image, cv2.CV_64F, 1, 0, ksize=5) sobel_y = cv2.Sobel(gray_image, cv2.CV_64F, 0, 1, ksize=5) sobel_magnitude = np.sqrt(sobel_x**2 + sobel_y**2) plt.figure(figsize=(5, 5)) plt.imshow(sobel_magnitude, cmap='gray') plt.title('Sobel Filtered Image') plt.axis('off') plt.show() # ### Conclusion # # After completing this notebook, you should understand the basics of digital image representation, and how to use OpenCV functions to execute simple image processing.