A Simple Introduction to OpenCV With Real-World Business Examples

mattpayne

Matt Payne

Posted on February 12, 2021

A Simple Introduction to OpenCV With Real-World Business Examples

Let’s take a look at one of the most popular and flexible computer vision libraries on the market, and some real work examples of these tools in action.

OpenCv has been one of the most used computer vision libraries for quite some time now. It’s incredibly popular and has lots of flexible classes and functions that allow you to easily customize your solution. When you are planning any computer vision project its a great idea to start with OpenCV and see what you can use right out of the box. In this article I will gently introduce you to the basics of the library as well as show you some real-world examples using OpenCV.

Alt Text

Reading an Image With OpenCV

Alt Text

Let’s start with just reading an image by the path to where the image is, then printing out the height and width. There's a few alternate versions of this function, each of which allows you to read images differently.

Alt Text

Extracting the RGB values of a pixel with OpenCV

Now we want to extract the RGB values of a single pixel, but notice the colors are read in with BGR order in OpenCV, so the first value will always be blue not red.

Alt Text

Resizing an image with OpenCV

Alt Text

There is a problem with this approach to resizing images, the aspect ratio of the image is not maintained when giving raw dimensions to resize. We must do a little extra math to insure a proper aspect ratio.

Alt Text

Rotating an image with OpenCV

Alt Text

There's alot going on in rotating an image, so let's break it down.

getRotationMatrix2D()
The three arguments it takes in are center, angle, and the scale. The center is the center coordinates of the image. Angle is simply the angle in degrees that we would like to rotate the image counterclockwise. The scale is simply the scaling factor. It returns a 2x3 matrix that has the values derived from alpha and beta. Alpha being scale * cos(angle) and beta being scale * sine(angle)

Alt Text

warpAffine()
The function warpAffine transforms the original image using the rotation matrix.

A few tips:
Another way to get the center of the image is something that looks like this, where we use a NumPy array.

If you want to rotate the image with easier degrees such as 180 or 90, you can simply use the .rotate() function and pass easy parameters seen below.

Alt Text

Drawing a Rectangle or Bounding Box OpenCV

Alt Text

The function takes in a few parameters, which are listed under the code.

Alt Text

Real World Projects & Tools Using OpenCV

Let’s take a look at a few real world examples of OpenCV being used to give you an idea of the different applications this incredible tool can be used for.

Extract and remove horizontal or vertical lines from images

This image processing technique used to remove horizontal or vertical lines has a ton of real world use cases. Using a few cv2 functions like erode and dilate we can identify and remove any sized horizontal and vertical lines from an image. You’ll see this used in product label readers along with 1D & 2D barcode scanning software.

Edge Detection Projects

Finding the edges of objects in images can be a challenging and exciting project for someone looking to see quick results with openCV. Detecting edges is extremely useful for predicting the size of objects or the distance between you and the object you see. You can also include this library in video feeds to automatically move objects closer or farther away from a target.

Inventory Management and Automated Product Listing

In that article linked above, the product shown as an example uses object detection and barcode reading to automate listing custom products on a website as the product photos are taken and uploaded. The computer vision figures out what product is shown in the image and automatically builds the title and description, along with handling all the backend management needed to run an online retailer.

Capturing data and building datasets

Using tools like object detection libraries in OpenCV allow you to build data capturing services that extract data from normal business operations and can be converted into high ROI datasets. Businesses can learn things like customer satisfaction from facial recognition tools, what users care about on a website landing page using heatmaps, and much more.

💖 💪 🙅 🚩
mattpayne
Matt Payne

Posted on February 12, 2021

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related