SetDirectory[NotebookDirectory[]]
tmp1=ReadList["memb.pgm",Number];
tmp2=Partition[tmp1,100];
obr=tmp2/255.0;

fHistogram[array_]:=Module[
{i,hist,j,k},
hist=Table[0,{i,0,256}];
For[i=1,i≤Length[array],i++,
{
For[j=1,j<=Length[array[[1]]],j++,
{
For[k=1,k≤256,k++,If[Round[array[[i,j]]]==k,hist[[k]]=hist[[k]]+1]];
}];}];
Return[hist]
]
fAOD[array_]:=Module[
{},
Return[(1/(Length[array]*Length[array[[1]]]))*(∑_(i=1)^(Length[array]))(∑_(j=1)^(Length[array[[1]]]))array[[i,j]]]
]
fCenter[array_]:=Module[
{i,j,L,q=256,vystup=Table[0,{i,1,Length[array]},{j,1,Length[array[[1]]]}]},
L=fAOD[array]//N;
For[i=1,i≤Length[array],i++,
{
For[j=1,j<=Length[array[[1]]],j++,
{
(*If[j==100,Print[array[[i,j]]-L+q/2]];*)
vystup[[i,j]]=array[[i,j]]-L+q/2;
}];}];
Return[vystup]
]
fFSHS[array_]:=Module[
{A=Min[array],B=Max[array],q=256,P,L,i,j,vystup=Table[0,{i,1,Length[array]},{j,1,Length[array[[1]]]}]},

P=((q-1)/(B-A));
L=-A*((q-1)/(B-A));
For[i=1,i≤Length[array],i++,
{
For[j=1,j<=Length[array[[1]]],j++,
{
vystup[[i,j]]=((q-1)/(B-A))*(array[[i,j]]-A);
}];}];
Return[vystup];
]
fNegativ[array_]:=Module[
{vystup=Table[0,{i,1,Length[array]},{j,1,Length[array[[1]]]}],i,j,q=256},
For[i=1,i≤Length[array],i++,
{
For[j=1,j<=Length[array[[1]]],j++,
{
vystup[[i,j]]=(q-array[[i,j]]);
}];}];
Return[vystup]
]

fZrovnomernenie[array_]:=Module[
{hist,pf=Table[0,{i,1,255}],i,j,m,n,vystup=Table[0,{i,1,Length[array]},{j,1,Length[array[[1]]]}]},
hist=fHistogram[array];
m=Length[array];
n=Length[array[[1]]];
For[i=1,i≤255,i++,
pf[[i]]=(1/(m*n))*hist[[i]];];

For[i=1,i≤Length[array],i++,
{
For[j=1,j<=Length[array[[1]]],j++,
{
vystup[[i,j]]=(∑_(k=1)^(array[[i,j]]))pf[[k]];
}];}];
Return[fFSHS[vystup]]
]

gOriginal=ArrayPlot[obr,ColorFunction->GrayLevel,ColorFunctionScaling->False]
hOriginal=fHistogram[tmp2];
ListPlot[hOriginal,Filling->Axis]

dCentered=fCenter[tmp2];
gCentered=ArrayPlot[dCentered]
hCentered=fHistogram[dCentered];
ListPlot[hCentered,Filling->Axis]

dFshs=fFSHS[dCentered];
gFshs=ArrayPlot[dFshs]
hFshs=fHistogram[dFshs];
ListPlot[hFshs,Filling->Axis]

dNegativ=fNegativ[tmp2];
gNegativ=ArrayPlot[dNegativ]
hNegativ=fHistogram[dNegativ];
ListPlot[hNegativ,Filling->Axis]

dZrovnomernenie=fZrovnomernenie[tmp2];
gZrovnomernenie=ArrayPlot[dZrovnomernenie]
hZrovnomernenie=fHistogram[dZrovnomernenie];
ListPlot[hZrovnomernenie,Filling->Axis]