0% found this document useful (0 votes)
26 views

Pattern Recognition

This document discusses using Python for pattern recognition tasks involving images. It loads two JPEG images, resizes them to 200x200 pixels, flattens the images into vectors, and calculates the Euclidean distance between the vectors to quantify similarity between the images. Key steps include reading in images, checking image shapes, resizing images to a common size, converting to vectors, and computing the distance between vectors.

Uploaded by

prerna sharma
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
26 views

Pattern Recognition

This document discusses using Python for pattern recognition tasks involving images. It loads two JPEG images, resizes them to 200x200 pixels, flattens the images into vectors, and calculates the Euclidean distance between the vectors to quantify similarity between the images. Key steps include reading in images, checking image shapes, resizing images to a common size, converting to vectors, and computing the distance between vectors.

Uploaded by

prerna sharma
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

pattern-recognition

February 26, 2024

[ ]: !pip install opencv-python


import cv2
import numpy as np
import matplotlib.pyplot as plt
from PIL import Image
import math

[6]: # Scanning jpeg image


file_name_1 = "C:/Users/Lenovo/Downloads/image 1.jpg"
file_name_2 = "C:/Users/Lenovo/Downloads/image 2.jpg"
my_img_1 = cv2.imread(file_name_1, -1)
my_img_2 = cv2.imread(file_name_2, -1)
7 #my_img = cv2.imread(filename, cv2.IMREAD_GRAYSCALE)
print(f"scanned: {file_name_1}")
print(f"scanned: {file_name_2}")

scanned: C:/Users/Lenovo/Downloads/image 1.jpg


scanned: C:/Users/Lenovo/Downloads/image 2.jpg

[8]: my_img_1 = np.array(Image.open('C:/Users/Lenovo/Downloads/image 1.jpg'))


my_img_2 = np.array(Image.open('C:/Users/Lenovo/Downloads/image 2.jpg'))
print("Image 1 shape:", my_img_1.shape)
print("Image 2 shape:", my_img_2.shape)
plt.imshow(my_img_1)
plt.show()
plt.imshow(my_img_2)
plt.show()

Image 1 shape: (194, 259, 3)


Image 2 shape: (981, 736, 3)

1
2
[9]: img1_resized = Image.fromarray(my_img_1).resize((200,200))
img2_resized = Image.fromarray(my_img_2).resize((200,200))
img1_resized = np.array(img1_resized)
img2_resized = np.array(img2_resized)

[10]: img1_vector = img1_resized.flatten()


img2_vector = img2_resized.flatten()
print("Image 1 vector shape:", img1_vector.shape)
print("Image 2 vector shape:", img2_vector.shape)

Image 1 vector shape: (120000,)


Image 2 vector shape: (120000,)

[11]: distance = math.dist(img1_vector, img2_vector)


print("Euclidean distance:", distance)

Euclidean distance: 34560.49747905837

[ ]:

You might also like