我有关于超像素聚类的作业,我遇到了一些困难。我被允许使用VLFeat库来查找超像素。
找到超像素后,我为每个超像素定义了一个特征向量值; [(平均" R"超像素中所有像素的值)(平均" G"超像素中所有像素的值)(平均" B"所有值的值超像素中的像素)(平均"色调"超像素中所有像素的值)(平均"饱和度"超像素中所有像素的值)(平均值"值&#34 ;超像素中所有像素的值)]。
之后我将特征向量和簇号发送到内置kmeans函数中来聚类超像素。
function [] = segmentIt (impath, clusNum)
%run vlfeat tools%
run ('vlfeat/toolbox/vl_setup');
%read input image%
image = imread (impath);
imDouble = im2double (image);
%save image W and H%
IH = size (image, 1);
IW = size (image, 2);
%convert input image to single%
imSingle = im2single (image);
%get superpixel data%
superPixels = vl_slic (imSingle, 80, 1);
%how many superpixel do we have?%
SPNUM = size (unique (superPixels), 1);
%changing color space for feature vector%
imHSV = rgb2hsv (image);
%create feature vector with [averageR averageG averageB averageH averageS averageV]%
FEATURE = zeros (SPNUM, 6);
for i=0:SPNUM-1
K = find (superPixels == i);
L = zeros (size(K,1), 2);
L(:,1) = ceil (K(:,1) ./ IW);
L(:,2) = mod(K(:,1), IW) + 1;
intensityTotalR = 0;
intensityTotalG = 0;
intensityTotalB = 0;
vTotal = 0;
sTotal = 0;
hTotal = 0;
for j = 1 : size (L, 1)
intensityTotalR = intensityTotalR + ( imDouble (L(j,1), L(j,2), 1));
intensityTotalG = intensityTotalG + ( imDouble (L(j,1), L(j,2), 2));
intensityTotalB = intensityTotalB + ( imDouble (L(j,1), L(j,2), 3));
vTotal = vTotal + imHSV (L(j,1), L(j,2), 3);
sTotal = sTotal + imHSV (L(j,1), L(j,2), 2);
hTotal = hTotal + imHSV (L(j,1), L(j,2), 1);
end
FEATURE(i+1,:) = [intensityTotalR/size(L,1) intensityTotalG/size(L,1) intensityTotalB/size(L,1) vTotal/size(L,1) sTotal/size(L,1) hTotal/size(L,1)];
end
RESULT = kmeans (FEATURE, clusNum);
newIMAGE = zeros(IH, IW, 3);
colorMultiplier = 1/(clusNum+5);
for i=1:clusNum
K = find (RESULT == i);
for j = 1: size (K,1)
L = find (superPixels == K(j,1));
S = zeros (size(L,1), 2);
S(:,1) = ceil (L(:,1) ./ IW);
S(:,2) = ceil (mod (L(:,1), IW)) + 1;
for z = 1:size(S,1)
newIMAGE (S(z,1), S(z,2), 1) = colorMultiplier * i;
newIMAGE (S(z,1), S(z,2), 2) = colorMultiplier * i;
newIMAGE (S(z,1), S(z,2), 3) = 1;
end
end
end
imshow (newIMAGE)
end
运行我的功能后,生成的图像看起来不太好。知道我的代码中缺少什么吗?
结果图片:
修改
好的,这真的很奇怪。我重新找回了我的超像素,这就是我得到的。
修改-2
我根据lennon310
所说的编辑了我的代码。和wolaa。问题是我使用find()函数的方式。
当群集号等于5时的结果。
从现在开始,我将尝试升级我的特征向量以获得更好的结果。
谢谢lennon310
答案 0 :(得分:3)
代码对我来说很好看。 colorMultiplier * i
作为小于1的值,imshow
应该没问题,但请仔细检查imagesc
以观察差异,但可能没有视觉差异。
如果不仔细查看图片,就不容易指出原因。你怎么定义好的'对于聚类结果?聚类方法对要素密度,大小和使用的指标非常敏感。我总是喜欢使用this image来展示不同的聚类算法如何导致严重的分割结果(不幸的是,在这个例子中没有k-means)。如果来自k-means的分段远离您的期望,您可能只需要尝试其他方法。
修改强>
如果您使用k-means,也许您可以在完整实施之前减少该功能。例如,您可以使用RGB空间要素,并通过在kmeans中将'Replicates'
设置为较小的数字来减少迭代,在继续执行更多步骤之前,请观察分割结果。
鉴于超像素,你能恢复原始图像吗?如果是,您可以尝试按RESULT
方法查看reshape
的图像。这可以在最终设置kmeans
newIMAGE
是否正常工作
<强> EDIT2 强>
更改
K = find (superPixels == i);
L = zeros (size(K,1), 2);
L(:,1) = ceil (K(:,1) ./ IW);
L(:,2) = mod(K(:,1), IW) + 1;
到
[K1,K2] = find (superPixels == i);
L = zeros (length(K1), 2);
L(:,1) = K1;
L(:,2) = K2;
答案 1 :(得分:0)
希望这篇文章不会太晚引起你的兴趣。我使用SLIC +分组解决了一些问题。但我发现一些修改可能会带来更好的结果:
下面的代码可以稍微提高速度:
function [newSegs] = supp_vl(input_im)
%SUPP compute the superpixel using vl feat function
imDouble = im2double (input_im);
%save image W and H%
IH = size (imDouble, 1);
IW = size (imDouble, 2);
%convert input image to single%
imSingle = im2single (imDouble);
%get superpixel data%
segments = vl_slic (imSingle, 80, 1);
%how many superpixel do we have?%
SPNUM = size (unique (segments), 1);
%SPNUM=max(segments(:));
%changing color space for feature vector%
imHSV = rgb2hsv (imDouble);
%create feature vector with [averageR averageG averageB averageH averageS averageV]%
FEATURE = zeros (SPNUM, 6);
for i=0:SPNUM-1
[K1,K2] = find (segments == i);
sz=length(K1);
intRGB= imDouble([K1,K2,ones(sz,1)]);
intHSV=(imHSV([K1,K2,ones(sz,1)]));
FEATURE(i+1,:) = [ sum( intRGB(:,1))/sz sum(intRGB(:,2))/sz sum(intRGB(:,3))/sz sum(intHSV(:,1))/sz sum(intHSV(:,2))/sz sum(intHSV(:,3))/sz];
end
clusNum=SPNUM/2;
RESULT = kmeans (FEATURE, ceil( clusNum));
newIMAGE = zeros(IH, IW, 3);
newSegs=zeros(IH,IW);
colorMultiplier = 1/(clusNum+5);
for i=1:clusNum
K = find (RESULT == i);
valid_segs=zeros(IH,IW);
for j = 1: size (K,1)
valid_segs=valid_segs+(segments== K(j));
end
newIMAGE (valid_segs~=0) = colorMultiplier * i;
newSegs(valid_segs~=0)=i;
end
imshow (newIMAGE)
end