Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/matlab/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/image-processing/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用距离提取特征并比较图像::matlab_Matlab_Image Processing - Fatal编程技术网

如何使用距离提取特征并比较图像::matlab

如何使用距离提取特征并比较图像::matlab,matlab,image-processing,Matlab,Image Processing,我试着从这两幅实际上很相似的图像中提取特征。我试图从两幅图像中提取交点,并计算从一个交点到所有其他点的距离。对所有点和两幅图像重复此过程 然后我比较了两幅图像中点之间的距离,但我发现即使是不同的图像,也会得到相同的距离,无法区分它们 在这个方法中是否有任何方法可以改进代码,或者是否有任何其他方法可以找到相似性 I = bwmorph(I,'skel',Inf); II = bwmorph(II,'skel',Inf); [i,j] = ind2sub(size(I),find(bwmorph(b

我试着从这两幅实际上很相似的图像中提取特征。我试图从两幅图像中提取交点,并计算从一个交点到所有其他点的距离。对所有点和两幅图像重复此过程

然后我比较了两幅图像中点之间的距离,但我发现即使是不同的图像,也会得到相同的距离,无法区分它们

在这个方法中是否有任何方法可以改进代码,或者是否有任何其他方法可以找到相似性

I = bwmorph(I,'skel',Inf);
II = bwmorph(II,'skel',Inf);
[i,j] = ind2sub(size(I),find(bwmorph(bwmorph(I,'thin',Inf),'branchpoint') == 1));
[i1,j1] = ind2sub(size(II),find(bwmorph(bwmorph(II,'thin',Inf),'branchpoint') == 1));
figure,imshow(I); hold on; plot(j,i,'rx');
figure,imshow(II); hold on; plot(j1,i1,'rx')
m=size(i,1);
n=size(j,1);
m1=size(i1,1);
n1=size(j1,1);
for x=1:m
   for y=1:n
       d1(y,x)=round(sqrt((i(y,1)-i(x,1)).^2+(j(y,1)-j(x,1)).^2));
   end
end
for x1=1:m1
    for y1=1:n1
       dd1(y1,x1)=round(sqrt((i1(y1,1)-i1(x1,1)).^2+(j1(y1,1)-j1(x1,1)).^2));
    end
end
size(d1);
k1=reshape(d1,1,m*n);
k=sort(k1);
k=unique(k);

size(dd1);
k2=reshape(dd1,1,m1*n1);
k2=sort(k2);
k2=unique(k2);

z = intersect(k,k2)
length(z);
if length(z)>20
    disp('similar images');
else
    disp('dissimilar images');
end
这是我试图提取特征的代码的一部分。 输入1 输入2 骨架1
skel2

我认为您的代码不是问题所在。相反,您的特征描述符似乎不够强大,或者您的比较方法不够强大,或者两者兼而有之。这为我们提供了几个选择,以探索问题的解决方案

特征描述符 您正在构建由骨架交点之间的距离组成的图像特征。这是一种不同寻常的方法,也是一种非常有趣的方法。它让我想起了尖峰星座,沙扎姆用它来播放指纹歌曲。如果你对探索这种更复杂的技术感兴趣,可以看看王丽春(Avery Li Chun Wang)的作品。我相信您可以根据您的应用程序调整它们的特性描述符

但是,如果您想要一个更简单的解决方案,还有其他一些选择。当前描述符使用
unique
查找骨架交点之间的一组唯一距离。请看下面的图像,其中一条直线和一个等边三角形都有5个单位的直线长度。如果我们使用顶点之间的唯一距离来创建特征,则两幅图像具有相同的特征,但我们也可以计算直方图中每种长度的线数

直方图作为特征的一部分保留了更多的图像结构。使用直方图可能有助于更好地区分相似和不同的情况

下面是一些使用Matlab演示图像pears.png和peppers.png的直方图功能的演示代码。我很难从您提供的图像中提取骨架,但您应该能够轻松地将此代码改编为您的应用程序

I1 = = im2bw(imread('peppers.png'));
I2 = = im2bw(imread('pears.png'));
I1_skel = bwmorph(I1,'skel',Inf);
I2_skel = bwmorph(I2,'skel',Inf);
[i1,j1] = ind2sub(size(I1_skel),find(bwmorph(bwmorph(I1_skel,'thin',Inf),'branchpoint') == 1));
[i2,j2] = ind2sub(size(I2_skel),find(bwmorph(bwmorph(I2_skel,'thin',Inf),'branchpoint') == 1));

%You used a for loop to find the distance between each pair of
%intersections. There is a function for this. 
d1 = round(pdist2([i1, j1], [i1, j1]));
d2 = round(pdist2([i2, j2], [i2, j2]));

%Choose a number of bins for the histogram. 
%This will be the length of the feature. 
%More bins will preserve more structure. 
%Fewer bins will help generalize between similar but not identical images.
num_bins = 100; 

%Instead of using `unique` to remove repetitions use `histcounts` in R2014b 
%feature1 = histcounts(d1(:), num_bins);
%feature2 = histcounts(d2(:), num_bins);

%Use `hist` for pre R2014b Matlab versions
feature1 = hist(d1(:), num_bins);
feature2 = hist(d2(:), num_bins);

%Normalize the features
feature1 = feature1 ./ norm(feature1);
feature2 = feature2 ./ norm(feature2);

figure; bar([feature1; feature2]'); 
title('Features'); legend({'Feature 1', 'Feature 2'}); 
xlim([0, num_bins]);
以下是每个图像中检测到的交点

下面是结果特性。您可以看到图像之间的明显差异。

特征比较 第二部分要考虑的是你如何比较你的特点。目前,您只需查找>20个类似距离。使用Matlab分发的“peppers.png”和“pears.png”测试图像,我在一张图像中找到了2000多个交点,在另一张图像中找到了260个交点。对于如此多的点,重叠>20个相似距离是微不足道的。在图像中,交点的数量要小得多。您可以仔细调整相似距离的阈值,但我认为这个度量可能过于简单

在机器学习中,比较两个特征向量的简单方法是向量相似度或距离。您可以探索多种距离度量。常见的有

  • 余弦距离

  • 欧几里德距离

  • score\u euclidean=pdist2(特征1,特征2);
    %设置欧几里德相似性阈值,其中越小越相似
    欧氏阈值=0.1;
    disp(‘欧几里德比较’)
    disp(分数和欧几里得)
    如果分数为欧几里德<欧几里德阈值
    disp(“类似图像”);
    其他的
    disp(“不同图像”);
    结束
    

    如果这些不起作用,您可能需要训练分类器,以找到更复杂的函数来区分相似和不同的图像

    您正在对距离进行排序,而不是按照它们出现的顺序使用它们。这可能就是问题所在。尝试在不排序的情况下比较差异。@optimist我也没有排序就编写了代码,但仍然无法匹配图像。有时,在不同的图像中,匹配距离的数量更多。关于排序,我不是专家,但出于好奇,我想排序会对结果产生怎样的影响,因为我猜排序是为了按照特定的顺序排列矩阵中的元素。你能在没有红色x的情况下发布原始图像吗?@Cecilia我也添加了输入图像和处理后的图像。谢谢@Cecilia的尝试。我在我的图像上尝试了你的代码版本。将阈值保持在90%对我来说效果不好。所有相似的图像都被识别为不同的图像。所以我把阈值改为0.5。此时,所有不同的图像都被识别为相似的图像。所以我选择了0.6,0.55。结果相同。我在这篇文章中添加了我的图片。发生这种情况是因为生成了交点。第一个和第三个图像是similar@ANUSHADEVI我又试了一次,但我想你可能最终需要一个分类器。我自己也找不到一个好的门槛。我已经添加了更多的细节,这样你可以自己探索所有的途径。非常感谢这些信息。我肯定会深入了解这些信息。但是你能帮我在这些骨架化的图像中找到一个解决方案吗。我也就此提出了一个问题,但没有得到任何积极的回答。我发现一些不必要的分支,而骨骼。我怎样才能摆脱它们。我想在开始提取之前我需要把它清理干净。非常感谢。。。
    score_cosine = feature1 * feature2'; %Cosine distance between vectors
    
    %Set a threshold for cosine similarity [0, 1] where 1 is identical and 0 is perpendicular
    cosine_threshold = .9;
    disp('Cosine Compare')
    disp(score_cosine)
    if score_cosine > cosine_threshold
        disp('similar images');
    else
        disp('dissimilar images');
    end
    
    score_euclidean = pdist2(feature1, feature2);
    
    %Set a threshold for euclidean similarity where smaller is more similar
    euclidean_threshold = 0.1;
    disp('Euclidean Compare')
    disp(score_euclidean)
    if score_euclidean < euclidean_threshold
        disp('similar images');
    else
        disp('dissimilar images');
    end