Image Processing in Python with OpenCV — Basic Operations

Turgay Ceylan
5 min readDec 19, 2022

--

Introduction image processing with python examples

What is OpenCV?

OpenCV (Open Source Computer Vision Library) is an open source computer vision and machine learning software library. You can use that library in various languages.

OpenCV and Python

Introduction

I’ll use Python language because community of python language is really wide. We must install Anaconda Navigator that provides you many editors, IDEs and libraries inside. We will use libraries such as; OpenCV, MediaPipe, NumPy, Pandas, Matplotlib.

Basic Operations

1 — Import and Show Image

Our first operation is importing an image, mode 0 provides importing grayscale image, also 1 colorful.

import cv2

# import image file
# img = cv2.imread(FILENAME, READING_TYPE)
# Reading Types 0 : Grayscale | 1 : Colorful
img = cv2.imread("steve.jpeg", 0)
cv2.imshow("Steve Jobs (Grayscale)", img)

img2 = cv2.imread("steve.jpeg", 1)
cv2.imshow("Steve Jobs (Colorful)", img2)

cv2.waitKey(1)

Source of code: https://github.com/turgay2317/opencv-python-examples/blob/main/Example%201%20-%20Import%20and%20Show%20Image/example.py

2— Import and Show Video

import cv2

# Actually this method provides importing video like an image frame by frame
capture = cv2.VideoCapture("street.mp4")

width = capture.get(3) # Get capture width
height = capture.get(4) # Get capture height

print("Width : {}, Height: {}".format(width, height))

if not capture.isOpened:
print("Failure")

while capture.isOpened():

# Capture frame
ret, frame = capture.read()

if not ret:
break

# Display result frame
cv2.imshow("Frame", frame)

# Press 'q' to exit
if cv2.waitKey(interval) & 0xFF == ord('q'):
break

# When everything ok, release vid capture object
capture.release()

# Close all windows
cv2.destroyAllWindows()

Source of code: https://github.com/turgay2317/opencv-python-examples/blob/main/Example%202%20-%20Import%20Video/example.py

3 —Shapes and Texts

We need import numpy library to do operation on matrices.

import cv2
import numpy as np

# Set Canvas width and height
width = 512
height = 512

# Create canvas with numpy zero matrix
# np.zeros((width, height, type), datatype)
canvas = np.zeros((width, height, 3), np.uint8)

# Show canvas
cv2.imshow("My Black Canvas", canvas)

# Create a line

# cv2.line(image, (startX, startY), (endX, endY), (BGR Color Code))
# BGR -> (Blue, Green, Red) color codes range 0-255
line1 = cv2.line(canvas, (0,0), (100,100), (255, 255, 255))
cv2.imshow("First line", line1)

line2 = cv2.line(canvas, (100,100), (100,200), (0,255,0))
cv2.imshow("Second line", line2)

# Create a rectangle
# cv2.rectangle(img, (startX, startY), (endX, endY), (BGR Color Code))
rectangle = cv2.rectangle(canvas, (100,200), (150,250), (255,0,0,0))
cv2.imshow("Rectangle", rectangle)

# OR you can fill the rect
rectangle2 = cv2.rectangle(canvas, (150,250), (200,300), (255,50,50), cv2.FILLED)
cv2.imshow("Rectangle filled", rectangle2)

# Create a circle
# cv2.circle(img, (startX, startY), radius, (BGR Color Code))
circle = cv2.circle(canvas, (200,300), 50, (0,0,255))
cv2.imshow("Circle", circle)

# Create a ellipse
# cv2.ellipse(img, center_coordinates, axesLength, angle, startAngle, endAngle, color, thickness)
ellipse = cv2.ellipse(canvas,(256,256),(50,50),0,0,180,255,-1)
cv2.imshow("Ellipse", ellipse)

# Create a text
# cv2.putText(img, value, (startX, startY), font, fontScale, color)
creator = cv2.putText(canvas, "Turgay Ceylan", (200,400), cv2.FONT_HERSHEY_SIMPLEX, 1, (255, 255, 255))
cv2.imshow("Creator", creator)

cv2.waitKey(1)

Output:

Output

4 — Perspective Operations

Let’s imagine we have a game card, but corners of that card is not linear. We determine 4 points to fix that problem then we can move the points however we want. Result of this operation, we get linear borders and regular card.

import cv2
import numpy as np

# Read original image (size : 470x470)
original_img = cv2.imread("view.png")

# Set start points of image
points1 = np.float32([[283,0], [0,281],[190,470],[465,187]])

# Set new start points of image
points2 = np.float32([[470,0], [0, 0], [0, 282], [465, 282]])

# define matrix as transform first points to last points
matrix = cv2.getPerspectiveTransform(points1, points2)

# Create output image by matrix
output_img = cv2.warpPerspective(original_img, matrix, (470, 282))

# Show output image
cv2.imshow("Rotated Image", output_img)

cv2.waitKey(1)

Source of code: https://github.com/turgay2317/opencv-python-examples/blob/main/Example%204%20-%20Image%20Perspective/example.py

Output:

Before:

Before
After perspective operation

5— Image Merge

Sometimes we need image merging. We’ll merge Photos of Steve Jobs and Bill Gates in example.

import cv2
import matplotlib.pyplot as plt

img = cv2.imread("steve.png")
img2 = cv2.imread("bill.png")

# BGR to RGB
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
img2 = cv2.cvtColor(img2, cv2.COLOR_BGR2RGB)

# Resize operations
img = cv2.resize(img, (400, 400))
img2 = cv2.resize(img2, (400,400))

# Mix operation
blended = cv2.addWeighted(src1 = img, alpha = 0.5, src2 = img2 , beta = 0.5, gamma = 0)
plt.figure()
plt.axis("off")
plt.imshow(blended)

Source of code: https://github.com/turgay2317/opencv-python-examples/blob/main/Example%204%20-%20Image%20Perspective/example.py

Output:

Output

6 — Image Thresholding

In digital image processing, thresholding is the simplest method of segmenting images. From a grayscale image, thresholding can be used to create binary images.

import cv2
import matplotlib.pyplot as plt

# Import image
img = cv2.imread("shine.jpeg")
img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

# Show original image
plt.figure()
plt.imshow(img, cmap="gray")
plt.axis("OFF")
plt.title("Original Image")

# Threshold Operation
""""
Binary Image: image definition as white and black
Threshold: it is method for converting image to binary image.

Some threshold types:
* THRESH_BINARY
* THRESH_BINARY_INV
* THRESH_TRUNC
* THRESH_TOZERO
* THRESH_TOZERO_INV
"""
_, thresh_image = cv2.threshold(img, thresh = 60, maxval = 255, type = cv2.THRESH_BINARY)
plt.figure()
plt.imshow(thresh_image, cmap="gray")
plt.axis("off")
plt.title("Thresh Binary")

# Adaptive Threshold Operation
"""
Some adaptive threshold algorithms:
* ADAPTIVE_THRESH_MEAN_C
* ADAPTIVE_THRESH_GAUSSIAN_C
"""
# Usage: .adaptiveThreshold(img, maxValue, threshing algorithm [constant c is 8], threshold type, block size, constant c)
thresh_image2 = cv2.adaptiveThreshold(img, 255, cv2.ADAPTIVE_THRESH_MEAN_C, cv2.THRESH_BINARY, 11,8)
plt.figure()
plt.imshow(thresh_image2, cmap="gray")
plt.axis("OFF")
plt.title("Adaptive Thresh")

cv2.waitKey(1)

Source of code: https://github.com/turgay2317/opencv-python-examples/blob/main/Example%206%20-%20Image%20Thresholding/example.py

Output:

Output of different thresholding types

I’ll share more examples with you. You can view all of my examples on my github.

My github repository: https://github.com/turgay2317/opencv-python-examples

Thanks.

--

--

Turgay Ceylan

Self learner, Computer Engineering student at Gazi University