0% found this document useful (0 votes)
25 views4 pages

Search Creators CG LAB Program-09

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

Search Creators CG LAB Program-09

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

21CSL66 | COMPUTER GRAPHICS AND IMAGE PROCESSING LABORATORY|

Subject Code:21CSL66

Subject: COMPUTER GRAPHICS AND IMAGE PROCESSING


LABORATORY

Program-09

9. Read an image and extract and display low-level features such as edges,
textures using filtering techniques.

Program

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

Search Creators…. Page 1


21CSL66 | COMPUTER GRAPHICS AND IMAGE PROCESSING LABORATORY|

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()

Output

Search Creators…. Page 2


21CSL66 | COMPUTER GRAPHICS AND IMAGE PROCESSING LABORATORY|

Search Creators…. Page 3


21CSL66 | COMPUTER GRAPHICS AND IMAGE PROCESSING LABORATORY|

Explanation

1. The necessary libraries, cv2 (OpenCV) and numpy, are imported.


2. The path to the input image file is specified (image_path). In this case, it's set
to "image/atc.jpg", assuming the image file named "atc.jpg" is located in a
directory named "image" relative to the script's location.
3. The image is loaded using cv2.imread().
4. The image is converted to grayscale using cv2.cvtColor(img,
cv2.COLOR_BGR2GRAY). This step is necessary for edge detection and
texture extraction, as these operations are typically performed on grayscale
images.
5. Edge detection is performed on the grayscale image using the Canny edge
detector (cv2.Canny(gray, 100, 200)). The Canny edge detector is a popular
algorithm for edge detection, and the two arguments (100 and 200) are the
lower and upper thresholds for hysteresis.
6. Texture extraction is performed using a simple averaging filter
(cv2.filter2D(gray, -1, kernel)). A 5x5 averaging kernel (kernel = np.ones((5,
5), np.float32) / 25) is defined, where each element is set to 1/25 (the sum of
the kernel elements is 1). This kernel is applied to the grayscale image using
cv2.filter2D(), which performs a 2D convolution between the image and the
kernel. The resulting image (texture) captures the texture information of the
original image.
7. The original image (img), the detected edges (edges), and the extracted texture
(texture) are displayed using cv2.imshow().
8. The script waits for a key press (cv2.waitKey(0)) before closing the windows.
9. Finally, all windows are closed using cv2.destroyAllWindows().

Search Creators…. Page 4

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy