function CSGV = CSGvektor(segment)
% proracun CSG vektora
%clear all;
%clc;

% ucitavanje slike
%[slika, map] = imread('jedna_tek_2_512.gif'); %angio2.tif je dimenzije 64x64
%slika = ind2gray(slika, map);
%segment = double(slika);
%segment=segment/(max(max(segment)));

% wavelet dekompozicija slike
[S(:,:,1), S(:,:,2), S(:,:,3), S(:,:,4)] = swt2(segment,1, 'db4');

% definicija maski. Velicina je 5x5, i ima se 15 razlicitih rotacija.
% maska_osnovna = [-1,-1,-1,-2,0,2,1,1,1;,...
%                  -1,-1,-1,-2,0,2,1,1,1;,...
%                  -1,-1,-1,-2,0,2,1,1,1;,...
%                  -1,-1,-1,-2,0,2,1,1,1;,...
%                  -1,-1,-1,-2,0,2,1,1,1;,...
%                  -1,-1,-1,-2,0,2,1,1,1;,...
%                  -1,-1,-1,-2,0,2,1,1,1;,...
%                  -1,-1,-1,-2,0,2,1,1,1;,...
%                  -1,-1,-1,-2,0,2,1,1,1;];
% velicina_maska_osnovna = max(size(maska_osnovna));
%      
% for i=1:36
%     temp = imrotate(maska_osnovna,i*10,'bilinear');
%     velicina_temp = max(size(temp));
%     if (velicina_temp==(velicina_maska_osnovna+4))
%         maska(:,:,i) =  temp;
%     else
%         maska(3:11,3:11,i) = temp;
%     end;
% end;

maska(:,:,1) = [-1,-2,0,2,1;,...
                -1,-2,0,2,1;,...
                -1,-2,0,2,1;,...
                -1,-2,0,2,1;,...
                -1,-2,0,2,1;];
maska(:,:,2) = [-2,0,2,1,1;,...
                -1,-2,0,2,1;,...
                -1,-2,0,2,1;,...
                -1,-2,0,2,1;,...
                -1,-1,-2,0,2];
maska(:,:,3) = [0,2,1,1,1;,...
                -2,0,2,2,1;,...
                -1,-2,0,2,1;,...
                -1,-2,-2,0,2;,...
                -1,-1,-1,-2,0];
maska(:,:,4) = [2,1,1,1,1;,...
                0,0,2,2,1;,...
                -2,-2,0,2,2;,...
                -1,-2,-2,0,0;,...
                -1,-1,-1,-1,-2];
maska(:,:,5) = [1,1,1,1,1;,...
                2,2,2,2,2;,...
                0,0,0,0,0;,...
                -2,-2,-2,-2,-2;,...
                -1,-1,-1,-1,-1];
maska(:,:,6) = [1,1,1,1,2;,...
                1,2,2,2,0;,...
                2,0,0,0,-2;,...
                0,-2,-2,-2,-1;,...
                -2,-1,-1,-1,-1];
maska(:,:,7) = rot90(maska(:,:,3));
maska(:,:,8) = rot90(maska(:,:,4));
maska(:,:,9) = fliplr(maska(:,:,1));
maska(:,:,10) = fliplr(flipud(maska(:,:,2)));
maska(:,:,11) = fliplr(flipud(maska(:,:,3)));
maska(:,:,12) = fliplr(flipud(maska(:,:,4)));
maska(:,:,13) = flipud(maska(:,:,5));
maska(:,:,14) = fliplr(flipud(maska(:,:,6)));
maska(:,:,15) = fliplr(flipud(maska(:,:,7)));
maska(:,:,16) = fliplr(flipud(maska(:,:,8)));

% vrtimo konvoluciju po svakoj maski i moramo imat 3-dimenzionalnu
% matricu dimenzija (velicina_slike, velicina_maske, 16)

for i=1:4
    arg1 = S(:,:,i);
    for j=1:16
        arg2 = maska(:,:,j);
        konvolucija_temp = conv2(arg1, arg2, 'same');
        % dimenzije konvolucije:
        %   prva - red konvolucije
        %   druga - stupac konvolucije
        %   treca - broj konvolucije
        %   cetvrta - kojem segmentu pripada
        konvolucija(:,:,j,i) = konvolucija_temp(:,:);
    end;
end;

velicina = max(size(S(:,:,1)));
SGV = zeros(velicina,velicina,4);
for i=1:4
    for k=1:velicina
        for l=1:velicina
            konv = max(konvolucija(k,l,:,i));
            if(konv>SGV(k,l,i))
                SGV(k,l,i) = konv;
            end;
        end;
    end;
end;

% slaganje CSG vektora
SGV1 = imhist(SGV(:,:,1)/(max(max(SGV(:,:,1)))),16);
SGV2 = imhist(SGV(:,:,2)/(max(max(SGV(:,:,2)))),16);
SGV3 = imhist(SGV(:,:,3)/(max(max(SGV(:,:,3)))),16);
SGV4 = imhist(SGV(:,:,4)/(max(max(SGV(:,:,4)))),16);
CSGV = [SGV1; SGV2; SGV3;];% SGV4];