OpenCV-Python Cheat Sheet: From Importing Images to Face Detection

Cropping, Resizing, Rotating, Thresholding, Blurring, Drawing & Writing on an image, Face Detection & Contouring to detect objects. All Explained.

What is OpenCV-Python?

OpenCV is an open source computer vision and machine learning library. It has 2500+ optimized algorithms—a comprehensive set of both classic and state-of-the-art computer vision and machine learning algorithms. It has many interfaces, including Python, Java, C++, and Matlab.

Here, we’re gonna tackle the Python interface.

Installation

  • For Windows, find the guide here.
  • For Linux, find the guide here.

Importing an Image & Viewing it

Warning 1: On reading images this way via openCV, it isn’t in RGB colorspace—it’s in BGR. Sometime this won’t be an issue with you, you’ll only have trouble if you want to add something colored to your image.
There are two solutions:

  1. Switch the R — 1st one(red) with the B — 3rd one(blue), so that Red is (0,0,255) instead of (255,0,0).
  2. Change the colorspace to RGB:

and go on with your code with rgb_image instead of image.

Warning 2: To close the window that’s displaying the image, press any button. If you use the close button it may cause freezes (happens to me when I’m on a Jupyter notebook).

Cropping

where image[10:500, 500:2000] is image[y:y+h, x:x+w]

Resizing

This resizing function maintains the dimension-ratio of the original image.

More image scaling functions here.

Rotating

image.shape outputs the height, width, and channels. M is the rotation matrix—this rotates the image 180 degrees around its center.
-ve angle rotates the image clockwise & +ve angle rotates the image counterclockwise.

Grayscaling and Thresholding (Black & White effect)

gray_image is the grayscale one-channeled version of the image.
This threshold function will turn all shades darker (smaller) than 127 to 0 and all brighter (greater) to 255.

Another example:

This will turn all shades smaller than 150 to 10 and all greater to 200.

More thresholding functions here.

Blurring/Smoothing

The GaussianBlur function takes 3 parameters:

  • The first parameter is the image you want to blur.
  • The second parameter must be a tuple of 2 positive odd numbers. When they increase, the blur effect increases.
  • The third parameter is The sigmaX and sigmaY. When left at 0, they’re automatically calculated from the kernel size.

More blurring functions here.

Drawing a Rectangle/Bounding Box on an Image

The rectangle function takes 5 parameters:

  • The first parameter is the image.
  • The second parameter is x1, y1 — Top Left Corner.
  • The third parameter is x2, y2 — Bottom Right Corner.
  • The fourth parameter is the rectangle color (GBR/RGB, depending on how you imported your image).
  • The fifth parameter is the rectangle line thickness.

Drawing a line

The line function takes 5 parameters

  • The first parameter is the image you want to draw a line on.
  • The second parameter is x1, y1.
  • The third parameter is x2, y2.
  • The fourth parameter is the line color (GBR/RGB depending on how you imported your image).
  • The fifth parameter is the line thickness.

Writing on an image

The putText function takes 7 parameters

  • The first parameter is the image you want to write on.
  • The second parameter is the text itself.
  • The third parameter is the x, y—the bottom left coordinate where the text starts.
  • The fourth parameter is the font type.
  • The fifth parameter is the font size.
  • The sixth parameter is the color (GBR/RGB depending on how you imported your image).
  • The seventh parameter is the thickness of the text.

Face Detection

Can’t have dog pictures here, sadly 🙁

The detectMultiScale function is a general function that detects objects. Since we’re calling it on the face cascade, that’s what it detects.

The detectMultiScale function takes 4 parameters

  • The first parameter is the grayscale image.
  • The second parameter is the scaleFactor. Since some faces may be closer to the camera, they would appear bigger than the faces in the back. The scale factor compensates for this.
  • The detection algorithm uses a moving window to detect objects. minNeighbors defines how many objects are detected near the current one before it declares the face found.
  • minSize, meanwhile, gives the size of each window.

Contours—A method for Object Detection

Using color-based image segmentation, you can detect objects.
cv2.findContours & cv2.drawContours are two functions that help you with that.

Recently, I’ve written a very detailed articled called Object detection via color-based image segmentation using Python. Everything you need to know about contours is there.

Finally, Saving the image

To Conclude

OpenCV is a library full of great easy-to-use algorithms that can be used in 3D modeling, advanced image & video editing, tracking an identifying objects in videos, classifying people who are doing a certain action in videos, finding similar images from a dataset of images, and much more.

The bottom line is that learning OpenCV is crucial for people who want to take part in machine learning projects that are image-related.

Fritz

Our team has been at the forefront of Artificial Intelligence and Machine Learning research for more than 15 years and we're using our collective intelligence to help others learn, understand and grow using these new technologies in ethical and sustainable ways.

Comments 0 Responses

Leave a Reply

Your email address will not be published. Required fields are marked *

wix banner square