简体   繁体   English

如何在Matlab中通过颜色识别图像中的对象数量

[英]How to Count number Objects in an Image identify by Color in Matlab

I want to count number of Objects in an Image identify by each color. 我想对每种颜色标识的图像中的对象数进行计数。

Example: I want to count yellow rices or green rices in the image. 示例:我想在图像中计算黄米或绿米。

在此处输入图片说明

Would you please give me some tip to do it? 你能给我一些小费吗?

You've taken this image from the rice demonstration that ships with MATLAB's Image Processing Toolbox . 您已经从MATLAB的Image Processing Toolbox附带的大米演示中拍摄了这张图像。 If you read through the demo, you already have some code that will isolate each rice grain individually - that's how the image was created. 如果您仔细阅读了该演示,则已经有一些代码可以单独隔离每个米粒-这就是创建图像的方式。

As for counting the number of grains that have a particular color such as yellow or green, again read through the demo: it's clear that the rice grains are not colored with a small set of discrete colors such as yellow or green (or orange or pink). 关于计数具有特定颜色(例如黄色或绿色)的谷物的数量,请再次阅读该演示:显然,米粒未使用少量离散颜色进行着色,例如黄色或绿色(或橙色或粉红色) )。 Rather, the rice grain colors have been specifically created so that they are equally spaced throughout the colormap spring . 相反,米粒颜色是专门创建的,因此在整个色图spring中它们的间距是相等的。

So to proceed with your task you're going to have to provide some definition of 'yellow' or 'green', perhaps in terms of being within a particular range of RGB values. 因此,要继续执行您的任务,您将必须提供“黄色”或“绿色”的一些定义,也许是在RGB值的特定范围内。

Having done this, you can then use the variable labeled (that is constructed for you in the demo) together with the regionprops command, to give you a list of the pixels that are within each rice grain boundary. 完成此操作后,您可以将带有labeled的变量(在演示中为您构造)与regionprops命令一起使用,从而为您提供每个米粒边界内的像素列表。 Simply compare those pixels to your definitions of yellow or green, and you're there. 只需将这些像素与您对黄色或绿色的定义进行比较,就可以了。

assuming you have an image in matrix a (sized m*n*3 ), and you want to find the number of objects with color [r,g,b] . 假设您有一个矩阵a (大小为m*n*3 )中的图像,并且您要查找颜色为[r,g,b]的对象的数量。

first, select only pixels with the correct color: 首先,仅选择颜色正确的像素:

bb = (a(:,:,1) == r & a(:,:,2) == g &a(:,:,3) == b);

Than: 比:

[~,num] = bwlabel(bb,8)

num is the number of objects. num是对象数。

This is best done by working in a different colorspace than the RGB one (think HSV, L a b*, ..). 最好通过在与RGB颜色空间不同的颜色空间中工作(例如HSV,L a b *,..)来完成此操作。

Steve Eddins posted a series of articles showing an example how to segment objects of certain color from an image (green M&Ms in his case): 史蒂夫·埃丁斯(Steve Eddins)发布了一系列文章,展示了如何从图像中分割某些颜色的对象的示例(在此例中为绿色M&M):

彩信

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM