1. 程式人生 > 實用技巧 >MATLAB小函式:展示灰度影象資料集的部分樣例

MATLAB小函式:展示灰度影象資料集的部分樣例

MATLAB小函式:展示灰度影象資料集的部分樣例

作者:凱魯嘎吉 - 部落格園http://www.cnblogs.com/kailugaji/

更多請看:MATLAB作圖

給定一個.mat檔案的灰度影象資料集,用MATLAB程式展示該影象資料集的一部分樣例,每一類都展示相同數目的樣例圖,並將它們彙總成一幅圖並儲存圖片。資料來源:Face databases (Yale, ORL, PIE and YaleB)

1. MATLAB程式

Image_integration.m

function Image_samples=Image_integration(data, real_label, N_samples)
% Gray image integration
% This code only applies to square matrices
% Input: 
% data: dataset. N*Dim
% real_label: GroundTruth. N*1
% N_samples: number of selected samples
% Output:
% Image_samples:Integrated image
% Author: kailugaji https://www.cnblogs.com/kailugaji/
[~, Dim]=size(data);
[~, b]=sort(real_label);
data=data(b, :); 
K=length(unique(real_label)); % number of cluster
[~, ID]=unique(real_label);
ID=ID-1;
image_10=cell(N_samples, K);
temp=cell(N_samples, K);
Image_samples=[];
for i=1:N_samples
    for j=1:K
        temp{i, j}=reshape(data(ID(j)+i, :), sqrt(Dim), sqrt(Dim)); % you can change its size
        image_10{i, j}=[image_10{i, j}, temp{i, j}];
    end
    Image_samples=[Image_samples; image_10{i, :}];
end

demo.m

clear
clc
% Author: kailugaji https://www.cnblogs.com/kailugaji/
interval=7; % The size of the middle space
N_samples=10; % number of selected samples
load('ORL_64x64.mat')
Image_samples=Image_integration(fea, gnd, N_samples);
A=mat2gray(Image_samples);
figure(1)
imshow(A, 'Border','tight'); 
print(gcf,'-r1000','-djpeg','My_ORL.jpg');

load('Yale_64x64.mat')
Image_samples=Image_integration(fea, gnd, N_samples);
B=mat2gray(Image_samples);
figure(1)
imshow(B, 'Border','tight'); 
print(gcf,'-r1000','-djpeg','My_Yale.jpg');

A_=imresize(A,[500, 2000]);
B_=imresize(B,[500, 750]);
C=[A_, 255.*ones(size(A_(:, 1:interval, :))), B_];
figure(3)
imshow(C, 'Border','tight'); 
print(gcf,'-r1000','-djpeg','My_Image.jpg');

2. 結果

ORL資料集部分樣例圖

Yale資料集部分樣例圖

兩個資料集合並在一起的樣例示意圖