Skip to content

Commit

Permalink
added statistics for background extraction
Browse files Browse the repository at this point in the history
  • Loading branch information
Markus Noga committed Jul 1, 2020
1 parent 7166291 commit 91e5187
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 9 deletions.
41 changes: 33 additions & 8 deletions internal/background.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package internal

import (
"fmt"
"math"
)

Expand All @@ -29,6 +30,9 @@ type Background struct {
NumCellRows int32
NumCells int32
Cells []BGCell
OutlierCells int32 // number of outlier cells replaced with interpolation of neighboring cells
Min BGCell // minimum alpha, beta, gamma values
Max BGCell // maximum alpha, beta, gamma values
}

// A background cell, which is modeled as a linear gradient
Expand All @@ -38,6 +42,12 @@ type BGCell struct {
Gamma float32 // constant offset
}

func (bg *Background) String() string {
return fmt.Sprintf("Background grid %d cells %dx%d outliers %d alpha [%f...%f] beta [%f...%f] gamma [%f...%f]",
bg.GridSpacing, bg.NumCellCols, bg.NumCellRows, bg.OutlierCells,
bg.Min.Alpha, bg.Max.Alpha, bg.Min.Beta, bg.Max.Beta, bg.Min.Gamma, bg.Max.Gamma )
}

// Creates new background by fitting linear gradients to grid cells of the given image, masking out areas in given mask
func NewBackground(src []float32, width int32, gridSpacing int32, sigma float32) (b *Background) {
// Allocate space for gradient cells
Expand All @@ -47,10 +57,11 @@ func NewBackground(src []float32, width int32, gridSpacing int32, sigma float32)
numCells :=numCellCols*numCellRows
cells :=make([]BGCell, numCells)

b=&Background{width, height, gridSpacing, numCellCols, numCellRows, numCells, cells}
b=&Background{Width:width, Height:height, GridSpacing:gridSpacing, NumCellCols:numCellCols, NumCellRows:numCellRows, NumCells:numCells, Cells:cells}

b.init(src, sigma)
b.smoothe()
b.calculateStats()

return b
}
Expand Down Expand Up @@ -84,23 +95,24 @@ func (bg *Background) smoothe() {
for i,_:=range(ignores) { ignores[i]=false }

// First iteratively update ignores
totalIgnores:=int32(0)
ignoredCells:=int32(0)
ignoresChanged:=int32(1)
for ; ignoresChanged>0; {
for i,cell:=range bg.Cells { buffer[i]=cell.Alpha }
alphaMAD, alphasChanged:=expandIgnores(buffer, ignores, bg.NumCellCols, bg.NumCellRows)
_, alphasChanged:=expandIgnores(buffer, ignores, bg.NumCellCols, bg.NumCellRows)

for i,cell:=range bg.Cells { buffer[i]=cell.Beta }
betaMAD, betasChanged:=expandIgnores(buffer, ignores, bg.NumCellCols, bg.NumCellRows)
_, betasChanged:=expandIgnores(buffer, ignores, bg.NumCellCols, bg.NumCellRows)

for i,cell:=range bg.Cells { buffer[i]=cell.Gamma }
gammaMAD, gammasChanged:=expandIgnores(buffer, ignores, bg.NumCellCols, bg.NumCellRows)
_, gammasChanged:=expandIgnores(buffer, ignores, bg.NumCellCols, bg.NumCellRows)

ignoresChanged=alphasChanged+betasChanged+gammasChanged
totalIgnores+=ignoresChanged
LogPrintf("alpha MAD %f changed %d, beta %f %d, gamma %f %d\n", alphaMAD, alphasChanged, betaMAD, betasChanged, gammaMAD, gammasChanged)
ignoredCells+=ignoresChanged
// LogPrintf("alpha MAD %f changed %d, beta %f %d, gamma %f %d\n", alphaMAD, alphasChanged, betaMAD, betasChanged, gammaMAD, gammasChanged)
}
LogPrintf("Total %d ignores\n", totalIgnores)
// LogPrintf("Total %d ignores\n", ignoredCells)
bg.OutlierCells=ignoredCells

// Then replace cells with interpolations
for neighbors:=16; neighbors>=0; neighbors-- {
Expand All @@ -122,6 +134,19 @@ func (bg *Background) smoothe() {
buffer, ignores=nil, nil
}

func (bg *Background) calculateStats() {
mf32:=float32(math.MaxFloat32)
bg.Min.Alpha, bg.Min.Beta, bg.Min.Gamma= mf32, mf32, mf32
bg.Max.Alpha, bg.Max.Beta, bg.Max.Gamma=-mf32, -mf32, -mf32
for _,c:=range bg.Cells {
if c.Alpha<bg.Min.Alpha { bg.Min.Alpha=c.Alpha }
if c.Alpha>bg.Max.Alpha { bg.Max.Alpha=c.Alpha }
if c.Beta <bg.Min.Beta { bg.Min.Beta =c.Beta }
if c.Beta >bg.Max.Beta { bg.Max.Beta =c.Beta }
if c.Gamma<bg.Min.Gamma { bg.Min.Gamma=c.Gamma }
if c.Gamma>bg.Max.Gamma { bg.Max.Gamma=c.Gamma }
}
}

func expandIgnores(params []float32, ignores []bool, width, height int32) (mad float32, numChanged int32) {
temp:=[]float32{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}
Expand Down
3 changes: 2 additions & 1 deletion internal/preprocess.go
Original file line number Diff line number Diff line change
Expand Up @@ -157,8 +157,9 @@ func PreProcessLight(id int, fileName string, darkF, flatF *FITSImage, debayer,

// automatic background extraction, if desired
if backGrid>0 {
LogPrintf("%d: Automatic background extraction with grid size %d\n", id, backGrid)
bg:=NewBackground(light.Data, light.Naxisn[0], backGrid, 2.0)
LogPrintf("%d: %s\n", id, bg)

if backPattern=="" {
bg.Subtract(light.Data)
} else {
Expand Down

0 comments on commit 91e5187

Please sign in to comment.