【发布时间】:2014-11-11 00:00:17
【问题描述】:
我正在尝试使用 MATLAB 识别图片中每个值的匹配数和硬币数。 这是起始图片,带有火柴和 4 个不同的硬币值。 (5小银、2小金、2大银、4大金币)
close all;
img = (imread('C:\Users\Torstein\Jottacloud\Skole\Visu\Prosjekt\sample_images\sample2.jpg'));
img_gray = rgb2gray(img);
% Filter image for easier edge detection
m = 12;
n = 12;
img_filter = imfilter(img_gray, fspecial('average', [m n]));
%figure, imshow(f), title('f')
% Edge detection
[~, threshold] = edge(img_filter, 'canny');
fudgeFactor = 1.5;
img_edge = edge(img_filter, 'canny', threshold * fudgeFactor);
figure, imshow(img_edge), title('edge detection')
% Dilate image to make the coin edges complete without holes
se_disk = strel('disk',4);
se_line1 = strel('line',3,100);
se_line2 = strel('line',3,100);
img_dilated = imdilate(img_edge, se_disk);
img_dilated = imdilate(img_dilated, [se_line1 se_line2]);
figure, imshow(img_dilated), title('dilate')
% Remove small objects (noise) and fill complete objects
img_clearborder = imclearborder(img_dilated, 4);
%figure, imshow(BWclear), title('cleared border image');
img_fill = imfill(img_clearborder, 'holes');
figure, imshow(img_fill), title('fill holes')
% Erode image to make a clear cut between objects
se_diamond = strel('diamond',2);
img_erode = imerode(img_fill,se_diamond);
for k=1:3
img_erode = imerode(img_erode,se_diamond);
end
img_nosmall = bwareaopen(img_erode,300);
figure, imshow(img_nosmall), title('erode')
[B, L] = bwboundaries(img_nosmall);
figure, imshow(label2rgb(L, @jet, [.5 .5 .5])), title('boundaries')
hold on
for k = 1:length(B)
boundary = B{k};
plot(boundary(:,2), boundary(:,1), 'w', 'LineWidth', 2)
end
stats = regionprops(L,img(:,:,1),...
'Area','Centroid','Orientation','EquivDiameter','MeanIntensity');
threshold = 0.80; % For differentiating coins from matches based on an objects circularity
coinCentroids = [];
coinIntensities = [];
matchCentroids = [];
matchAngles = [];
coinRatios = [];
for k = 1:length(B)
boundary = B{k};
delta_sq = diff(boundary).^2;
perimeter = sum(sqrt(sum(delta_sq,2)));
area = stats(k).Area;
metric = 4*pi*area/perimeter^2;
metric_string = sprintf('%2.2f',metric);
angle_string = sprintf('%2.2f',stats(k).Orientation);
centroid = stats(k).Centroid;
if metric > threshold
% Object is round, therefore a coin
coinCentroids = [coinCentroids; centroid];
coinIntensities = [coinIntensities; stats(k).MeanIntensity];
coinRatios = [coinRatios; stats(k).EquivDiameter/area];
else
% Object is a match
angle = stats(k).Orientation;
matchCentroids = [matchCentroids; centroid];
matchAngles = [matchAngles; angle];
end
plot(centroid(1),centroid(2),'ko');
% text(boundary(1,2)-35,boundary(1,1)+13,angle_string,'Color','y',...
% 'FontSize',14,'FontWeight','bold');
end
如您所见,我已经确定了哪些对象是硬币,哪些对象是火柴。 但是,我很难确定硬币的价值。
例如,硬币的面积/直径给出以下结果。我看不到任何明确的方法来仅根据这些数据来区分不同类型的硬币;数字太接近了。
0.0041 0.0042 0.0043 0.0043 0.0044 0.0045 0.0048 0.0048 0.0053 0.0054 0.0055 0.0055 0.0056
我也尝试从每枚硬币的起始图片中获取平均颜色强度,但这并不能帮助我将银色硬币与金色硬币分开。
红色通道的平均强度没有给出有 6 个金色硬币和 6 个银色硬币的信息。
105.0104
105.4408
107.9070
112.4762
116.3412
127.3481
132.1418
137.9697
149.6601
159.2506
167.6910
181.1673
215.0395
问题:如何识别不同的硬币价值?
(在这里询问如何分离两个连接的对象:Separate two overlapping circles in an image using MATLAB)
谢谢
【问题讨论】:
-
我看不到图片。错误 403。
-
尝试
regionprops与属性'Image'应该会给你一个很好的分离 -
@tsom 做得很好。这是一个非常好的问题 - 表明您付出了很多努力并取得了进步。
-
@tos 另一个可能有帮助的量是每个物体的直径和面积之间的比率。
-
你很亲密,所以我不会提供答案。继续 Shai 所说的内容,请查看此帖子:mathworks.com/matlabcentral/answers/85363#answer_94853。有一个很好的公式可以计算物体的圆度。如果该值更接近 1,则它更接近圆形,而小于 1 则不太像圆形。使用
regionprops和公式中的参数计算圆形度,然后使用0.5之类的阈值提取圆形对象。该帖子还惊呼要小心使用偏心,如果您决定使用它
标签: matlab image-processing image-segmentation