function nl_srgb2=histogram_equalization(nl_srgb, col_out, neg, yuv)
%Algoritmus pro Histogram Equalization využívající kumulační distribuční
%funkci
R=nl_srgb(:,:,1); G=nl_srgb(:,:,2); B=nl_srgb(:,:,3);
if yuv==1 && strcmp(col_out,'RGB_HE') && neg==0
Y = 0.299*R + 0.587*G + 0.114*B; %využití YUV prostoru abychom zachovali poměr barev
U=(B-Y)*0.492; %měníme pouze Y složku, který představuje jas
V=(R-Y)*0.877;
pom=round(Y*255);
pom=uint8(pom);
[CDF, x]=imhist(pom);
velikost=size(Y);
CDF(:,2)=x;
CDF(any(CDF(:,1)==0,2),:)=[];
CDF(:,3)=cumsum(CDF(:,1));
for i=1:length(CDF(:,1))
     CDF(i,4)=round((CDF(i,3)-CDF(1,1))/(velikost(1)*velikost(2)-CDF(1,1))*255);
end
for i=2:length(CDF)
   loc= pom==CDF(i,2);
   Y(loc)=CDF(i,4);
end
Y=Y/255;
R = Y + 1.13983*V;
G = Y - 0.39465*U - 0.58060*V;
B = Y + 2.03211*U;
nl_srgb2=cat(3,R,G,B);
elseif yuv==1 && neg==1 && strcmp(col_out,'RGB_HE')
    Y = 0.299*R + 0.587*G + 0.114*B; %využití YUV prostoru abychom zachovali poměr barev
U=(B-Y)*0.492; %měníme pouze Y složku, který představuje jas
V=(R-Y)*0.877;
pom=round(Y*255);
pom=uint8(pom);
[CDF, x]=imhist(pom);
velikost=size(Y);
CDF(:,2)=x;
CDF(any(CDF(:,1)==0,2),:)=[];
CDF(:,3)=cumsum(CDF(:,1));
for i=1:length(CDF(:,1))
     CDF(i,4)=round((CDF(i,3)-CDF(1,1))/(velikost(1)*velikost(2)-CDF(1,1))*255);
end
for i=2:length(CDF)
   loc= pom==CDF(i,2);
   Y(loc)=CDF(i,4);
end
Y=Y/255;
Y=1-Y;
R = Y + 1.13983*V;
G = Y - 0.39465*U - 0.58060*V;
B = Y + 2.03211*U;
nl_srgb2=cat(3,R,G,B);
elseif yuv==1 && strcmp(col_out,'R_HE') && neg==0
    Y = 0.299*R + 0.587*G + 0.114*B; %využití YUV prostoru abychom zachovali poměr barev
V=(R-Y)*0.877;
pom=round(Y*255);
pom=uint8(pom);
[CDF, x]=imhist(pom);
velikost=size(Y);
CDF(:,2)=x;
CDF(any(CDF(:,1)==0,2),:)=[];
CDF(:,3)=cumsum(CDF(:,1));
for i=1:length(CDF(:,1))
     CDF(i,4)=round((CDF(i,3)-CDF(1,1))/(velikost(1)*velikost(2)-CDF(1,1))*255);
end
for i=2:length(CDF)
   loc= pom==CDF(i,2);
   Y(loc)=CDF(i,4);
end
Y=Y/255;
R = Y + 1.13983*V;
nl_srgb2=R;
elseif yuv==1 && strcmp(col_out,'R_HE') && neg==1
    Y = 0.299*R + 0.587*G + 0.114*B; %využití YUV prostoru abychom zachovali poměr barev
V=(R-Y)*0.877;
pom=round(Y*255);
pom=uint8(pom);
[CDF, x]=imhist(pom);
velikost=size(Y);
CDF(:,2)=x;
CDF(any(CDF(:,1)==0,2),:)=[];
CDF(:,3)=cumsum(CDF(:,1));
for i=1:length(CDF(:,1))
     CDF(i,4)=round((CDF(i,3)-CDF(1,1))/(velikost(1)*velikost(2)-CDF(1,1))*255);
end
for i=2:length(CDF)
   loc= pom==CDF(i,2);
   Y(loc)=CDF(i,4);
end
Y=Y/255;
Y=1-Y;
R = Y + 1.13983*V;
nl_srgb2=R;
elseif yuv==1 && neg==0 && strcmp(col_out,'G_HE')
    Y = 0.299*R + 0.587*G + 0.114*B; %využití YUV prostoru abychom zachovali poměr barev
U=(B-Y)*0.492; %měníme pouze Y složku, který představuje jas
V=(R-Y)*0.877;
pom=round(Y*255);
pom=uint8(pom);
[CDF, x]=imhist(pom);
velikost=size(Y);
CDF(:,2)=x;
CDF(any(CDF(:,1)==0,2),:)=[];
CDF(:,3)=cumsum(CDF(:,1));
for i=1:length(CDF(:,1))
     CDF(i,4)=round((CDF(i,3)-CDF(1,1))/(velikost(1)*velikost(2)-CDF(1,1))*255);
end
for i=2:length(CDF)
   loc= pom==CDF(i,2);
   Y(loc)=CDF(i,4);
end
Y=Y/255;
G = Y - 0.39465*U - 0.58060*V;
nl_srgb2=G;
elseif yuv==1 && neg==1 && strcmp(col_out,'G_HE')
    Y = 0.299*R + 0.587*G + 0.114*B; %využití YUV prostoru abychom zachovali poměr barev
U=(B-Y)*0.492; %měníme pouze Y složku, který představuje jas
V=(R-Y)*0.877;
pom=round(Y*255);
pom=uint8(pom);
[CDF, x]=imhist(pom);
velikost=size(Y);
CDF(:,2)=x;
CDF(any(CDF(:,1)==0,2),:)=[];
CDF(:,3)=cumsum(CDF(:,1));
for i=1:length(CDF(:,1))
     CDF(i,4)=round((CDF(i,3)-CDF(1,1))/(velikost(1)*velikost(2)-CDF(1,1))*255);
end
for i=2:length(CDF)
   loc= pom==CDF(i,2);
   Y(loc)=CDF(i,4);
end
Y=Y/255;
Y=1-Y;
G = Y - 0.39465*U - 0.58060*V;
nl_srgb2=G;
elseif yuv==1 && neg==0 && strcmp(col_out,'B_HE')
    Y = 0.299*R + 0.587*G + 0.114*B; %využití YUV prostoru abychom zachovali poměr barev
U=(B-Y)*0.492; %měníme pouze Y složku, který představuje jas
pom=round(Y*255);
pom=uint8(pom);
[CDF, x]=imhist(pom);
velikost=size(Y);
CDF(:,2)=x;
CDF(any(CDF(:,1)==0,2),:)=[];
CDF(:,3)=cumsum(CDF(:,1));
for i=1:length(CDF(:,1))
     CDF(i,4)=round((CDF(i,3)-CDF(1,1))/(velikost(1)*velikost(2)-CDF(1,1))*255);
end
for i=2:length(CDF)
   loc= pom==CDF(i,2);
   Y(loc)=CDF(i,4);
end
Y=Y/255;
B = Y + 2.03211*U;
nl_srgb2=B;
elseif yuv==1 && neg==1 && strcmp(col_out,'B_HE')
    Y = 0.299*R + 0.587*G + 0.114*B; %využití YUV prostoru abychom zachovali poměr barev
U=(B-Y)*0.492; %měníme pouze Y složku, který představuje jas
pom=round(Y*255);
pom=uint8(pom);
[CDF, x]=imhist(pom);
velikost=size(Y);
CDF(:,2)=x;
CDF(any(CDF(:,1)==0,2),:)=[];
CDF(:,3)=cumsum(CDF(:,1));
for i=1:length(CDF(:,1))
     CDF(i,4)=round((CDF(i,3)-CDF(1,1))/(velikost(1)*velikost(2)-CDF(1,1))*255);
end
for i=2:length(CDF)
   loc= pom==CDF(i,2);
   Y(loc)=CDF(i,4);
end
Y=Y/255;
Y=1-Y;
B = Y + 2.03211*U;
nl_srgb2=B;
elseif yuv==0 && strcmp(col_out,'RGB_HE') && neg==0
pom1=round(R*255);
pom2=round(G*255);
pom3=round(B*255);
pom1=uint8(pom1);
pom2=uint8(pom2);
pom3=uint8(pom3);
[CDF_R, x_R]=imhist(pom1);
[CDF_G, x_G]=imhist(pom2);
[CDF_B, x_B]=imhist(pom3);
velikost_R=size(R);
velikost_G=size(G);
velikost_B=size(B);
CDF_R(:,2)=x_R;
CDF_G(:,2)=x_G;
CDF_B(:,2)=x_B;
CDF_R(any(CDF_R(:,1)==0,2),:)=[];
CDF_G(any(CDF_G(:,1)==0,2),:)=[];
CDF_B(any(CDF_B(:,1)==0,2),:)=[];
CDF_R(:,3)=cumsum(CDF_R(:,1));
CDF_G(:,3)=cumsum(CDF_G(:,1));
CDF_B(:,3)=cumsum(CDF_B(:,1));
for i=1:length(CDF_R(:,1))
     CDF_R(i,4)=round((CDF_R(i,3)-CDF_R(1,1))/(velikost_R(1)*velikost_R(2)-CDF_R(1,1))*255);
end
for i=1:length(CDF_G(:,1))
     CDF_G(i,4)=round((CDF_G(i,3)-CDF_G(1,1))/(velikost_G(1)*velikost_G(2)-CDF_G(1,1))*255);
end
for i=1:length(CDF_B(:,1))
     CDF_B(i,4)=round((CDF_B(i,3)-CDF_B(1,1))/(velikost_B(1)*velikost_B(2)-CDF_B(1,1))*255);
end
for i=2:length(CDF_R)
   loc= pom1==CDF_R(i,2);
   R(loc)=CDF_R(i,4);
end
for i=2:length(CDF_G)
   loc= pom2==CDF_G(i,2);
   G(loc)=CDF_G(i,4);
end
for i=2:length(CDF_B)
   loc= pom3==CDF_B(i,2);
   B(loc)=CDF_B(i,4);
end
R=R/255;
G=G/255;
B=B/255;
nl_srgb2=cat(3,R,G,B);
elseif yuv==0 && strcmp(col_out,'RGB_HE') && neg==1
pom1=round(R*255);
pom2=round(G*255);
pom3=round(B*255);
pom1=uint8(pom1);
pom2=uint8(pom2);
pom3=uint8(pom3);
[CDF_R, x_R]=imhist(pom1);
[CDF_G, x_G]=imhist(pom2);
[CDF_B, x_B]=imhist(pom3);
velikost_R=size(R);
velikost_G=size(G);
velikost_B=size(B);
CDF_R(:,2)=x_R;
CDF_G(:,2)=x_G;
CDF_B(:,2)=x_B;
CDF_R(any(CDF_R(:,1)==0,2),:)=[];
CDF_G(any(CDF_G(:,1)==0,2),:)=[];
CDF_B(any(CDF_B(:,1)==0,2),:)=[];
CDF_R(:,3)=cumsum(CDF_R(:,1));
CDF_G(:,3)=cumsum(CDF_G(:,1));
CDF_B(:,3)=cumsum(CDF_B(:,1));
for i=1:length(CDF_R(:,1))
     CDF_R(i,4)=round((CDF_R(i,3)-CDF_R(1,1))/(velikost_R(1)*velikost_R(2)-CDF_R(1,1))*255);
end
for i=1:length(CDF_G(:,1))
     CDF_G(i,4)=round((CDF_G(i,3)-CDF_G(1,1))/(velikost_G(1)*velikost_G(2)-CDF_G(1,1))*255);
end
for i=1:length(CDF_B(:,1))
     CDF_B(i,4)=round((CDF_B(i,3)-CDF_B(1,1))/(velikost_B(1)*velikost_B(2)-CDF_B(1,1))*255);
end
for i=2:length(CDF_R)
   loc= pom1==CDF_R(i,2);
   R(loc)=CDF_R(i,4);
end
for i=2:length(CDF_G)
   loc= pom2==CDF_G(i,2);
   G(loc)=CDF_G(i,4);
end
for i=2:length(CDF_B)
   loc= pom3==CDF_B(i,2);
   G(loc)=CDF_B(i,4);
end
R=R/255;
R=1-R;
G=G/255;
G=1-G;
B=B/255;
B=1-B;
nl_srgb2=cat(3,R,G,B);
elseif yuv==0 && neg==0 && strcmp(col_out,'R_HE')
   pom=round(R*255);
pom=uint8(pom);
[CDF, x]=imhist(pom);
velikost=size(R);
CDF(:,2)=x;
CDF(any(CDF(:,1)==0,2),:)=[];
CDF(:,3)=cumsum(CDF(:,1));
for i=1:length(CDF(:,1))
     CDF(i,4)=round((CDF(i,3)-CDF(1,1))/(velikost(1)*velikost(2)-CDF(1,1))*255);
end
for i=2:length(CDF)
   loc= pom==CDF(i,2);
   R(loc)=CDF(i,4);
end
R=R/255;
nl_srgb2=R;
elseif yuv==0 && neg==1 && strcmp(col_out,'R_HE')
   pom=round(R*255);
pom=uint8(pom);
[CDF, x]=imhist(pom);
velikost=size(R);
CDF(:,2)=x;
CDF(any(CDF(:,1)==0,2),:)=[];
CDF(:,3)=cumsum(CDF(:,1));
for i=1:length(CDF(:,1))
     CDF(i,4)=round((CDF(i,3)-CDF(1,1))/(velikost(1)*velikost(2)-CDF(1,1))*255);
end
for i=2:length(CDF)
   loc= pom==CDF(i,2);
   R(loc)=CDF(i,4);
end
R=R/255;
R=1-R;
nl_srgb2=R;
elseif yuv==0 && neg==0 && strcmp(col_out,'G_HE')
   pom=round(G*255);
pom=uint8(pom);
[CDF, x]=imhist(pom);
velikost=size(G);
CDF(:,2)=x;
CDF(any(CDF(:,1)==0,2),:)=[];
CDF(:,3)=cumsum(CDF(:,1));
for i=1:length(CDF(:,1))
     CDF(i,4)=round((CDF(i,3)-CDF(1,1))/(velikost(1)*velikost(2)-CDF(1,1))*255);
end
for i=2:length(CDF)
   loc= pom==CDF(i,2);
   G(loc)=CDF(i,4);
end
G=G/255;
nl_srgb2=G;
elseif yuv==0 && neg==1 && strcmp(col_out,'G_HE')
   pom=round(G*255);
pom=uint8(pom);
[CDF, x]=imhist(pom);
velikost=size(G);
CDF(:,2)=x;
CDF(any(CDF(:,1)==0,2),:)=[];
CDF(:,3)=cumsum(CDF(:,1));
for i=1:length(CDF(:,1))
     CDF(i,4)=round((CDF(i,3)-CDF(1,1))/(velikost(1)*velikost(2)-CDF(1,1))*255);
end
for i=2:length(CDF)
   loc= pom==CDF(i,2);
   G(loc)=CDF(i,4);
end
G=G/255;
G=1-G;
nl_srgb2=G;
elseif yuv==0 && neg==0 && strcmp(col_out,'B_HE')
   pom=round(B*255);
pom=uint8(pom);
[CDF, x]=imhist(pom);
velikost=size(B);
CDF(:,2)=x;
CDF(any(CDF(:,1)==0,2),:)=[];
CDF(:,3)=cumsum(CDF(:,1));
for i=1:length(CDF(:,1))
     CDF(i,4)=round((CDF(i,3)-CDF(1,1))/(velikost(1)*velikost(2)-CDF(1,1))*255);
end
for i=2:length(CDF)
   loc= pom==CDF(i,2);
   B(loc)=CDF(i,4);
end
B=B/255;
nl_srgb2=B;
elseif yuv==0 && neg==1 && strcmp(col_out,'B_HE')
    pom=round(B*255);
    pom=uint8(pom);
    [CDF, x]=imhist(pom);
    velikost=size(B);
    CDF(:,2)=x;
    CDF(any(CDF(:,1)==0,2),:)=[];
    CDF(:,3)=cumsum(CDF(:,1));
    for i=1:length(CDF(:,1))
        CDF(i,4)=round((CDF(i,3)-CDF(1,1))/(velikost(1)*velikost(2)-CDF(1,1))*255);
    end
    for i=2:length(CDF)
        loc= pom==CDF(i,2);
        B(loc)=CDF(i,4);
    end
    B=B/255;
    B=1-B;
    nl_srgb2=B;
    elseif yuv==1 && neg==0 && strcmp(col_out,'GB_HE')
    Y = 0.299*R + 0.587*G + 0.114*B; %využití YUV prostoru abychom zachovali poměr barev
U=(B-Y)*0.492; %měníme pouze Y složku, který představuje jas
V=(R-Y)*0.877;
pom=round(Y*255);
pom=uint8(pom);
[CDF, x]=imhist(pom);
velikost=size(Y);
CDF(:,2)=x;
CDF(any(CDF(:,1)==0,2),:)=[];
CDF(:,3)=cumsum(CDF(:,1));
for i=1:length(CDF(:,1))
     CDF(i,4)=round((CDF(i,3)-CDF(1,1))/(velikost(1)*velikost(2)-CDF(1,1))*255);
end
for i=2:length(CDF)
   loc= pom==CDF(i,2);
   Y(loc)=CDF(i,4);
end
Y=Y/255;
B = Y + 2.03211*U;
G = Y - 0.39465*U - 0.58060*V;
B=(G+B)/2;
nl_srgb2=B;
elseif yuv==1 && neg==1 && strcmp(col_out,'GB_HE')
    Y = 0.299*R + 0.587*G + 0.114*B; %využití YUV prostoru abychom zachovali poměr barev
U=(B-Y)*0.492; %měníme pouze Y složku, který představuje jas
V=(R-Y)*0.877;
pom=round(Y*255);
pom=uint8(pom);
[CDF, x]=imhist(pom);
velikost=size(Y);
CDF(:,2)=x;
CDF(any(CDF(:,1)==0,2),:)=[];
CDF(:,3)=cumsum(CDF(:,1));
for i=1:length(CDF(:,1))
     CDF(i,4)=round((CDF(i,3)-CDF(1,1))/(velikost(1)*velikost(2)-CDF(1,1))*255);
end
for i=2:length(CDF)
   loc= pom==CDF(i,2);
   Y(loc)=CDF(i,4);
end
Y=Y/255;
Y=1-Y;
B = Y + 2.03211*U;
G = Y - 0.39465*U - 0.58060*V;
B=(G+B)/2;
nl_srgb2=B;
elseif yuv==0 && neg==0 && strcmp(col_out,'GB_HE')
    B=(G+B)/2;
   pom=round(B*255);
pom=uint8(pom);
[CDF, x]=imhist(pom);
velikost=size(B);
CDF(:,2)=x;
CDF(any(CDF(:,1)==0,2),:)=[];
CDF(:,3)=cumsum(CDF(:,1));
for i=1:length(CDF(:,1))
     CDF(i,4)=round((CDF(i,3)-CDF(1,1))/(velikost(1)*velikost(2)-CDF(1,1))*255);
end
for i=2:length(CDF)
   loc= pom==CDF(i,2);
   B(loc)=CDF(i,4);
end
B=B/255;
nl_srgb2=B;
elseif yuv==0 && neg==1 && strcmp(col_out,'GB_HE')
    B=(G+B)/2;
    pom=round(B*255);
    pom=uint8(pom);
    [CDF, x]=imhist(pom);
    velikost=size(B);
    CDF(:,2)=x;
    CDF(any(CDF(:,1)==0,2),:)=[];
    CDF(:,3)=cumsum(CDF(:,1));
    for i=1:length(CDF(:,1))
        CDF(i,4)=round((CDF(i,3)-CDF(1,1))/(velikost(1)*velikost(2)-CDF(1,1))*255);
    end
    for i=2:length(CDF)
        loc= pom==CDF(i,2);
        B(loc)=CDF(i,4);
    end
    B=B/255;
    B=1-B;
    nl_srgb2=B;
end
