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

Coin Matching Image Processing

Coin matching using image processing
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Coin Matching Image Processing

Coin matching using image processing
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

13/07/2024, 01:55 210218M_q06

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

#Display the Images


plt.figure(figsize=(10,10))
plt.subplot(1, 5, 1)
plt.imshow(coins, cmap='gray')
plt.title('Coins')
plt.axis('off')

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

Out[2]: (-0.5, 299.5, 299.5, -0.5)

Visualizing matched plots


In [3]: p_resp = cv.matchTemplate(coins, p, cv.TM_CCOEFF_NORMED) #correlation
n_resp = cv.matchTemplate(coins, n, cv.TM_CCOEFF_NORMED) #TM_CCOEFF_NORMED is used to normalize the image
d_resp = cv.matchTemplate(coins, d, cv.TM_CCOEFF_NORMED)
q_resp = cv.matchTemplate(coins, q, cv.TM_CCOEFF_NORMED)

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

Out[3]: (-0.5, 3208.5, 2180.5, -0.5)

In [4]: print('shape before stacked',p_resp.shape)


stacked = np.stack(arrays=[p_resp, n_resp, d_resp, q_resp], axis=2)
print('shape after stacked',stacked.shape)

shape before stacked (2181, 3209)


shape after stacked (2181, 3209, 4)

In [5]: pastel_colors = [
(255, 182, 193), # Pastel Pink
(173, 216, 230), # Pastel Blue
(119, 221, 119), # Pastel Green
(253, 253, 150) # Pastel Yellow
]

# Make a color image to draw on


coins_color = cv.cvtColor(coins, cv.COLOR_GRAY2BGR)

# Find the local maxima in the stacked responses


coordinates = peak_local_max(stacked, exclude_border=0, min_distance=5, threshold_abs=0.8)

localhost:8888/lab/workspaces/auto-q 3/6
13/07/2024, 01:55 210218M_q06

# Plot on each color


for coord in coordinates:
y, x, c = coord
template_shape = [p.shape, n.shape, d.shape, q.shape][c]
cv.rectangle(coins_color, (x, y), (x + template_shape[1], y + template_shape[0]), pastel_colors[c], thickness=12)

template_names = ["Penny", "Nickel", "Dime", "Quarter"]

legend_scale = 0.8
rectangle_size = 100 # Width and height of the rectangles in pixels

# Calculate the total height occupied by the legend


legend_height = int(len(template_names) * rectangle_size * legend_scale) # Adjusted legend height based on scale

# Calculate the starting y-coordinate to vertically center the legend


start_y = (coins_color.shape[0] - legend_height) // 2

# 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

# Calculate rectangle coordinates


rect_top_left = (10, start_y + int(rectangle_size * legend_scale * i))
rect_bottom_right = (10 + int(rectangle_size * legend_scale), start_y + int(rectangle_size * legend_scale + rectang

# Draw rectangle
cv.rectangle(coins_color, rect_top_left, rect_bottom_right, color=color, thickness=-1)

# Calculate text position


text_x = 30 + int(rectangle_size * legend_scale)
text_y = start_y + int(rectangle_size * legend_scale + 0.5 * rectangle_size * legend_scale * (2 * i + 1)) - int(rec

# 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

Minimum value that maxima can have is 0.8


No overlapping coins(Coins are sufficiently spaced)
Size of each image matches with size of that coin in main image

localhost:8888/lab/workspaces/auto-q 6/6

You might also like