簡體   English   中英

如何使用距離提取特征並比較圖像:: Matlab

[英]How to use distance to extract features and compare images: : matlab

我試圖為從兩個圖像中提取特征進行編碼,這實際上是相似的。 我試圖從兩個圖像中提取交點,並計算出一個交點到所有其他點的距離。 對所有點和兩個圖像都重復執行此過程。

然后,我比較了兩個圖像中點之間的距離,但是我發現,即使對於不同的圖像,它們也具有相同的距離,並且無法區分它們。

此方法中是否有任何方法可以改善代碼,還是有其他方法可以找到相似之處。

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 在此處輸入圖片說明

我認為您的代碼不是問題。 相反,似乎您的功能描述符不夠強大或比較方法不夠強大,或者兩者結合。 這為我們提供了幾種探索問題解決方案的選擇。

功能描述符

您正在構建由骨架相交點之間的距離組成的圖像特征。 這是一種不尋常的方法,也是一種非常有趣的方法。 它使我想起了峰值星座,Shazam使用它來錄制音頻指紋歌曲。 如果您有興趣探索更復雜的技術,請看一下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個相似距離是微不足道的。 在您的圖像中,相交點的數量要少得多。 您可以仔細調整相似距離的閾值,但是我認為此指標可能過於簡單。

在機器學習中,比較兩個特征向量的一種簡單方法是向量相似度或距離。 您可以探索多個距離指標。 常見的包括

  1. 余弦距離

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
  1. 歐氏距離

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

如果這些方法不起作用,則可能需要訓練分類器以找到更復雜的函數,以區分相似和不相似的圖像。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM