Guide to Image 💫️ Manipulation in Python with OpenCV 🗿️

Guide to Image 💫️ Manipulation in Python with OpenCV 🗿️

·

11 min read

I wanted to write this tutorial playing with Image using OpenCV long before but due to a hectic work schedule unable to do so. Here I'm packed and loaded with Magic wand and ready to spells Alohomora ⚡🧙

Introduction

Whether you want to build a complex machine learning model or live face recognition program or just trying to build a graduate project for college, you will see the importance of OpenCV along the way.

OpenCV is a huge image and video processing library designed to work with many languages such as python, C/C++, Java, and more. It is the foundation for many of the applications you know that deal with image processing.

  • Ease of coding
  • Fast prototyping
  • Vast libraries for machine/deep learning
  • Open Source

Today we will learn how to work with OpenCV, and I’ll do my best to keep it simple.

Install OpenCV

Now it’s the time to install OpenCV using only one simple command:

pip install opencv-python

Note: I recommend to use any python environment then install the package to avoid any such error and conflicts.

Importing a simple image

The first thing you will need to learn is importing a simple image and displaying it using OpenCV.

The code is straightforward:

import cv2
img = cv2.imread("test_image.jpg")
cv2.imshow("Image", img)
cv2.waitKey(0)
cv2.destroyAllWindows()

Output:

497-4979354_computer-guy-meme-hd-png-download.png

Let's just explore what happened here,

  1. we have first imported the package into code
  2. imread('test_image.jpg') loaded the image into memory
  3. After that If we stop the program now, we would have loaded the image but done nothing with it, so instead let's at least present the image into a new window so the user can see the result. For that, we will use cv2.imshow and passing the window name and the image as arguments.
  4. Lastly, we tell Python not to exit the program until we press a key or close the window. Then we clean everything up by destroying all windows we opened.

Loading Videos

OpenCV is great at dealing not only with images but also with videos. The video streams can be loaded from a video file or directly from a video source such as a webcam.

In the next example, we will load a video from the webcam and present it on a new window:

import cv2
cap = cv2.VideoCapture('test_videos.mp4')

while(cap.isOpened()):
    ret, frame = cap.read()
    cv2.imshow('frame',frame)
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break

cap.release()
cv2.destroyAllWindows()

Output:

deal1.gif

Let's see what just happened here:

  • The code is self-explanatory, but let’s review it in detail. We use the method VideoCapture to load the video resource.
  • Each frame we read from a video load in the same way as an image is crucial because it means that we have the entire arsenal of OpenCV functions at our disposal when dealing with videos.
  • Now the video is playing, but there’s no way out of the while loop, so let’s build an exit strategy by detecting if the q key has been pressed. If it is, then we exit the loop for the cleanup activities.

Resizing Images

Changing image sizes has a wide range of applications, from optimizing for sizes, zooming, or even feeding a neural network to perform some magic. If resizing an image is what you want, OpenCV got you covered.

Let’s now see an example of how to resize an image:

import cv2

src = cv2.imread('test.jpg', cv2.IMREAD_UNCHANGED)

#percent by which the image is resized
scale_percent = 50

#calculate the 50 percent of original dimensions
width = int(src.shape[1] * scale_percent / 100)
height = int(src.shape[0] * scale_percent / 100)

# dsize
dsize = (width, height)

# resize image
output = cv2.resize(src, dsize)

cv2.imwrite('test-resize-image-50.png',output)

Output:

combine.jpg

It’s pretty simple, so we added some flavor to it, and instead of simply resizing the image to a specific size, we are scaling down the image by a factor of X (50% for the example). Note that the code will be simpler if we are targeting specific dimensions.

Switching Color Spaces

When we read an image with OpenCV we think of colors as channels or the depth of the image array where each channel or dimension corresponds to a color.

The most common color space and the one you probably already know is RGB, consisting of 3 channels: red, green, and blue. But other systems can represent a color on an image, like LAB, YCrCb, HLS, and HSV, among others.

A more popular option for setting colors on an image is grayscale, where only one channel defines each pixel. Let’s see an example of how we can transform a color image into a greyscale one.

import cv2

img = cv2.imread('test.jpg')
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
cv2.imshow("Gray", gray)
cv2.waitKey(0)
cv2.destroyAllWindows()

Output:

test-resize-image-gray.jpg

The function where all the magic happens is cvtColor, which expects two arguments, the image, and the color space, and returns the new image without altering the original.

Drawing on Images

So far, we have been playing with the images without adding anything new to them. It’s time we change that. OpenCV allows us not only to perform transformations and effects to the images but to change them or draw on them.

Let’s draw a few geometric shapes on images to show how it works.

Drawing a line

We will try to draw a line on an image using the line function:

import cv2

img = cv2.imread('test.jpg')
line = cv2.line(img, (40,40), (200, 150), (255, 0, 0), 5)
cv2.imshow("Line", line)
cv2.waitKey(0)
cv2.destroyAllWindows()

Output:

image_line.jpg

Let's see what happened here: The line function expects the image and four more arguments: the start of the line in (x1, y1), the end of the line in (x2, y2), the line’s color (in BGR for our image), and its thickness in pixels.

Drawing a rectangle

I think rectangles are the most used shape, at least in the AI world, as they are commonly used to track objects like faces, cars, or traffic signs on the images. They are also super easy to use.

Let's just code here with an example:

import cv2

img = cv2.imread('test.jpg')
line = cv2.rectangle(img, (200,200), (340, 340), (255, 0, 0), 5)
cv2.imshow("Rectangle", line)
cv2.waitKey(0)
cv2.destroyAllWindows()

Output:

image_rectange.jpg

Looks like we detected a book handing by Ron Weasley ✨️

The rectangle function is very similar to the line function. It expects the image and four more arguments: the top-left corner of the rectangle in (x1, y1), the bottom-right of it in (x2, y2), the line’s color (in BGR for our image), and its thickness in pixels.

Image smoothing

OpenCV offers tools to smooth an image and help reduce the noise in it. The process is fully automated, and all the complexity of how it works is encapsulated for us on a single, simple to use the function.

Here is an example of how to smooth an image:

import cv2

img = cv2.imread('test.jpg')
line = cv2.blur(img, (5,5))
cv2.imshow("Blur", line)
cv2.waitKey(0)
cv2.destroyAllWindows()

Output: Original Image opencv-4_hu3bdf41378a17a6420a984135f80fa876_436188_700x0_resize_q75_box.jpg

Smoothed Image

opencv-5_hu49098c221b6b5d45585db7a0fa2da69e_209602_700x0_resize_q75_box.jpg

The image looks much better, but how does it work? Through the method blur on the OpenCV library, which expects the image and the kernel size as arguments.

Note: that different values of x and y will result in different outputs, so you will have to play around with those values for your images.

There are other ways to smooth an image using for example, gaussianBlur or medianBlur that works similarly.

Additional: Combining Two Images

You can also use OpenCV's inbuilt function cv2.vconcat`` andcv2.hconcat`` which like their names suggested are used to join images horizontally and vertically respectively.

Let's just code here with an example:

import cv2

img1 = cv2.imread('opencv/lena.jpg')
img2 = cv2.imread('opencv/baboon.jpg')

v_img = cv2.vconcat([img1, img2])
h_img = cv2.hconcat([img1, img2])

cv2.imshow('Horizontal', h_img)
cv2.imshow('Vertical', v_img)
cv2.waitKey(0)
cv2.destroyAllWindows()

Output: Horizontal

uiuAO.jpg

Vertical*

Qx2EG.jpg

Conclusion

OpenCV is an exciting and powerful library for dealing with images and videos. Today I have covered just a small percentage of what this library is capable of.

If you liked this content make sure to follow me and subscribe newsletter for more such content 💣️