#Program-7 Write a Program to read a digital image.
Split and display image into 4
#quadrants, up, down, right and left.
import cv2
from matplotlib import pyplot as plt
# Load image
image = [Link]('D:\\[Link]')
# create figure
[Link](figsize=(7, 8))
# setting values to rows and column variables
rows = 3; columns = 2
#Get the height and width of the image
(h, w) = [Link][:2]
#Converting BGR to RGB
image=image[:,:,::-1]
#[Link]('Original', image)
(cX, cY) = (w // 2, h // 2)
# crop the image into four parts which will be labelled as
# top left, top right, bottom left, and bottom right.
topLeft = image[0:cY, 0:cX]
topRight = image[0:cY, cX:w]
bottomLeft = image[cY:h, 0:cX]
bottomRight = image[cY:h, cX:w]
# Adds a subplot at the 1st position
[Link](rows, columns, 1)
# showing image
[Link](image)
[Link]('off')
[Link]("Original")
# Adds a subplot at the 2nd position
[Link](rows, columns, 3)
# showing image
[Link](topLeft)
[Link]('off')
[Link]("topLeft")
# Adds a subplot at the 3rd position
[Link](rows, columns, 4)
# showing image
[Link](topRight)
[Link]('off')
[Link]("topRight")
# Adds a subplot at the 4th position
[Link](rows, columns, 5)
# showing image
[Link](bottomLeft)
[Link]('off')
[Link]("bottomLeft")
# Adds a subplot at the 4th position
[Link](rows, columns, 6)
# showing image
[Link](bottomRight)
[Link]('off')
[Link]("bottomRight")
# Program-8 program to show rotation, scaling, and translation on an image
#Python program to explain [Link]() method, [Link](),translate
# importing cv2
import cv2
from matplotlib import pyplot as plt
import numpy as np
fig = [Link](figsize=(20, 10))
rows = 3
columns = 2
# Reading an image in default mode
src = [Link]('D:\\[Link]')
# Using [Link]() method
# Using cv2.ROTATE_90_CLOCKWISE rotate
# by 90 degrees clockwise
image = [Link](src, cv2.ROTATE_90_CLOCKWISE)
# Adds a subplot at the 1st position
fig.add_subplot(rows, columns, 1)
# showing image
img2=src[:,:,::-1]
[Link](img2)
[Link]('off')
[Link]("Original")
# Adds a subplot at the 2nd position
fig.add_subplot(rows, columns, 2)
# showing image
img3=image[:,:,::-1]
[Link](img3)
[Link]('off')
[Link]("Rotated")
# Displaying the image
img2=image[:,:,::-1]
[Link](img2)
r = 150.0 / [Link][1]
dim = (150, int([Link][0] *r))
#r, c = [Link]
img_shrinked = [Link](src, dim, interpolation=cv2.INTER_AREA)
# Adds a subplot at the 2nd position
fig.add_subplot(rows, columns, 3)
# showing image
img4=img_shrinked[:,:,::-1]
[Link](img4)
[Link]('off')
[Link]("scaled")
# shift the image 25 pixels to the right and 50 pixels down
M = np.float32([[1, 0, 25], [0, 1, 50]])
shifted = [Link](src, M, ([Link][1], [Link][0]))
# Adds a subplot at the 2nd position
fig.add_subplot(rows, columns, 4)
# showing image
img5=shifted[:,:,::-1]
[Link](img5)
[Link]('off')
[Link]("Translated")
#9. Read an image and extract and display low-level features such as edges, textures using
#filtering techniques.
import cv2
from matplotlib import pyplot as plt
import numpy as np
# Load the image
img = [Link]("D:\\[Link]")
img=img[:,:,::-1]
# Convert the image to grayscale
gray = [Link](img, cv2.COLOR_BGR2GRAY)
# Edge detection
edges = [Link](gray, 100, 200) # Use Canny edge detector
# Texture extraction
kernel = [Link]((5, 5), np.float32) / 25 # Define a 5x5 averaging kernel
texture = cv2.filter2D(gray, -1, kernel) # Apply the averaging filter for texture extraction
# Display the original image, edges, and texture
[Link](figsize=(7, 8))
[Link](2, 2, 1)
# showing image
[Link](img)
[Link]('off')
[Link]("Original Image")
[Link](2, 2, 2)
# showing image
[Link](edges)
[Link]('off')
[Link]("Edges")
[Link](2, 2,3)
# showing image
[Link](texture)
[Link]('off')
[Link]("Texture")
#Program-10 Write a program to blur and smoothing an image.
#Smoothing an image using an average blur.
#Notice as how the kernel size increases, the image becomes progressively more blurred.
import cv2
from matplotlib import pyplot as plt
import numpy as np
fig = [Link](figsize=(20, 10))
rows = 3
columns = 2
# Reading an image in default mode
src = [Link]('D:\\[Link]')
i=1
kernelSizes = [(3, 3), (9, 9), (15, 15)]
# loop over the kernel sizes
for (kX, kY) in kernelSizes:
# apply an "average" blur to the image using the current kernel
# size
blurred = [Link](src, (kX, kY))
fig.add_subplot(rows, columns, i)
# showing image
img4= blurred[:,:,::-1]
[Link](img4)
[Link]('off')
[Link]("blurred")
i+=1
#Program-11 Write a program to contour an image.
# Grayscale
import cv2
from matplotlib import pyplot as plt
import numpy as np
fig = [Link](figsize=(20, 10))
rows = 3
columns = 2
# Reading an image in default mode
src = [Link]('D:\\[Link]')
gray = [Link](src, cv2.COLOR_BGR2GRAY)
# Find Canny edges
edged = [Link](gray, 30, 200)
contours, hierarchy = [Link](edged,cv2.RETR_EXTERNAL,
cv2.CHAIN_APPROX_NONE)
fig.add_subplot(rows, columns, 1)
[Link](edged)
[Link]('off')
[Link]("Canny Edges After Contouring")
print("Number of Contours found = " + str(len(contours)))
# Draw all contours
# -1 signifies drawing all contours
[Link](src, contours, -1, (0, 255, 0), 3)
fig.add_subplot(rows, columns, 2)
# showing image
img11= src[:,:,::-1]
[Link](img11)
[Link]('off')
[Link]("Contours")
#Program-12 Write a program to detect a face/s in an image
#[Link]
import cv2
from matplotlib import pyplot as plt
import numpy as np
# Reading an image in default mode
src = [Link]('D:\\[Link]')
gray_image = [Link](src, cv2.COLOR_BGR2GRAY)
face_classifier = [Link](
[Link] + "haarcascade_frontalface_default.xml"
)
face = face_classifier.detectMultiScale(
gray_image, scaleFactor=1.1, minNeighbors=5, minSize=(40, 40)
)
for (x, y, w, h) in face:
[Link](src, (x, y), (x + w, y + h), (0, 255, 0), 4)
img_rgb = [Link](src, cv2.COLOR_BGR2RGB)
[Link](figsize=(20,10))
[Link](img_rgb)
[Link]('off')