2017-02-01 73 views
0

我已經創建了一個項目測試程序,測試圖像是否相同,以確定它是否是相同的圖像。我決定使用關聯,因爲我使用的圖像是以相同的方式進行設置的,因此,我已經能夠使所有的工作都達到此目的。MatLab - 根據圖像的相關性創建一個圖像陣列

我現在想再次創建一個圖像數組,但是這次是按照它們的相關性。例如,如果我測試一個50便士硬幣,然後測試50個便士硬幣上的50個圖像,我希望將最高的5個相關性存儲到一個數組中,然後將其用於以後使用。但我不確定如何做到這一點,因爲數組中的每個項目都需要有多個變量,這將是圖像的圖像位置/名稱以及相關百分比。

%Program Created By Ben Parry, 2016. 

clc(); %Simply clears the console window 

%Targets the image the user picks 
inputImage = imgetfile(); 
%Targets all the images inside this directory 
referenceFolder = 'M:\Project\MatLab\Coin Image Processing\Saved_Images'; 
if ~isdir(referenceFolder) 
    errorMessage = print('Error: Folder does not exist!'); 
    uiwait(warndlg(errorMessage)); %Displays an error if the folder doesn't exist 
    return; 
end 

filePattern = fullfile(referenceFolder, '*.jpg'); 
jpgFiles = dir(filePattern); 
for i = 1:length(jpgFiles) 
    baseFileName = jpgFiles(i).name; 
    fullFileName = fullfile(referenceFolder, baseFileName); 
    fprintf(1, 'Reading %s\n', fullFileName); 
    imageArray = imread(fullFileName); 
    imshow(imageArray); 
    firstImage = imread(inputImage); %Reading the image 

    %Converting the images to Black & White 
    firstImageBW = im2bw(firstImage); 
    secondImageBW = im2bw(imageArray); 

    %Finding the correlation, then coverting it into a percentage 
    c = corr2(firstImageBW, secondImageBW); 
    corrValue = sprintf('%.0f%%',100*c); 

    %Custom messaging for the possible outcomes 
    corrMatch = sprintf('The images are the same (%s)',corrValue); 
    corrUnMatch = sprintf('The images are not the same (%s)',corrValue); 

    %Looping for the possible two outcomes 
    if c >=0.99 %Define a percentage for the correlation to reach 

     disp(' '); 
     disp('Images Tested:'); 
     disp(inputImage); 
     disp(fullFileName); 
     disp (corrMatch); 
     disp(' '); 
    else 

     disp(' '); 
     disp('Images Tested:'); 
     disp(inputImage); 
     disp(fullFileName); 
     disp(corrUnMatch); 
     disp(' '); 
    end; 

    imageArray = imread(fullFileName); 


    imshow(imageArray); 


end 

回答

0

您可以使用struct()函數來創建結構。

初始化結構的陣列:

imStruct = struct('fileName', '', 'image', [], 'correlation', 0); 
imData = repmat(imStruct, length(jpgFiles), 1); 

設置字段值:

for i = 1:length(jpgFiles) 
    % ... 
    imData(i).fileName = fullFileName; 
    imData(i).image = imageArray; 
    imData(i).correlation = corrValue; 
end 

correlation字段的提取物值,並選擇最高5間的相關性:

corrList = [imData.correlation]; 
[~, sortedInd] = sort(corrList, 'descend'); 

selectedData = imData(sortedInd(1:5));