利用MATLAB绘制箱线图
欢迎学习交流!
邮箱: z…@1…6.com
网站: https://zephyrhours.github.io/
一、箱线图在高光谱图像处理中的应用
箱线图也称箱须图、箱形图、盒图,用于反映一组或多组连续型定量数据分布的中心位置和散布范围。箱形图包含数学统计量,不仅能够分析不同类别数据各层次水平差异,还能揭示数据间离散程度、异常值、分布差异等等。
目前已经在异常检测,目标探测,红外小目标检测、分类等多个方向上有广泛的应用,具体箱线图的含义,读者可以百度,或者查看第三部分的相关参考文献,此处不再赘述。
二、箱线图具体MATLAB代码分享
来,咱们直接上代码,硬核分享,具体如下:
%% 箱线图(背景分离)程序
% Compiled by Zephyr Hou on 2019-01-23
%% 加载数据
clc;clear;close all;
% load('Gulfport_Res.mat')
% load('SpecTIR_Res.mat')
load('SanDiego_Res.mat')
[rows,cols] = size(hsi_gt);
% figure(1);imshow(hsi_gt,[]);title('原始影像gt ');
%% 探测结果进行归一化(0-1)
max1 = max(max(R1));min1 = min(min(R1));
R1 = (R1-min1)/(max1-min1);
max2 = max(max(R2));min2 = min(min(R2));
R2 = (R2-min2)/(max2-min2);
% max3 = max(max(R3));min3 = min(min(R3));
% R3 = (R3-min3)/(max3-min3);
max4 = max(max(R4));min4 = min(min(R4));
R4 = (R4-min4)/(max4-min4);
max5 = max(max(R5));min5 = min(min(R5));
R5 = (R5-min5)/(max5-min5);
max6 = max(max(R6));min6 = min(min(R6));
R6 = (R6-min6)/(max6-min6);
max7 = max(max(R7));min7 = min(min(R7));
R7 = (R7-min7)/(max7-min7);
%% 二维转换为一维
label_value = reshape(hsi_gt,1,rows*cols);
R1_value = reshape(R1,1,rows*cols);
R2_value = reshape(R2,1,rows*cols);
% R3_value = reshape(R3,1,rows*cols);
R4_value = reshape(R4,1,rows*cols);
R5_value = reshape(R5,1,rows*cols);
R6_value = reshape(R6,1,rows*cols);
R7_value = reshape(R7,1,rows*cols);
%%
ind_tar = find(label_value == 1);
ind_bac = find(label_value == 0);
% targ back为列向量
num_targ = length(ind_tar);
num_back = length(ind_bac);
num_meth = 6; % 异常探测方法种类
targ1 = R1_value(ind_tar);
targ2 = R2_value(ind_tar);
% targ3 = R3_value(ind_tar);
targ4 = R4_value(ind_tar);
targ5 = R5_value(ind_tar);
targ6 = R6_value(ind_tar);
targ7 = R7_value(ind_tar);
back1 = R1_value(ind_bac);
back2 = R2_value(ind_bac);
% back3 = R3_value(ind_bac);
back4 = R4_value(ind_bac);
back5 = R5_value(ind_bac);
back6 = R6_value(ind_bac);
back7 = R7_value(ind_bac);
X_targ = [targ1;targ2;targ4;targ5;targ6;targ7]';
X_back = [back1;back2;back4;back5;back6;back7]';
X = [X_targ(:);X_back(:)];
X = X(:);
g1_targ = [ones(1,num_targ); 2*ones(1, num_targ); 3*ones(1, num_targ);4*ones(1, num_targ);...
5*ones(1, num_targ);6*ones(1, num_targ)]';
g1_back = [ones(1, num_back); 2*ones(1, num_back); 3*ones(1, num_back);4*ones(1, num_back);...
5*ones(1, num_back);6*ones(1, num_back)]';
g1 = [g1_targ(:); g1_back(:)];
g1 = g1(:);
g2 = [ones(num_meth*num_targ,1);2*ones(num_meth*num_back,1)];
g2 = g2(:);
positions = [[1:num_meth],[1:num_meth]+0.3];
%%
figure(2);
bh=boxplot(X, {g2,g1} ,'whisker',10000,'colorgroup',g2, 'symbol','.','outliersize',4,'widths',0.2,'positions',positions);
set(bh,'LineWidth',1.7)
ylabel('Detection test statistic range');
% grid on
% set(gca,'YLim',[0,0.5],'gridLineStyle', '-.');
% ylim([0,0.0065]) % 用于y轴的坐标轴显示范围的控制
Xtick_pos = [1:num_meth]+0.15;% 确定label显示的位置
Xtick_label ={'GRX','LRX','RPCA','CRD','CRBPSW\_LS','CRBPSW\_CR'};
set(gca,'XTickLabel',Xtick_label, 'XTick',Xtick_pos);
% xtickangle(15)% 旋转标签角度
% 显示图例
h=findobj(gca,'tag','Outliers');
delete(h)
legend(findobj(gca,'Tag','Box'),'背景','异常')
%% 最大值与最小值(箱须至高与至低点:whisker 为0-100%)
p_targ = prctile(X_targ,[0 100]);
p_back = prctile(X_back,[0 100]);
% p_targ = prctile(X_targ,[10 90]);
% p_back = prctile(X_back,[10 90]);
p = [];
for i = 1:num_meth
p = [p,p_targ(:,i),p_back(:,i)];
end
% 箱子的上边缘与下边缘 (异常、背景区域10% 与 90% 统计)
q_targ = quantile(X_targ,[0.1 0.9]);
q_back = quantile(X_back,[0.1 0.9]);
% q_targ = quantile(X_targ,[0.09 0.81]);
% q_back = quantile(X_back,[0.09 0.81]);
q = [];
for i = 1:num_meth
q = [q,q_targ(:,i),q_back(:,i)];
end
h = flipud(findobj(gca,'Tag','Box'));
for j = 1:length(h)
q10 = q(1,j);
q90 = q(2,j);
set(h(j),'YData',[q10 q90 q90 q10 q10]);
end
% Replace upper end y value of whisker
h = flipud(findobj(gca,'Tag','Upper Whisker'));
for j=1:length(h);
% ydata = get(h(j),'YData');
% ydata(2) = p(2,j);
% set(h(j),'YData',ydata);
set(h(j),'YData',[q(2,j) p(2,j)]);
end
% Replace all y values of adjacent value
h = flipud(findobj(gca,'Tag','Upper Adjacent Value'));
for j=1:length(h);
% ydata = get(h(j),'YData');
% ydata(:) = p(2,j);
set(h(j),'YData',[p(2,j) p(2,j)]);
end
% Replace lower end y value of whisker
h = flipud(findobj(gca,'Tag','Lower Whisker'));
for j=1:length(h);
% ydata = get(h(j),'YData');
% ydata(1) = p(1,j);
set(h(j),'YData',[q(1,j) p(1,j)]);
end
% Replace all y values of adjacent value
h = flipud(findobj(gca,'Tag','Lower Adjacent Value'));
for j=1:length(h);
% ydata = get(h(j),'YData');
% ydata(:) = p(1,j);
set(h(j),'YData',[p(1,j) p(1,j)]);
end
具体效果如下,各位科研小伙伴可以根据该代码进行相应更改。
三、参考文献
关于每个箱子含义,以及更详细的解释,读者可以查阅下面两篇原文,里面有更为详细的解释。
[1] Zengfu Hou, Wei Li, Ran Tao, Pengge Ma, and Weihua Shi. Collaborative Representation with Background Purification and Saliency Weight for Hyperspectral Anomaly Detection[J]. Science China Information Sciences, 2022 Jan, 65(1):1-12. doi: https://doi.org/10.1007/s11432-020-2915-2.
[2] Jun Liu, Zengfu Hou, Wei Li, Ran Tao, Pengge Ma, Danilo Orlando, and Hongbin Li. Multipixel Anomaly Detection With an Unknown Pattern for Hyperspectral Imagery[J]. IEEE Transactions on Neural Networks and Learning Systems, 2021, doi: 10.1109/TNNLS.2021.3071026.