本文介绍了谱聚类,图像分割和特征向量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

基于计算机视觉与现代方法一书第425页,我试图使用特征向量进行图像分割。

Based on the book Computer Vision a Modern Approach page 425, I attempted to use eigenvectors for image segmentation.

作者提到图像像素亲缘关系可以在矩阵A中捕获。然后我们可以最大化w ^ TA w产品,其中w是权重。在一些代数得到Aw = \ lambda w之后,找到w就像找到特征向量一样。然后找到最佳聚类是找到具有最大特征向量的特征值,该特征向量内的值是聚类成员资格值。我写了这段代码

The author mentions that image pixel affinites can be captured in matrix A. Then we can maximize w^T A w product where w's are weights. After some algebra one obtains Aw = \lambda w, finding w is like finding eigenvectors. Then finding the best cluster is finding the eigenvalue with largest eigenvector, the values inside that eigenvector are cluster membership values. I wrote this code

import matplotlib.pyplot as plt
import numpy as np

Img = plt.imread("twoObj.jpg")
(n,dummy) = Img.shape
Img2 = Img.flatten()
(nn,) = Img2.shape

A = np.zeros((nn,nn))

for i in range(nn):
    for j in range(nn):
        N=Img2[i]-Img2[j];
        A[i,j]=np.exp(-(N**2))

V,D = np.linalg.eig(A)
V = np.real(V)
a = np.real(D[1])

threshold = 1e-10 # filter
a = np.reshape(a, (n,n))
Img[a<threshold] = 255
plt.imshow(Img)
plt.show()

图片

我可以从中得到的最佳结果如下。我感觉结果会更好。

Best result I could get from this is below. I have a feeling the results can be better.

在Numpy中,特征值从大到小排序,我尝试了第一个,但没有用,然后我尝试了第二个用于下面的结果。通过反复试验选择阈值。有关如何改进此算法的任何想法?

The eigenvalues are sorted from largest to smallest in Numpy, I tried the first one, that did not work, then I tried the second one for the results seen below. Threshold value was chosen by trial and error. Any ideas on how this algorithm can be improved?

推荐答案

我刚刚尝试了Mathematica中的算法,它在你的图像上运行正常,所以必须是你代码中的一个微妙的错误。

I've just tried the algorithm in Mathematica, it works fine on your image, so there must be a subtle bug in your code.

这部分:

V,D = np.linalg.eig(A)
V = np.real(V)
res = n_max(V,1) # take largest 
idx = res[0][1][0] 
a = np.real(D[idx]) # look at corresp eigv

看起来很奇怪:我知道的所有线性代数包都会返回排序的特征值/特征向量,所以你只需要在列表中取第一个特征向量。这是对应于最高特征值的那个。尝试绘制特征值列表以确认。

looks strange: all linear algebra packages I know return the eigenvalues/eigenvectors sorted, so you'd just take the first eigenvector in the list. That's the one that corresponds to the highest eigenvalue. Try plotting the eigenvalues list to confirm that.

此外,您从哪里获得固定阈值?您是否尝试过对图像进行标准化以显示它?

Also, where did you get the fixed threshold from? Have you tried normalizing the image to display it?

对于它的值,我得到的前3个特征向量的结果是:

For what it's worth, the results I'm getting for the first 3 eigenvectors are:

这是我使用的Mathematica代码:

This is the Mathematica code I use:

pixels = Flatten[image];
weights = Table[N[Exp[-(pixels[[i]] - pixels[[j]])^2]], {i, 1, 900}, {j, 1, 900}];
eigenVectors = Eigenvectors[weights];
ImageAdjust[Image[Partition[eigenVectors[[1]], 30]]]

这篇关于谱聚类,图像分割和特征向量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-25 07:28