OpenCV Python Notes - Basic Image Operations

OpenCV Python Notes - Basic Image Operations

Basic Image Operations

Reference: OpenCV for Python Developers on Linkedin Learning

Link: https://www.linkedin.com/learning/opencv-for-python-developers/

In [3]:
import numpy as np
import cv2
from matplotlib import pyplot as plt

Utility functions for displaying images.

In [31]:
def show_image(title, bgr_image):
    rgb_image = cv2.cvtColor(bgr_image, cv2.COLOR_BGR2RGB)
    plt.axis('off')
    plt.title(title)
    plt.imshow(rgb_image)
    plt.show()


def show_bgra_image(title, bgra_image):
    b = bgra_image[:,:,0]
    g = bgra_image[:,:,1]
    r = bgra_image[:,:,2]
    a = bgra_image[:,:,3]
    rgba_image = cv2.merge([r, g, b, a])
    
    plt.axis('off')
    plt.title(title)
    plt.imshow(rgba_image)
    plt.show()

Pixel Data Manipulation

In [16]:
image = cv2.imread("opencv-logo.png")
show_image("OpenCV Logo", image)
In [8]:
type(image)
Out[8]:
numpy.ndarray
In [9]:
image.shape
Out[9]:
(739, 600, 3)
In [10]:
image.dtype
Out[10]:
dtype('uint8')
In [11]:
image.size
Out[11]:
1330200
In [12]:
image
Out[12]:
array([[[255, 255, 255],
        [255, 255, 255],
        [255, 255, 255],
        ...,
        [255, 255, 255],
        [255, 255, 255],
        [255, 255, 255]],

       [[255, 255, 255],
        [255, 255, 255],
        [255, 255, 255],
        ...,
        [255, 255, 255],
        [255, 255, 255],
        [255, 255, 255]],

       [[255, 255, 255],
        [255, 255, 255],
        [255, 255, 255],
        ...,
        [255, 255, 255],
        [255, 255, 255],
        [255, 255, 255]],

       ...,

       [[255, 255, 255],
        [255, 255, 255],
        [255, 255, 255],
        ...,
        [255, 255, 255],
        [255, 255, 255],
        [255, 255, 255]],

       [[255, 255, 255],
        [255, 255, 255],
        [255, 255, 255],
        ...,
        [255, 255, 255],
        [255, 255, 255],
        [255, 255, 255]],

       [[255, 255, 255],
        [255, 255, 255],
        [255, 255, 255],
        ...,
        [255, 255, 255],
        [255, 255, 255],
        [255, 255, 255]]], dtype=uint8)
In [13]:
image[:, :, 0]
Out[13]:
array([[255, 255, 255, ..., 255, 255, 255],
       [255, 255, 255, ..., 255, 255, 255],
       [255, 255, 255, ..., 255, 255, 255],
       ...,
       [255, 255, 255, ..., 255, 255, 255],
       [255, 255, 255, ..., 255, 255, 255],
       [255, 255, 255, ..., 255, 255, 255]], dtype=uint8)
In [18]:
black = np.zeros([150, 200, 1],'uint8')
show_image("Black", black)
print(black[0, 0, :])
[0]
In [19]:
ones = np.ones([150, 200, 3], 'uint8')
show_image("Ones", ones)
print(ones[0, 0, :])
[1 1 1]
In [21]:
white = np.ones([150, 200, 3], 'uint16')
white = white * (2**8 - 1)
show_image("White", white)
print(white[0, 0, :])
[255 255 255]
In [22]:
color = ones.copy()
color[:, :] = (255, 0, 0)
show_image("Blue", color)
print(color[0, 0, :])
[255   0   0]
In [23]:
color = cv2.imread("butterfly.jpg", cv2.IMREAD_COLOR)
show_image("Butterfly", color)
print(color.shape)
height,width,channels = color.shape
(356, 493, 3)
In [24]:
b,g,r = cv2.split(color)

rgb_split = np.empty([height,width * 3, 3],'uint8')

rgb_split[:, 0:width] = cv2.merge([b,b,b])
rgb_split[:, width:width*2] = cv2.merge([g,g,g])
rgb_split[:, width*2:width*3] = cv2.merge([r,r,r])

show_image("Channels",rgb_split)
In [25]:
hsv = cv2.cvtColor(color, cv2.COLOR_BGR2HSV)
h,s,v = cv2.split(hsv)
hsv_split = np.concatenate((h,s,v),axis=1)
show_image("Split HSV",hsv_split)
In [26]:
gray = cv2.cvtColor(color, cv2.COLOR_RGB2GRAY)
show_image("gray",gray)
In [29]:
b = color[:,:,0]
g = color[:,:,1]
r = color[:,:,2]

bgra = cv2.merge((b,g,r,g))
show_bgra_image("bgra",bgra)

Blur, Dilation, and Erosion

In [32]:
image = cv2.imread("thresh.jpg")
show_image("Original",image)

blur = cv2.GaussianBlur(image, (5,55), 0)
show_image("Blur",blur)

kernel = np.ones((5,5),'uint8')

dilate = cv2.dilate(image,kernel,iterations=1)
erode = cv2.erode(image,kernel,iterations=1)

show_image("Dilate",dilate)
show_image("Erode",erode)

Scale and Rotate Images

In [33]:
img = cv2.imread("players.jpg", cv2.IMREAD_COLOR)

# Scale
img_half = cv2.resize(img, (0,0), fx=0.5, fy=0.5)
img_stretch = cv2.resize(img, (600,600))
img_stretch_near = cv2.resize(img, (600,600), interpolation=cv2.INTER_NEAREST)

show_image("Half",img_half)
show_image("Stretch",img_stretch)
show_image("Stretch near",img_stretch_near)

# Rotation
M = cv2.getRotationMatrix2D((0,0), -30, 1)
rotated = cv2.warpAffine(img, M, (img.shape[1], img.shape[0]))
show_image("Rotated",rotated)