Open_CV BASICS

 


OpenCV is a library of programming functions mainly aimed at real-time computer vision.

1.   HOW TO READ AN IMAGE:

OpenCv is a image processing and computer vision library that was made by INTEL.

We do not have to treat a image like an image but we will treat it as a MATRIX.

Always save the image in that folder where you are creating your python project otherwise you have to give the whole location of that image.

import cv2
img = cv2.imread(
'img1.jpg')
cv2.imshow(
'Output Image',img)
cv2.waitKey(
0) cv2.destroyAllWindows()

#cv2 is class inside cv2 lib

#imshow()function is used to show image
#waitkey()function used to hold the img untill a key is pressed
#destroyAllWindows function is used as soon as we hit any key it will destroy all windows

 

2.   HOW TO WRITE AN IMAGE:

import cv2
img = cv2.imread(
'img2.jpg')
cv2.imshow(
'Original image',img)
cv2.imwrite(
'Output.jpg',img)
cv2.imwrite(
'Output.png',img)
cv2.waitKey(
0) cv2.destroyAllWindows()
#imwrite function will make a clone of 'img' image and it will create a new file of it
#Write the name always with the extension

3.   HOW TO GET IMAGE INFO:

import cv2
img=cv2.imread(
'img2.jpg')
cv2.imshow(
'output',img)
print(img.shape)
print('Height pixel value:',img.shape[0])
print('Width pixel value:',img.shape[1])
print('Layer in image:',img.shape[2])
cv2.waitKey(
0) cv2.destroyAllWindows()
 
OUTPUT:
(545, 634, 3)
Height pixel value: 545
Width pixel value: 634
Layer in image: 3

#img.shape is used to find the pixel value of the image,it gives a tuple type information,tuple is a data format were data is kept but we cannot change it,(height,width,layer)



HOW TO CONVERT RGB TO GRAY SACLE IMAGE:

import cv2

img=cv2.imread(
'img3.jpg')

cv2.imshow(
'Original',img)

cv2.waitKey(
0)

gray_img=cv2.cvtColor(img
,cv2.COLOR_BGR2GRAY)
cv2.imshow(
'Gray scale image',gray_img)

cv2.waitKey(
0)
cv2.destroyWindow()

#cvtColor() func. is used to convert convert RGB img to grey scale img ,cvtColor(image,color)


One more way to convert RGB img to Gray scale img :

import cv2

img=cv2.imread(
'img3.jpg',0)

cv2.imshow(
'Original',img)

cv2.waitKey(
0)
cv2.destroyWindow()

#0 is used to convert RGB to Gray scale img


2.   HOW TO CONVERT RGB TO BINARY IMAGE:

1 is for white pixel  and 0 is for black pixel.

First convert the image into Gray scale then we will apply Thresh Holding technique.

Thresholding is a technique in OpenCV, which is the assignment of pixel values in relation to the threshold value provided. In thresholding, each pixel value is compared with the threshold value. If the pixel value is smaller than the threshold, it is set to 0, otherwise, it is set to a maximum value (generally 255).

import cv2

img=cv2.imread(
'img3.jpg',0)

cv2.imshow(
'Gray img',img)
cv2.waitKey(
0)

ret
, bw=cv2.threshold(img,127,255,cv2.THRESH_BINARY)
cv2.imshow(
'Binary img', bw)
cv2.waitKey(
0)
cv2.destroyAllWindows()

#threshold func. will return a value which is in the form of tuple which will go under ret variable,that’s why ret is imp to store the value

#all the pixels values which are under 127 all pixels are converted to black and above 127 all are converted to white


3.   HOW TO CONVERT RGB TO HSV IMAGE:

Color space and color filtering

HSV (Hue Saturation Value) it is a color plane ,it is similar to RGB (Red Green Blue).

HUE-It covers all the color values (H=0deg to 180deg) (H=0 to 180)

SATURATION-It cover the shades of particular color (SHSV=1,0deg to360deg) (S=0 to 255)

VALUE-It gives us the intensity of the color (V=1/2,) (V=0 to 255)

import cv2

img=cv2.imread(
'img3.jpg')

cv2.imshow(
'Original img',img)
cv2.waitKey(
0)

hsv=cv2.cvtColor(img
,cv2.COLOR_BGR2HSV)
cv2.imshow(
'HSV img',hsv)

cv2.imshow(
'Hue channel:',hsv[:,:,0]) 
cv2.imshow(
'Saturation channel:',hsv[:,:,1])
cv2.imshow(
'Value channel:',hsv[:,:,2])
cv2.waitKey(
0)
cv2.destroyAllWindows()

#[:,:,0] all the rows and all the columns are selected for hue

             

4.   HOW TO CONVERT RGB TO HSV IMAGE:

The actual color set of the image are in the form of BGR.

import cv2
import numpy as np

img = cv2.imread(
'img3.jpg')

cv2.imshow(
'Output Image',img)

B
,G,R=cv2.split(img)

zeros=np.zeros(img.shape[:
2], dtype="uint8")

cv2.imshow(
'Red',cv2.merge([zeros,zeros,R]))
cv2.waitKey(
0)
cv2.imshow(
'Blue',cv2.merge([zeros,G,zeros]))
cv2.waitKey(
0)
cv2.imshow(
'Green',cv2.merge([B,zeros,zeros]))
cv2.waitKey(
0)
cv2.destroyAllWindows()

#split() func, is used to split the color

#np.zeros()is a numpy func. will create a zeros matrix were all value of matrix will be zero and zeros variable will hold it so that we can do the masking of img

#dtype=”unit8” it is a unsigned datatype integer 8
#img.shape[:2] it will store height and width and wil run till 2-1=1 i.e.=0 to 1

#merge() func. is used to extract the color

 

5.   IMAGE TRANSLATION OR IMAGE DISPLACEMENT:

warpAffine transformation: In this the width and the height of the img is linear to each other or say parallel to eachother and are in precise form.

warpnonAffine transformation: In this the width and the height of the img is non-linear to each other or say not parallel to eachother and are in tilt form. 

import cv2
import numpy as np

img = cv2.imread(
'img3.jpg')

height
,width = img.shape[:2]
print('Height:', height)
print('Width:', width)

quater_height
,quater_width=height/8, width/8
print('quater_height:', quater_height)
print('quater_width:', quater_width)

T=np.float32([[
1, 0, quater_width],
              
[0, 1, quater_height]])
print(T)

img_translation = cv2.warpAffine(img
, T, (width, height))
cv2.imshow(
'Original img', img)
cv2.imshow(
'Translation img', img_translation)

cv2.waitKey(
0)
cv2.destroyAllWindows()

OUTPUT:

Height: 2080

Width: 960

quater_height: 260.0

quater_width: 120.0

[[  1.   0. 120.]

 [  0.   1. 260.]]

#height and width store height and width of image

#quater_height and width are translation factor

# T(Translation Matrix)= |1  0  Tx|
                         |0  1  Ty|

#we store the matrix in the form of list

#warpAffine(original img, matrix, original height and width)

 

6.   IMAGE ROTATION:

import cv2
import numpy as np

img = cv2.imread(
'lena.jpg')

height
,width = img.shape[:2]

rotation_matrix= cv2.getRotationMatrix2D((width/
2,height/2),75,.7)

rotated_img=cv2.warpAffine(img
,rotation_matrix,(width,height))

cv2.imshow(
"Original img",img)
cv2.imshow(
"Rotated img",rotated_img)
cv2.waitKey(
0)
cv2.destroyAllWindows()

#getRotationMatrix2D(center,degree(0-360),scaling factor or say determine the size of the image) and it is stored in the form of tuple

 

7.   IMAGE TRANSPOSE:

Transpose is used to rotate the image by 90 degree without any information loss.

All the column pixel get converted to width pixel and vice versa.

import cv2
import numpy as np

img = cv2.imread(
'lena.jpg')

rotated_img= cv2.transpose(img)

cv2.imshow(
'Orignal',img)
cv2.imshow(
'Transposed',rotated_img)
cv2.waitKey(
0)
cv2.destroyAllWindows()

#transpose() func.is a inbuilt func.


IMAGE RESIZING:

Interpolation:  It is a technique used to resize a image.

Cubic-interpolation- It is used to increase the size

Linear-interpolation-It is used to reduce the size

import cv2
import numpy as np

img = cv2.imread(
'lena.jpg')

cv2.imshow(
'Original', img)
cv2.waitKey(
0)

img_scaled=cv2.resize(img
, None, fx=0.75, fy=0.75)
cv2.imshow(
'Linear Interplotatio',img_scaled)
cv2.waitKey(
0)

img_scaled1=cv2.resize(img
, None, fx=2, fy=2,interpolation=cv2.INTER_CUBIC)
cv2.imshow(
'Cubic Interplotatio',img_scaled1)
cv2.waitKey(
0)

img_scaled3=cv2.resize(img
,(1366,768),interpolation=cv2.INTER_AREA)
cv2.imshow(
'Skewed Image',img_scaled3)
cv2.waitKey(
0)
cv2.destroyAllWindows()
 

#the image is reduced by 25%

#resize() is func.that is used to change the size of the image via interpolation technique

#cv2.INTER_CUBIC is used to cubic interpolation

#In the place of none we can set the dimension of the image (width,height)

 

2.   IMAGE PYRAMID:

It is a technique of resizing an image into half or double it’s size. 

import cv2
import numpy as np

img = cv2.imread(
'lena.jpg')
cv2.imshow(
"original",img)

small=cv2.pyrDown(img)
large=cv2.pyrUp(img)

cv2.imshow(
'Small',small)
cv2.imshow(
'Large',large)
cv2.waitKey(
0)
cv2.destroyAllWindows()

#pyrDown() and pyrUp() func. is used to resize the image


3.   IMAGE CROPPING:

import cv2
import numpy as np

img = cv2.imread(
'lena.jpg')

height
,width=img.shape[:2]

s_row
,s_col =int(height*.25),int(width*.25)
e_row
,e_col=int(height*.75),int(width*.75)

cropped= img[s_row:e_row
,s_col:e_col]

cv2.imshow(
'Original',img)
cv2.imshow(
'Cropped',cropped)
cv2.waitKey(
0)
cv2.destroyAllWindows()

#starting pixel co-ordinate top left corner,since .25 is a float value that’s why we type casted it into integer value

#bottom right corner

#we have cropped the image from 25%-75%

#img list is created to croop the image and the value is stored in cropped


4.   IMAGE AIRTHMETIC:

In image arithmetic we perform add or sub etc. to the pixels of the image.

If we want to perform arithmetic operation on an img then we need an another matrix through which we can perform arithmetic operation.

import cv2
import numpy as np

img = cv2.imread(
'lena.jpg')
cv2.imshow(
'Original',img)
cv2.waitKey(
0)

M=np.ones(img.shape
,dtype="uint8") * 150
M1=np.zeros(img.shape,dtype="uint8") + 150

added=cv2.add(img,M)
cv2.imshow(
'Added',added)
cv2.waitKey(
0)

sub=cv2.subtract(img
,M)
cv2.imshow(
'Subtracted',sub)
cv2.waitKey(
0)

mul=cv2.multiply(img
,M1)
cv2.imshow(
'Multiplied',mul)
cv2.waitKey(
0)
cv2.destroyAllWindows()

#np.ones() is used to create a one's matrix ,img.shape is used so that the new matrix that has beeen created should the size similar to the original img

#add() func. is used to add both img

#added-bright img

 Sub-dim img

 Mul-extra bright


5.   IMAGE BLURRING:

Blurring is done to make the image smooth.

We cannot take even kernel matrix.Only (3x3,5x5,..) matrix are allowed.

import cv2
import numpy as np

img = cv2.imread(
'lena.jpg')
cv2.imshow(
"Original",img)
cv2.waitKey(
0)

kernel_3x3 = np.ones((
3,3),np.float32)/9

blurred=cv2.filter2D(img,-1,kernel_3x3)

cv2.imshow('Blurred',blurred)
cv2.waitKey(
0)

kernel_7x7 = np.ones((
7,7),np.float32)/9


blurred=cv2.filter2D(img,-1,kernel_7x7)
cv2.imshow(
'Blurred_7x7',blurred)
cv2.waitKey(
0)
cv2.destroyAllWindows()

#we made a kernel 3x3 matrix all the values will be 1/9,it is basically a filter matrix,we will use this matrix to blur the original img

#filter2D(original img,it’s depth,kernel_matrix) func. Is used to blur the original image using kernel matrix


IMAGE BITWISE OPERATION:

AND NOT OR XOR operation is performed between the pixels.

NOT operation is done over a single image

import cv2
import numpy as np

box1 = np.zeros((
300,300),np.uint8)
cv2.rectangle(box1
,(50,50),(250,250),255,-1)
cv2.imshow(
'Box1',box1)
cv2.waitKey(
0)

box2 = np.zeros((
300,300),np.uint8)
cv2.ellipse(box2
,(150,150),(150,150),30,0,180,255,-1)
cv2.imshow(
'Box2',box2)
cv2.waitKey(
0)

And=cv2.bitwise_and(box1
,box2)
cv2.imshow(
'ADD',And)
cv2.waitKey(
0)

Or=cv2.bitwise_or(box1
,box2)
cv2.imshow(
'Or',Or)
cv2.waitKey(
0)
cv2.destroyAllWindows()

#a black(since np.zeros) box is made of dimension 300,300

#rectangle() func. is used to create a rectangle (inside which rec is to be drawn, starting point, end point, color, line width(it’s -1 cz we want to completely fill the rectangle)


2.   IMAGE SMOOTHING:

Averaging is done by convolving the image with a normalize box filter,this takes the pixel under the box and replace the central element. The box size should be odd and positive.

import cv2
import numpy as np

img = cv2.imread(
'lena.jpg')
cv2.imshow(
"original",img)

blur=cv2.blur(img
,(3,3))
cv2.imshow(
'Blur',blur)
cv2.waitKey(
0)

gaussian=cv2.GaussianBlur(img
,(7,7),0)
cv2.imshow(
'gaussian',gaussian)
cv2.waitKey(
0)

median= cv2.medianBlur(img
,5)
cv2.imshow(
'Median',median)
cv2.waitKey(
0)

bilateral=cv2.bilateralFilter(img
,5,75,75)
cv2.imshow(
'bilateral',bilateral)
cv2.waitKey(
0)
cv2.destroyWindow()

#for smoothing a img we use averaging filter for this 'blur()' func. is used
#blur(image,kernel matrix its is a tuple)

#Gaussian blur is more powerful than simple blur

#Median blur takes all the pixels under kernel area and central element is replaced with this median value.

#Bilateral blur is most effective and most powerful (image,sigma,sigma color,sigma space)


3.   IMAGE EDGE DETECTION:

Image edge detection is used to find any object.

Sobel technique is used for edge detection, in this we use to collect different pixels of different height and width,it is the poorest technique for edge detection.

Laplacian technique little better than sobel.

Canny Edge Detection is most efficient technique for edge detection, it uses gradient value as thresholds.

import cv2
import numpy as np

img = cv2.imread(
'lena.jpg',0)

height
,width= img.shape

sobel_x = cv2.Sobel(img
,cv2.CV_64F,1,0,ksize=5)
sobel_y = cv2.Sobel(img
,cv2.CV_64F,0,1,ksize=5)

cv2.imshow(
'orignal',img)

cv2.imshow(
'sobel_x',sobel_x)
cv2.waitKey(
0)

cv2.imshow(
'sobel_y',sobel_y)
cv2.waitKey(
0)

sobel_or=cv2.bitwise_or(sobel_x
,sobel_y)
cv2.imshow(
'Sobel_or',sobel_or)
cv2.waitKey(
0)

laplacian=cv2.Laplacian(img,cv2.CV_64F)
cv2.imshow(
'laplacian',laplacian)
cv2.waitKey(
0)

canny=cv2.Canny(img
,20,170)
cv2.imshow(
'canny',canny)
cv2.waitKey(
0)

cv2.destroyAllWindows()

#sobel() func. is used to extract slop edges (orig img,data type of kernel,width,height(enabled or disabled),kernel size)

#laplacian() func.(orig img, data type)

#canny(orig img,threshold value,threshold value) it ask for 2 threshold values


4.   WEBCAM:

Since we want a video so need an infinite loop to hold the video.

import cv2

cap = cv2.VideoCapture(
0)
while True :
    ret
, frame = cap.read()
   
print(frame)
    cv2.imshow(
'My live image',frame)

   
if cv2.waitKey(1)==13:
       
break

cap.release()
cv2.destroyAllWindows()

#videocapture() func is used to access the webcam,cap variable hold the videocapture func. that we have called via cv2

#with the help of cap variable we called read() func. via read we will start reading the camera screen and access it via frame variable

#cap.read() also return a value that is held by ret variable

#ASCII code of ENTER is 13

#release() func.is used to close the camera port 


5.   CAPTURE IMAGE:

Since we have to capture the image there is no need of a infinite loop.

xticks() Function. The annotate() function in pyplot module of matplotlib library is used to get and set the current tick locations and labels of the x-axis.

import cv2
import matplotlib.pyplot as plt

cap= cv2.VideoCapture(
0)

if cap.isOpened():
    ret
,frame =cap.read()
   
print(ret)
   
print(frame)

else:
    ret =
False

img1 = cv2.cvtColor(frame,cv2.COLOR_BGR2RGB)
plt.imshow(img1)
plt.title(
"Camera img-1")
plt.xticks([])
plt.yticks([])
plt.show()

cap.release()

#cap.isOpened() func. tells us that the value that we are getting cap variable by videocapture() func. is opened

#COLOR_BGR2RGB is used to convert BGR img to RGB
#Image to be kept in plane for that matplot lib library help us to do it


6.   EDGE DETECTION USING WEBCAM:

To edge detect the webcam output, first we start the webcam then convert the video to gray scale then apply blurring on it then by applying edge detection techquie after that we convert it into binary format(black/white).

import cv2

cap=cv2.VideoCapture(
0)

def sketch(image):
    img_gray=cv2.cvtColor(image
,cv2.COLOR_BGR2GRAY)
    img_gray_blur= cv2.GaussianBlur(img_gray
,(5,5),0)
    canny_edge=cv2.Canny(img_gray_blur
,10,70)
    ret
,mask=cv2.threshold(canny_edge,70,255,cv2.THRESH_BINARY)

   
return mask

while True:
    ret
,frame=cap.read()
    cv2.imshow(
'Our like Sketch',sketch(frame))
   
if cv2.waitKey(1)==13:
       
break

cap.release()
cv2.destroyAllWindows()


7.   COLOR FILTERING USING WEBCAM:

HSV is used in range of color and BGR is used for fixed color.

We are filtering out blue color

import cv2
import numpy as np

cap=cv2.VideoCapture(
0)

while True:
    ret
,frame=cap.read()
    hsv=cv2.cvtColor(frame
,cv2.COLOR_BGR2HSV)

    l_range=np.array([
110,50,50])
    u_range=np.array([
130,255,255])
    mask=cv2.inRange(hsv
,l_range,u_range)
    cv2.imshow(
'Filter',mask)
    cv2.imshow(
'Original',frame)

   
if cv2.waitKey(1)==13:
       
break

cap.release()
cv2.destroyAllWindows()

#inRange() func. is used to filter only the color which are in the range and will hold all the other color value

#np.array() func. Is used to create a array to set the lower and upper range of hsv value


1.   COLOR FILTERING WITH BITWISE OPERATOR:

import cv2
import numpy as np

cap=cv2.VideoCapture(
0)

while True:
    ret
,frame=cap.read()
    hsv=cv2.cvtColor(frame
,cv2.COLOR_BGR2HSV)

    l_range=np.array([
110,50,50])
    u_range=np.array([
130,255,255])
    mask=cv2.inRange(hsv
,l_range,u_range)

    result1=cv2.bitwise_and(frame
,frame,mask=mask)
    cv2.imshow(
'Show',mask)
    cv2.imshow(
'result',result1)
    cv2.imshow(
'Show1',frame)


   
if cv2.waitKey(1)==13:
       
break

cap.release()
cv2.destroyAllWindows()

#we will apply bitwise operator and it takes (actual frame,frame to be were and operation is applied,mask were and operator is applied


2.   IMAGE SHARPING:

It is just opposite of blurring.We use negative kernel to sharpen the image.

import cv2
import numpy as np
img=cv2.imread(
'lena.jpg')
cv2.imshow(
'Original image',img)
cv2.waitKey(
0)

kernel= np.array([[-
1,-1,-1,-1,-1],
                 
[-1,-1,-1,-1,-1],
                  
[-1,-1,25,-1,-1],
                 
[-1,-1,-1,-1,-1],
                 
[-1,-1,-1,-1,-1]])

kernel2= np.array([[-
1,-1,-1],
                   
[-1,9,-1],
                  
[-1,-1,-1]])
sharpenimg=cv2.filter2D(img
,-1,kernel)
cv2.imshow(
'Sharpened Image',sharpenimg)
cv2.waitKey(
0)

sharpenimg2=cv2.filter2D(img
,-1,kernel2)
cv2.imshow(
'Sharpened Image 2',sharpenimg2)
cv2.waitKey(
0) cv2.destroyAllWindows()

#we took a kernel matrix of 5x5,we don't normalize the matrix since the sum of the matrix is 1

#-1 in the filter2D() func. Is the D depth means the desired output img


3.   MOUSE CALLBACK:

When we click the mouse an event is generated and if somehow we are able to catch the click then we will be able to perform some operations.

import cv2
import numpy as np

window_name=
'Drawing'
img= np.zeros((512,512,3),np.uint8)

def draw_circle():
   
if event==cv2.EVENT_LBUTTONDBLCLK:
        cv2.circle(img
,(x,y),40,(0,255,0),-1)

   
if event==cv2.EVENT_MBUTTONDOWN:
        cv2.circle(img
,(x,y),30,(255,0,0),-1)

   
if event==cv2.EVENT_RBUTTONDOWN:
        cv2.circle(img
,(x,y),20,(0,0,255),-1)

cv2.setMouseCallback(window_name
,draw_circle)

while True:
    cv2.imshow(window_name
,img)
   
if cv2.waitKey(1)==13:
       
break

cv2.destroyAllWindows()

#np.zeros() func. is used to create a blank box ,((x-axis,y-axis,3 represents number of color layers),data type)

#circle() func. Consist of (place were we want to draw our circle,center,color, -1 is used for filling the circle with that color)

#setmousecallback() func. is used were we want the event to happen i.e. window_name and we called the draw_circle func() to perform certain operation

#imshow() func. will go to window_name and it will call img

 

4.   CREATE COLOR TRACKBAR:

It’s is a special GUI component through which we can vary the values within number of range.

import cv2
import numpy as np

def nothing(x):
   
pass

img= np.zeros((300,512,3),np.uint8)
cv2.namedWindow(
'track')

cv2.createTrackbar(
'Red','track',0,255,nothing)
cv2.createTrackbar(
'Green','track',0,255,nothing)
cv2.createTrackbar(
'Blue','track',0,255,nothing)

switch =
'0 : OFF \n 1 : ON'
cv2.createTrackbar(switch,'track',0,1,nothing)

while True:
    cv2.imshow(
'Image',img)
   
if cv2.waitKey(1)==13:
       
break

   
r=cv2.getTrackbarPos('Red','track')
    g=cv2.getTrackbarPos(
'Green','track')
    b=cv2.getTrackbarPos(
'Blue','track')
    s=cv2.getTrackbarPos(switch
,'track')

   
if s==0:
        img[:]=
0
   
else:
        img[:]=[b
,g,r]
       
cv2.destroyAllWindows()

#namedwindow() func. is used to give the name to the blank box we have created

#createTrackbar() func. is used to create a trackbar that consist (name of trackbar,window name were we are placing our trackbar,color value range,no change value to be passed)

#if 0 then window will be blank if 1 window will show color

#getTrackbarpos() is used to set the position of the different trackbar

#img[:] is used to select the whole img space area and b,g,r is used to fill the color in the space named img

 

 

----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

Comments

Popular posts from this blog

SQL Course (PART-1)

PYTHON BASICS OF BEGINNER's (PART-2)