OpenCv For Beginners (Part-I)

Murli Jadhav
4 min readFeb 25, 2020

--

Here we are covering the basics to advance of opencv for beginners. In last of the tutorial series we also creating a basic sketch mini project.

Here we are covering Reading and writing image,GrayScaling ColorSpaces, Drawing Images, Sharpening, Thresholding, Binarisation of Images

Image Source= Here

What is OpenCV and Why we need it ?

OpenCv is founded in Intel labs in 1999 by Gary Bradsky. The First launched in 2000. The OpenCv developed in c++. Now a days we are using the wrapper of python on the top of c++ implementation. OpenCv supports the wide variety of programming languages like java,python. The more information of Opencv found here

Installing OpenCv using Python :-

we install openCv same as we are installing any libraries in python. The command is

!sudo pip install opencv-python#For Importing use
import cv2
#For Displaying the version of OpenCv
print (cv2.__version__)

Reading and Writing the Images :-

Here we are reading the images from the local drive. For that use following commands

import cv2#Loading the images from the given path
path = cv2.imread('./images/input.jpg')
#syntax=imshow(Title_of_image , path)
cv2.imshow('Hello World', input)
#Closing the open window
#here when you click any key then it closes the opened window
cv2.waitKey()
#Following command closes all the opened windows
cv2.destroyAllWindows()

Reading the dimensions and depth of images:-

print(input_image.shape)
output = (512, 256, 3)
#from above output we can see that 512 pixels is a width and 256 pixels is height and 3 is depth(RGB)

GrayScaling of the Image :-

here we are using the cv2.cvtColor() for converting the colour images to grayscale image. For more information about cvtColor() click here

import cv2# Load input image
image = cv2.imread(‘./images/input.jpg’)
cv2.imshow(‘Original’, image)
cv2.waitKey()
# Converting to gray scale
converted_im = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
cv2.imshow(‘Grayscale’, converted_image)
cv2.waitKey()
cv2.destroyAllWindows()

Introduction to color spaces

Mostly color spaces are used for the extracting the RGB values from an image pixels. If you want to perform the color operation or want values of color you will get it using color spaces

input_image = cv2.imread(‘input_1.jpg’)
#Showing the image
cv2.imshow(‘Taj Mahal Image’, input_image)
cv2.waitKey()
# BGR Values for the first 0,0 pixel
B, G, R = input_image[0, 0]
print(B, G, R)
print(input_image.shape)
cv2.destroyAllWindows()

Drawing on empty image :-

If you want to do some operations on image first draw the empty blank image and draw the shapes on it.

# Creating an empty image
image = np.zeros((512,512,3), np.uint8)
#Drawing line on empty image
cv2.line(image, (0,0), (511,511), (255,127,0), 5)
cv2.imshow(“Blue Line”, image)
cv2.waitKey(0)
cv2.destroyAllWindows()

For Drawing the circle :-

image = np.zeros((512,512,3), np.uint8)#Drawing Circle on Empty Image
cv2.circle(image, (350, 350), 100, (15,75,50), -1)
cv2.imshow("Circle", image)
cv2.waitKey(0)
cv2.destroyAllWindows()

Sharpening the image :-

If you want to sharpening the image then first you have to create a convolutional matrix. The convolutional matrix is a matrix which contains the values for the performing some operations like sharpening, blurring. If we do element wise multiplication of conv. values and image pixels then we get desired results

import cv2
import numpy as np
image = cv2.imread(‘input_1.jpg’)
cv2.imshow(‘Original’, image)
# Create our shapening kernel, we don’t normalize since the
# the values in the matrix sum to 1
kernel_sharpening = np.array([[-1,-1,-1],
[-1,9,-1],
[-1,-1,-1]])
# applying different kernels to the input image
sharpened = cv2.filter2D(image, -1, kernel_sharpening)
cv2.imshow(‘Image Sharpening’, sharpened)cv2.waitKey(0)
cv2.destroyAllWindows()

Edge Detection:-

import cv2
import numpy as np
#reading the image
image = cv2.imread(‘input_1.jpg’,0)
height, width = image.shape# Extract Sobel Edges
sobel_x = cv2.Sobel(image, cv2.CV_64F, 0, 1, ksize=5)
sobel_y = cv2.Sobel(image, cv2.CV_64F, 1, 0, ksize=5)
cv2.imshow(‘Original’, image)
cv2.waitKey(0)
cv2.imshow(‘Sobel X’, sobel_x)
cv2.waitKey(0)
cv2.imshow(‘Sobel Y’, sobel_y)
cv2.waitKey(0)
#Printing the sobel edges for both x and y axis
sobel_OR = cv2.bitwise_or(sobel_x, sobel_y)
cv2.imshow(‘sobel_OR’, sobel_OR)
cv2.waitKey(0)
laplacian = cv2.Laplacian(image, cv2.CV_64F)
cv2.imshow(‘Laplacian’, laplacian)
cv2.waitKey(0)
#Detecting the canny edges
canny = cv2.Canny(image,50, 150)
cv2.imshow(‘Canny’, canny)
cv2.waitKey(0)
canny = cv2.Canny(image,50, 150)
cv2.imshow(‘Canny’, canny)
cv2.waitKey(0)
cv2.destroyAllWindows()
(A)Taj Mahal Gray Scale Image (B) Taj Mahal After Detecting the canny edges

Mini-Project (Live Sketch):-

Please refer my github file link for the all the code and the project

Link :- Click HERE

Also Check my Part-2 of this tutorial bellow

— — — — — — -Part-2 Soon — — — —

--

--