Working with Images – OpenCV

Working with Images – OpenCV

3 min read

As AI becomes a important part of our day-to-day life. Computer Vision also becoming popular day by day and images perform an essential role in it. Images have many properties so, OpenCV use these and make images perfect for extracting data or other purposes.

Let’s first understand what really images are and how they stored in computer.

What are Images?

Images are 2-D representation of the visible light spectrum and combination of color, brightness, saturation. It can also be represented in 3-Dimensional. Images with 2 Dimensions can only represented in Gray Scale that is in black and white color but images with 3 Dimensions represented in RGB scale.

How images stored?

Computer stored images in variety of formats which is the mixture of color, saturation and brightness. The common format, computer vision stores images is RGB format(Red, Green, Blue) where each pixel is a combination of brightness of Red, Green, Blue ranges from 0 to 255.

Reading, Storing, Showing, and Saving Images in OpenCV.

The basic things we should know is that how can we read and show image on the screen in OpenCV. Here are the code explanation –

import cv2
img = cv2.imread(image-path) //reading images
cv2.imshow(img) //showing images
cv2.waitKey()
cv2.destroyAllWindows()

We can also save image after making some change in it e.g. – We can change colors of image.

import cv2
img = cv2.imread(image-path) //reading images
grayscaleimg = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
cv2.imshow("gray",grayscaleimg) 

Create an image and make some figures.

Through OpenCV, you can make a plain image and do some creativity on this. Lets do,

##image with black background
new1 = np.zeros((512,512,3),np.uint8)
cv2.imshow("Black image ",new1)
##draw line 
cv2.line(new1,(0,0),(156,200),(255,127,0),8) ##parameters :- starting pt, ending pt, color, thickness of line
##draw rectangle
cv2.rectangle(new1,(50,50),(100,100),(255,172,1),7)
##draw circles
cv2.circle(new1,(200,200),150,(15,25,75),-1) ##parameters :- image,centre,radius,color,fill
##put text
cv2.putText(new1,"Hey, you are learning opencv",(75,290),cv2.FONT_HERSHEY_COMPLEX,3,(100,150,0),2)
            ##parameters :- image,text,margin,Font,font size,color,thickness
cv2.imshow(new1) //showing images
cv2.waitKey()
cv2.destroyAllWindows()

Transformation of Images

Transformation is used to remove distortion. It is of two types Affline and Non-Affline. In Affline, we do Translation, Scaling, Rotation. Lets implement-

Translation

import cv2
import numpy as np

pic = cv2.imread(image-path)

height,width = pic.shape[:2]
tx,ty = height/4,width/4

T = np.float32([[1,0,tx],[0,1,ty]])

translate_pic = cv2.warpAffine(pic,T,(width,height))
cv2.imshow("Translation",translate_pic)
cv2.waitKey(0)
cv2.destroyAllWindows()

Rotation

import cv2
import numpy as np

pic = cv2.imread(image-path)

height,width = pic.shape[:2]
center_x,center_y = height/2,width/2

R = cv2.getRotationMatrix2D((center_x,center_y),45,1)
rotated_pic = cv2.warpAffine(pic,R,(width,height))

cv2.imshow("Rotation",rotated_pic)
cv2.waitKey(0)
cv2.destroyAllWindows()

Scaling

import cv2
import numpy as np

pic = cv2.imread(image-path)

scale_image = cv2.resize(pic,None,fx = 0.75,fy = 0.75)
cv2.imshow("Scaling without interpolation",scale_image)
cv2.waitKey(0)

scale_image_cubic = cv2.resize(pic,None,fx = 2,fy = 2,interpolation = cv2.INTER_CUBIC)
cv2.imshow("Scaling with Cubic interpolation",scale_image_cubic)
cv2.waitKey(0)

scale_image_skewed = cv2.resize(pic,(900,400),scale_image,interpolation = cv2.INTER_CUBIC)
cv2.imshow("Scaling-skewed",scale_image_skewed)
cv2.waitKey(0)

cv2.destroyAllWindows()

Remove blurredness of image

If an image is blurred so we can remove it by making its color sharp. It will make an image visible clearly for extracting information by few lines of code.

import cv2
image = cv2.imread(image-path)
sharpen_kernel = np.array([[-1,-1,-1], [-1,9,-1], [-1,-1,-1]])
image = cv2.filter2D(image, -1, sharpen_kernel)
cv2.imshow(image)
cv2.waitKey()
cv2.destroyAllWindows()

For source code and other functionshttps://github.com/ishvik/Learning-Opencv-Python

Choose your Reaction!
Leave a Comment