%% Building Detection Using the MBI and Postprocessing Algorithms
% If you use the toolbox, please cite the following three papers:
% 
% Huang, Xin, and Liangpei Zhang. "A multidirectional and multiscale 
% morphological index for automatic building extraction from multispectral 
%GeoEye-1 imagery." Photogrammetric Engineering & Remote Sensing 
% 77.7 (2011): 721-732.
% 
% Huang, Xin, and Liangpei Zhang. "Morphological building/shadow index 
% for building extraction from high-resolution imagery over testimage areas." 
% IEEE Journal of Selected Topics in Applied Earth Observations and Remote  
% Sensing, 5.1 (2012): 161-172.

% Xin Huang, and Wenliang Yuan. "A new building extraction postprocessing 
% framework for high-spatial-resolution remote-sensing imagery." IEEE  
% Journal of Selected Topics in Applied Earth Observations and Remote 
% Sensing, 2016, DOI: 10.1109/JSTARS.2016.2587324.

% Code by Wenliang Yuan and Xin Huang 

%% Step 1: Load Images
% Read 4-bands (Blue Green Red Infrared) high-resolution remotely sensed
% image.

addpath('../data')
testimage = imread('Hangzhou.tif');

%% Step 2: Calculate MBI
% Input: 
% image: The input image 
% resolution: The resolution of the input image
% scale_min: The minimal size of the buildings in the input image. 
%            If the minimal size is 5m, then scale_min=5. 
%            Default: 5 
% scale_max: The maximal size of the buildings in the input image. 
%            If the maximal size is 85m, then scale_max=85. 
%            Default: 85

% Output:
% F_MBI: The MBI feature

mbi = MBI(testimage,2,5,85);

%% Step 3: Enhance the MBI Using Spectral Constraints 
% Input:
% image: The input image:4 bands (band 1:Blue band 2:Green band 3:Red band 
%        4:Near Infrared)
% resolution: The resolution of the input image
% mbi: The original MBI feature image.
% Tndvi_min: The minimal threshold of the NDVI 
%            Default value: 0.12    
%            Suggested range: [0.1, 0.16]
% Tndvi_max: The maximal threshold of the NDVI
%            Default value: 0.2     
%            Suggested range: [0.2, 0.3]

% Output:
% F_MBI: The refined MBI feature image

mbi_spec = MBI_Spectral(testimage,2,mbi,0.12,0.2);

%% Step 4: Binarization
% Tmbi: The threshold of refined MBI.
% Suggested range: [0.05, 0.3]

Tmbi = 0.12;
buildings_init = mbi_spec > Tmbi;

%% Step 5: Shadow Verification
% Input:
% image: 4 bands (band 1:Blue band 2:Green band 3:Red band 4:Near Infrared)
% building_init: The initial building extraction  result (binarization 
%                image, 1 stands for building, 0 stands for background).
% direction: The opposite direction of illumination.
%            illumination directin + 180
%            Range: [0, 360)
% Tnir: The threshold which is used to detect shadow in the NIR band.
%       Selected by obseving shadow in the image of the NIR band. 
% Trgb: The threshold which is used to detect shadow in the RGB bands(mean)
%       Selected by obseving shadow in the image of the RGB bands(mean).
% Ts: The size of the SE used to verify candidate buildings and their shadows. 
%     Default value: 11
%     Suggested range: [7, 15]
% Tarea: The minimum area of the shadow structure
%        Default value:1            
%        Suggested range: [1, 9]

% Output:
% buildings: The buildings refined by shadow verification

buildings_sv = MBI_Shadow(testimage,buildings_init,285,230,440,11,1);

%% Step 6: Shape Constraints
% Input:
% building_tmp: The temporary building extraction  result (binarization 
%               image, 1 stands for building, 0 stands for background).
% Tarea: The threshold of building area (pixels ), according to the 
%        resolution and building area.
%        Default value: 9 
% Textent: The ratio of pixels in the region to pixels in the total bounding box.
%          Default value: 0.12
% Tratio: The length-width ratio of  'BoundingBox'.
%         Default value: 7             
%         Suggested range: [7,10]

% Output:
% buildings: the refined buildings

buildings_sc = MBI_Shape(buildings_sv,9,0.12,7);

%% Step 7£ºSave the result

imwrite(buildings_sc,'testimage_buildings.tif','tif');

clear