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]

sum=Table[Random[Real,{-0.25,0.25}],{i,1,100},{j,1,100}];
ArrayPlot[sum]

zasumene=Table[0,{k,1,10},{i,1,100},{j,1,100}];

For[k=1,k≤10,k++,
{
sum=Table[Random[Real,{-1,1}],{i,1,100},{j,1,100}];
zasumene[[k]]=dZrovnomernenie+sum;

}];

vystup=Table[0,{i,1,100},{j,1,100}];

For[k=1,k≤10,k++,
{
vystup=vystup+zasumene[[k]];
}];


For[i=1,i≤100,i++,
{
For[j=1,j≤100,j++,
{
vystup[[i,j]]=vystup[[i,j]]/10;
}];}];

ArrayPlot[vystup]
ArrayPlot[dZrovnomernenie]

fTranslacia[array_, b1_,b2_]:=Module[
{bcg=Table[0,{i,1,Length[array]*3},{j,1,3*Length[array[[1]]]}],
vystup=Table[0,{i,1,Length[array]},{j,1,Length[array[[1]]]}],
i,j,a1,a2
},

For[i=1,i≤Length[array],i++,
{
For[j=1,j≤Length[array[[1]]],j++,
{
bcg[[i+100,j+100]]=array[[i,j]];
}];}];

bcg=Table[0,{i,1,Length[array]*3},{j,1,3*Length[array[[1]]]}];
For[i=1,i≤Length[bcg],i++,
{
For[j=1,j≤Length[bcg[[1]]],j++,
{

    a1=i-b1;
    a2=j-b2;
 
If[  ( a1 >0)   &&      (a1 <Length[array])    &&      (a2 <Length[array[[1]]])    &&      (a2 >0)  ,bcg[[i+100,j+100]]=array[[a1,a2]]]

(* bcg[[i,j]]=bcg[[i-b1,j-b2]];*)
}];}];
Return[bcg]
]

Manipulate[ArrayPlot[fTranslacia[tmp2, i,0]],{i,1,100,10}]

fRotacia[array_, S1_,S2_,ϵ_]:=Module[
{bcg=Table[0,{i,1,Length[array]*3},{j,1,3*Length[array[[1]]]}],
vystup=Table[0,{i,1,Length[array]},{j,1,Length[array[[1]]]}],
i,j,a1,a2
},

For[i=1,i≤Length[array],i++,
{
For[j=1,j≤Length[array[[1]]],j++,
{
bcg[[i+100,j+100]]=array[[i,j]];
}];}];

(*bcg=Table[0,{i,1,Length[array]*3},{j,1,3*Length[array[[1]]]}];*)

For[i=1,i≤Length[bcg],i++,
{
For[j=1,j≤Length[bcg[[1]]],j++,
{

    a1=Round[S1+(i-S1)*Cos[ϵ]-(j-S2)*Sin[ϵ]];
     a2=Round[S2+(i-S1)*Sin[ϵ]+(j-S2)*Cos[ϵ]];
 
If[  ( a1 >0)   &&      (a1 <Length[array])    &&      (a2 <Length[array[[1]]])    &&      (a2 >0)  ,bcg[[i,j]]=array[[a1,a2]]]

}];}];
Return[bcg]
]

Manipulate[ArrayPlot[fRotacia[tmp2, 100,100,i]],{i,0,1,0.1}]

fZoom[array_, c_,d_]:=Module[
{bcg=Table[0,{i,1,Length[array]},{j,1,Length[array[[1]]]}],
vystup=Table[0,{i,1,Length[array]},{j,1,Length[array[[1]]]}],
i,j,a1,a2
},
(*
For[i=1,i≤Length[array],i++,
{
For[j=1,j≤Length[array[[1]]],j++,
{
bcg[[i+100,j+100]]=array[[i,j]];
}];}];
*)
bcg=Table[0,{i,1,Length[array]},{j,1,Length[array[[1]]]}];

For[i=1,i<Length[bcg],i++,
{
For[j=1,j<Length[bcg[[1]]],j++,
{

    a1=Round[(i/c)];
    a2=Round[(j/d)];
 
If[  ( a1 >0)   &&      (a1 <Length[array])    &&      (a2 <Length[array[[1]]])    &&      (a2 >0)  ,bcg[[i,j]]=array[[a1,a2]]]

}];}];
Return[bcg]
]

Manipulate[ArrayPlot[fZoom[tmp2,i,i]],{i,0.1,2,0.2}]

fZoomBilinear[array_, c_,d_]:=Module[
{bcg=Table[0,{i,1,Length[array]},{j,1,Length[array[[1]]]}],
vystup=Table[0,{i,1,Length[array]},{j,1,Length[array[[1]]]}],
a1,a2,n,l,minDL,
riesenie,i0,j0,i1,j1,i2,j2,i3,j3,B,ps,x,y
},

For[i=1,i<Length[bcg],i++,
{
For[j=1,j<Length[bcg[[1]]],j++,
{

    a1=Floor[(i/c)];
    a2=Floor[(j/d)];

x=i/c;
y=j/d;

If[(a1>1&&a1<Length[bcg])&&(a2>1&&a2<Length[bcg[[1]]]),
{
i0=a1;
j0=a2;

i1=i0+1;
j1=j0;

i2=i0;
j2=j0+1;

i3=i0+1;
j3=j0+1;

B={{1,i0,j0,i0*j0},{1,i1,j1,i1*j1},{1,i2,j2,i2*j2},{1,i3,j3,i3*j3}};
(*B=Transpose[B];*)
ps={array[[i0,j0]],array[[i1,j1]],array[[i2,j2]],array[[i3,j3]]};

riesenie=LinearSolve[B,ps];

bcg[[i,j]]=riesenie[[1]]+riesenie[[2]]*x+riesenie[[3]]*y+riesenie[[4]]*x*y;
}
]
}];}];

Return[bcg]
]

ArrayPlot[fZoomBilinear[obr,2,2]]
ArrayPlot[fZoom[tmp2,2,2]]

fRotacia[array_, S1_,S2_,ϵ_]:=Module[
{bcg=Table[0,{i,1,Length[array]*3},{j,1,3*Length[array[[1]]]}],
vystup=Table[0,{i,1,Length[array]},{j,1,Length[array[[1]]]}],
i,j,a1,a2
},

For[i=1,i≤Length[array],i++,
{
For[j=1,j≤Length[array[[1]]],j++,
{
bcg[[i+100,j+100]]=array[[i,j]];
}];}];

(*bcg=Table[0,{i,1,Length[array]*3},{j,1,3*Length[array[[1]]]}];*)

For[i=1,i≤Length[bcg],i++,
{
For[j=1,j≤Length[bcg[[1]]],j++,
{

    a1=Round[S1+(i-S1)*Cos[ϵ]-(j-S2)*Sin[ϵ]];
     a2=Round[S2+(i-S1)*Sin[ϵ]+(j-S2)*Cos[ϵ]];
 
If[  ( a1 >0)   &&      (a1 <Length[array])    &&      (a2 <Length[array[[1]]])    &&      (a2 >0)  ,bcg[[i,j]]=array[[a1,a2]]]

}];}];
Return[bcg]
]

Manipulate[ArrayPlot[fRotacia[tmp2, 100,100,i]],{i,0,1,0.1}]

fZoom[array_, c_,d_]:=Module[
{bcg=Table[0,{i,1,Length[array]*3},{j,1,3*Length[array[[1]]]}],
vystup=Table[0,{i,1,Length[array]},{j,1,Length[array[[1]]]}],
i,j,a1,a2
},

For[i=1,i≤Length[array],i++,
{
For[j=1,j≤Length[array[[1]]],j++,
{
bcg[[i+100,j+100]]=array[[i,j]];
}];}];

bcg=Table[0,{i,1,Length[array]*3},{j,1,3*Length[array[[1]]]}];

For[i=1,i≤Length[bcg],i++,
{
For[j=1,j≤Length[bcg[[1]]],j++,
{

    a1=Round[(i/c)];
    a2=Round[(j/d)];
 
If[  ( a1 >0)   &&      (a1 <Length[array])    &&      (a2 <Length[array[[1]]])    &&      (a2 >0)  ,bcg[[i,j]]=array[[a1,a2]]]

}];}];
Return[bcg]
]

Manipulate[ArrayPlot[fZoom[tmp2,i,i]],{i,0.1,2,0.2}]

fZoomBilinear[array_, c_,d_]:=Module[
{bcg=Table[0,{i,1,Length[array]*3},{j,1,3*Length[array[[1]]]}],
vystup=Table[0,{i,1,Length[array]},{j,1,Length[array[[1]]]}],
i,j,a1,a2,n,l,minDL,
riesenie,i0,j0,i1,j1,i2,j2,i3,j3,B,ps
},

For[i=1,i≤Length[array],i++,
{
For[j=1,j≤Length[array[[1]]],j++,
{
bcg[[i+100,j+100]]=array[[i,j]];
}];}];

bcg=Table[0,{i,1,Length[array]*3},{j,1,3*Length[array[[1]]]}];

For[i=1,i≤Length[bcg],i++,
{
For[j=1,j≤Length[bcg[[1]]],j++,
{

    a1=(i/c);
    a2=(j/d);

i0=i;
j0=j;

i1=i+1;
j1=j;

i2=i;
j2=j+1;

i3=i+1;
j3=j+1;

B={{1,i0,j0,i0*j0},{1,i1,j1,i1*j1},{1,i2,j2,i2*j2},{1,i3,j3,i3*j3}};
ps={bcg[[i0,j0]],bcg[[i1,j1]],bcg[[i2,j2]],bcg[[i3,j3]]};

riesenie=LinearSolve[B,ps];


bcg[[i,j]]=riesenie[[1]]+riesenie[[2]]*i+riesenie[[3]]*j+riesenie[[4]]*i*j;


If[  ( a1 >0)   &&      (a1 <Length[array])    &&      (a2 <Length[array[[1]]])    &&      (a2 >0)  ,bcg[[i,j]]=array[[a1,a2]]]

}];}];

Return[bcg]
]

ArrayPlot[fZoomBilinear[tmp2, 2,2]]
ArrayPlot[fZoom[tmp2, 2,2]]