1.
Write a computer program capable of performing the given basic operations in OpenCV
a. Read an image
b. Display the image
c. Find the shape of the image
d. Resize it to a desired size
e. Write the resized image
f. Convert to grayscale image
g. Pad the image
import cv2
import matplotlib.pyplot as plt
import os
import numpy as np
img=cv2.imread('/content/drive/MyDrive/dahlia-red-gaysorn-and-
beautiful-flowers.webp')
plt.imshow(img)
plt.show()
rimg=cv2.resize(img,(256,256))
path = '/content/drive/MyDrive/dahlia-red-gaysorn-and-beautiful-
flowers.webp'
cv2.imwrite(os.path.join(path , 'resized_42049.jpg'), rimg)
cv2.waitKey(0)
print(rimg.shape)
cimg=cv2.cvtColor(rimg, cv2.COLOR_BGR2RGB)
plt.imshow(cimg)
white = [255,255,255]
img_pad=cv2.copyMakeBorder(cimg,50,50,50,50,cv2.BORDER_CONSTANT,value=w
hite)
plt.imshow(img_pad)
img_pad = cv2.copyMakeBorder(cimg, 50, 50, 50, 50,
cv2.BORDER_REPLICATE)
plt.imshow(img_pad)
2.Write a computer program capable of performing the given arithmetic operations between two images.
a. Addition
b. Subtraction
c. Multiplication
d. Division
2. from google.colab import drive
3. drive.mount('/content/drive')
4. import cv2
5. import matplotlib.pyplot as plt
img1 = cv2.imread('/content/drive/MyDrive/Flower.jpg')
img2 = cv2.imread('/content/drive/MyDrive/Flower_2.jpg')
new_width = 640
new_height = 480
img1 = cv2.resize(img1, (new_width, new_height))
img2 = cv2.resize(img2, (new_width, new_height))
#ADDITION OF TWO IMAGES
dst_addition = cv2.add(img1, img2)
plt.imshow(dst_addition)
plt.show()
#multiplication of two images
dst_multiply=cv2.multiply(img1,img2)
plt.imshow(dst_multiply)
#division of two images
dst_divide=cv2.divide(img1,img2)
plt.imshow(dst_divide)
#subtraction of two images
dst_sub=cv2.subtract(img1,img2)
plt.imshow(dst_sub)
3.Write a computer program capable of performing the given Bitwise logical operations between two images.
a. Bitwise AND
b. Bitwise OR
c. Bitwise XOR
d. Bitwise NOT
from google.colab import drive
drive.mount('/content/drive')
import cv2
import matplotlib.pyplot as plt
import os
import numpy as np
img1 = cv2.imread('/content/drive/MyDrive/Flower.jpg')
img2 = cv2.imread('/content/drive/MyDrive/Flower_2.jpg')
new_width = 640
new_height = 480
img1 = cv2.resize(img1, (new_width, new_height))
img2 = cv2.resize(img2, (new_width, new_height))
result = cv2.bitwise_not(img1)
plt.imshow(result)
result = cv2.bitwise_not(img2)
plt.imshow(result)
result=cv2.bitwise_or(img1,img2)
plt.imshow(result)
result2=cv2.bitwise_and(img1,img2)
plt.imshow(result2)
result2=cv2.bitwise_xor(img1,img2)
plt.imshow(result2)
4.Write a computer program capable of performing the given geometric transformations on an image
a. Translation
b. Rotation
c. Scaling
d. Vertical shear
e. Horizontal shear
f. Reflection
from google.colab import drive
drive.mount('/content/drive')
import numpy as np;
import matplotlib.pyplot as plt
import cv2
img = cv2.imread('/content/drive/MyDrive/Flower.jpg')
rows,cols,dim=img.shape
M=np.float32([[2,0,0],[0,2,0],[0,0,1]])
simg = cv2.warpPerspective(img,M,(cols*2,rows*2))
plt.imshow(simg)
print(simg.shape)
#ROTATION
angle = np.radians(10)
M = np.float32([[np.cos(angle), -(np.sin(angle)), 0],
[np.sin(angle), np.cos(angle), 0] ])
rotated_img = cv2.warpAffine(img, M, (int(cols),int(rows)))
plt.imshow(rotated_img)
#TRANSLATION
M_left=np.float32([[1,0,-50],[0,1,0]])
trans_dst=cv2.warpAffine(img, M_left,(cols,rows))
plt.imshow(trans_dst)
#SHEAR
M=np.float32([[1,0,0],[0.5,1,0],[0,0,1]])
sheared_img = cv2.warpPerspective(img,M,(int(cols*1.5),int(rows*1.5)))
plt.imshow(sheared_img)
#REFLECTION
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)
1. 5.Write a computer program capable of performing the following intensity transformations to
enhance an image.
a. Image negative
b. Log transformation
c. Power law transformation
d. Thresholding
2. #Image enhancement using intensity transformations
3. from google.colab import drive
4. drive.mount('/content/drive')
5. import cv2
6. import matplotlib.pyplot as plt
7. import os
8. import numpy as np
img = cv2.imread('/content/drive/MyDrive/Flower.jpg')
#image negative
img_neg=cv2.bitwise_not(img)
plt.imshow(img_neg)
#log transformation
gray_img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
c = 255/(np.log(1 + np.max(gray_img)))
print(c)
img_log = c * np.log(1 + gray_img)
img_log = np.array(img_log, dtype = np.uint8)
plt.imshow(img_log)
#gamma correction
gamma = 1.5
img_gamma = np.array(255*(img / 255) ** gamma, dtype = 'uint8')
plt.imshow(img_gamma)
# Assuming you have loaded the image 'img' using cv2.imread()
ret, thresh1 = cv2.threshold(img, 127, 255, cv2.THRESH_BINARY)
ret, thresh2 = cv2.threshold(img, 127, 255, cv2.THRESH_BINARY_INV)
print(ret)
plt.imshow(thresh1)
plt.show()
6.Write a computer program to perform the following spatial filtering operations on an image.
a. Box filter
b. Weighted Averaging
c. Gaussian filtering
d. Median filtering
#Spatial Filtering Operations Linera and non Linear
from google.colab import drive
drive.mount('/content/drive')
import cv2
import matplotlib.pyplot as plt
import os
import numpy as np
img = cv2.imread('/content/drive/MyDrive/Flower.jpg')
plt.imshow(cv2.cvtColor(img, cv2.COLOR_BGR2RGB))
plt.title('Original Image')
plt.show()
# Averaging
kernel = np.array([
[1, 1, 1],
[1, 1, 1],
[1, 1, 1]
])/9
dst_avg = cv2.filter2D(img, -1, kernel)
# Display the averaged image
plt.imshow(cv2.cvtColor(dst_avg, cv2.COLOR_BGR2RGB))
plt.title('Averaging')
plt.show()
# Weighted Averaging
kernel_weighted = np.array([
[1, 2, 1],
[2, 4, 2],
[1, 2, 1]
])/16
dst_weighted = cv2.filter2D(img, -1, kernel_weighted)
# Display the weighted averaged image
plt.imshow(cv2.cvtColor(dst_weighted, cv2.COLOR_BGR2RGB))
plt.title('Weighted Averaging')
plt.show()
# Smoothing using cv2.blur
blur = cv2.blur(img, (5,5))
# Display the blurred image
plt.imshow(cv2.cvtColor(blur, cv2.COLOR_BGR2RGB))
plt.title('Blurred')
plt.show()
# Smoothing using Gaussian filter
Gblur = cv2.GaussianBlur(img, (5,5), 21)
# Display the Gaussian blurred image
plt.imshow(cv2.cvtColor(Gblur, cv2.COLOR_BGR2RGB))
plt.title('Gaussian Blurred')
plt.show()
# Non Linear Filter - Median Blur
MedianPic = cv2.medianBlur(img, 5)
# Display the image with median blur
plt.imshow(cv2.cvtColor(MedianPic, cv2.COLOR_BGR2RGB))
plt.title('Median Blur')
plt.show()
1. Write a program to enhance an image using Laplacian Filter.
from google.colab import drive
drive.mount('/content/drive')
import cv2
import matplotlib.pyplot as plt
import numpy as np
img= cv2.imread('/content/drive/MyDrive/Flower.jpg')
# Define Laplacian kernels for enhancement
kernel1 = np.array([
[1, 1, 1],
[1, -8, 1],
[1, 1, 1]
])
kernel2 = np.array([
[1, 1, 1],
[1, -9, 1],
[1, 1, 1]
])
# Apply Laplacian filter
dst_lap = cv2.filter2D(img, -1, kernel1)
# Display original and Laplacian filtered images
plt.subplot(121),plt.imshow(cv2.cvtColor(img,
cv2.COLOR_BGR2RGB)),plt.title('Original')
plt.subplot(122),plt.imshow(cv2.cvtColor(dst_lap,
cv2.COLOR_BGR2RGB)),plt.title('Laplacian Filtered')
plt.show()
# Sharpened image
Img_sharp = cv2.subtract(img, dst_lap)
# Display the sharpened image
plt.imshow(cv2.cvtColor(Img_sharp, cv2.COLOR_BGR2RGB))
plt.title('Sharpened Image')
plt.show()
# Alternate way of using Laplacian filter
LaplacePic = cv2.Laplacian(img, cv2.CV_64F, ksize=5)
# Sharpened image using alternate method
Img_sharp_alt = cv2.subtract(img, LaplacePic, dtype=cv2.CV_8U)
# Display the sharpened image using alternate method
plt.imshow(cv2.cvtColor(Img_sharp_alt, cv2.COLOR_BGR2RGB))
plt.title('Sharpened Image (Alternate Method)')
plt.show()
1. Write a program to sharpen an image using the unsharp masking technique.
from google.colab import drive
drive.mount('/content/drive')
import cv2
import matplotlib.pyplot as plt
import numpy as np
img= cv2.imread('/content/drive/MyDrive/Flower.jpg')
#sharpening using unsharp masking technique
blur = cv2.blur(img,(5,5))
mask=cv2.subtract(img,blur)
img_sharp=cv2.add(img,mask)
plt.imshow(img_sharp)
1. Write a computer program to calculate the histogram of an image.
2. from google.colab import drive
3. drive.mount('/content/drive')
4. import cv2
5. import matplotlib.pyplot as plt
6. import numpy as np
7. from google.colab import files
8. uploaded = files.upload()
img = cv2.imread(next(iter(uploaded)))
gray_img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
hist = cv2.calcHist([gray_img],[0],None,[256],[0,256])
plt.hist(gray_img.ravel(),256,[0,256])
plt.show()
color = ('b','g','r')
for i,col in enumerate(color):
histr = cv2.calcHist([img],[i],None,[256],[0,256])
plt.plot(histr,color = col)
plt.xlim([0,256])
1. Write a computer program to Implement the Histogram Equalization technique.
2. from google.colab import drive
3. drive.mount('/content/drive')
4. import cv2
5. import matplotlib.pyplot as plt
6. import numpy as np
from google.colab import files
uploaded = files.upload()
img = cv2.imread(next(iter(uploaded)))
# Calculating and Plotting Histogram for Original Image
hist = cv2.calcHist([img],[0],None,[256],[0,256])
plt.hist(img.ravel(),256,[0,256])
plt.title('Histogram of Original Image')
plt.show()
# Convert the Image to Grayscale
img_gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# Histogram Equalization
equ = cv2.equalizeHist(img_gray)
# Plotting Histogram after Equalization
hist_eq = cv2.calcHist([equ],[0],None,[256],[0,256])
plt.hist(equ.ravel(),256,[0,256])
plt.title('Histogram after Equalization')
plt.show()
# Displaying the Equalized Image
plt.imshow(equ, cmap='gray')
plt.title('Equalized Image')
plt.show()