OpenCV

OpenCV, also known as the Open Source Computer Vision Library, is a freely available software library for computer vision and machine learning. Its purpose is to offer a unified framework for computer vision applications and facilitate the integration of machine perception into commercial products.

Use Cases

There are many changes we can make to images using OpenCV. Some examples are sharpening an image, thresholding, edge detection, transformation and image classification. To give an example of what did in class, we will talk about Perspective Transform.

Perspective Transform

When humans see things that are close by, they look bigger than objects that are farther away. This is called perspective. Transforming is change the state of an object to another state. Therefore, perspective transform is the act of converting a 3d world into a 2d image. The example we did in class is changing the perspective of a piece of paper.

The code is as follows:

image = cv2.imread('opencv/scan.jpg', 0)
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)

plt.figure(figsize=(20, 20))

plt.subplot(2, 2, 1)
plt.title("Original")
plt.imshow(image)

# First, we get the coordinates of the 4 corners in the original image
points_A = np.float32([[320, 15], [700,215], [85,610], [530,780]])

# We get the coordinates of the desired output
points_B = np.float32([[0,0], [420, 0], [0,594], [420,594]])

M = cv2.getPerspectiveTransform(points_A, points_B)

new_image = cv2.warpPerspective(image, M, (420,594))

plt.subplot(2, 2, 2)
plt.title("Perspective Transform")
plt.imshow(new_image)

plt.show()

The code above transforms the piece of paper in the follow way:

Untitled

OpenCV allows us to perform these changes to an image. It can be very useful when working with image recognition for machine learning problems.