使用 MATLAB 从图像中识别不同的硬币面值

2024-04-03

I'm trying to identify the number of matches and coins of each value in a picture using MATLAB. Here is the starting picture, with matches and 4 different coin values. (5 small silver, 2 small gold, 2 big silver, 4 big gold coins)

The output: Here's the code:

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

问:如何辨别不同硬币的面值?

(在这里询问如何分离两个连接的对象:使用 MATLAB 分离图像中两个重叠的圆 https://stackoverflow.com/questions/26906928/separate-two-overlapping-circles-in-an-image-using-matlab )

Thanks


First, regionprops 'BoundingBox',我使用从起始图片中剪出了硬币的图片imcrop以及已识别硬币的 BoundingBox。

然后,使用imfindcircles我可以检测到银色硬币上的孔洞。最后,我根据硬币的面积来确定硬币的价值。

最终代码:

close all;
img = (imread('C:\Users\Torstein\Jottacloud\Skole\Visu\Prosjekt\sample_images\sample1.jpg'));
%figure, imshow(img);
img_gray = rgb2gray(img);

% img_hsv = rgb2hsv(img); 
% imgv = img_hsv(:,:,3);
% [Gx, Gy] = imgradientxy(imgv);
% [Gmag, Gdir] = imgradient(Gx, Gy);
% Gmag could be useful

% 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 stuff touching the image border 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); % Remove small objects (noise)
%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','Image','BoundingBox');
threshold = 0.80; % For differentiating coins from matches based on an objects circularity

coinCentroids = [];
coinTypes = []; % 0 for Silver, 1 for Gold
coinValues = []; % 1, 5, 10 eller 20 kroning
coinAreas = [];
silverCoinAreas = [];
goldCoinAreas = [];
matchCentroids = [];
matchAngles = [];
radiusRange = [8,40];

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
        coinValues = [coinValues; 0];
        coinAreas = [coinAreas; area];
        coinCentroids = [coinCentroids; centroid];
        bbox = stats(k).BoundingBox;
        im = imcrop(img,bbox);
        %figure, imshow(im);
        [centers,radii] = imfindcircles(im,radiusRange,'ObjectPolarity','bright');
        %viscircles(centers,radii);
        if length(centers) > 0
            % Coin has a hole, therefore either 1-kroning or 5-kroning
            coinTypes = [coinTypes; 0];
            silverCoinAreas = [silverCoinAreas; area];

        else
            % Coin does not have hole, therefore either 10-kroning or
            % 20-kroning
            coinTypes = [coinTypes; 1];
            goldCoinAreas = [goldCoinAreas; area];
        end

    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

goldThreshold = 0.1;
silverThreshold = 0.1;
maxSilver = max(silverCoinAreas);
maxGold = max(goldCoinAreas);
for k=1:length(coinTypes)
    area = coinAreas(k);
    if coinTypes(k) == 0
        if  area >= maxSilver-maxSilver*silverThreshold
            % 5-kroning
            coinValues(k) = 5;
        else
            % 1-kroning
            coinValues(k) = 1;
        end
    else
        if area >= maxGold-maxGold*goldThreshold
            % 20-kroning
            coinValues(k) = 20;
        else
            % 10-kroning
            coinValues(k) = 10;
        end
    end
end

% OUTPUT:
coinCentroids
coinValues
matchCentroids
matchAngles

Thanks

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

使用 MATLAB 从图像中识别不同的硬币面值 的相关文章

  • 直方图均衡结果

    I am trying to code histogram equalization by my self but the results are different from the built in function in matlab
  • 如何去除给定图像中的噪声,使 ocr 输出完美?

    我已经对这个孟加拉文本图像进行了大津阈值处理 并使用 tesseract 进行 OCR 但输出非常糟糕 我应该应用什么预处理来消除噪音 我也想校正图像 因为它有轻微的倾斜 我的代码如下 import tesserocr from PIL i
  • 如何在向量中的所有点之间绘制线?

    我有一个包含二维空间中一些点的向量 我希望 MATLAB 用从每个点到每个其他点绘制的线来绘制这些点 基本上 我想要一个所有顶点都连接的图 你能用情节来做到这一点吗 如果可以 怎么做 一种解决方案是使用该函数为每个点组合创建一组索引MESH
  • 在 matlab 代码中使用 dll 文件

    我需要使用 Matlab 中由 dll 文件定义的函数 我有一个例子 那个家伙将 dll 转换为 mexw32 文件 但我知道我是如何做到这一点的 我尝试使用加载库但它没有创建任何文件 我怎样才能做到这一点 loadlibrary http
  • Matlab 一个图上有多个图例 2014b

    我想在一个地块上有多个传说 该解决方案在 2014b 版本之前完美运行 我试图弄清楚如何使用手柄优雅地制作它 但到目前为止还没有成功 欢迎任何想法 2013b 的示例 x 1 50 y1 sin x 2 y2 cos x 2 f figur
  • 从包含带边框的表格的图像中提取表格结构

    我正在尝试提取下表中的单元格位置 应用自适应阈值处理后 我能够获得细胞位置周围的轮廓 并且 HoughLines 获得垂直和水平结构元素 这是我的代码 img cv2 imread os path join img path file im
  • MATLAB 编译器与 MATLAB 编码器

    两者有什么区别 据我了解 MATLAB Compiler将MATLAB代码包装成 exe文件 这样就可以在不安装MATLAB的情况下使用它 并且只需要MCR 除此之外 MATLAB Builder NE 还可以用于生成与 Net 框架一起使
  • 如何使用 opencv python 计算乐高积木上的孔数?

    我正在开发我的 python 项目 我需要计算每个乐高积木组件中有多少个孔 我将从输入 json 文件中获取有关需要计算哪个程序集的信息 如下所示 img 001 red 0 blue 2 white 1 grey 1 yellow 1 r
  • matlab中无限while嵌套在for循环中

    我想做一个while循环 嵌套在for在 Matlab 中循环以查找数据中不同对之间的距离 我的数据具有以下形式 ID lon lat time 1 33 56 40 89 803 2 32 45 41 03 803 3 35 78 39
  • 低质量相机的模糊内核

    我正在做一些图像增强实验 所以我用我的廉价相机拍照 相机有马赛克伪像 所有图像看起来都像网格 我认为药盒 失焦 内核和高斯内核不是最佳候选 有什么建议么 EDIT Sample 我怀疑这不能通过恒定的内核来完成 因为对像素的影响并不相同 因
  • matlab中更快的插值方法

    我正在使用 interp1 来插值一些数据 temp 4 30 4 rand 365 10 depth 1 10 dz 0 5 define new depth interval bthD min depth dz max depth ne
  • 如何在 MATLAB 编译的应用程序中运行外部 .m 代码? [复制]

    这个问题在这里已经有答案了 我有一个 MATLAB 项目 我使用 MCC 对其进行编译以获得单个可执行文件 然后我想知道外部程序员是否可以在 exe 中执行他的一些 m 文件 而无需重新编译整个项目 重点是提供一个应用程序 其他开发人员可以
  • 使用 ruby​​ 调整动画 GIF 图像的大小?

    我正在尝试将 GIF 图像调整为不同的尺寸 我在 ruby 中使用 RMagick 库 但对于某些 gif 图像 即使我缩小 GIF 的大小 文件大小似乎也会增加 我正在以相同的纵横比调整图像图像的大小 这是我的代码 require rma
  • 照片马赛克算法。如何在给定基本图像和瓷砖列表的情况下创建马赛克照片?

    Hy 我要做的是创建一个程序 使用 C 或 C 它将 24 位 像素位图和图像集合作为输入 我必须创建一个马赛克图像 类似于使用库的输入图像给定的图像 创建与输入类似的马赛克照片 到目前为止 我可以访问输入的图像像素及其颜色 但我有点卡住了
  • 通过 cuFFT 进行逆 FFT 缩放

    每当我使用 cuFFT 绘制程序获得的值并将结果与 Matlab 的结果进行比较时 我都会得到相同形状的图形 并且最大值和最小值位于相同的点 然而 cuFFT 得到的值比 Matlab 得到的值大得多 Matlab代码是 fs 1000 s
  • 如何选择面积最大的对象?

    我用过bwconvhull检测图像的某个部分 正如您在图像中看到的那样 有许多具有特定质心的对象 我想做的是检测面积最大的物体 左起第一个大物体 并忽略其他物体 我应该遵循哪种方法 我将非常感谢您的帮助 以下是代码 由于我仍在努力 所以写得
  • 在 Matlab 中保存 Kinect 深度图像?

    通过使用 Kinect 我可以获得深度图像 其中每个深度图像像素存储相机和物体之间的距离 以毫米为单位 现在我想保存它们以便以后使用 最好的推荐是什么 我正在考虑将深度图像保存为图像 jpg png等 然而 该值通常是从50毫米到10000
  • matlab 中的动画绘图

    我正在尝试创建一个三角形的动画图 最终结果应该是十个三角形 后面跟着两个更大的三角形 后面跟着一条直线 使用matlab文档 https de mathworks com help matlab ref drawnow html 我最终得到
  • 如何使用 zbar 获取图像上检测到的二维码的 x、y 位置?

    我在下图的两个二维码中编码了数字1639 可下载 here https i stack imgur com c0FVK jpg 我打印了它 拍了一张照片并尝试检测它 import zbar from PIL import Image sca
  • 通过颜色渐变修补圆

    我正在尝试绘制一个颜色渐变 我希望它沿轴均匀 在下图由角度定义的情况下 pi 7 当我使用patch命令 绘图与所需的梯度方向匹配 但沿其方向并不均匀 沿圆的点之间形成各种三角形 这是代码 N 120 theta linspace pi p

随机推荐

  • Java中的Goto语句[关闭]

    Closed 这个问题需要细节或清晰度 help closed questions 目前不接受答案 我执行了下面的代码Eclipse http en wikipedia org wiki Eclipse 28software 29 但是GO
  • 人眼注视检测:识别用户正在看板上的位置[关闭]

    Closed 这个问题是无法重现或由拼写错误引起 help closed questions 目前不接受答案 我正在开发一个项目 上面有板和相机 目标是识别正在看黑板的学生 并确定他们视线的位置 在黑板上 目前 我计划从以下几个方面来应对挑
  • 如何关闭 ASP.NET 必填字段验证器“失去焦点”行为

    我有一些代码 其中一个控件需要两个单独的必填字段验证器 两个验证器都位于单独的验证组中 然后由两个单独的按钮进行验证 当单击按钮时 这种方法效果很好 但如果我在文本框中输入一个值然后将其删除 两个验证器都会显示 有没有办法关闭这种 失去焦点
  • boto s3 Bucket 与 get_bucket

    我尝试访问存储桶内的密钥 尽管我有该密钥的权限 但我没有该权限 为了能够做到get key this is my key 我需要桶对象 conn boto connect s3 key secret key my bucket conn g
  • 实体框架 4 的 System.Reflection.ReflectionTypeLoadException

    我在 Windows 窗体应用程序中使用 EF4 每当我在未安装 Visual Studio 2010 的计算机中运行发布文件时 我都会遇到问题 我总是收到此错误 System Reflection ReflectionTypeLoadEx
  • 如何修改Makefile以支持交叉编译?

    我有以下 Makefile CC g top srcdir SRC DIR cpp src INCLUDES I top srcdir I top srcdir command classes I top srcdir platform I
  • 从 URL 加载 UITableViewCell 的图像(需要异步加载)

    我有一个自定义类 它可以解析 XML 并获取图像的 URL 字符串 我将其存储在数组中 然后我想检索这些字符串来加载图像并将每个字符串显示在 UITableViewCell 中 这是我的代码 UITableViewCell tableVie
  • 禁用 yum 事务检查文件冲突

    如何禁用文件的 yum 事务检查 Transaction check error file usr local xenco backend current from install of xenco rr 1 9 6 104 x86 64
  • Ruby:对象深复制

    我正在研究一些在 Ruby 中深度复制对象的技术 MRI 1 9 3 我遇到了以下示例 但我不确定 dup方法实施 我测试了它并且它确实有效 但是我不理解该方法的逻辑步骤 因此我不舒服在我自己的代码中使用它 是声明 name name du
  • 使用 Python Flask 运行 REST API 版本的最佳方法是什么 [关闭]

    Closed 这个问题是基于意见的 help closed questions 目前不接受答案 我正在 Python Flask 中创建 REST API 想知道创建引用特定 git 标签的版本控制 API 的选项有哪些 我想要做的是指定
  • 结构数组初始化中的 MISRA-C 错误

    我有以下内容 typedef struct uint8 t BlockID uint32 t Copies uint16 t Size NVMM ConfigType const NVMM ConfigType NvmmCnf Layout
  • 交互式 Python:尽管正确导入了 line_profiler,但无法使 `%lprun` 工作

    Problem 大多数 iPython 神奇函数 对我来说立刻就可以正常工作 hist time prun等等 但是 我注意到 lprun无法使用 iPython 找到它 因为我最初安装了它 尝试解决 然后我发现我应该安装line prof
  • Google diff-match-patch:如何取消修补以获取原始字符串?

    我正在使用 Google diff match patch JAVA 插件在两个 JSON 字符串之间创建补丁并将补丁存储到数据库中 diff match patch dmp new diff match patch LinkedList
  • wait() 和yield() 之间的区别

    到目前为止 我对wait 和yield 方法的理解是 当线程没有执行任何任务并让CPU执行其他线程时 会调用yield wait 当某个线程被搁置时使用 通常用于同步的概念 但是 我无法理解它们功能的差异 并且不确定我的理解是对还是错 有人
  • 在模拟器中运行时,libgdx 在 helloworld 应用程序上抛出异常

    我已经按照教程进行了操作并使用 libgdx 库创建了一个简单的应用程序 该应用程序具有红色背景 当我将其作为独立的桌面应用程序运行时 效果很好 但是当我运行 android 版本时 会引发以下异常 并且应用程序无法启动 02 04 18
  • 优化 2 个字节数组的求和

    我正在迭代一个字节数组 并在 for 循环中添加另一个字节数组的值 var random new Random byte bytes new byte 20 000 000 byte bytes2 new byte 20 000 000 f
  • iphone sdk 支持从 rtmp 流播放 mp4 吗?

    iphone sdk支持播放RTMP流中的mp4文件吗 我希望将文件存储在 CloudFront 中 在标记为流式传输的存储桶中 并希望 iPhone 应用程序能够播放它们 这可能吗 还是我最好将文件存储为 CloudFront 上的 mp
  • 嵌套布尔查询?

    我正在使用 BooleanQuery 来组合多个查询 我发现如果我向 BooleanQuery 添加 BooleanQuery 则不会返回任何结果 添加的 BooleanQuery 是一个 MUST NOT 查询 例如 city id 10
  • PHP curl 方法可以在同一请求中上传文件和带有前导 @ 字符的字符串吗?

    我想使用 PHP curl 方法构造一个 POST 请求 该请求执行以下操作 上传文件 因此请求必须作为 multipart form data 提交 发送一串文本 该字符串以 字符开头 例如 以下代码有效 因为文本字符串中没有前导 字符
  • 使用 MATLAB 从图像中识别不同的硬币面值

    I m trying to identify the number of matches and coins of each value in a picture using MATLAB Here is the starting pict