Question: I need help writing MATLAB code that will detect and identify coins from the image below. Here's the code I have so far % Load

I need help writing MATLAB code that will detect and identify coins from the image below. Here's the code I have so far % Load the image
imgPath ='C:\Users\Nezuko\Downloads\IMG_4541.jpg';
img = imread(imgPath);
figure; imshow(img); title('Original Image');
% Convert to grayscale and enhance contrast
gray_img = rgb2gray(img);
filtered_img = medfilt2(gray_img, [33]);
contrast_img = adapthisteq(filtered_img);
figure; imshow(contrast_img); title('Contrast Enhanced Image After Preprocessing');
% Detect potential coin circles with optimized parameters
sensitivity =0.95; % Increased sensitivity for weaker edges
edgeThreshold =0.01; % Lower threshold to include more potential edges
[centers, radii, metric]= imfindcircles(contrast_img, [1832], 'ObjectPolarity', 'bright', 'Sensitivity', sensitivity, 'EdgeThreshold', edgeThreshold);
figure; imshow(contrast_img); viscircles(centers, radii, 'EdgeColor', 'b'); title('Detected Circles Before Filtering');
% Classify and label each detected coin based on its radius
known_positions =[]; % Store positions of identified coins
labels = cell(length(radii),1);
for i =1:length(radii)
if any(pdist2(known_positions, centers(i,:))10)% Check if this coin has been processed
continue;
end
% Add position to known positions
known_positions =[known_positions; centers(i,:)];
% Coin classification based on radius
if radii(i)>=26 && radii(i)=32
labels{i}= 'Quarter';
elseif radii(i)>=23 && radii(i)=25
labels{i}= 'Nickel';
elseif radii(i)>=19 && radii(i)=22
labels{i}= 'Penny';
elseif radii(i)>=18 && radii(i)=18
labels{i}= 'Dime';
else
labels{i}= 'Unknown';
end
end
% Display the final image with labels
figure; imshow(img);
hold on;
for i =1:size(centers,1)
if ~isempty(labels{i})
text(centers(i,1), centers(i,2), labels{i}, 'Color', 'yellow', 'FontSize', 12, 'FontWeight', 'bold', 'HorizontalAlignment', 'center', 'BackgroundColor', 'black');
viscircles(centers(i,:), radii(i), 'EdgeColor', 'b');
end
end
title('Final Image with Coin Classification and Labeling');
hold off;
 I need help writing MATLAB code that will detect and identify

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!