0% found this document useful (0 votes)
17 views2 pages

Program 9 10

DSV program 9 and 10

Uploaded by

Tushar Chopra
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views2 pages

Program 9 10

DSV program 9 and 10

Uploaded by

Tushar Chopra
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

9.

Read an image and extract and display low-level features such as edges, textures using
filtering techniques.
import cv2
import numpy as np

# Load the image


image_path = "image/atc.jpg" # Replace with the path to your image
img = cv2.imread(image_path)

# Convert the image to grayscale


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

# Edge detection
edges = cv2.Canny(gray, 100, 200) # Use Canny edge detector

# Texture extraction
kernel = np.ones((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


cv2.imshow("Original Image", img)
cv2.imshow("Edges", edges)
cv2.imshow("Texture", texture)

# Wait for a key press and then close all windows


cv2.waitKey(0)
cv2.destroyAllWindows()
10. Write a program to blur and smoothing an image.
img = cv2.imread("/content/sample_data/smaple.jpg",cv2.IMREAD_GRAYSCALE)
image_array = np.array(img)
print(image_array)
def sharpen():
return np.array([[1,1,1],[1,1,1],[1,1,1]])
def filtering(image, kernel):
m, n = kernel.shape
if (m == n):
y, x = image.shape
y = y - m + 1 # shape of image - shape of kernel + 1
x=x-m+1
new_image = np.zeros((y,x))
for i in range(y):
for j in range(x):
new_image[i][j] = np.sum(image[i:i+m, j:j+m]*kernel)
return new_image
# Display the original and sharpened images
plt.figure(figsize=(10, 5))
plt.subplot(1, 2, 1)
plt.imshow(image_array,cmap='gray')
plt.title("Original Grayscale Image")
plt.axis("off")

plt.subplot(1, 2, 2)
plt.imshow(filtering(image_array, sharpen()),cmap='gray')
plt.title("Blurred Image")
plt.axis("off")
plt.show()

You might also like