Skip to main content

OpenCV and Images

Shenzhen, China

Github Repo

Setup OpenCV

Create and activate a virtual work environment:

python -m venv .env
source .env/bin/activate
python -m pip install --upgrade pip

Add a file dependencies.txt with all project pip dependencies:

opencv-python

Install all dependencies with:

pip install -r dependencies.txt

OpenCV Basic Operations

Working with Images

import cv2

import_path = r'./resources/test-image.jpg'
# Read image
image = cv2.imread(import_path)
# Display image for 5s
cv2.imshow('Zhu Jiang New Town, Guangzhou, Guangdong, China', image)
cv2.waitKey(5000)
cv2.destroyAllWindows()
# Save image to output folder
cv2.imwrite('./processed/processed_test_image.jpg', image)

Manipulating Images

Colour Space

# Display image properties
print(image.shape)

The Shape command displays the height, width and colour channels: (476, 715, 3).

Colour space conversion:

## RGB -> Gray
image_monochrome = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
export_path_monochrome = ('./processed/monochrome-' + timestamp + '.jpg')
cv2.imwrite(export_path_monochrome, image_monochrome)
print(image_monochrome.shape)
## RGB -> HSV
image_hsv = cv2.cvtColor(image, cv2.COLOR_BGR2HSV)
export_path_colour = ('./processed/colour-' + timestamp + '.jpg')
cv2.imwrite(export_path_colour, image_hsv)
print(image_hsv.shape)
(476, 715, 3)
(476, 715)
(476, 715, 3)

You can also use a parameter in the imread() function:

  • 0 - Grayscale
  • 1 - Colour
  • -1 - Unchanged
img_grayscale = cv2.imread(import_path, 0)
img_colour = cv2.imread(import_path, 1)
img_original = cv2.imread(import_path, -1)

Resizing Images

img_resized = cv2.resize(image, (450, 450))
cv2.imshow('Zhu Jiang New Town, Guangzhou, Guangdong, China', img_resized)
cv2.waitKey(2000)
cv2.destroyAllWindows()

Adding Text

watermark = "Hello World!"
coordinates = (50, 150)
font = cv2.QT_FONT_NORMAL
fontScale = 1
color = (255, 0, 0)
thickness = 2

img_watermarked = cv2.putText(image, watermark, coordinates, font, fontScale, color, thickness)
cv2.imshow('Zhu Jiang New Town, Guangzhou, Guangdong, China', img_watermarked)
cv2.waitKey(2000)
cv2.destroyAllWindows()

Drawing Shapes

  • Lines
start = (0, 0)
end = (715, 476)
color = (0, 0, 255)
thickness = 2

img_line = cv2.line(image, start, end, color, thickness)
cv2.imshow('Zhu Jiang New Town, Guangzhou, Guangdong, China', img_line)
cv2.waitKey(2000)
cv2.destroyAllWindows()
  • Circles
center = (100, 100)
radius = 80
color = (255, 255, 255)
thickness = 2

img_line = cv2.circle(image, center, radius, color, thickness)
cv2.imshow('Zhu Jiang New Town, Guangzhou, Guangdong, China', img_line)
cv2.waitKey(2000)
cv2.destroyAllWindows()
  • Rectangle
start = (280, 80)
end = (350, 350)
color = (0, 255, 0)
thickness = 2

img_line = cv2.rectangle(image, start, end, color, thickness)
cv2.imshow('Zhu Jiang New Town, Guangzhou, Guangdong, China', img_line)
cv2.waitKey(2000)
cv2.destroyAllWindows()
  • Ellipse
center = (120, 100)
axesLength = (100, 50)
angle = 30
startAngle = 0
endAngle = 360
color = (0, 0, 255)
thickness = 2

img_line = cv2.ellipse(image, center, axesLength, angle, startAngle, endAngle, color, thickness)
cv2.imshow('Zhu Jiang New Town, Guangzhou, Guangdong, China', img_line)
cv2.waitKey(2000)
cv2.destroyAllWindows()