wei=WEIGHT;
FileIsRead=0;
% reference order the paper used
refm1=[3 4 5 3 4 5 %(ref 1)
4 5 2 4 5 2
5 2 3 5 2 3
2 3 4 2 3 4];

% other references
refm2=[4 5 3 4 5 3 %(ref 2)
5 2 4 5 2 4
2 3 5 2 3 5
3 4 2 3 4 2];
refm3=[5 3 4 5 3 4 %(ref 3)
2 4 5 2 4 5
3 5 2 3 5 2
4 2 3 4 2 3];

missions=[3 5 2 4];
passes=[5 5 3 3];
D=zeros(4,6);
FA=zeros(4,6);

for nw=size(x1,1):size(x1,1)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% This part is feature selecting part. It tries to cut the weight short
% to see if better result can be achieved

% [w id]=sort(abs(wei),'descend');
% nx=size(xx,2);
% nx1=size(x1,2);
% nx2=size(x2,2);
% clear xx1 xx2 xxx
% xxx=zeros(nw,nx);
% xx1=zeros(nw,nx1);
% xx2=zeros(nw,nx2);
% for i=1:nx1
% xx1(1:nw,i)=x1(id(1:nw),i);
% end
% for i=1:nx2
% xx2(1:nw,i)=x2(id(1:nw),i);
% end
% xxx=[xx1 xx2];
% clear history weight;
% [weit, history] = IRELIEF(xxx, targ, para);
% yyy=weit'*xxx;
% minyyy=min(yyy);
% maxyyy=max(yyy);
% weight=zeros(size(wei));
% weight(id(1:nw))=weit(1:nw);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% weight=WEIGHT; %use the weights learned in learning phase directly
fn=sprintf('learned_wnd%d_ref1.mat',2*r+1);
load(fn);
fn=sprintf('learned_wnd%d_ref2.mat',2*r+1);
load(fn);
fn=sprintf('learned_wnd%d_ref3.mat',2*r+1);
load(fn);
idx_th=1;
for thresh=5:5%1:0.5:7%1.65:1.65
for mis=2:5
for pas=1:6
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%ref1
refm=refm1;
%Read target image into im1 and reference image into im2
fn=sprintf('Mission%d\\pass%d',mis,pas);
info = VHF_get_image_info();
im1 = VHF_read_image(fn,info.n_cols,info.n_rows,1,info.n_cols,1,info.n_rows);
fn=sprintf('Mission%d\\pass%d',refm(mis-1,pas),pas);
im2 = VHF_read_image(fn,info.n_cols,info.n_rows,1,info.n_cols,1,info.n_rows);
fn=sprintf('Mission%d\\Targets.txt',mis);
deploy=VHF_read_target_list(fn);

%Get the difference
dif=im1-im2;
dif=dif.*(dif>thresh_1); %only consider the positive part
% dif1=single(dif);
dif=conv2(dif,h);
dif1=(dif(nborder+1:size(dif,1)-nborder,nborder+1:size(dif,2)-nborder));

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%ref2
refm=refm2;
%Read target image into im1 and reference image into im2
fn=sprintf('Mission%d\\pass%d',mis,pas);
info = VHF_get_image_info();
im1 = VHF_read_image(fn,info.n_cols,info.n_rows,1,info.n_cols,1,info.n_rows);
fn=sprintf('Mission%d\\pass%d',refm(mis-1,pas),pas);
im2 = VHF_read_image(fn,info.n_cols,info.n_rows,1,info.n_cols,1,info.n_rows);
% fn=sprintf('Mission%d\\Targets.txt',mis);
% deploy=VHF_read_target_list(fn);

%Get the difference
dif=im1-im2;
dif=dif.*(dif>thresh_1); %only consider the positive part
% dif1=single(dif);
dif=conv2(dif,h);
dif2=(dif(nborder+1:size(dif,1)-nborder,nborder+1:size(dif,2)-nborder));

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%ref3
refm=refm3;
%Read target image into im1 and reference image into im2
fn=sprintf('Mission%d\\pass%d',mis,pas);
info = VHF_get_image_info();
im1 = VHF_read_image(fn,info.n_cols,info.n_rows,1,info.n_cols,1,info.n_rows);
fn=sprintf('Mission%d\\pass%d',refm(mis-1,pas),pas);
im2 = VHF_read_image(fn,info.n_cols,info.n_rows,1,info.n_cols,1,info.n_rows);
% fn=sprintf('Mission%d\\Targets.txt',mis);
% deploy=VHF_read_target_list(fn);

%Get the difference
dif=im1-im2;
dif=dif.*(dif>thresh_1); %only consider the positive part
% dif1=single(dif);
dif=conv2(dif,h);
dif3=(dif(nborder+1:size(dif,1)-nborder,nborder+1:size(dif,2)-nborder));
clear dif;
clear im2 diff result1 result2 L result3 result4
disp(sprintf('\nTesting mission %d pass %d thresh %f...',mis,pas,thresh));

clear result result1 result11 result111;
result_1=GetMask_Dist_Ratio(dif1,weight1,PA,xtarg1,xback1);
result_2=GetMask_Dist_Ratio(dif2,weight2,PA,xtarg2,xback2);
result_3=GetMask_Dist_Ratio(dif3,weight3,PA,xtarg3,xback3);

%save result to files so that we don't have to recalculate
%them later for different thresh values
fn=sprintf('DistanceRatio_wnd%d_ref1\\Mission%dpass%d.mat',2*r+1,mis,pas);
save(fn,'result_1');
fn=sprintf('DistanceRatio_wnd%d_ref2\\Mission%dpass%d.mat',2*r+1,mis,pas);
save(fn,'result_2');
fn=sprintf('DistanceRatio_wnd%d_ref3\\Mission%dpass%d.mat',2*r+1,mis,pas);
save(fn,'result_3');
end
end
idx_th=idx_th+1;
end
end