$HistoryLength=0

Needs["CCompilerDriver`"]
If[Length@CCompilers[]≠0,If[First["Name"/.CCompilers[]]=="Cygwin GCC",compilationtarget="WVM",compilationtarget="C"],compilationtarget="WVM"];

If[$MachineName=="mockau",Unprotect[$ProcessorCount];$ProcessorCount=2;]
If[$ProcessorCount>1,MyMap:=ParallelMap;MyTable:=ParallelTable,MyMap:=Map;MyTable:=Table];
CloseKernels[];
LaunchKernels[$ProcessorCount*2/4];
$ProcessorCount
If[$ProcessorCount>1,parallelization=True,parallelization=False];
parallelization=False;

Clear[BernsenKern];
BernsenKern=Compile[{{list,_Real,1},{cm,_Real}},
Module[{min,max,diff,sum,thr},
max=Max[list];
min=Min[list];
diff=(max-min);
sum=max+min;
thr=0.5*sum;
If[diff<cm,If[EuclideanDistance[thr,max]<EuclideanDistance[thr,min],thr=min,thr=max]];
UnitStep[list[[Ceiling[Length[list]/2]]]-thr]
],CompilationTarget->compilationtarget,RuntimeAttributes->{Listable},Parallelization->parallelization];

Clear[MyBernsenFilter];
Options[MyBernsenFilter]={"ContrastMeasure"->15,"WindowHalfWidth"->15,"Mask"->"Box"};
MyBernsenFilter[im_Image,OptionsPattern[]]:=Module[{flatelpos,padim,whw,cm,el},
whw=Round[OptionValue["WindowHalfWidth"]];
cm=N[OptionValue["ContrastMeasure"]/255];
el=Switch[OptionValue["Mask"],
"Box",Table[1,{2whw+1},{2whw+1}],
"Circle",Ceiling[Rescale[Sign[Table[(x^2+y^2),{x,-whw,whw},{y,-whw,whw}]-whw*(whw+1/2)],{1,-1}]],
_,Table[1,{2whw+1},{2whw+1}]
];
flatelpos=Flatten[Position[Flatten[el],1]];
padim=ArrayPad[ImageData[im,"Real"],Floor[Dimensions[el]/2],"Reversed"];
padim=Developer`PartitionMap[(BernsenKern[Flatten[#,1][[flatelpos]],cm])&,padim,Dimensions[el],{1,1},Ceiling[Dimensions[el]/2]];
Image[ArrayPad[padim,-Floor[Dimensions[el]/2]],"Bit"]
];

Ton3CD21dreikanalausgleichF2=;(*Ki67,Pax-5,DAPI*)

Ton2CD21dreikanalausgleichF1=;(*Ki67,Pax-5,DAPI*)

ParallelMap[MyBernsenFilter,ColorSeparate[Ton3CD21dreikanalausgleichF2]]

ParallelMap[MyBernsenFilter,ColorSeparate[Ton2CD21dreikanalausgleichF1]]