Matlab的bwlabel,regionprops& amp;质心函数 [英] Explanation of Matlab's bwlabel,regionprops & centroid functions

查看:949
本文介绍了Matlab的bwlabel,regionprops& amp;质心函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我花了一整天时间阅读上述MATLAB函数。即使在MathWorks网站上,我也无法在网上找到任何好的解释!

I have spent all day reading up on the above MATLAB functions. I can't seem to find any good explanations online, even on the MathWorks website!

如果有人能解释 bwlabel regionprops 质心。如果应用于灰度图像,它们如何工作?

I would be very grateful if anyone could explain bwlabel, regionprops and centroid. How do they work if applied to a grayscale image?

具体来说,它们在下面的代码中使用。以上功能如何应用于以下代码?

Specifically, they are being used in this code below. How do the above functions apply to the code below?

fun=@minutie; L = nlfilter(K,[3 3],fun); 
%% Termination LTerm=(L==1); 
figure; imshow(LTerm) 
LTermLab=bwlabel(LTerm); 
propTerm=regionprops(LTermLab,'Centroid'); 
CentroidTerm=round(cat(1,LTerm(:).Centroid)); 
figure; imshow(~K) 
set(gcf,'position',[1 1 600 600]); hold on 
plot(CentroidTerm(:,1),CentroidTerm(:,2),'ro') 


推荐答案

这很难解释!......然而,我很乐意向你解释。但是,我有点惊讶你无法理解MathWorks的文档。它实际上很好地解释了他们的功能(如果不是全部......)。

That's quite a mouthful to explain!... nevertheless, I'd love to explain it to you. However, I'm a bit surprised that you couldn't understand the documentation from MathWorks. It's actually quite good at explaining a lot (if not all...) of their functions.

BTW, bwlabel regionprops 未定义用于灰度图像。您只能将这些应用于二进制图像

BTW, bwlabel and regionprops are not defined for grayscale images. You can only apply these to binary images.

更新: bwlabel 仍然有接受二进制图片但<$的限制c $ c> regionprops 不再有此限制。它还可以采用标签矩阵,该矩阵通常从 bwlabel 以及二进制图像输出。

Update: bwlabel still has the restriction of accepting a binary image but regionprops no longer has this restriction. It can also take in a label matrix that is usually output from bwlabel as well as binary images.

假设二进制图像是你想要的,我对每个函数的解释如下。

Assuming binary images is what you want, my explanations for each function is as follows.

bwlabel 接收二进制图像。此二进制图像应包含一组彼此分开的对象。属于对象的像素用 1 / true 表示,而那些作为背景的像素是 0 / false 。例如,假设我们有一个如下所示的二进制图像:

bwlabel takes in a binary image. This binary image should contain a bunch of objects that are separated from each other. Pixels that belong to an object are denoted with 1 / true while those pixels that are the background are 0 / false. For example, suppose we have a binary image that looks like this:

0  0  0  0  0  1  1  1  0  0
0  1  0  1  0  0  1  1  0  0
0  1  1  1  0  0  0  0  0  0
0  0  0  0  0  0  0  0  0  1
0  0  0  0  0  0  0  0  1  1
0  0  1  1  1  1  0  0  1  1

你可以看到在此图像中,此图像中有四个对象。对象的定义是那些 1 的像素,它们通过查看本地邻域链接在一起。我们通常会查看8像素的社区,您可以看到北,东北,东,东南,南,西南,西,西北方向。另一种说法是对象 8-connected 。为简单起见,有时候人们会看到4像素的社区,在那里您可以看到北,东,南和西方向。这个woudl意味着对象 4-connected

You can see in this image that there are four objects in this image. The definition of an object are those pixels that are 1 that are connected in a chain by looking at local neighbourhoods. We usually look at 8-pixel neighbourhoods where you look at the North, Northeast, East, Southeast, South, Southwest, West, Northwest directions. Another way of saying this is that the objects are 8-connected. For simplicity, sometimes people look at 4-pixel neighbourhoods, where you just look at the North, East, South and West directions. This woudl mean that the objects are 4-connected.

bwlabel 的输出将为您提供一个整数映射,其中每个对象分配一个唯一ID 即可。因此, bwlabel 的输出将如下所示:

The output of bwlabel will give you an integer map where each object is assigned a unique ID. As such, the output of bwlabel would look something like this:

0  0  0  0  0  3  3  3  0  0
0  1  0  1  0  0  3  3  0  0
0  1  1  1  0  0  0  0  0  0
0  0  0  0  0  0  0  0  0  4
0  0  0  0  0  0  0  0  4  4
0  0  2  2  2  2  0  0  4  4

因为MATLAB处理专业列中的内容,所以标签就是你在上面看到的原因。因此, bwlabel 为您提供每个像素的成员资格。这会告诉您每个像素属于哪个属于它的对象。此地图中的 0 对应于背景。要拨打 bwlabel ,您可以执行以下操作:

Because MATLAB processes things in column major, that's why the labelling is how you see above. As such, bwlabel gives you the membership of each pixel. This tells you where each pixel belongs to if it falls on an object. 0 in this map corresponds to the background. To call bwlabel, you can do:

L = bwlabel(img);

img 将是您的二进制图像提供给函数和 L 是我刚才谈到的整数映射。此外,您可以向 bwlabel 提供2个输出,第二个参数告诉您图像中存在多少个对象。因此:

img would be the binary image that you supply to the function and L is the integer map I just talked about. Additionally, you can provide 2 outputs to bwlabel, and the second parameter tells you how many objects exist in the image. As such:

[L, num] = bwlabel(img);

上面的例子中, num 将是4.作为另一种调用方法,您可以指定要检查的连接像素邻域,因此您可以这样做:

With our above example, num would be 4. As another method of invocation, you can specify the connected pixel neighbourhoods you would examine, and so you can do this:

[L, num] = bwlabel(img, N);

N 将是您想要的像素邻域检查(即4或8)。

N would be the pixel neighbourhood you want to examine (i.e. 4 or 8).

regionprops 是我每天使用的非常有用的功能。 regionprops 测量黑白图像中的各种图像数量和特征。具体而言,给定黑白图像,它自动确定8连接的每个连续白色区域的属性。其中一个特殊属性是质心。这也是质量中心。您可以将此视为对象的中间。这将是(x,y)每个对象中间位置的位置。因此, regionprops Centroid 的工作原理是,对于图像中看到的每个对象,这将计算对象的质心和 regionprops 的输出将返回一个结构,其中此结构的每个元素都会告诉您黑色和每个对象的质心是什么白色图像。 Centroid 只是其中一个属性。还有其他有用的功能,但我假设你不想这样做。要调用 regionprops ,您可以这样做:

regionprops is a very useful function that I use daily. regionprops measures a variety of image quantities and features in a black and white image. Specifically, given a black and white image it automatically determines the properties of each contiguous white region that is 8-connected. One of these particular properties is the centroid. This is also the centre of mass. You can think of this as the "middle" of the object. This would be the (x,y) locations of where the middle of each object is located. As such, the Centroid for regionprops works such that for each object that is seen in your image, this would calculate the centre of mass for the object and the output of regionprops would return a structure where each element of this structure would tell you what the centroid is for each of the objects in your black and white image. Centroid is just one of the properties. There are other useful features as well, but I'm assuming you don't want to do this. To call regionprops, you would do this:

s = regionprops(img, 'Centroid');

上面的代码将计算图像中每个对象的质心。您可以为 regionprops 指定其他标志,以指定所需的每个功能。我非常鼓励您查看 regionprops 可以计算的所有可能功能,因为有许多功能可用于各种不同的应用程序和情况。

The above code will calculate the centroids of each of your objects in the image. You can specify additional flags to regionprops to specify each feature that you want. I do highly encourage that you take a look at all of the possible features that regionprops can calculate, as there are many that are useful in a variety of different applications and situations.

此外,通过省略任何标记作为函数的输入,您将默认计算图像中所有的功能。因此,如果我们在MATLAB中声明上面看到的图像,那么在运行 regionprops 之后会发生这种情况。之后,让我们计算出质心是什么:

Also, by omitting any flags as input into the function, you would calculate all of the features in your image by default. Therefore, if we were to declare the image that we have seen above in MATLAB, this is what would happen after I run regionprops. After, let's calculate what the centroids are:

img = logical(...
   [0  0  0  0  0  1  1  1  0  0;
    0  1  0  1  0  0  1  1  0  0;
    0  1  1  1  0  0  0  0  0  0;
    0  0  0  0  0  0  0  0  0  1;
    0  0  0  0  0  0  0  0  1  1;
    0  0  1  1  1  1  0  0  1  1]);
s = regionprops(img, 'Centroid');

...最后当我们显示质心时:

... and finally when we display the centroids:

>> disp(cat(1,s.Centroid))

3.0000    2.6000
4.5000    6.0000
7.2000    1.4000
9.6000    5.2000

因此,第一个质心位于(x,y)=(3,2.6),下一个质心位于(x,y)=(4.5,6),依此类推。请特别注意 x 坐标是,而 y 是纵坐标是

As such, the first centroid is located at (x,y) = (3, 2.6), the next centroid is located at (x,y) = (4.5, 6) and so on. Take special note that the x co-ordinate is the column while the y co-ordinate is the row.

希望这是明确的!

这篇关于Matlab的bwlabel,regionprops&amp; amp;质心函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆