KEMBAR78
CVPR5 | PDF | Graphics | Computer Graphics
0% found this document useful (0 votes)
25 views7 pages

CVPR5

Uploaded by

archit.muj
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 views7 pages

CVPR5

Uploaded by

archit.muj
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/ 7

TANISHQ SANDEEP PATIL 229311050 AIML - 6D

#Compute the edge detection using Sobel,Prewitt,and Canny operator


import cv2
import numpy as np
import matplotlib.pyplot as plt
from skimage import filters

image_path = '/content/pic.jpg'
image = cv2.imread(image_path, cv2.IMREAD_GRAYSCALE)

if image is None:
raise ValueError("Image not found or unable to load.")

# Sobel
sobel_x = cv2.Sobel(image, cv2.CV_64F, 1, 0, ksize=5)
sobel_y = cv2.Sobel(image, cv2.CV_64F, 0, 1, ksize=5)
sobel_edges = np.hypot(sobel_x, sobel_y)
sobel_edges = np.uint8(sobel_edges)

#Prewitt
prewitt_kernel_x = np.array([[1, 0, -1],
[1, 0, -1],
[1, 0, -1]])
prewitt_kernel_y = np.array([[1, 1, 1],
[0, 0, 0],
[-1, -1, -1]])
prewitt_x = cv2.filter2D(image, -1, prewitt_kernel_x)
prewitt_y = cv2.filter2D(image, -1, prewitt_kernel_y)
prewitt_edges = np.hypot(prewitt_x, prewitt_y)
prewitt_edges = np.uint8(prewitt_edges)

#Canny
canny_edges = cv2.Canny(image, 100, 200)

plt.figure(figsize=(12, 8))

plt.subplot(2, 2, 1)
plt.title('Original Image')
plt.imshow(image, cmap='gray')
plt.axis('off')

plt.subplot(2, 2, 2)
plt.title('Sobel Edge Detection')
plt.imshow(sobel_edges, cmap='gray')
plt.axis('off')

plt.subplot(2, 2, 3)
plt.title('Prewitt Edge Detection')
plt.imshow(prewitt_edges, cmap='gray')
plt.axis('off')

plt.subplot(2, 2, 4)
plt.title('Canny Edge Detection')
plt.imshow(canny_edges, cmap='gray')
plt.axis('off')

plt.tight_layout()
plt.show()

#Implement the Harris corner detector algorithm to determine the


corner of the image
import cv2
import numpy as np
import matplotlib.pyplot as plt

def corner_response(image, k=0.05):


Sx = np.array([[1, 0, -1], [2, 0, -2], [1, 0, -1]])
Sy = Sx.T

dx = cv2.filter2D(image, -1, Sx)


dy = cv2.filter2D(image, -1, Sy)
Ixx = dx * dx
Iyy = dy * dy
Ixy = dx * dy

G = cv2.GaussianBlur(Ixx, (5, 5), 0)


H = cv2.GaussianBlur(Iyy, (5, 5), 0)
Ixy = cv2.GaussianBlur(Ixy, (5, 5), 0)

det = (G * H) - (Ixy ** 2)
trace = G + H
R = det - k * (trace ** 2)

return R

def get_harris_corners(image, threshold=0.01):


R = corner_response(image)
corners = np.argwhere(R > threshold * R.max())
return corners

image_path = '/content/pic.jpg'
image = cv2.imread(image_path, cv2.IMREAD_GRAYSCALE)

if image is None:
raise ValueError("Image not found or unable to load.")

corners = get_harris_corners(image)

image_out = cv2.cvtColor(image, cv2.COLOR_GRAY2BGR)


for y, x in corners:
cv2.circle(image_out, (x, y), radius=3, color=(0, 255, 0),
thickness=-1)

plt.figure(figsize=(10, 10))
plt.subplot(1, 2, 1)
plt.title('Original Image')
plt.imshow(image, cmap='gray')
plt.axis('off')

plt.subplot(1, 2, 2)
plt.title('Harris Corners')
plt.imshow(image_out)
plt.axis('off')

plt.tight_layout()
plt.show()
#Implement the Harris Corner Detector algorithm without the inbuilt
OpenCV()function
import numpy as np
import matplotlib.pyplot as plt
from scipy.ndimage import gaussian_filter

def compute_gradients(image):
sobel_x = np.array([[1, 0, -1],
[2, 0, -2],
[1, 0, -1]])
sobel_y = np.array([[1, 2, 1],
[0, 0, 0],
[-1, -2, -1]])
dx = convolve2d(image, sobel_x)
dy = convolve2d(image, sobel_y)
return dx, dy

def convolve2d(image, kernel):


kernel_height, kernel_width = kernel.shape
pad_height = kernel_height // 2
pad_width = kernel_width // 2

padded_image = np.pad(image, ((pad_height, pad_height),


(pad_width, pad_width)), mode='constant')

output = np.zeros_like(image)

for i in range(image.shape[0]):
for j in range(image.shape[1]):
output[i, j] = np.sum(padded_image[i:i + kernel_height,
j:j + kernel_width] * kernel)
return output

def harris_response(dx, dy, k=0.04):


Ixx = dx * dx
Iyy = dy * dy
Ixy = dx * dy
Ixx = gaussian_filter(Ixx, sigma=1)
Iyy = gaussian_filter(Iyy, sigma=1)
Ixy = gaussian_filter(Ixy, sigma=1)

det = (Ixx * Iyy) - (Ixy ** 2)


trace = Ixx + Iyy
R = det - k * (trace ** 2)
return R

def get_harris_corners(image, threshold=0.01):


dx, dy = compute_gradients(image)
R = harris_response(dx, dy)
corners = np.argwhere(R > threshold * R.max())
return corners

image_path = '/content/pic.jpg'
image = plt.imread(image_path)

if image.ndim == 3:
image = np.mean(image, axis=2)

corners = get_harris_corners(image)

image_out = np.dstack((image, image, image))


for y, x in corners:
cv2.circle(image_out, (x, y), radius=3, color=(0, 255, 0),
thickness=-1)

plt.figure(figsize=(10, 10))
plt.subplot(1, 2, 1)
plt.title('Original Image')
plt.imshow(image, cmap='gray')
plt.axis('off')

plt.subplot(1, 2, 2)
plt.title('Harris Corners')
plt.imshow(image_out)
plt.axis('off')

plt.tight_layout()
plt.show()

WARNING:matplotlib.image:Clipping input data to the valid range for


imshow with RGB data ([0..1] for floats or [0..255] for integers). Got
range [0.0..255.0].
import cv2
import numpy as np
import matplotlib.pyplot as plt

def hough_transform(image):
edges = cv2.Canny(image, 50, 150, apertureSize=3)

height, width = edges.shape

max_dist = int(np.sqrt(height**2 + width**2))

thetas = np.deg2rad(np.arange(-90, 90))


rhos = np.arange(-max_dist, max_dist)

accumulator = np.zeros((len(rhos), len(thetas)), dtype=int)

y_indices, x_indices = np.nonzero(edges)


for i in range(len(x_indices)):
x = x_indices[i]
y = y_indices[i]
for theta_index in range(len(thetas)):
theta = thetas[theta_index]
rho = int(x * np.cos(theta) + y * np.sin(theta)) +
max_dist
accumulator[rho, theta_index] += 1
return accumulator, thetas, rhos

def draw_hough_lines(image, accumulator, thetas, rhos, threshold=100):


y_indices, x_indices = np.nonzero(accumulator > threshold)
lines_image = np.copy(image)

for i in range(len(y_indices)):
rho = rhos[y_indices[i]]
theta = thetas[x_indices[i]]
a = np.cos(theta)
b = np.sin(theta)
x0 = a * rho
y0 = b * rho
x1 = int(x0 + 1000 * (-b))
y1 = int(y0 + 1000 * (a))
x2 = int(x0 - 1000 * (-b))
y2 = int(y0 - 1000 * (a))
cv2.line(lines_image, (x1, y1), (x2, y2), (0, 255, 0), 2)
return lines_image

image_path = '/content/pic.jpg'
image = cv2.imread(image_path, cv2.IMREAD_GRAYSCALE)

if image is None:
raise ValueError("Image not found or unable to load.")

accumulator, thetas, rhos = hough_transform(image)

lines_image = draw_hough_lines(image, accumulator, thetas, rhos,


threshold=100)

plt.figure(figsize=(12, 6))
plt.subplot(1, 2, 1)
plt.title('Original Image')
plt.imshow(image, cmap='gray')
plt.axis('off')

plt.subplot(1, 2, 2)
plt.title('Detected Lines')
plt.imshow(lines_image, cmap='gray')
plt.axis('off')

plt.tight_layout()
plt.show()

You might also like