close all; clear all; clc; profile on; 

tic;

% velikost okolí
hs=20;
hr1=1/15;
hr2=1/15;

filename='..\vstup_vid\segmenty.avi';


obj=mmreader(filename);
frames=get(obj,'numberOfFrames');
frames_s=get(obj,'FrameRate');



for frame=21324:frames
% x=ceil(rand(1)*frames);

x=read(obj,frame);

x=x(1:end-50,:,:);
x=imresize(x,1/8);

x=imfilter(x,fspecial('gaussian',3,1));
img=x;

% figure,imshow(x);

x=rgb2hsv(x);
[a b c]=size(x);

x=(im2vect(x)');
y=x;
[row,col,dim] = size(x);

maximum=max(x,[],2);
minimum=min(x,[],2);
pause(0.01)
hyst=zeros(1,col);

for i=1:col
stred=x(:,i);
rozdil=100;
cetnost=0;
while (rozdil>.1)

% vmezení akruálního okolí
range(1,1)=stred(1)-hs;
range(1,2)=stred(1)+hs;
range(2,1)=stred(2)-hs;
range(2,2)=stred(2)+hs;
range(3,1)=stred(3)-hr2;
range(3,2)=stred(3)+hr2;
range(4,1)=stred(4)-hr1;
range(4,2)=stred(4)+hr1;
range(5,1)=stred(5)-hr1;
range(5,2)=stred(5)+hr1;

% získání dat z okolí
x_temp=x;

data_temp=find(x_temp(1,:)<range(1,2) & x_temp(1,:)>range(1,1) );
x_temp=x_temp(:,data_temp);

data_temp=find(x_temp(2,:)<range(2,2) & x_temp(2,:)>range(2,1) );
x_temp=x_temp(:,data_temp);

data_temp=find(x_temp(3,:)<range(3,2) & x_temp(3,:)>range(3,1) );
x_temp=x_temp(:,data_temp);

data_temp=find(x_temp(4,:)<range(4,2) & x_temp(4,:)>range(4,1) );
x_temp=x_temp(:,data_temp);

data_temp=find(x_temp(5,:)<range(5,2) & x_temp(5,:)>range(5,1) );
x_temp=x_temp(:,data_temp);

% výpočet nové pozice
new_stred=mean(x_temp,2);
rozdil=sqrt(sum((new_stred-stred).^2));

stred=new_stred;

cetnost=cetnost+1;

end

y(:,i)=(stred);
hyst(i)=cetnost;

end
% výsledek mean-shift
out(:,:,1)=reshape(y(3,:),[a b]);
out(:,:,2)=reshape(y(4,:),[a b]);
out(:,:,3)=reshape(y(5,:),[a b]);
out=hsv2rgb(out);


% figure(1234562),imshow(out);
% figure(),bar(hyst);
disp('mean-shift');
toc

%% segmentace
yy=y;
prah(1,1,:)=[4.5;
             4.5;
             0.015;
             0.010;
             0.100];
         
z(:,:,1)=(reshape(y(1,:),[a b]));
z(:,:,2)=(reshape(y(2,:),[a b]));
z(:,:,3)=(reshape(y(3,:),[a b]));
z(:,:,4)=(reshape(y(4,:),[a b]));
z(:,:,5)=(reshape(y(5,:),[a b]));

% figure
% subplot(2,3,1)
% imshow(z(:,:,1),[]),colormap('default')
% subplot(2,3,2)
% imshow(z(:,:,2),[]),colormap('default')
% subplot(2,3,3)
% imshow(z(:,:,3),[]),colormap('default')
% subplot(2,3,4)
% imshow(z(:,:,4),[]),colormap('default')
% subplot(2,3,5)
% imshow(z(:,:,5),[]),colormap('default')
% subplot(2,3,6)
% imshow(img,[])

%supergrid
s = ones( 2*a+1, 2*b+1, 'int8' );
s(1:2:(2*a+1),:) = zeros( a+1, 2*b+1, 'int8' );
s(:,1:2:(2*b+1)) = zeros( 2*a+1, b+1, 'int8' );

disp('supergrid')
tic
i=0;
for m=2:2:2*a
    i=i+1;
    j=0;
    for n=3:2:2*b
        j=j+1;
        if (sqrt(sum((z(i,j,3:5)-z(i,j+1,3:5)).^2))<0.05)
        s(m,n)=1;
        end
    end
end

i=0;
for m=3:2:2*a
    i=i+1;
    j=0;
    for n=2:2:2*b
        j=j+1;
        if (sqrt(sum((z(i,j,3:5)-z(i+1,j,3:5)).^2))<0.05)
        s(m,n)=1;
        end
    end
end
toc

% figure,imshow(s,[]);

l = bwlabel(s,4); % find connected regions
% figure,imshow(label2rgb(l))
l = l(2:2:2*a, 2:2:2*b); % extract labeling
segmented=label2rgb(l);
% figure,imshow(segmented)

transparent = imlincomb(1, img, 0.5, 255-segmented);
% figure,imshow(transparent,[])

maximum=max(max(l))
[m,n]=size(l);
out2=zeros(m,n,3);

disp('obarveni')
tic
for h=1:maximum
    for k=1:3
        BGM = l == h;
        BGM = uint8(BGM).*img(:,:,k);

        [x,y]=find(BGM>0);
        m=length(x);
        prumer=sum(sum(BGM))/m;

        for i=1:m
            out2(x(i),y(i),k)=prumer;
        end
    end
end
toc

% figure,imshow(uint8(out2)),text(8,88,['oblastí: ' num2str(maximum)])
out5=out2;
figure(1)
subplot(2,2,2),imshow(out),title('Výsledek mean-shift')
subplot(2,2,1),imshow(img),title('Orginální obrázek')
subplot(2,2,[3 4]),imshow(uint8(out2)),text(8,a-10,['oblastí: ' num2str(maximum)]),title('Výsledek po segmentaci')

%% morfologie pro optimalizaci regionů

se=strel('diamond',1);
s=imfill(s,'holes');
% figure,imshow(s,[])
s=imerode(s,se);
% figure,imshow(s,[])
s=imdilate(s,se);
% figure,imshow(s,[])
s=bwareaopen(s,ceil(a/20));
% figure,imshow(s,[])

l = bwlabel(s,4); % find connected regions
% figure,imshow(label2rgb(l))
l = l(2:2:2*a, 2:2:2*b); % extract labeling
segmented=label2rgb(l);
% figure,imshow(segmented)


transparent = imlincomb(1, img, 0.5, 255-segmented);
% figure,imshow(transparent,[])

maximum=max(max(l))
[m,n]=size(l);
out2=zeros(m,n,3);

disp('obarveni')
tic
for h=1:maximum
    for k=1:3
        BGM = l == h;
        BGM = uint8(BGM).*img(:,:,k);

        [x,y]=find(BGM>0);
        m=length(x);
        prumer=sum(sum(BGM))/m;

        for i=1:m
            out2(x(i),y(i),k)=prumer;
        end
    end
end
toc
% figure,imshow(uint8(out2)),text(8,88,['oblastí: ' num2str(maximum)])

saveim=figure(2),
subplot(2,2,2),imshow(out),title('Výsledek mean-shift')
subplot(2,2,1),imshow(img),title('Orginální obrázek')
subplot(2,2,4),imshow(uint8(out2)),text(8,a-10,['oblastí: ' num2str(maximum)]),title('Výsledek po segmentaci + odstraneni malých oblastí')
subplot(2,2,3 ),imshow(uint8(out5)),text(8,a-10,['oblastí: ' num2str(maximum)]),title('Výsledek po segmentaci')

% saveas(saveim,['img\4\',num2str(iter_vid),'.png'],'png');
pause()

end

