skimage.measure.label

Overview

对于二值图像来说,每个像素点的值只有类似0/1的两种可能性,一般为0(黑)/255(白)。
若两个像素点位置相邻且取值相同,那么这两个像素点即处于同一个相互连通的区域内。

从视觉上看,彼此连通的电形成了一个区域,而该区域中所有连通点构成的集合,我们称之为连通区域
在图像中,每个像素当以自身为中心时,周围一般存在8个邻接像素。
在判断两个像素是否属于同一连通区域时,根据邻接关系,通常存在两种判定方法:4连通或8连通。
4连通只考虑4个邻接像素,即上下左右,如下左图所示;
8连通则总共考虑8个邻接像素,额外还包括了对角线位置的点,如下右图所示。

值得注意的是,二值连通区域还具有简单而特殊的传递性:
  若像素点A与B同值并邻接,我们称A与B连通,易得,
  若A与B连通,B与C连通,则A与C连通。

一个连通区域可能包含很多个像素点,而若将同一个连通区域的所有像素点都用同一个数值/符号来进行标记,这个过程就称为连通区域标记

Label

SciKit-Image是一种Python下的图像处理工具包,scikit-image — Image processing in Python:Official Site
在skimage包中,使用measure子模块下的label函数即可实现连通区域标记。
参数input表示需要处理的二值图像,connectivity表示判定连通的模式(1代表4连通,2代表8连通),输出labels为一个从0开始的标记数组。

skimage.measure.label(input, neighbors = None, background = None, return_num = False, connectivity = None)[source]**

Parameters:
@input : Image to label [ndarray of dtype int]
@neighbors : Deprecated, use @connectivity instead [{4, 8}, int, optional]
@background : Consider all pixels with this value as background pixels, and label them as 0. By default, 0-valued pixels are considered as background pixels. [int, optional]
@return_num : Whether to return the number of assigned labels [bool, optional]
@connectivity : Maximum number of orthogonal hops to consider a pixel/voxel as a neighbor. Accepted values are ranging from 1 to input.ndim. If None, a full connectivity of input.ndim is used. [int, optional]
Returns:
@labels : Labeled array, where all connected regions are assigned the same integer value. [ndarray of dtype int]
@num : Number of labels, which equals the maximum label index and is only returned if return_num is True. [int, optional]

Examples:

>>> import numpy as np
>>> x = np.eye(3).astype(int)
>>> print(x)
[[1 0 0]
 [0 1 0]
 [0 0 1]]
>>> print(label(x, connectivity = 1))
[[1 0 0]
 [0 2 0]
 [0 0 3]]
>>> print(label(x, connectivity = 2))
[[1 0 0]
 [0 1 0]
 [0 0 1]]
>>> print(label(x, background = -1))
[[1 2 2]
 [2 1 2]
 [2 2 1]]
>>> x = np.array([[1, 0, 0],
...               [1, 1, 5],
...               [0, 0, 0]])
>>> print(label(x))
[[1 0 0]
 [1 1 2]
 [0 0 0]]

Properties

此外,使用measure子模块中的regionprops()函数可以很方便地对每一个连通区域进行属性获取和操作,比如计算面积、外接矩形、凸包面积等等。
计算结果返回为所有连通区域的属性列表,列表长度为连通区域个数(第i个连通区域的attribute属性可以通过properties[i].attribute获取)。

skimage.measure.regionprops(label_image, intensity_image = None, cache = True)[source]

Parameters:
@label_image : Labeled input image. Labels with value 0 are ignored. [(N, M) ndarray]
@intensity_image : Intensity (i.e., input) image with same size as labeled image. Default is None. [(N, M) ndarray, optional]
@cache : Determine whether to cache calculated properties. The computation is much faster for cached properties, whereas the memory consumption increases. [bool, optional]
Returns:
@properties : Each item describes one labeled region, and can be accessed using the attributes listed below. [list of RegionProperties]

Property Keys:
The following properties can be accessed as attributes or keys:
@area: [int] Number of pixels of region.
@bbox: [tuple] Bounding box (min_row, min_col, max_row, max_col). Pixels belonging to the bounding box are in the half-open interval [min_row; max_row) and [min_col; max_col).
@bbox_area: [int] Number of pixels of bounding box.
@centroid: [array] Centroid coordinate tuple (row, col).
@convex_area: [int] Number of pixels of convex hull image.
@convex_image: [(H, J) ndarray] Binary convex hull image which has the same size as bounding box.
@coords: [(N, 2) ndarray] Coordinate list (row, col) of the region.
@eccentricity: [float] Eccentricity of the ellipse that has the same second-moments as the region. The eccentricity is the ratio of the focal distance (distance between focal points) over the major axis length. The value is in the interval [0, 1). When it is 0, the ellipse becomes a circle.
@equivalent_diameter: [float] The diameter of a circle with the same area as the region.
@euler_number: [int] Euler characteristic of region. Computed as number of objects (= 1) subtracted by number of holes (8-connectivity).
@extent: [float] Ratio of pixels in the region to pixels in the total bounding box. Computed as area / (rows * cols)
@filled_area: [int] Number of pixels of filled region.
@filled_image: [(H, J) ndarray] Binary region image with filled holes which has the same size as bounding box.
@image: [(H, J) ndarray] Sliced binary region image which has the same size as bounding box.
@inertia_tensor: [(2, 2) ndarray] Inertia tensor of the region for the rotation around its mass.
@inertia_tensor_eigvals: [tuple] The two eigen values of the inertia tensor in decreasing order.
@intensity_image: [ndarray] Image inside region bounding box.
@label: [int] The label in the labeled input image.
@local_centroid: [array] Centroid coordinate tuple (row, col), relative to region bounding box.
@major_axis_length: [float] The length of the major axis of the ellipse that has the same normalized second central moments as the region.
@max_intensity: [float] Value with the greatest intensity in the region.
@mean_intensity: [float] Value with the mean intensity in the region.
@min_intensity: [float] Value with the least intensity in the region.
@minor_axis_length: [float] The length of the minor axis of the ellipse that has the same normalized second central moments as the region.
@moments: [(3, 3) ndarray] Spatial moments up to 3rd order:
m_ji = sum{ array(x, y) * x^j * y^i }
where the sum is over the x, y coordinates of the region.
@moments_central: [(3, 3) ndarray] Central moments (translation invariant) up to 3rd order:
mu_ji = sum{ array(x, y) * (x - x_c)^j * (y - y_c)^i }
where the sum is over the x, y coordinates of the region, and x_c and y_c are the coordinates of the region’s centroid.
@moments_hu: [tuple] Hu moments (translation, scale and rotation invariant).
@moments_normalized: [(3, 3) ndarray] Normalized moments (translation and scale invariant) up to 3rd order:
nu_ji = mu_ji / m_00^[(i+j)/2 + 1]
where m_00 is the zeroth spatial moment.
@orientation: [float] Angle between the X-axis and the major axis of the ellipse that has the same second-moments as the region. Ranging from -pi/2 to pi/2 in counter-clockwise direction.
@perimeter: [float] Perimeter of object which approximates the contour as a line through the centers of border pixels using a 4-connectivity.
@solidity: [float] Ratio of pixels in the region to pixels of the convex hull image.
@weighted_centroid: [array] Centroid coordinate tuple (row, col) weighted with intensity image.
@weighted_local_centroid: [array] Centroid coordinate tuple (row, col), relative to region bounding box, weighted with intensity image.
@weighted_moments: [(3, 3) ndarray] Spatial moments of intensity image up to 3rd order:
wm_ji = sum{ array(x, y) * x^j * y^i }
where the sum is over the x, y coordinates of the region.
@weighted_moments_central: [(3, 3) ndarray] Central moments (translation invariant) of intensity image up to 3rd order:
wmu_ji = sum{ array(x, y) * (x - x_c)^j * (y - y_c)^i }
where the sum is over the x, y coordinates of the region, and x_c and y_c are the coordinates of the region’s weighted centroid.
@weighted_moments_hu: [tuple] Hu moments (translation, scale and rotation invariant) of intensity image.
@weighted_moments_normalized: [(3, 3) ndarray] Normalized moments (translation and scale invariant) of intensity image up to 3rd order:
wnu_ji = wmu_ji / wm_00^[(i+j)/2 + 1]
where wm_00 is the zeroth spatial moment (intensity-weighted area).

Examples:

>>> from skimage import data, util
>>> from skimage.measure import label
>>> img = util.img_as_ubyte(data.coins()) > 110
>>> label_img = label(img, connectivity = img.ndim)
>>> props = regionprops(label_img)
>>> # centroid of first labeled object
>>> props[0].centroid
(22.729879860483141, 81.912285234465827)
>>> # centroid of first labeled object
>>> props[0]['centroid']
(22.729879860483141, 81.912285234465827)

详情请移步:http://blog.csdn.net/jkwwwwwwwwww/article/details/54381298

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 217,734评论 6 505
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 92,931评论 3 394
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 164,133评论 0 354
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,532评论 1 293
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 67,585评论 6 392
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,462评论 1 302
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,262评论 3 418
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 39,153评论 0 276
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,587评论 1 314
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,792评论 3 336
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 39,919评论 1 348
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,635评论 5 345
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,237评论 3 329
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,855评论 0 22
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,983评论 1 269
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 48,048评论 3 370
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,864评论 2 354

推荐阅读更多精彩内容

  • 文/木子 01 时间过得很快,转眼许夏凉五岁了。 “妈妈,我要穿这个裙子。”一大早许夏凉指着柜子里的那件浅蓝色蕾丝...
    木子色的柚子阅读 317评论 0 2
  • 今天刚下载了简书,然后……emmm……发现了一些十分巧妙神奇的事。 99年二刺猿,来勾搭呀!
    旧时棠前燕阅读 204评论 0 0
  • 微风吹起少年的衣角,那是她年少时的心事。 那些精心策划的偶遇,隐藏了无数青涩与悸动。 一场名为青春的别离,从此别了...
    清风叹wzt阅读 189评论 0 0
  • “话”人人会说,只是不见得人人会说话;有话好说,只是不见得人人说好话。 在某十大新闻颁奖典礼上,为呼吁...
    小花米麻麻阅读 148评论 0 2
  • 刚刚与朋友讨论小学数学里四舍五入的约数问题,若以万位约分中的最大与最小的数字时,当写下4999与5000的时候,恍...
    闲着也是闲着lily阅读 432评论 1 2