Shubham Image File
Shubham Image File
Shubham Image File
A - Read an image Display the image B - Find the shape of the image C - Resize it to a desired size
D - Write the resized image E - Convert to grayscale image F - Pad the image
#CODE
drive.mount('/content/drive')
import cv2
import os
import numpy as np
img = cv2.imread('/content/Lana.png')
plt.imshow(img)
plt.show()
path = '/content/Lana.png'
plt.imshow(rimg)
plt.show()
path = '/content/Lana.png'
cv2.waitKey(0)
print(rimg.shape)
cimg = cv2.cvtColor(rimg, cv2.COLOR_BGR2RGB)
plt.imshow(cimg)
plt.show()
x = cv2.copyMakeBorder(cimg, 50,50,50,50,
cv2.BORDER_CONSTANT, value= (0,0,0))
plt.imshow(x)
plt.imshow(y)
Q2 - Write a computer program capable of performing the given arithmetic operations between two
images.
#CODE
#Addition
print(img1.shape)
print(img2.shape)
print(img1.shape)
print(img2.shape)
dst = cv2.addWeighted(img2,0.7,img1,0.3,0)
dst = cv2.add(img2,img1)
plt.imshow(dst)
#Multiplication
dstmultiply=cv2.multiply(img1,img2)
plt.imshow(dstmultiply)
#Division
dstdivide=cv2.divide(img1,img2)
plt.imshow(dstdivide)
#Subtraction
dstsub=cv2.subtract(img1,img2)
plt.imshow(dstdivide)
Q3 - Write a computer program capable of performing the given Bitwise logical operations between two
images.
Bitwise AND
Bitwise OR
Bitwise XOR
Bitwise NOT
#CODE
X = cv2.bitwise_and(img1,img2)
plt.imshow(x)
y = cv2.bitwise_or(img1,img2)
plt.imshow(y)
z = cv2.bitwise_not(img1)
plt.imshow(z)
w = cv2.bitwise_xor(img1,img2)
plt.imshow(w)
Q4 - Write a computer program capable of performing the given geometric transformations on an image
Translation
Rotation
Scaling
Vertical shear
Horizontal shear
Reflection
#CODE
rows,cols,dim=img.shape
#M=np.float32([[2,0,0],[0,2,0]])
M=np.float32([[2,0,0],[0,2,0],[0,0,1]])
#simg=cv2.warpAffine(img,M,(cols*2,rows*4))
simg = cv2.warpPerspective(img,M,(cols*2,rows*2))
plt.imshow(simg)
print(simg.shape)
#ROTATION
angle = np.radians(10)
# [np.sin(angle), np.cos(angle), 0]
# ])
# [0, 0, 1]])
plt.imshow(rotated_img)
#TRANSLATION
#M_left=np.float32([[1,0,-50],[0,1,0],[0,0,1]])
M_left=np.float32([[1,0,-50],[0,1,0]])
#M_right=np.float32([[1,0,50],[0,1,0]])
#M_bottom=np.float32([[1,0,0],[0,1,50]])
#M_top=np.float32([[1,0,0],[0,1,-50]])
#trans_dst=cv2.warpPerspective(img, M_left,(cols,rows))
trans_dst=cv2.warpAffine(img, M_left,(cols,rows))
plt.imshow(trans_dst)
#SHEAR
#along x axis
#M=np.float32([[1,0.5,0],[0,1,0]])
#M=np.float32([[1,0.5,0],[0,1,0],[0,0,1]])
#sheared_img = cv2.warpAffine(img,M,
(int(cols*1.5),int(rows*1.5)))
#along y axis
#M=np.float32([[1,0,0],[0.5,1,0]])
M=np.float32([[1,0,0],[0.5,1,0],[0,0,1]])
#sheared_img = cv2.warpAffine(img,M,(int(cols*1.5),int(rows*1.5)))
sheared_img = cv2.warpPerspective(img,M,(int(cols*1.5),int(rows*1.5)))
plt.imshow(sheared_img)
#REFLECTION
#M = np.float32([[1, 0, 0 ],
# [0, 0, 1 ]])
M = np.float32([[-1, 0, cols],
[ 0, 1, 0],
[ 0, 0, 1]])
reflected_img = cv2.warpPerspective(img,M,(int(cols),int(rows)))
plt.imshow(reflected_img)
Q5 - Write a computer program capable of performing the following intensity transformations to
enhance an image.
#image negative
img_neg=cv2.bitwise_not(img)
plt.imshow(img_neg)
# Log Transformation
c = 255/(np.log(1 + np.max(grayimg)))
print(c)
imglog = c*np.log(1+grayimg)
plt.imshow(imglog)
#gamma Correction
plt.imshow(imggamma)
#Thresholding
import cv2
import os
import numpy as np
img1 = cv2.imread('/content/Mona.jpg')
print(ret)
plt.imshow(thresh1)
Q6 - Write a computer program to perform the following spatial filtering operations o an image.
# Box filter
kernel = np.array([[1,1,1,1],[1,1,1,1],[1,1,1,1],
[1,1,1,1]])/9
plt.imshow(dst)
# Weighted Averaging
kernel = np.array([[1,2,1],[2,4,2],[1,2,1]])/16
plt.imshow(dst)
# GAUSSIAN FILTER
Gblur = cv2.GaussianBlur(img1,(5,5),21)
plt.imshow(img1)
MedianPic = cv2.medianBlur(img1,5)
plt.imshow(MedianPic)
#BLUR IMAGE
plt.imshow(img1)
Q7 - Write a program to enhance an image using Laplacian Filter.
#CODE
# Laplacian 1
kernel = np.array([[0,1,0],[1,-4,1],[0,1,0]])
plt.imshow(dst)
plt.subplot(121), plt.imshow(img1),
plt.title('original')
plt.subplot(122), plt.imshow(dst),
plt.title('Laplacian')
sub = cv2.subtract(img1,dst)
plt.imshow(sub)
# Laplacian 2
kernel = np.array([[0,-1,0],[-1,4,-1],[0,-1,0]])
plt.imshow(dst)
sub = cv2.subtract(img1,dst)
plt.imshow(sub)
Q8 - Write a program to sharpen an image using the unsharp masking technique.
#CODE
# Unmasking
sub = cv2.subtract(img1,kblur)
imgsharp = cv2.add(img1,sub)
plt.imshow(imgsharp)
#CODE
# HistoGram - GrayScale
plt.show
plt.plot(histr,color = col)
plt.xlim([0,256])
Q10 - Write a computer program to Implement the Histogram Equalization technique.
#CODE
equ = cv2.equalizeHist(grayimg)
plt.hist(equ.ravel(),256,[0,256]);
plt.show()
LAB 11
Morphological operations
import cv2
import numpy as np
LAB 12
Edge Detection
import cv2
import numpy as np
from matplotlib import pyplot as plt
# Read the image in grayscale
img = cv2.imread('img1.jpg', 0)
# Use the Canny edge detector
edges = cv2.Canny(img, 100, 200)
# Plot the original and edge images
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()