%SCRIPT_SampleImagesForBigFigure
%
%intent: generate truesem, learnedsem, and context images for a given SSImg.

%CAidx = 918;

FigRoot = '/cbcl/scratch01/bileschi/PrecomputedFeatures/StreetScenes/AppearanceDetector/Figures/BigPicture';
SAVEON = 1;
load ModelLinSVMContext_bicycle;%-->ModelLinSVMContext_bicycle
load ModelLinSVMContext_Pedestrian;%-->ModelLinSVMContext_Pedestrian
load ModelLinSVMContext_car;%-->ModelLinSVMContext_car
%load ModelLinSVMContext_Bicycle_noColTex;%-->ModelLinSVMContext_Bicycle_noColTex
%load ModelLinSVMContext_Pedestrian_noColTex;%-->ModelLinSVMContext_Pedestrian_noColTex
%load ModelLinSVMContext_Car_noColTex;%-->ModelLinSVMContext_Car_noColTex
%ModelLinSVMContext_bicycle = ModelLinSVMContext_Bicycle_noColTex;
%ModelLinSVMContext_car = ModelLinSVMContext_Car_noColTex;
%ModelLinSVMContext_Pedestrian = ModelLinSVMContext_Pedestrian_noColTex;


load 'Norm800Car';%-->Ntru_car, Nsvm_car;
load 'Norm800Ped';%-->Ntru_ped, Nsvm_ped;
load 'Norm800Bike';%-->Ntru_bike, Nsvm_bike;

load CAFileList_FilterSS;%-->CAfilelist;
PrecSemRoot = '/cbcl/scratch01/bileschi/PrecomputedFeatures/StreetScenes/LowLevelPixelDetectors';
PrecSemTemplate = 'Semantic_%.5d.mat';
MasksRoot = '/cbcl/scratch01/bileschi/PrecomputedFeatures/StreetScenes/semanticmasks';
MasksTemplate = 'semman_%.5d.mat';
Layer800Root = '/cbcl/scratch01/bileschi/EzContext/LayerImgs800_3547_LinSVMSem';
Layer800Template = 'Lay800_%.5d.mat';

im = imread(CAfilelist{CAidx}.imagename);

h = figure(1);
imshow(im);
if(SAVEON)
  imwrite(imresize(im,[240 320],'bilinear'),fullfile(FigRoot,sprintf('SourceImage_%.5d.jpg',CAidx)),'jpg');
  saveas(h,fullfile(FigRoot,sprintf('SourceImage_%.5d.epsc',CAidx)),'epsc');
end

load(fullfile(MasksRoot,sprintf(MasksTemplate,CAidx)));%-->Masks
figure(2);
z = imresize(Masks.building,[240 320]);
h = imshow(z);
if(SAVEON)
  imwrite(z,fullfile(FigRoot,sprintf('MaskTrue_Building_%.5d.jpg',CAidx)),'jpg');
  saveas(h,fullfile(FigRoot,sprintf('MaskTrue_Building_%.5d.epsc',CAidx)),'epsc');
end
figure(3);
z = imresize(Masks.tree,[240 320]);
h = imshow(z);
if(SAVEON)
  imwrite(z,fullfile(FigRoot,sprintf('MaskTrue_Tree_%.5d.jpg',CAidx)),'jpg');
  saveas(h,fullfile(FigRoot,sprintf('MaskTrue_Tree_%.5d.epsc',CAidx)),'epsc');
end
h = figure(4);
z = imresize(Masks.road,[240 320]);
imshow(z);
if(SAVEON)
  imwrite(z,fullfile(FigRoot,sprintf('MaskTrue_Road_%.5d.jpg',CAidx)),'jpg');
  saveas(h,fullfile(FigRoot,sprintf('MaskTrue_Road_%.5d.epsc',CAidx)),'epsc');
end
h = figure(5);
z = imresize(Masks.sky,[240 320]);
imshow(z);
if(SAVEON)
  imwrite(z,fullfile(FigRoot,sprintf('MaskTrue_Sky_%.5d.jpg',CAidx)),'jpg');
  saveas(h,fullfile(FigRoot,sprintf('MaskTrue_Sky_%.5d.epsc',CAidx)),'epsc');
end

h = figure(6);
load(fullfile(PrecSemRoot, 'building',sprintf(PrecSemTemplate,CAidx)));%-->CtxWeight
z = CtxWeight;
imshow(z);
if(SAVEON)
  imwrite(z,fullfile(FigRoot,sprintf('MaskEmp_Building_%.5d.jpg',CAidx)),'jpg');
  saveas(h,fullfile(FigRoot,sprintf('MaskEmp_Building_%.5d.epsc',CAidx)),'epsc');
end
h = figure(7);
load(fullfile(PrecSemRoot, 'tree',sprintf(PrecSemTemplate,CAidx)));%-->CtxWeight
z = 1-CtxWeight;
imshow(z);
if(SAVEON)
  imwrite(z,fullfile(FigRoot,sprintf('MaskEmp_Tree_%.5d.jpg',CAidx)),'jpg');
  saveas(h,fullfile(FigRoot,sprintf('MaskEmp_Tree_%.5d.epsc',CAidx)),'epsc');
end
h = figure(8);
load(fullfile(PrecSemRoot, 'road',sprintf(PrecSemTemplate,CAidx)));%-->CtxWeight
z = 1-CtxWeight;
imshow(z);
if(SAVEON)
  imwrite(z,fullfile(FigRoot,sprintf('MaskEmp_Road_%.5d.jpg',CAidx)),'jpg');
  saveas(h,fullfile(FigRoot,sprintf('MaskEmp_Road_%.5d.epsc',CAidx)),'epsc');
end
h = figure(9);
load(fullfile(PrecSemRoot, 'sky',sprintf(PrecSemTemplate,CAidx)));%-->CtxWeight
z = CtxWeight;
imshow(z);
if(SAVEON)
  imwrite(z,fullfile(FigRoot,sprintf('MaskEmp_Sky_%.5d.jpg',CAidx)),'jpg');
  saveas(h,fullfile(FigRoot,sprintf('MaskEmp_Sky_%.5d.epsc',CAidx)),'epsc');
end

load(fullfile(Layer800Root, sprintf(Layer800Template,CAidx)));%-->LayerImg800
L800f = nLayerImage2MatrixOfPixels(LayerImg800);
d.bRemoveSky = 0;
d.bRemoveColTex = 0;
d.bRemoveBTRS = 0;
d.bRemovePosition = 0;
d.bRemoveNearby = 0;
FSlist = FeatureSelection_BuildFrom800(d);

h = figure(10);
NX = outnormalizeDataX(L800f,Nsvm_bike);
NX = NX(find(FSlist),:);
[l,Ctx_bicycle] = CLSgentleBoostC(NX,ModelLinSVMContext_bicycle);
Ctx_bicycle = reshape(Ctx_bicycle,[60 80]);
z = (Ctx_bicycle + 4)/4;
imshow(z,[0 1]);
if(SAVEON)
  imwrite(z,fullfile(FigRoot,sprintf('Context_Bicycle_%.5d.jpg',CAidx)),'jpg');
  saveas(h,fullfile(FigRoot,sprintf('Context_Bicycle_%.5d.epsc',CAidx)),'epsc');
end

h = figure(11);
NX = outnormalizeDataX(L800f,Nsvm_car);
NX = NX(find(FSlist),:);
[l,Ctx_car] = CLSgentleBoostC(NX,ModelLinSVMContext_car);
Ctx_car = reshape(Ctx_car,[60 80]);
z = (Ctx_car + 4)/4;
imshow(z,[0 1]);
if(SAVEON)
  imwrite(z,fullfile(FigRoot,sprintf('Context_Car_%.5d.jpg',CAidx)),'jpg');
  saveas(h,fullfile(FigRoot,sprintf('Context_Car_%.5d.epsc',CAidx)),'epsc');
end

h= figure(12);
NX = outnormalizeDataX(L800f,Nsvm_ped);
NX = NX(find(FSlist),:);
[l,Ctx_Pedestrian] = CLSgentleBoostC(NX,ModelLinSVMContext_Pedestrian);
Ctx_Pedestrian = reshape(Ctx_Pedestrian,[60 80]);
z = (Ctx_Pedestrian + 4)/4;
imshow(z,[0 1]);
if(SAVEON)
  imwrite(z,fullfile(FigRoot,sprintf('Context_Pedestrian_%.5d.jpg',CAidx)),'jpg');
  saveas(h,fullfile(FigRoot,sprintf('Context_Pedestrian_%.5d.epsc',CAidx)),'epsc');
end
