Untitled

mail@pastecode.io avatar
unknown
plain_text
2 months ago
1.4 kB
3
Indexable
Never
#Scharr
import cv2 
import numpy as np 
from scipy import ndimage
import matplotlib.pyplot as plt


import cv2
import numpy as np
import matplotlib.pyplot as plt

def Convolution(image, kernel):
    im_height, im_width = image.shape
    result = np.zeros((im_height, im_width), dtype="float32")
    n = kernel.shape[0]
    pad = n // 2
    for x in range(pad, im_height - pad):
        for y in range(pad, im_width - pad):
            imh_crop = image[x - pad:x + pad + 1, y - pad:y + pad + 1]
            result[x][y] = np.sum(imh_crop * kernel)
    return result

img = cv2.imread("/content/Characters Test Pattern 688x688.tif")
resized_img = cv2.resize(img, (300, 300))
gray_img = cv2.cvtColor(resized_img, cv2.COLOR_BGR2GRAY)

kernel_y = np.array([[-3, 0, 3],
                   [-10, 0, 10],
                   [-3, 0, 3]])
kernel_x = np.array([[3, 10, -3],
                   [0, 0, 0],
                   [-3, -10, -3]])
Gx = Convolution(gray_img, kernel_x)
Gy = Convolution(gray_img, kernel_y)
G = np.sqrt(Gx**2 + Gy**2)

# Display images side by side
plt.figure(figsize=(12, 4))

plt.subplot(1, 3, 1)
plt.imshow(cv2.cvtColor(resized_img, cv2.COLOR_BGR2RGB))
plt.title('Original Image')

plt.subplot(1, 3, 2)
plt.imshow(gray_img, cmap='gray')
plt.title('Grayscale Image')

plt.subplot(1, 3, 3)
plt.imshow(G, cmap='gray')
plt.title('Scharr Filtered Image')

plt.show()
Leave a Comment