Coin Matching Image Processing
Coin Matching Image Processing
Importing Libraries
In [1]: import numpy as np
import cv2 as cv
import matplotlib.pyplot as plt
from skimage.feature import peak_local_max
Visualizing Images
In [2]: coins = cv.imread('images/coins/coins.png', cv.IMREAD_GRAYSCALE)
assert coins is not None
p = cv.imread('images/coins/Penny.png', cv.IMREAD_GRAYSCALE)
assert p is not None
n = cv.imread('images/coins/Nickel.png', cv.IMREAD_GRAYSCALE)
assert n is not None
d = cv.imread('images/coins/Dime.png', cv.IMREAD_GRAYSCALE)
assert d is not None
q = cv.imread('images/coins/Quarter.png', cv.IMREAD_GRAYSCALE)
assert q is not None
plt.subplot(1, 5, 2)
plt.imshow(p, cmap='gray')
plt.title('Penny')
plt.axis('off')
plt.subplot(1, 5, 3)
plt.imshow(n, cmap='gray')
plt.title('Nickel')
plt.axis('off')
localhost:8888/lab/workspaces/auto-q 1/6
13/07/2024, 01:55 210218M_q06
plt.subplot(1, 5, 4)
plt.imshow(d, cmap='gray')
plt.title('Dime')
plt.axis('off')
plt.subplot(1, 5, 5)
plt.imshow(q, cmap='gray')
plt.title('Quarter')
plt.axis('off')
plt.figure(figsize=(10,10))
plt.subplot(1, 4, 1)
plt.imshow(p_resp, cmap='gray')
plt.title('Penny Response')
plt.axis('off')
plt.subplot(1, 4, 2)
plt.imshow(n_resp, cmap='gray')
plt.title('Nickel Response')
plt.axis('off')
localhost:8888/lab/workspaces/auto-q 2/6
13/07/2024, 01:55 210218M_q06
plt.subplot(1, 4, 3)
plt.imshow(d_resp, cmap='gray')
plt.title('Dime Response')
plt.axis('off')
plt.subplot(1, 4, 4)
plt.imshow(q_resp, cmap='gray')
plt.title('Quarter Response')
plt.axis('off')
In [5]: pastel_colors = [
(255, 182, 193), # Pastel Pink
(173, 216, 230), # Pastel Blue
(119, 221, 119), # Pastel Green
(253, 253, 150) # Pastel Yellow
]
localhost:8888/lab/workspaces/auto-q 3/6
13/07/2024, 01:55 210218M_q06
legend_scale = 0.8
rectangle_size = 100 # Width and height of the rectangles in pixels
# Print a legend
for i, (name, color) in enumerate(zip(template_names, pastel_colors)):
scaled_font_scale = legend_scale * 3 # Adjust font scale based on legend scale
# Draw rectangle
cv.rectangle(coins_color, rect_top_left, rect_bottom_right, color=color, thickness=-1)
# Draw text
cv.putText(coins_color, name, (text_x, text_y), fontFace=cv.FONT_HERSHEY_SIMPLEX, fontScale=scaled_font_scale, colo
plt.figure(figsize=(15,15))
plt.imshow(X=coins_color)
plt.axis("off")
plt.show()
localhost:8888/lab/workspaces/auto-q 4/6
13/07/2024, 01:55 210218M_q06
In [6]: print(coordinates)
localhost:8888/lab/workspaces/auto-q 5/6
13/07/2024, 01:55 210218M_q06
[[ 166 2892 1]
[1002 2892 1]
[1074 572 2]
[1638 2016 0]
[1962 1092 2]
[1654 1316 0]
[1662 472 0]
[ 214 1044 2]
[ 294 328 3]
[ 526 2268 2]
[1090 1604 3]
[1794 2748 3]]
Calculating Price
In [7]: x = np.zeros(4)
for coord in coordinates:
x[coord[2]] += 1
print(f"Number of coins: {x[0]} Pennies, {x[1]} Nickels, {x[2]} Dimes, {x[3]} Quarters")
Number of coins: 3.0 Pennies, 2.0 Nickels, 4.0 Dimes, 3.0 Quarters
Assumptions
localhost:8888/lab/workspaces/auto-q 6/6