You are on page 1of 43

Subject: Digital Image Processing

Branch: BSC AIML (Artificial Intelligence and Machine Learning )

INDEX

Sr. No. Date Aim Sign


1 Turn your webcam feed into a pencil drawing wizard.

2 Counting the circles and ellipses

3 Display date and time while webcam is open on current video

4 Perform Image Processing Models such as follows:


a. Changing Color Space (BGR-HSV-GRAY, Masking on
images)
b. Geometric Transformations of Images(Rotation, Affine
Transformation, Perspective Transformation)
c. Image Thresholding (SIMPLE Thresholding ,ADAPTIVE
Thresholding)
d. Smoothing Images (Blur images with various low pass
filters, Apply custom-made filters to images (2D
convolution))
e. Morphological Transformations(Erosion, Dilation,
Opening, Closing etc.)
f. Image Gradients(Sobel, Laplacian)
g. Canny Edge Detection

5 Perform Object Detection and Object Tracking Using HSV Color


Space

6 Detect a specific object using your webcam and also find the specific
pattern in an image

7 Perform mouse Events (examples such as continuous line, display


coordinates while left mouse click, right mouse click display RGB
Values)
8 Facial Recognition – Make your computer recognize you

9 Perform GUI Features in an image (draw lines, rectangles, ellipses,


circles ,arrow segment line, put text)

10 Perform Basic operation on Image


a. Access pixel values and modify them
b. Access image properties ie. Shape
c. Setting Region of Image (ROI)
d. Splitting and Merging images
e. Making Borders for Images
f. Arithmetic Operation (ADDITION ,SUBSTRACTION, AND
OR XOR XOR NOT)
g. Image Blending
PRACTICAL NO. 1
AIM:
Turn your webcam feed into a pencil drawing wizard.
CODE:
import numpy as np
import cv2
from collections import deque

# Define the upper and lower boundaries for a color to be considered "Blue"
blueLower = np.array([100, 60, 60])
blueUpper = np.array([140, 255, 255])

# Define a 5x5 kernel for erosion and dilation


kernel = np.ones((5, 5), np.uint8)

# Setup deques to store separate colors in separate arrays


bpoints = [deque(maxlen=512)]
gpoints = [deque(maxlen=512)]
rpoints = [deque(maxlen=512)]
ypoints = [deque(maxlen=512)]

bindex = 0
gindex = 0
rindex = 0
yindex = 0

colors = [(255, 0, 0), (0, 255, 0), (0, 0, 255), (0, 255, 255)]
colorIndex = 0

# Setup the Paint interface


paintWindow = np.zeros((471,636,3)) + 255
paintWindow = cv2.rectangle(paintWindow, (40,1), (140,65), (0,0,0), 2)
paintWindow = cv2.rectangle(paintWindow, (160,1), (255,65), colors[0], -1)
paintWindow = cv2.rectangle(paintWindow, (275,1), (370,65), colors[1], -1)
paintWindow = cv2.rectangle(paintWindow, (390,1), (485,65), colors[2], -1)
paintWindow = cv2.rectangle(paintWindow, (505,1), (600,65), colors[3], -1)
cv2.putText(paintWindow, "CLEAR ALL", (49, 33), cv2.FONT_HERSHEY_SIMPLEX,
0.5, (0, 0, 0), 2, cv2.LINE_AA)
cv2.putText(paintWindow, "BLUE", (185, 33), cv2.FONT_HERSHEY_SIMPLEX, 0.5,
(255, 255, 255), 2, cv2.LINE_AA)
cv2.putText(paintWindow, "GREEN", (298, 33), cv2.FONT_HERSHEY_SIMPLEX, 0.5,
(255, 255, 255), 2, cv2.LINE_AA)
cv2.putText(paintWindow, "RED", (420, 33), cv2.FONT_HERSHEY_SIMPLEX, 0.5,
(255, 255, 255), 2, cv2.LINE_AA)
cv2.putText(paintWindow, "YELLOW", (520, 33), cv2.FONT_HERSHEY_SIMPLEX,
0.5, (150,150,150), 2, cv2.LINE_AA)

cv2.namedWindow('Paint', cv2.WINDOW_AUTOSIZE)

# Load the video


camera = cv2.VideoCapture(0)

# Keep looping
while True:
# Grab the current paintWindow
(grabbed, frame) = camera.read()
frame = cv2.flip(frame, 1)
hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)

# Add the coloring options to the frame


frame = cv2.rectangle(frame, (40,1), (140,65), (122,122,122), -1)
frame = cv2.rectangle(frame, (160,1), (255,65), colors[0], -1)
frame = cv2.rectangle(frame, (275,1), (370,65), colors[1], -1)
frame = cv2.rectangle(frame, (390,1), (485,65), colors[2], -1)
frame = cv2.rectangle(frame, (505,1), (600,65), colors[3], -1)
cv2.putText(frame, "CLEAR ALL", (49, 33), cv2.FONT_HERSHEY_SIMPLEX,
0.5, (255, 255, 255), 2, cv2.LINE_AA)
cv2.putText(frame, "BLUE", (185, 33), cv2.FONT_HERSHEY_SIMPLEX, 0.5,
(255, 255, 255), 2, cv2.LINE_AA)
cv2.putText(frame, "GREEN", (298, 33), cv2.FONT_HERSHEY_SIMPLEX, 0.5,
(255, 255, 255), 2, cv2.LINE_AA)
cv2.putText(frame, "RED", (420, 33), cv2.FONT_HERSHEY_SIMPLEX, 0.5,
(255, 255, 255), 2, cv2.LINE_AA)
cv2.putText(frame, "YELLOW", (520, 33), cv2.FONT_HERSHEY_SIMPLEX, 0.5,
(150,150,150), 2, cv2.LINE_AA)

# Check to see if we have reached the end of the video


if not grabbed:
break

# Determine which pixels fall within the blue boundaries and then blur
the binary image
blueMask = cv2.inRange(hsv, blueLower, blueUpper)
blueMask = cv2.erode(blueMask, kernel, iterations=2)
blueMask = cv2.morphologyEx(blueMask, cv2.MORPH_OPEN, kernel)
blueMask = cv2.dilate(blueMask, kernel, iterations=1)

# Find contours in the image


# (_, cnts, _) = cv2.findContours(blueMask.copy(), cv2.RETR_EXTERNAL,
# cv2.CHAIN_APPROX_SIMPLE)
cnts, _ = cv2.findContours(blueMask.copy(), cv2.RETR_EXTERNAL,
cv2.CHAIN_APPROX_SIMPLE)
center = None

# Check to see if any contours were found


if len(cnts) > 0:
# Sort the contours and find the largest one -- we
# will assume this contour correspondes to the area of the bottle
cap
cnt = sorted(cnts, key = cv2.contourArea, reverse = True)[0]
# Get the radius of the enclosing circle around the found contour
((x, y), radius) = cv2.minEnclosingCircle(cnt)
# Draw the circle around the contour
cv2.circle(frame, (int(x), int(y)), int(radius), (0, 255, 255), 2)
# Get the moments to calculate the center of the contour (in this
case Circle)
M = cv2.moments(cnt)
center = (int(M['m10'] / M['m00']), int(M['m01'] / M['m00']))

if center[1] <= 65:


if 40 <= center[0] <= 140: # Clear All
bpoints = [deque(maxlen=512)]
gpoints = [deque(maxlen=512)]
rpoints = [deque(maxlen=512)]
ypoints = [deque(maxlen=512)]

bindex = 0
gindex = 0
rindex = 0
yindex = 0

paintWindow[67:,:,:] = 255
elif 160 <= center[0] <= 255:
colorIndex = 0 # Blue
elif 275 <= center[0] <= 370:
colorIndex = 1 # Green
elif 390 <= center[0] <= 485:
colorIndex = 2 # Red
elif 505 <= center[0] <= 600:
colorIndex = 3 # Yellow
else :
if colorIndex == 0:
bpoints[bindex].appendleft(center)
elif colorIndex == 1:
gpoints[gindex].appendleft(center)
elif colorIndex == 2:
rpoints[rindex].appendleft(center)
elif colorIndex == 3:
ypoints[yindex].appendleft(center)
# Append the next deque when no contours are detected (i.e., bottle cap
reversed)
else:
bpoints.append(deque(maxlen=512))
bindex += 1
gpoints.append(deque(maxlen=512))
gindex += 1
rpoints.append(deque(maxlen=512))
rindex += 1
ypoints.append(deque(maxlen=512))
yindex += 1

# Draw lines of all the colors (Blue, Green, Red and Yellow)
points = [bpoints, gpoints, rpoints, ypoints]
for i in range(len(points)):
for j in range(len(points[i])):
for k in range(1, len(points[i][j])):
if points[i][j][k - 1] is None or points[i][j][k] is None:
continue
cv2.line(frame, points[i][j][k - 1], points[i][j][k],
colors[i],
2) cv2.line(paintWindow, points[i][j][k - 1], points[i][j][k],

colors[i],
2)

# Show the frame and the paintWindow image


cv2.imshow("Tracking", frame)
cv2.imshow("Paint", paintWindow)

# If the 'q' key is pressed, stop the loop


if cv2.waitKey(1) & 0xFF == ord("q"):
break

# Cleanup the camera and close any open windows


camera.release()
cv2.destroyAllWindows()

OUTPUT:
PRACTICAL NO. 2
AIM:
Counting the circles and ellipses.
CODE:
import cv2
import numpy as np

# Load image
image = cv2.imread("blob.jpg", 0)

# Set our filtering parameters


# Initialize parameter setting using cv2.SimpleBlobDetector
params = cv2.SimpleBlobDetector_Params()

# Set Area filtering parameters


params.filterByArea = True
params.minArea = 100

# Set Circularity filtering parameters


params.filterByCircularity = True
params.minCircularity = 0.9

# Set Convexity filtering parameters


params.filterByConvexity = True
params.minConvexity = 0.2

# Set inertia filtering parameters


params.filterByInertia = True
params.minInertiaRatio = 0.01

# Create a detector with the parameters


detector = cv2.SimpleBlobDetector_create(params)

# Detect blobs
keypoints = detector.detect(image)

# Draw blobs on our image as red circles


blank = np.zeros((1, 1))
blobs = cv2.drawKeypoints(image, keypoints, blank, (0, 0, 255),
cv2.DRAW_MATCHES_FLAGS_DRAW_RICH_KEYPOINTS)

number_of_blobs = len(keypoints)
text = "Number of Circular Blobs: " + str(len(keypoints))
cv2.putText(blobs, text, (20, 550),
cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 100, 255), 2)

# Show blobs
cv2.imshow("Filtering Circular Blobs Only", blobs)
print(("Filtering Circular Blobs Only", number_of_blobs))
cv2.waitKey(0)
cv2.destroyAllWindows()
OUTPUT:
PRACTICAL NO. 3
AIM:
Display date and time while webcam is open on current video.
CODE:
import cv2
from datetime import datetime
# Open the Camera
cap = cv2.VideoCapture(0)
while True:
ret, img = cap.read()
# Put current DateTime on each frame
font = cv2.FONT_HERSHEY_SIMPLEX
cv2.putText(img,str(datetime.now()),(10,30), font,
1,(255,255,255),2,cv2.LINE_AA)
# Display the image
cv2.imshow('Mohan',img)
# wait for keypress
k = cv2.waitKey(10)
if k == ord('q'):
break
cap.release()
cv2.destroyAllWindows()

OUTPUT:
PRACTICAL NO. 4
AIM:
Perform Image Processing Models such as follows:
a) Changing Color Space (BGR-HSV-GRAY, Masking on images).
CODE Of (a):
import cv2
import numpy as np

def nothing(x):
pass

cap = cv2.VideoCapture(0);

cv2.namedWindow("Tracking")
cv2.createTrackbar("LH", "Tracking", 0, 255, nothing)
cv2.createTrackbar("LS", "Tracking", 0, 255, nothing)
cv2.createTrackbar("LV", "Tracking", 0, 255, nothing)
cv2.createTrackbar("UH", "Tracking", 255, 255, nothing)
cv2.createTrackbar("US", "Tracking", 255, 255, nothing)
cv2.createTrackbar("UV", "Tracking", 255, 255, nothing)

while True:
#frame = cv2.imread('smarties.png')
_, frame = cap.read()

hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)

l_h = cv2.getTrackbarPos("LH", "Tracking")


l_s = cv2.getTrackbarPos("LS", "Tracking")
l_v = cv2.getTrackbarPos("LV", "Tracking")

u_h = cv2.getTrackbarPos("UH", "Tracking")


u_s = cv2.getTrackbarPos("US", "Tracking")
u_v = cv2.getTrackbarPos("UV", "Tracking")

l_b = np.array([l_h, l_s, l_v])


u_b = np.array([u_h, u_s, u_v])

mask = cv2.inRange(hsv, l_b, u_b)

res = cv2.bitwise_and(frame, frame, mask=mask)

cv2.imshow("frame", frame)
cv2.imshow("mask", mask)
cv2.imshow("res", res)

key = cv2.waitKey(1)
if key == 27:
break

cap.release()
cv2.destroyAllWindows()
OUTPUT Of (a):
b) Geometric Transformations of Images(Rotation, Affine Transformation, Perspective
Transformation)
Rotation
CODE:
import cv2 as cv
import matplotlib.pyplot as plt

img = cv.imread('messi5.jpg',0)
rows,cols = img.shape
# cols-1 and rows-1 are the coordinate limits.
M = cv.getRotationMatrix2D(((cols-1)/2.0,(rows-1)/2.0),90,1)
dst = cv.warpAffine(img,M,(cols,rows))
plt.subplot(121),plt.imshow(img),plt.title('Input')
plt.subplot(122),plt.imshow(dst),plt.title('Output')
plt.show()

OUTPUT:
Affine Transformation
CODE:
import numpy as np
import cv2 as cv
import matplotlib.pyplot as plt

img = cv.imread('messi5.jpg')
rows,cols,ch = img.shape
pts1 = np.float32([[50,50],[200,50],[50,200]])
pts2 = np.float32([[10,100],[200,50],[100,250]])
M = cv.getAffineTransform(pts1,pts2)
dst = cv.warpAffine(img,M,(cols,rows))
plt.subplot(121),plt.imshow(img),plt.title('Input')
plt.subplot(122),plt.imshow(dst),plt.title('Output')
plt.show()

Output:
Perspective Transformation
CODE:
import numpy as np
import cv2 as cv
import matplotlib.pyplot as plt

img = cv.imread('sudoku.png')
rows,cols,ch = img.shape
pts1 = np.float32([[56,65],[368,52],[28,387],[389,390]])
pts2 = np.float32([[0,0],[300,0],[0,300],[300,300]])
M = cv.getPerspectiveTransform(pts1,pts2)
dst = cv.warpPerspective(img,M,(300,300))
plt.subplot(121),plt.imshow(img),plt.title('Input')
plt.subplot(122),plt.imshow(dst),plt.title('Output')
plt.show()

OUTPUT:
c) Image Thresholding (SIMPLE Thresholding ,ADAPTIVE Thresholding).
SIMPLE Thresholding
CODE:
import cv2 as cv
import numpy as np
from matplotlib import pyplot as plt
img = cv.imread('gradient.png',0)
ret,thresh1 = cv.threshold(img,127,255,cv.THRESH_BINARY)
ret,thresh2 = cv.threshold(img,127,255,cv.THRESH_BINARY_INV)
ret,thresh3 = cv.threshold(img,127,255,cv.THRESH_TRUNC)
ret,thresh4 = cv.threshold(img,127,255,cv.THRESH_TOZERO)
ret,thresh5 = cv.threshold(img,127,255,cv.THRESH_TOZERO_INV)
titles = ['Original
Image','BINARY','BINARY_INV','TRUNC','TOZERO','TOZERO_INV']
images = [img, thresh1, thresh2, thresh3, thresh4, thresh5]
for i in range(6):
plt.subplot(2,3,i+1),plt.imshow(images[i],'gray',vmin=0,vmax=255)
plt.title(titles[i])
plt.xticks([]),plt.yticks([])
plt.show()

OUTPUT:
ADAPTIVE Thresholding.
CODE:
import cv2 as cv
import numpy as np
from matplotlib import pyplot as plt
img = cv.imread('sudoku.png',0)
img = cv.medianBlur(img,5)
ret,th1 = cv.threshold(img,127,255,cv.THRESH_BINARY)
th2 = cv.adaptiveThreshold(img,255,cv.ADAPTIVE_THRESH_MEAN_C,\
cv.THRESH_BINARY,11,2)
th3 = cv.adaptiveThreshold(img,255,cv.ADAPTIVE_THRESH_GAUSSIAN_C,\
cv.THRESH_BINARY,11,2)
titles = ['Original Image', 'Global Thresholding (v = 127)',
'Adaptive Mean Thresholding', 'Adaptive Gaussian Thresholding']
images = [img, th1, th2, th3]
for i in range(4):
plt.subplot(2,2,i+1),plt.imshow(images[i],'gray')
plt.title(titles[i])
plt.xticks([]),plt.yticks([])
plt.show()

OUTPUT:
d) Smoothing Images (Blur images with various low pass filters, Apply custom-made filters
to images (2D convolution)).
Blur images with various low pass filters.
CODE:
import cv2 as cv
import numpy as np
from matplotlib import pyplot as plt

img = cv.imread('opencv-logo-white.png')
blur = cv.blur(img,(5,5))
blur = cv.GaussianBlur(img,(5,5),0)
blur = cv.medianBlur(img,55)
blur = cv.bilateralFilter(img,90,750,750)
plt.subplot(121),plt.imshow(img),plt.title('Original')
plt.xticks([]), plt.yticks([])
plt.subplot(122),plt.imshow(blur),plt.title('Blurred')
plt.xticks([]), plt.yticks([])
plt.show()

OUTPUT:
Blur:

Gaussian Blur:
Median Blur:

Bilateral Filter:
Apply custom-made filters to images (2D convolution)).
CODE:
import numpy as np
import cv2 as cv
from matplotlib import pyplot as plt

img = cv.imread('opencv-logo.png')
kernel = np.ones((5,5),np.float32)/25
dst = cv.filter2D(img,-1,kernel)
plt.subplot(121),plt.imshow(img),plt.title('Original')
plt.xticks([]), plt.yticks([])
plt.subplot(122),plt.imshow(dst),plt.title('Averaging')
plt.xticks([]), plt.yticks([])
plt.show()

OUTPUT:
e) Morphological Transformations(Erosion, Dilation, Opening, Closing etc.).
CODE:
import cv2
import numpy as np

img = cv2.imread('j.png',0)
kernel = np.ones((5,5), np.uint8)

img_erosion = cv2.erode(img, kernel, iterations=1)


img_dilation = cv2.dilate(img, kernel, iterations=1)
img_opening = cv2.morphologyEx(img, cv2.MORPH_OPEN, kernel)
img_closing = cv2.morphologyEx(img, cv2.MORPH_CLOSE, kernel)

cv2.imshow('Input', img)
cv2.imshow('Erosion', img_erosion)
cv2.imshow('Dilation', img_dilation)
cv2.imshow('Opening', img_opening)
cv2.imshow('Closing', img_closing)

cv2.imwrite('Erosion.png', img_erosion)
cv2.imwrite('Dilation.png', img_dilation)
cv2.imwrite('Opening.png', img_opening)
cv2.imwrite('Closing.png', img_closing)

cv2.waitKey(0)
cv2.destroyAllWindows()

OUTPUT:
Input:

Erosion:
Dilation:

Opening:

Closing:
f) Image Gradients(Sobel, Laplacian).
CODE:
import numpy as np
import cv2 as cv
from matplotlib import pyplot as plt

img = cv.imread('sudoku.png',0)
laplacian = cv.Laplacian(img,cv.CV_64F)
sobelx = cv.Sobel(img,cv.CV_64F,1,0,ksize=5)
sobely = cv.Sobel(img,cv.CV_64F,0,1,ksize=5)
plt.subplot(2,2,1),plt.imshow(img,cmap = 'gray')
plt.title('Original'), plt.xticks([]), plt.yticks([])
plt.subplot(2,2,2),plt.imshow(laplacian,cmap = 'gray')
plt.title('Laplacian'), plt.xticks([]), plt.yticks([])
plt.subplot(2,2,3),plt.imshow(sobelx,cmap = 'gray')
plt.title('Sobel X'), plt.xticks([]), plt.yticks([])
plt.subplot(2,2,4),plt.imshow(sobely,cmap = 'gray')
plt.title('Sobel Y'), plt.xticks([]), plt.yticks([])
plt.show()

OUTPUT:
g) Canny Edge Detection.
CODE:
import numpy as np
import cv2 as cv
from matplotlib import pyplot as plt

img = cv.imread('messi5.jpg',0)
edges = cv.Canny(img,100,200)
plt.subplot(121),plt.imshow(img,cmap = 'gray')
plt.title('Original Image'), plt.xticks([]), plt.yticks([])
plt.subplot(122),plt.imshow(edges,cmap = 'gray')
plt.title('Edge Image'), plt.xticks([]), plt.yticks([])
plt.show()

OUTPUT:
PRACTICAL NO. 5
AIM:
Perform Object Detection and Object Tracking Using HSV Color Space.
Object Detection.
CODE:
import numpy as np
import cv2

face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
eye_cascade = cv2.CascadeClassifier('haarcascade_eye.xml')

img = cv2.imread('person.png')
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

faces = face_cascade.detectMultiScale(gray, 1.3, 5)


for (x,y,w,h) in faces:
img = cv2.rectangle(img,(x,y),(x+w,y+h),(255,0,0),2)
roi_gray = gray[y:y+h, x:x+w]
roi_color = img[y:y+h, x:x+w]
eyes = eye_cascade.detectMultiScale(roi_gray)
for (ex,ey,ew,eh) in eyes:
cv2.rectangle(roi_color,(ex,ey),(ex+ew,ey+eh),(0,255,0),2)

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

OUTPUT:
Object Tracking Using HSV Color Space.
CODE:
import cv2
import numpy as np

def nothing(x):
pass

cv2.namedWindow("Tracking")
cv2.createTrackbar("LH", "Tracking", 0, 255, nothing)
cv2.createTrackbar("LS", "Tracking", 0, 255, nothing)
cv2.createTrackbar("LV", "Tracking", 0, 255, nothing)
cv2.createTrackbar("UH", "Tracking", 255, 255, nothing)
cv2.createTrackbar("US", "Tracking", 255, 255, nothing)
cv2.createTrackbar("UV", "Tracking", 255, 255, nothing)

while True:
frame = cv2.imread('smarties.png')

hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)

l_h = cv2.getTrackbarPos("LH", "Tracking")


l_s = cv2.getTrackbarPos("LS", "Tracking")
l_v = cv2.getTrackbarPos("LV", "Tracking")

u_h = cv2.getTrackbarPos("UH", "Tracking")


u_s = cv2.getTrackbarPos("US", "Tracking")
u_v = cv2.getTrackbarPos("UV", "Tracking")

l_b = np.array([l_h, l_s, l_v])


u_b = np.array([u_h, u_s, u_v])

mask = cv2.inRange(hsv, l_b, u_b)

res = cv2.bitwise_and(frame, frame, mask=mask)

cv2.imshow("frame", frame)
cv2.imshow("mask", mask)
cv2.imshow("res", res)

key = cv2.waitKey(1)
if key == 27:
break

cv2.destroyAllWindows()

OUTPUT:
PRACTICAL NO. 6
AIM:
Detect a specific object using your webcam and also find the specific pattern in an image.
CODE:
import cv2

thres = 0.45 # Threshold to detect object

cap = cv2.VideoCapture(0)
cap.set(3, 1280)
cap.set(4, 720)
cap.set(10, 70)

classNames = []
classFile = 'coco.names'
with open(classFile,'rt') as f:
classNames = f.read().rstrip('\n').split('\n')

configPath = 'ssd_mobilenet_v3_large_coco_2020_01_14.pbtxt'
weightsPath = 'frozen_inference_graph.pb'

net = cv2.dnn_DetectionModel(weightsPath, configPath)


net.setInputSize(320, 320)
net.setInputScale(1.0 / 127.5)
net.setInputMean((127.5, 127.5, 127.5))
net.setInputSwapRB(True)

while True:
success, img = cap.read()
classIds, confs, bbox = net.detect(img, confThreshold=thres)
print(classIds, bbox)

if len(classIds) != 0:
for classId, confidence, box in zip(classIds.flatten(),
confs.flatten(), bbox):
cv2.rectangle(img, box, color=(0, 255, 0), thickness=2)
cv2.putText(img, classNames[classId - 1].upper(), (box[0] +
10, box[1] + 30),
cv2.FONT_HERSHEY_COMPLEX, 1, (0, 255, 0), 2)
cv2.putText(img, str(round(confidence * 100, 2)), (box[0] +
200, box[1] + 30),
cv2.FONT_HERSHEY_COMPLEX, 1, (0, 255, 0), 2)

cv2.imshow("Output", img)
cv2.waitKey(1)
OUTPUT:
PRACTICAL NO. 7
AIM:
Perform mouse Events (examples such as continuous line, display coordinates while left
mouse click, right mouse click display RGB Values).
Continuous Line:
CODE:
import numpy as np
import cv2

# drawing=False # true if mouse is pressed


# mode=True

# def click_event(event, x, y, flags, param):


# # global ix, iy, drawing, mode
# if event == cv2.EVENT_LBUTTONDOWN:
# cv2.circle(img, (x,y), 3,(0,0,255),-1)
# points.append((x,y))
# if len(points)>=2:
# cv2.line(img, points[-1], points[-2],(255,0,0),5)
# cv2.imshow('img',img)
def event_handling_for(event,x,y,flags,param):
if event == cv2.EVENT_LBUTTONDOWN:
cv2.circle(img,(x,y),3,(0,0,244),-1)
points.append((x,y))
if len(points) >=2:
cv2.line(img,points[-1],points[-2],(255,0,0),5)
cv2.imshow('image',img)

img = np.zeros((512,512,3), np.uint8)


# img = cv2.imread('lena.jpg')
cv2.imshow('image',img)
points= []

cv2.setMouseCallback('image',event_handling_for)

cv2.waitKey(0)
cv2.destroyAllWindows()

OUTPUT:
Right and Left Mouse Click:
CODE:
import numpy as np
import cv2

# events = [i for i in dir(cv2) if 'EVENT' in i]


# print(events)

def click_event(event, x, y, flags, param):


if event == cv2.EVENT_LBUTTONDOWN:
print(x,', ' ,y)
font = cv2.FONT_HERSHEY_COMPLEX
strXY = str(x) + ', '+ str(y)
cv2.putText(img, strXY, (x, y), font, 1, (255, 0, 0), 2)
cv2.imshow('image', img)
if event == cv2.EVENT_RBUTTONDOWN:
blue = img[y, x, 0]
green = img[y, x, 1]
red = img[y, x, 2]
font = cv2.FONT_HERSHEY_SIMPLEX
strBGR = str(blue) + ', '+ str(green)+ ', '+ str(red)
cv2.putText(img, strBGR, (x, y), font, .8, (0, 255, 255), 2)
cv2.imshow('image', img)

# img = np.zeros((512, 512, 3), np.uint8)


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

cv2.setMouseCallback('image', click_event)

cv2.waitKey(0)
cv2.destroyAllWindows()

OUTPUT:
PRACTICAL NO. 8
AIM:
Facial Recognition – Make your computer recognize you.
CODE:
import cv2
import sys

cascPath = "haarcascade_frontalface_default.xml"
faceCascade = cv2.CascadeClassifier(cascPath)

video_capture = cv2.VideoCapture(0)

while True:
# Capture frame-by-frame
ret, frame = video_capture.read()

gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)

faces = faceCascade.detectMultiScale(
gray,
scaleFactor=1.1,
minNeighbors=5,
minSize=(30, 30),
flags=cv2.CASCADE_SCALE_IMAGE
)

# Draw a rectangle around the faces


for (x, y, w, h) in faces:
cv2.rectangle(frame, (x, y), (x+w, y+h), (0, 255, 0), 2)

# Display the resulting frame


cv2.imshow('Video', frame)

if cv2.waitKey(1) & 0xFF == ord('q'):


break

# When everything is done, release the capture


video_capture.release()
cv2.destroyAllWindows()

OUTPUT:
PRACTICAL NO. 9
AIM:
Perform GUI Features in an image (draw lines, rectangles, ellipses, circles ,arrow segment
line, put text).
CODE:
import numpy as np
import cv2

img = cv2.imread('lena.jpg', 1)
# img = np.zeros([512, 512, 3], np.uint8)
img = cv2.line(img, (0,0), (255,255), (147, 96, 44), 10) # 44, 96, 147
img = cv2.arrowedLine(img, (0,255), (255,255), (255, 0, 0), 10)

img = cv2.rectangle(img, (384, 0), (510, 128), (0, 0, 255), 10)

img = cv2.circle(img, (447, 63), 63, (0, 255, 0), -1)

font = cv2.FONT_HERSHEY_SIMPLEX
img = cv2.putText(img, 'OpenCv', (10, 500), font, 4, (0, 255, 255), 10,
cv2.LINE_AA)

img = cv2.ellipse(img,(256,256),(100,50),0,0,180,255,-1)

pts = np.array([[10,5],[20,30],[70,20],[50,10]], np.int32)


pts = pts.reshape((-1,1,2))

img = cv2.polylines(img,[pts],True,(0,255,255))
cv2.imshow('image', img)

cv2.waitKey(0)
cv2.destroyAllWindows()

OUTPUT:
PRACTICAL NO. 10
AIM:
Perform Basic operation on Image.
a) Access pixel values and modify them.
CODE:
import cv2
import numpy as np
img = cv2.imread('messi5.jpg')

px = img[100,100]
print(px)
blue = img[100,100,0]
print(blue)

img[100,100] = [255,255,255]
print(img[100,100])

OUTPUT:
b) Access image properties i.e., Shape.
c) Setting Region of Image (ROI).
d) Splitting and Merging images.
CODE:
import numpy as np
import cv2

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

print(img.shape)
print(img.size)
print(img.dtype)

b,g,r = cv2.split(img)
img = cv2.merge((b, g, r))

ball = img[280:340, 330:390]


img[273:333, 100:160] = ball

px = img[100,100]
print( px )

blue = img[100,100,0]
print( blue )
img[100,100] = [255,255,255]
print( img[100,100] )

cv2.imshow('imagewindow',img)
cv2.waitKey(0)
cv2.destroyAllWindows()

OUTPUT:
e) Making Borders for Images.
CODE:
import cv2 as cv
import numpy as np
from matplotlib import pyplot as plt
BLUE = [255,0,0]
img1 = cv.imread('opencv-logo.png')
replicate = cv.copyMakeBorder(img1,10,10,10,10,cv.BORDER_REPLICATE)
reflect = cv.copyMakeBorder(img1,10,10,10,10,cv.BORDER_REFLECT)
reflect101 = cv.copyMakeBorder(img1,10,10,10,10,cv.BORDER_REFLECT_101)
wrap = cv.copyMakeBorder(img1,10,10,10,10,cv.BORDER_WRAP)
constant= cv.copyMakeBorder(img1,10,10,10,10,cv.BORDER_CONSTANT,value=BLUE)
plt.subplot(231),plt.imshow(img1,'gray'),plt.title('ORIGINAL')
plt.subplot(232),plt.imshow(replicate,'gray'),plt.title('REPLICATE')
plt.subplot(233),plt.imshow(reflect,'gray'),plt.title('REFLECT')
plt.subplot(234),plt.imshow(reflect101,'gray'),plt.title('REFLECT_101')
plt.subplot(235),plt.imshow(wrap,'gray'),plt.title('WRAP')
plt.subplot(236),plt.imshow(constant,'gray'),plt.title('CONSTANT')
plt.show()

OUTPUT:
f) Arithmetic Operation (Addition, Subtraction, Bitwise OR, Bitwise And).
Addition:
CODE:
import cv2
import numpy as np

img1 = cv2.imread('baboon.jpg',1)
img2 = cv2.imread('messi5.jpg',1)

img2_resized = cv2.resize(img2, (img1.shape[1], img1.shape[0]))


dst = cv2.addWeighted(img1, 0.7, img2_resized, 0.9, 0)

cv2.imshow('dst',dst)
cv2.waitKey(0)
cv2.destroyAllWindows()

OUTPUT:
Subtraction:
CODE:
import cv2
import numpy as np

img1=cv2.imread('baboon.jpg')
img2=cv2.imread('messi5.jpg')
img2_resized = cv2.resize(img2, (img1.shape[1], img1.shape[0]))

sub = cv2.subtract(img1, img2_resized)

cv2.imshow('Subtracted Image', sub)

if cv2.waitKey(0) & 0xff == 27:


cv2.destroyAllWindows()

OUTPUT:
Bitwise OR:
CODE:
import cv2
import numpy as np

img1=cv2.imread('baboon.jpg')
img2=cv2.imread('messi5.jpg')

img2_resized = cv2.resize(img2, (img1.shape[1], img1.shape[0]))


dest_and = cv2.bitwise_or(img1, img2_resized, mask=None)

cv2.imshow('Bitwise Or', dest_and)

if cv2.waitKey(0) & 0xff == 27:


cv2.destroyAllWindows()

OUTPUT:
Bitwise And:
CODE:
import cv2
import numpy as np

img1=cv2.imread('baboon.jpg')
img2=cv2.imread('messi5.jpg')

img2_resized = cv2.resize(img2, (img1.shape[1], img1.shape[0]))


dest_and = cv2.bitwise_and(img1, img2_resized, mask=None)
cv2.imshow('Bitwise And', dest_and)

if cv2.waitKey(0) & 0xff == 27:


cv2.destroyAllWindows()

OUTPUT:
C) Image Blending.
CODE:
import cv2
import numpy as np
apple = cv2.imread('apple.jpg')
orange = cv2.imread('orange.jpg')
print(apple.shape)
print(orange.shape)
apple_orange = np.hstack((apple[:, :256], orange[:, 256:]))

# generate Gaussian pyramid for apple


apple_copy = apple.copy()
gp_apple = [apple_copy]
for i in range(6):
apple_copy = cv2.pyrDown(apple_copy)
gp_apple.append(apple_copy)

# generate Gaussian pyramid for orange


orange_copy = orange.copy()
gp_orange = [orange_copy]
for i in range(6):
orange_copy = cv2.pyrDown(orange_copy)
gp_orange.append(orange_copy)

# generate Laplacian Pyramid for apple


apple_copy = gp_apple[5]
lp_apple = [apple_copy]
for i in range(5, 0, -1):
gaussian_expanded = cv2.pyrUp(gp_apple[i])
laplacian = cv2.subtract(gp_apple[i-1], gaussian_expanded)
lp_apple.append(laplacian)

# generate Laplacian Pyramid for orange


orange_copy = gp_orange[5]
lp_orange = [orange_copy]
for i in range(5, 0, -1):
gaussian_expanded = cv2.pyrUp(gp_orange[i])
laplacian = cv2.subtract(gp_orange[i-1], gaussian_expanded)
lp_orange.append(laplacian)

# Now add left and right halves of images in each level


apple_orange_pyramid = []
n = 0
for apple_lap, orange_lap in zip(lp_apple, lp_orange):
n += 1
cols, rows, ch = apple_lap.shape
laplacian = np.hstack((apple_lap[:, 0:int(cols/2)], orange_lap[:,
int(cols/2):]))
apple_orange_pyramid.append(laplacian)
# now reconstruct
apple_orange_reconstruct = apple_orange_pyramid[0]
for i in range(1, 6):
apple_orange_reconstruct = cv2.pyrUp(apple_orange_reconstruct)
apple_orange_reconstruct = cv2.add(apple_orange_pyramid[i],
apple_orange_reconstruct)

cv2.imshow("apple", apple)
cv2.imshow("orange", orange)
cv2.imshow("apple_orange", apple_orange)
cv2.imshow("apple_orange_reconstruct", apple_orange_reconstruct)
cv2.waitKey(0)
cv2.destroyAllWindows()
OUTPUT:

You might also like