%%

clear all;close all;clc


%% Load a image

image_folder = '/usr/local/share/Kitti/person/2011_09_28_drive_0053/background_masks/data/';
image_name = sprintf('%010d.png',15);

img = imread(fullfile(image_folder,image_name));

%% Apply filters
close all

img_median = medfilt2(img);
img_filled = imfill(img_median);

figure
imshow(img)

figure
imshow(img_median)

figure
imshow(img_filled)

contour = bwtraceboundary(img, [row, col], 'W', 8, 50,'counterclockwise'); 

imcontour(img,3)

stats = regionprops(img_median,'Area','Image');

areas=[];
for i=1:size(stats,1)
    areas=[areas; stats(i).Area];
end

[areas_sorted index]=sort(areas,'descend');

stats = stats(index);


%% Do loop 

files = dir(fullfile(image_folder,'*.png'));


for t=1:size(files,1)
    
    img = imread(fullfile(image_folder,files(t).name));
    img = logical(img);
    
    img_median = medfilt2(img);
    
    STATS = regionprops(img_median,'Area');
    
    img_filled = imfill(img_median,'holes');
    
    
    figure(1)
    imshow(img_filled)
    pause(0.1)
end