Hi Georg!
I hope i understood your idea properly - i am not sure. Could be also the opposite...
So, using your fomula i started with the original image and not not with the DBE-Output,
as you remove in your formula again with "tmb=$T-bg" the Background.
So did an ABE but used only the resulting bg image and did no subtraction.
To create a fg image with mainly stars i use the Blend Script from Hartmut Bornemann
Extended by me with the attached formula. Very simple it looks for the edges in an Image
and based on the edge Parameter value it gets mor or less stars or foreground objects.
This formula is based on an idea of "Edoardo" ! ( i made it only "Script-Usable).
Than created with RangeMask a mask for the "ground".
Than your PixelMath Expression slightly changed to
off=0.15;
tmb=(iif($T<0.0001,0,$T-bg));
fmb=(iif(fg>bg,$T-bg,$T));
sum=fmb[0]+fmb[1]+fmb[2];
norm=fmb*3/sum;
norm2=iif(sum==0,1,norm);
norm3=max(0.5,min(2,norm2));
iif($T<0.0005,0,tmb/norm3+off)
Than using the fg Image as a mask did the colorcalibration
and some Satincrease of the stars with Curves S.
This resulted in the following Image which in my opinion shows some changes but i am
not sure if this are the changes you wanted to achieve:
Basically the Script uses this formula to achieve what i understand under fg Image.
var P = new PixelMath;
P.expression = " IMG1 = Image05; IMG2 = Image05; MEDD = med(Image05);0.000 * IMG1 + 1.000 * (iif((x()==0||x()==w()-1 || y()==0 || y()==h()-1),0,iif((((sqrt((mean(-(pixel($T,x(),y())-pixel($T,x()-1,y())),(pixel($T,x(),y())-pixel($T,x()+1,y()))))^2+(mean(-(pixel($T,x(),y())-pixel($T,x(),y()-1)),(pixel($T,x(),y())-pixel($T,x(),y()+1))))^2))>edge)||($T>Fill)),1,0)))";
P.symbols = "IMG1, IMG2, MEDD, edge=0.07, Fill=0.8";
I could provide to you the Blend Script from Hartmut which i adapted.
So you could Play around if the results are "fg" as you mean it.
The Blend script allows also many 2 layer operations with masks.
Gerald