使用 OpenCV 计算特征向量
-
13-09-2019 - |
题
我有这个矩阵 A,表示图像像素强度的相似度。例如:考虑一个 10 x 10
图像。在这种情况下,矩阵 A 的维度为 100 x 100
, ,元素 A(i,j) 的值范围为 0 到 1,表示像素 i 到 j 在强度方面的相似度。
我使用OpenCV进行图像处理,开发环境是Linux上的C。
目的是计算矩阵 A 的特征向量,我使用了以下方法:
static CvMat mat, *eigenVec, *eigenVal;
static double A[100][100]={}, Ain1D[10000]={};
int cnt=0;
//Converting matrix A into a one dimensional array
//Reason: That is how cvMat requires it
for(i = 0;i < affnDim;i++){
for(j = 0;j < affnDim;j++){
Ain1D[cnt++] = A[i][j];
}
}
mat = cvMat(100, 100, CV_32FC1, Ain1D);
cvEigenVV(&mat, eigenVec, eigenVal, 1e-300);
for(i=0;i < 100;i++){
val1 = cvmGet(eigenVal,i,0); //Fetching Eigen Value
for(j=0;j < 100;j++){
matX[i][j] = cvmGet(eigenVec,i,j); //Fetching each component of Eigenvector i
}
}
问题: 执行后,我几乎所有特征向量的所有分量都为零。我尝试了不同的图像,还尝试用 0 到 1 之间的随机值填充 A,但结果相同。
返回的顶部特征值中很少有如下所示:
9805401476911479666115491135488.000000
-9805401476911479666115491135488.000000
-89222871725331592641813413888.000000
89222862280598626902522986496.000000
5255391142666987110400.000000
我现在正在考虑使用 CVSVD() 它执行实浮点矩阵的奇异值分解,并可能产生特征向量。但在此之前我想在这里问一下。我现在的做法有什么荒谬的地方吗?我使用的 API 是否正确,即 CVEigenVV() 对于正确的输入矩阵(我的矩阵 A 是浮点矩阵)?
干杯
解决方案
请注意,以飨读者:这篇文章起初看似无关的话题,但请参阅讨论在评论上述的
以下是我在执行尝试的 谱聚类 强>算法应用于图像的像素中 MATLAB 。我是严格遵循纸由@Andriyev提到:
安德鲁伍,乔丹,和亚伊尔韦斯(2002)。 在谱聚类:分析和算法。 在T. Dietterich,S.贝克尔和Z. Ghahramani(编辑), 在神经信息处理系统14麻省理工学院出版社进展
代码:
%# parameters to tune
SIGMA = 2e-3; %# controls Gaussian kernel width
NUM_CLUSTERS = 4; %# specify number of clusters
%% Loading and preparing a sample image
%# read RGB image, and make it smaller for fast processing
I0 = im2double(imread('house.png'));
I0 = imresize(I0, 0.1);
[r,c,~] = size(I0);
%# reshape into one row per-pixel: r*c-by-3
%# (with pixels traversed in columwise-order)
I = reshape(I0, [r*c 3]);
%% 1) Compute affinity matrix
%# for each pair of pixels, apply a Gaussian kernel
%# to obtain a measure of similarity
A = exp(-SIGMA * squareform(pdist(I,'euclidean')).^2);
%# and we plot the matrix obtained
imagesc(A)
axis xy; colorbar; colormap(hot)
%% 2) Compute the Laplacian matrix L
D = diag( 1 ./ sqrt(sum(A,2)) );
L = D*A*D;
%% 3) perform an eigen decomposition of the laplacian marix L
[V,d] = eig(L);
%# Sort the eigenvalues and the eigenvectors in descending order.
[d,order] = sort(real(diag(d)), 'descend');
V = V(:,order);
%# kepp only the largest k eigenvectors
%# In this case 4 vectors are enough to explain 99.999% of the variance
NUM_VECTORS = sum(cumsum(d)./sum(d) < 0.99999) + 1;
V = V(:, 1:NUM_VECTORS);
%% 4) renormalize rows of V to unit length
VV = bsxfun(@rdivide, V, sqrt(sum(V.^2,2)));
%% 5) cluster rows of VV using K-Means
opts = statset('MaxIter',100, 'Display','iter');
[clustIDX,clusters] = kmeans(VV, NUM_CLUSTERS, 'options',opts, ...
'distance','sqEuclidean', 'EmptyAction','singleton');
%% 6) assign pixels to cluster and show the results
%# assign for each pixel the color of the cluster it belongs to
clr = lines(NUM_CLUSTERS);
J = reshape(clr(clustIDX,:), [r c 3]);
%# show results
figure('Name',sprintf('Clustering into K=%d clusters',NUM_CLUSTERS))
subplot(121), imshow(I0), title('original image')
subplot(122), imshow(J), title({'clustered pixels' '(color-coded classes)'})
...和使用简单的房子图像I在画图画,结果是:
和的方式,所使用的第一特征值4分别为:
1.0000
0.0014
0.0004
0.0002
和对应的特征向量[长度r的列×C = 400]:
-0.0500 0.0572 -0.0112 -0.0200
-0.0500 0.0553 0.0275 0.0135
-0.0500 0.0560 0.0130 0.0009
-0.0500 0.0572 -0.0122 -0.0209
-0.0500 0.0570 -0.0101 -0.0191
-0.0500 0.0562 -0.0094 -0.0184
......
请注意有步执行上面,你没有在你的问题提(拉普拉斯矩阵,其正常化行)
其他提示
我会推荐这文章。笔者实现了面部识别特征脸。第4页,你可以看到他使用cvCalcEigenObjects生成从图像的特征向量。在文章为此所需的计算整个预处理步骤被示出。
下面是一个不是非常有帮助的答案:
这是什么理论(或数学上写下一张纸上)告诉你的特征向量应该是?约。
什么是另一个库告诉你的特征向量应该是?理想的情况是什么,如数学或枫木系统(它可以说服计算任意精度)告诉你的特征向量应该是?如果没有为至少为一个测试尺寸问题的制造-sixed问题。
我不和图像处理方面的专家,所以我不能更有益的,但我花了很多时间与科学家和经验告诉我,就可以避免很多的眼泪和愤怒做一些数学第一,形成什么样的结果,你应该知道为什么你得到0所有的地方之前得到的期望。当然它可能是在算法的执行错误,这可能是失精或其他一些数字问题。但你不知道,不应该跟进调查那些行呢。
此致
标记