Skip to main content

Posts

Showing posts from October, 2020

Beginner's tutorial on OpenCV using Python

OpenCV is an open-source cross-platform computer vision library having interfaces in different languages like C++, Java, Python. In this post, we are going to look at some of the basic functionality offered by OpenCV. Let's start with understanding some basics on images.  In simpler terms, an image can be represented in a 2-D matrix of pixels. As an example, if we have to represent number 1, box/pixel which is displaying black text can be represented by 0 and others by 1. This is known as Binary Image or 2-level image . Gray Scale Image : An image having 256 levels i.e. an 8 bit gray scale image means 2^8 levels from black (0) to white (255). In grayscale images, 2-dimensional matrix will have values between 0-255. Colored Image : A colored image will have three gray scale images representing intensity of Red, Green and Blue channels. Installing OpenCV : We can install OpenCV using  pip install opencv-python Reading Images : import cv2 import numpy as np # Reading Images

Python : Warp Perspective using OpenCV

 In perspective transformation, the perspective of an image is tranformed to get a better view or insights about the required informatin. We need points of the image from which we want to gather information and also the points inside which we want to display our image. In the example below, we will extract the visitor card from the image on the left and display it as shown in the right image. # Warp Perspective import opencv2 img = cv2.imread("opencv_resources/visitor_card.png") # end-points of the image that we want to extract # use tool like ms-paint to find the end points by placing your mouse cursor # upper-left pt (point1), upper-right pt (point2), lower-left pt (point3), lower-right pt (point4) pts1 = np.float32([[100,168], [555,60], [168,450], [667,282]]) # points to be mapped with the size of the extracted image i.e. map the points with 2D image width = 400 height = 200 pts2 = np.float32([[0,0], [width,0], [0,height], [width,height]]) # Transform perspective