Welcome back to our computer vision blog series! The foundations of image processing and feature extraction were covered in the earlier sections. We’re going to explore a useful application of computer vision today: Working with OpenCV Images. An open-source software library for computer vision and machine learning is called OpenCV (Open Source Computer Vision Library). It has more than 2,500 well-optimized algorithms that can be applied to a range of image and video processing applications.

We will look at how to use OpenCV with Python to read and edit images in this blog. The following subjects will be discussed:

  1. Configuring OpenCV.
  2. Interpreting and projecting visuals.
  3. Executing simple image editing.
  4. Adding text and shapes to pictures.

Configuring OpenCV

Make sure OpenCV is installed first. you can install it using pip

pip install opencv-python

Reading and Displaying Images

import cv2

# Read an image from file
image = cv2.imread('path_to_your_image.jpg')

# Check if the image was successfully loaded
if image is None:
    print('Error: Could not open or find the image.')
else:
    # Display the image in a window
    cv2.imshow('Image', image)

    # Wait for a key press indefinitely or for a specified amount of time in milliseconds
    cv2.waitKey(0)

    # Close all OpenCV windows
    cv2.destroyAllWindows()

In this script, we use cv2.imread() to read the image and cv2.imshow() to display it in a window. The cv2.waitKey(0) function waits for a key press before closing the window with cv2.destroyAllWindows().

Performing Basic Image Manipulations

Several fundamental image manipulation operations, including resizing, cropping, rotating, and colour space conversion, are supported by OpenCV. Here are a few instances:

Resizing an Image:

# Resize the image to a specific width and height
resized_image = cv2.resize(image, (300, 300))

# Display the resized image
cv2.imshow('Resized Image', resized_image)
cv2.waitKey(0)
cv2.destroyAllWindows()

Cropping an Image:

# Crop a region of interest (ROI) from the image
# Define the ROI as a rectangle [startY:endY, startX:endX]
cropped_image = image[50:200, 100:300]

# Display the cropped image
cv2.imshow('Cropped Image', cropped_image)
cv2.waitKey(0)
cv2.destroyAllWindows()

Rotating an Image:

# Get the dimensions of the image
(h, w) = image.shape[:2]

# Define the center of the image
center = (w // 2, h // 2)

# Rotate the image by 45 degrees
M = cv2.getRotationMatrix2D(center, 45, 1.0)
rotated_image = cv2.warpAffine(image, M, (w, h))

# Display the rotated image
cv2.imshow('Rotated Image', rotated_image)
cv2.waitKey(0)
cv2.destroyAllWindows()

Converting Color Spaces:

# Convert the image from BGR to Grayscale
gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

# Display the grayscale image
cv2.imshow('Grayscale Image', gray_image)
cv2.waitKey(0)
cv2.destroyAllWindows()

Drawing Shapes and Text on Images

With OpenCV, you can add text to images and draw different shapes. Here are a few instances:

Drawing a Rectangle:

# Draw a rectangle on the image
# Define the top-left and bottom-right corners of the rectangle
cv2.rectangle(image, (50, 50), (200, 200), (0, 255, 0), 2)

# Display the image with the rectangle
cv2.imshow('Rectangle', image)
cv2.waitKey(0)
cv2.destroyAllWindows()

Drawing a Circle:

# Draw a circle on the image
# Define the center and radius of the circle
cv2.circle(image, (150, 150), 50, (255, 0, 0), 3)

# Display the image with the circle
cv2.imshow('Circle', image)
cv2.waitKey(0)
cv2.destroyAllWindows()

Drawing a Line:

# Draw a line on the image
# Define the start and end points of the line
cv2.line(image, (50, 50), (250, 250), (0, 0, 255), 4)

# Display the image with the line
cv2.imshow('Line', image)
cv2.waitKey(0)
cv2.destroyAllWindows()

Adding Text:

# Add text to the image
# Define the text, position, font, font scale, color, and thickness
cv2.putText(image, 'OpenCV', (100, 100), cv2.FONT_HERSHEY_SIMPLEX, 2, (255, 255, 255), 3)

# Display the image with the text
cv2.imshow('Text', image)
cv2.waitKey(0)
cv2.destroyAllWindows()

We looked at the fundamentals of using OpenCV to work with images in this blog. Python was used to teach us how to read and display images, manipulate them in a basic way, and add text and shapes to them. These abilities are essential for any computer vision project and serve as a strong basis for our upcoming blog posts on more complex subjects.

Watch this space for our upcoming blog post, where we’ll delve deeper into the process of recognising and locating objects in images. We’ll be examining a variety of algorithms and techniques for this purpose. Have fun with coding!

Categories: Uncategorized