基于matlab的硬币计数代码和实验结果
clear all;
clc;
RGB = imread('yb.png');
I = rgb2gray(RGB);
subplot(231),imshow(I)
% »Ò¶ÈͼÏñ¶þÖµ»¯£¬È«¾ÖãÐÖµ·Ö¸î×î´ó»¯Àà¼ä·½²î
level = graythresh(I);
BW = im2bw(I,level);
subplot(232),imshow(BW)
% ¿×¶´Ìî³äºÍÐÎ̬ѧ¿ªÔËËã
BW1 = imfill(BW,'holes');
subplot(233),imshow(BW1)
se1 = strel('square',10);
BW2 = imopen(BW1,se1);
subplot(234),imshow(BW2)
% ÐÎ̬ѧ¸¯Ê´ÔËË㣬²¿·ÖÄ¿±êÎïÓÐÕ³Á¬ÏÖÏó£¬È¥³ýÕ³Á¬
se2 = strel('disk',15);
BW3 = imerode(BW2,se2);
subplot(235),imshow(BW3)
% ÐÎ̬ѧ¿ªÔËË㣬ȥ³ýÓ°Ïì¼ÆÊýµÄ¸ÉÈÅ¿ÅÁ£
se3 = strel('disk',5);
BW4 = imopen(BW3,se3);
subplot(236),imshow(BW4)
[L,N] = bwlabel(BW4); % N¼´ÎªÄ¿±ê¸öÊý
% ±ê¼ÇÄ¿±êÎï
figure,imshow(RGB)
hold on
for k = 1:N
[r,c] = find(L == k);
rbar = mean(r);
cbar = mean(c);
plot(cbar,rbar,'marker','*','markeredgecolor','b','markersize',10); end
% ¶Ô»°¿òÏÔʾĿ±êÎï¸öÊý
h = dialog('Name','Ä¿±ê¸öÊý','position',[500 500 200 70]); %
´´½¨Ò»¸ö¶Ô»°¿ò´°¿Ú
uicontrol('Style','text','units','pixels','position',[45 40 120 20],... 'fontsize',15,'parent',h,'string',num2str(N)); % ´´½¨Îı¾ÄÚÈÝ uicontrol('units','pixels','position',[80 10 50 20],'fontsize',10,... 'parent',h,'string','È·¶¨','callback','delete(gcf)'); %
´´½¨¡¾È·¶¨¡¿°´Å¥