使用 Matlab 在对象上绘制矩形

发布于 2024-10-30 23:45:34 字数 112 浏览 0 评论 0原文

我有一个包含硬币图像的图像。硬币的形状可以是矩形、正方形、圆形、椭圆形等。我想在硬币上画一个矩形,并将硬币从背景中分割出来。我无法给出矩形的 x 或 y 值,因为硬币可能位于图像中的任何位置。有谁知道该怎么做?

I have an image which contains a coin image. Shape of the coin may be rectangle, square, circle, oval and etc. I want to draw a rectangle over the coin and segment the coin from it's background. I can't give x or y values of the rectangle, since coin may be in anywhere in the image. Does anyone know how to do this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

朱染 2024-11-06 23:45:34

我下面的回答假设您已经识别了硬币,并且您有一个可以使用的干净图像(二进制会很好)。

coin=load('penny.mat'); %#load matlab's stock image
img=zeros(256,256);
img(65:192,65:192)=coin.P;%# this is an approximation to the sort of image that I think you have

在此处输入图像描述

现在我们需要图像的范围才能知道边界矩形的大小。由于数组在有图像的地方不为零,而在其他地方为零,因此下面给出了边的长度。

sideX=sum(sum(img,1)>0);
sideY=sum(sum(img,2)>0);

使用 kmeans 查找图像的质心。

[indX,indY]=ind2sub(size(img),find(img(:)>0));
[~,centroid]=kmeans([indX,indY],1);

现在最后将矩形覆盖在图像的顶部。

imagesc(img);colormap(gray);hold on
rectangle('Position',([centroid,sideX,sideY]-[sideX,sideY,0,0]/2),'EdgeColor','w');hold off

结果:

在此处输入图像描述

如果您有一个嘈杂的图像(即图像外部不是均匀为零,那么您' d 必须设置一个阈值来找到边界框)

My answer below assumes that you have already identified the coin and you have a cleaned image (binary would be nice) that you can work with.

coin=load('penny.mat'); %#load matlab's stock image
img=zeros(256,256);
img(65:192,65:192)=coin.P;%# this is an approximation to the sort of image that I think you have

enter image description here

Now we need the extents of the image in order to know the size of the bounding rectangle. Since the array is non-zero where there's an image and zero elsewhere, the following gives the length of the sides.

sideX=sum(sum(img,1)>0);
sideY=sum(sum(img,2)>0);

Find the centroid of the image using kmeans.

[indX,indY]=ind2sub(size(img),find(img(:)>0));
[~,centroid]=kmeans([indX,indY],1);

Now finally overlay the rectangle on top of the image.

imagesc(img);colormap(gray);hold on
rectangle('Position',([centroid,sideX,sideY]-[sideX,sideY,0,0]/2),'EdgeColor','w');hold off

Result:

enter image description here

If you have a noisy image (i.e., it's not uniformly zero outside the image, then you'd have to set a threshold to find the bounding box)

近箐 2024-11-06 23:45:34

首先阅读 MathWorks 有关模式识别的建议 。具体如何解决问题取决于很多因素,例如

  • 图像中除了硬币之外还有什么?

  • 所有硬币的颜色都是相同的还是不同的颜色?

  • 您是否有已识别硬币位置的图像训练集?

Start by reading the MathWorks advice on pattern recognition. Exactly how you solve the problem depends on many things, for example

  • what else is in the image apart from a coin?

  • are all the coins the same colour or different colours?

  • do you have a training set of images that with identified coin locations?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文