OpenCV

Naman Mehra
2 min readJun 22, 2021

Open-CV is a python library which is used to solve computer vision problems.
In OpenCV all the images are converted to numpy arrays. This makes it easier to integrate it with other libraries like scipy and matplotlib. OpenCV is used to provide a common infrastructure for computer vision applications and accelerate use of machine perception in the commercial products.

There are lots of application which are solved using OpenCV like face recognition, automated inspection and surveillance, number of people(count), interactive art installations, anomaly detection in the manufacturing process, video/image search, object recognition, driver-less car navigation and control and many more.

We can import OpenCV library by writing the following code.

import cv2 as cv

Reading an image

image = cv.imread('image.png')

Extracting height and width of an image

h,w = image.shape[:2]

Extracting RGB values of a pixel

(B,G,R) = image[100,100] #we are extracting rgb value of the pixel 100,100
G = image[100,100,0] #the value of the G in the pixel 100,100

Extracting region of interest

ROI = image[100:500,200:700]

Resizing the Image with maintaining the aspect ratio

ratio = 900/w #calculating the width and height
WH = (900,int(h*ratio)) #creating a tuple containing width and heigth
resizing = cv.resize(image,WH) #resizing the image

Rotaing the Image

center = (w//2,h//2) #w and h are the width and height
matrix = cv.getRotationMatrix2D(center,-40,1.0)
#getRotationMatrix2D() takes 3 arguments center, angle by which #image should be rotated and scaling factor and returns 2*3 matrix
#here alpha = scale*cos(angle)
#beta = scale*sine(angle)
rotated = cv.warpAffine(image,matrix,(w,h))
#warAffine() function transforms the source image using the rotation matrix. The newly defined x and y coordinates for the rotated image will be dst(x, y) = src(M11X + M12Y + M13, M21X + M22Y + M23)

We can use many other functions in OpenCV
You can refer to the following links to know more about OpenCV:-
Link1, Link2.

--

--