SetDirectory[NotebookDirectory[]]

tmp1=ReadList["memb.pgm",Number];

tmp2=Partition[tmp1,100];
obr=tmp2/255.0;

ArrayPlot[obr,ColorFunction->GrayLevel,ColorFunctionScaling->False]

histogram[array_]:=Module[
{i,hist,j},
hist=Table[0,{i,0,255}];
For[i=1,i≤Length[array],i++,
{
For[j=1,j<=256,j++,
{
If[array[[i]]==j,hist[[j]]=hist[[j]]+1];
}];}];
Return[hist]
]

hist=histogram[tmp1];

AOD[array_]:=Module[
{},
Return[(1/(Length[array]*Length[array[[1]]]))*(∑_(i=1)^(Length[array]))(∑_(j=1)^(Length[array[[1]]]))array[[i,j]]]
]

AOD[tmp2]//N

ListPlot[hist,Filling->Axis]

center[array_]:=Module[
{L,q=256,vystup=Table[0,{i,1,Length[array]},{j,1,Length[array[[1]]]}]},
L=AOD[array];
Return[array-L+q/2]
]

ArrayPlot[center[obr],ColorFunction->GrayLevel,ColorFunctionScaling->False]