A Simple Introduction to OpenCV With Real-World Business Examples
Tags: machinelearning, computerscience, python
Published: 2021-02-12 01:17:32

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](https://opencv-python-tutroals.readthedocs.io/en/latest/index.html) 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](https://dev-to-uploads.s3.amazonaws.com/i/bwo3z7f4tzqqg9hace61.jpg)

## Reading an Image With OpenCV 

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/v0pwi53j7kwi37xeuxf3.PNG)

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](https://dev-to-uploads.s3.amazonaws.com/i/4bdgd9lbj5ph19cytk71.PNG)

## 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](https://dev-to-uploads.s3.amazonaws.com/i/dfvl9pvfs7kb6s0frjta.PNG)

## Resizing an image with OpenCV

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/berm316tngt5yf4p1d0n.PNG)

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](https://dev-to-uploads.s3.amazonaws.com/i/uhrpeflr893932l8npjj.PNG)

## Rotating an image with OpenCV

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/sj8kklij7bfe1ubp8zdr.PNG)

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](https://dev-to-uploads.s3.amazonaws.com/i/s9dwj6rlcji4nzv5sgcp.png)

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](https://dev-to-uploads.s3.amazonaws.com/i/t5zx5e4lf89kwpee46dt.PNG)

## Drawing a Rectangle or Bounding Box OpenCV

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/b9st5ehde7mpa9ebdsjt.png)

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

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/lf9nkm4h8m8v44yszvqu.PNG)

# 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.](https://hackernoon.com/3-image-processing-techniques-for-building-barcode-scanning-software-x04r31w5)

## Edge Detection Projects 

[Finding the edges of objects](https://towardsdatascience.com/simple-edge-detection-model-using-python-91bf6cf00864) 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](https://www.scalr.ai/post/inventory-automation-computer-vision)

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](https://www.scalr.ai/post/data-capture-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.