A Feature Descriptor: Shape Context

A Feature Descriptor: Shape Context

参考资料:
Paper: Shape Matching and Object Recognition Using Shape Contexts
Wikipedia of Shape Context

Shape Context 其实就是Object Recognition中的一个特征描述子,Wikipedia 上比论文描述得更清晰简明。


Theory: 描述如何计算Shape Context Descriptor


Shape Context (形状上下文)是基于物体轮廓样本点进行描述的,具体来说是基于计算形状相似性以及点与点之间的相关性来描述形状的。

前期的预处理工作包括:边缘(轮廓/contour)提取、采样(均匀采样k个点即可),得到一个物体形状的点集合:I = {p1, p2, p3, …, pk}
对单个点进行形状信息描述: 对于点集合中的每一个点pi, 需要计算剩余n-1个点和该点pi的相对位置关系,就是计算出n-1个vectors,然而这些相对向量集合是一个rich description of the shape localized at that point ,但是它真的太detailed了,因此为了避免信息冗余,所以计算这些vectors的分布即可。

Here comes a robust, compact, and highly discriminative descriptor. 所以,对于点 pi, the coarse histogram of the relative coordinates of the remaining n-1 points: hi(k) = # {q != pi: (q - pi) belong to bin(k)}. 这些相对向量就是该点pi的shape context,标准情况下是在极坐标下统计, 如下图所示:
Shape Context (a)与(b)是两个形状的轮廓样本点,( c )是(a)中标记点在log-polar bins下计算shape context。
(d)是(a)中标记点的shape context,(e)是(b)中方形标记点的shape context,(f)是(b)中三角形标记点的shape context。可以看出(d)和(e)这两个相关点是很相似的,(d)和(f)却非常不同。
实际情况中,会有一些点不在极坐标的覆盖范围内(范围可以根据需要改变),所以,这个在一定范围上决定了形状描述的局部性。在原论文中,极坐标基本上覆盖住点集最大距离的一半。

为了使Shape Context Descriptor 更加广泛使用,它需要有一些不变性(invariances),它需要对平移,缩放(距离除以mean/median),小扰动(small perturbations),还有一些应用上的旋转(但是完全的旋转不变性是不需要的,因为如果它是完全旋转不变的,那么某些局部特征会失去辨别能力,比方说“6”和“9”,如果针对相同的框架)。具体可查看Implementation中的Step 3.


Matching: Use in Shape Matching


这部分主要是总结如何使用shape context descriptor 来进行matching的,具体实现参照Implementation.
① 选点 ➨(边缘提取[Canny]、均匀采样)
② 计算Shape Context Descriptor ➨(计算①得到的点集中每个点的Shape Context)
③ Matching ➨(形状匹配算法等[最费时])
➥ 将已知形状的每一个点和未知形状的某一个点进行匹配。 因为它这个步骤十分费时,所以为了减少匹配的cost,首先要对两个形状进行校准变换(transformation),然后选择形状上的点对应已知形状上最接近的点。
④ 计算Shape Distance
➥ 由于通过③已经计算出点对了,然后计算这些点对的一些距离,然后算出权重之和。
⑤ 确定未知图形(属于哪一类)
➥ 使用nearest-neighbor classifier, 比如说MNIST手写字就用了3-NN。(Shape Distance越小就是越接近某个形状)


Implementation: How to recognize an object


该节讨论如何用Shape Context 来进行对象识别的。

Step 1: Finding a list of points on shape edges [选点]

使用 Canny edge detector 来找出形状上外部轮廓以及内部轮廓上的点,形成集合。
然后是等距离空间(roughly uniform spacing)来采样,点集中点可以不是关键点(如曲率最大的点 (maxima of curvature),拐点(inflection points)).

Step 2: Computing the shape context

如何计算Shape Context Descriptor,在Theory 这个Section中已经详细阐述了。(不重复描述)

Step 3: Computing the cost matrix

计算两个形状之间的matching所需要的代价矩阵。

首先是考虑计算它们之间每个样本点(如p, q )之间的差别,这两点已经有自己的shape context(就是k-bins theta-log(r)直方图),然后通过计算两个样本点间的χ2 test statistic[Chi-squared test]作为这两个点的cost:↴ 而这个值大小在0到1之间)。

除此以外,还要有对外观的cost的估算,比如说,使用切角线相异性(tangent angle dissimilarity)来表示外观的cost:↴ 这个在数字识别中很有用。它是单位圆中的theta1和theta2两个角度间的弦长度的一半,也是大小在0到1之间。

最后两个点之间总的cost是就是这个两个cost的权重之和:

在此之后,需要得到每个点之间的差别,就是要计算形状P中的每一个点pi和形状Q中每一个点qj (标记为Ci,j), 然后获得cost matrix C.

Step 4: Finding the matching that minimizes total cost

找出matching的最小总代价,其实就是找出最优匹配。Step 3中获得了一个代价矩阵cost,然后运行一个最优匹配算法(如 Hungarian method 匈牙利算法等)逐个找出一个最优匹配(每个匹配的代价都使之最小),使整个代价最小,最后基于这个最优匹配,得到整个形状代价,这个可以作为两个形状之间的差别衡量,代价越小,形状越相似。形状代价如下:
qπ(i)是每个点对应的最优解。故H(π)为最优解,总代价最小。

Hungarian method: 匈牙利算法是一种在多项式时间内求解任务分配问题的组合优化算法,并推动了后来的原始对偶方法。美国数学家哈罗德·库恩于1955年提出该算法。此算法之所以被称作匈牙利算法,是因为算法很大一部分是基于以前匈牙利数学家Dénes Kőnig和Jenő Egerváry的工作之上创建起来的。时间复杂度为O(N3)

为了使处理形状中的outliers时更有鲁棒性,通常会加入一些”dummy” nodes, 但通常这样代价矩阵的matching代价会变很大。而且如果这些伪点没有真的match,会导致匹配算法会将异常值与伪点匹配。

Step 5: Modeling transformation [找出形状间变换映射]

Step 4只是找到一个两个形状间点的对应关系而已,需要进一步用一个变换 T 来衡量形状之间的转变,所以,最后的形状距离可以用估计的变换来表示。 T : R^2 ⇒ R^2 可以用来估算一个形状的任何点与另外的形状的点之间的映射。

  • Affine 仿射模型
    是一种标准选择:
    矩阵A的解以及偏移变量o的算方式如下:
    P和Q是形状的点的齐次坐标☟;Q^+ 是Q的伪逆矩阵。

  • Thin plate spline

    TPS模型是在使用Shape Context时用于变换的最广泛使用的模型, 而且很高效。在Regularized的格式下,Affine model也是属于TPS模型中的一种special case. 变换表达式如下:
    从一维内插问题说起,令pi = (xi, yi), 而设向量vi = (xi, yi). 然后TPS内插f(x,y)可以最小化bending energy 而且f(x,y)的公式如下:

    然后Kernel function U(r) = r2 log r2. 具体细节以及计算参赛可以再看一遍论文的Section 4, 我看得有点懵

    • Regularized TPS

      由于TPS公式是要求点对是准确的匹配的, 所以需要规范化TPS可以使映射T 不那么受到noise的影响。 对于noise data, 最好是放松精确的要求,放宽需求以最小化H(f),使得获得的f比较合适。

      这里 If 是 bending energy; λ是regularization parameter.

在很多情况下,无论使用哪种变换映射,初始的相关性的估计都会有误差,这些会导致计算出来的变换映射不是那么精确,因此为了提高变换矩阵的精确性,通常需要使用新的变换形状Q’重复 steps 2–5。一般情况下需要3次迭代才能获得比较可以接受的结果。

Step 6: Computing the shape distance

最后计算两个形状P和Q之间的形状距离,Shape Distance是三个potential terms的权重之和。

Appearance cost:as the sum of squared brightness differences in Gaussian windows around corresponding image points. 形状矫正以后,Appearance cost 是对应图像点在高斯模糊下的亮度平方差的和,反应的是灰度值的相似度。IPIQ 是灰度图,而且IQ 是wrapping后的图像。 G 是高斯窗户函数。

Transformation cost: 最后的Cost是Dbe(P, Q), 是用来计算需要多少变换可以使得两个图像可以对准(校准)。 在使用TPS模型的情况下,变换代价是用bending energy表示。

获得Shape Distance之后,我们可以使用nearest neighbor classifier (如k-NN)来进行分类,上一个section有提及,距离越小越靠近某一类。

将模板库中出现的SC聚成k类,每个类的中心叫一个Shapeme。
对模板库的每个形状Si统计,将其形状上下文分别量化到最近的Shapeme,得到一个对Shapeme统计的K槽直方图。对待查询形状也进行这样的统计。用一个k-bins Shapeme直方图代替了原来的s个d-bins 形状上下文。两个Shapeme直方图的距离是它们的Chi2距离。通过这样的量化将对s*d的比较变成k的比较,速度很快,精度损失却不大。


Result


其实有很多应用,如。。。,但是我比较注重是Digit Recognition,因为和我课题破解验证码有关。

Digit recognition

使用MNIST 数据集,采样是每个字符找出100个edge points;
定义matching cost是 Cij = (1-β) Cijsc + β Cijtan ;
Cijsc 依然是shape context cost;
Cijtan 是tangent angle dissimilarity, Cijtan = 0.5(1-cos(θi-θj));
β = 0.1.

识别过程中是使用3-NN 分类器,距离函数为D = 1.6 Dac + Dsc + 0.3 Dbe.

使用20,000 训练样本,3-NN的error rate是 0.63%.



优点&缺点

最后摘录一下优缺点:
优点:
▏ 充分利用上下文信息,在非刚性物体匹配中,具有很好的鲁棒性。

主要缺点:
▏ 标准的形状上下文不能很好地解决物体类内部之间的变形,后期改进的基于内部距离的形状上下文改善了一点;
▏ 对于有背景、噪声点过多的情况,匹配效果很不好。




Wikipedia of Shape Context
Shape Context概括

### Global Context-Efficient Channel Attention (GC-EC) Block In deep learning architectures, the Global Context-Efficient Channel Attention (GC-EC) block is designed to enhance feature representation by integrating global context information into channel attention mechanisms effectively[^1]. This mechanism allows models to focus on informative channels while suppressing less useful ones based on comprehensive spatial and cross-channel dependencies. The GC-EC block typically consists of several key components: #### Spatial Pooling Layer A spatial pooling layer aggregates feature maps across all positions within each channel, capturing long-range dependencies that are essential for understanding complex patterns in data such as images or videos. By doing so, this operation generates a compact descriptor representing global contextual cues from input features[^2]. ```python import torch.nn.functional as F def spatial_pool(x): batch_size, num_channels, height, width = x.size() pooled_features = F.avg_pool2d(x, kernel_size=(height, width)) return pooled_features.view(batch_size, -1) ``` #### Transformation Module Following the aggregation step, a transformation module processes these descriptors through fully connected layers or convolutional operations with nonlinear activations like ReLU. Such transformations aim at modeling intricate relationships between different parts of an image scene efficiently without increasing computational cost significantly[^3]. ```python class TransformModule(nn.Module): def __init__(self, in_planes, ratio=8): super(TransformModule, self).__init__() hidden_units = int(in_planes / ratio) self.fc = nn.Sequential( nn.Linear(in_planes, hidden_units), nn.ReLU(inplace=True), nn.Linear(hidden_units, in_planes) ) def forward(self, x): b, c = x.shape[:2] y = self.fc(x).view(b, c, 1, 1) return y ``` #### Sigmoid Activation Function Finally, sigmoid activation applies element-wise over transformed outputs producing weights ranging continuously between zero and one which can be multiplied back onto original inputs thereby emphasizing important regions/channels dynamically during inference time[^4]. ```python attention_weights = torch.sigmoid(transformed_output) refined_feature_maps = original_input * attention_weights.expand_as(original_input) ``` Through combining above elements together seamlessly within neural networks architecture design process, developers achieve better performance gains especially when dealing tasks requiring fine-grained visual recognition capabilities where subtle differences matter most.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值