save Tmat in binary format

This commit is contained in:
nuknal
2024-06-15 03:04:50 +08:00
parent 4a2fc805c9
commit 0c17fee9c7
6 changed files with 245 additions and 90 deletions

10
go.mod
View File

@@ -15,6 +15,13 @@ require (
require (
cloud.google.com/go v0.112.0 // indirect
cloud.google.com/go/storage v1.36.0 // indirect
git.sr.ht/~sbinet/gg v0.5.0 // indirect
github.com/ajstarks/svgo v0.0.0-20211024235047-1546f124cd8b // indirect
github.com/campoy/embedmd v1.0.0 // indirect
github.com/go-fonts/liberation v0.3.2 // indirect
github.com/go-latex/latex v0.0.0-20231108140139-5c1ce85aa4ea // indirect
github.com/go-pdf/fpdf v0.9.0 // indirect
github.com/golang/freetype v0.0.0-20170609003504-e2365dfdc4a0 // indirect
github.com/golang/protobuf v1.5.4 // indirect
github.com/google/uuid v1.6.0 // indirect
github.com/hashicorp/hcl v1.0.0 // indirect
@@ -30,6 +37,7 @@ require (
github.com/onsi/gomega v1.33.1 // indirect
github.com/pelletier/go-toml/v2 v2.1.0 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect
github.com/sagikazarmark/locafero v0.4.0 // indirect
github.com/sagikazarmark/slog-shim v0.1.0 // indirect
github.com/sourcegraph/conc v0.3.0 // indirect
@@ -45,9 +53,11 @@ require (
go.uber.org/multierr v1.9.0 // indirect
golang.org/x/crypto v0.23.0 // indirect
golang.org/x/exp v0.0.0-20240222234643-814bf88cf225 // indirect
golang.org/x/image v0.14.0 // indirect
golang.org/x/oauth2 v0.16.0 // indirect
golang.org/x/term v0.20.0 // indirect
golang.org/x/text v0.15.0 // indirect
gonum.org/v1/plot v0.14.0 // indirect
google.golang.org/api v0.155.0 // indirect
google.golang.org/genproto v0.0.0-20240123012728-ef4313101c80 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20240311173647-c811ad7063a7 // indirect

17
go.sum
View File

@@ -768,6 +768,8 @@ cloud.google.com/go/workflows v1.11.1/go.mod h1:Z+t10G1wF7h8LgdY/EmRcQY8ptBD/nvo
dmitri.shuralyov.com/gpu/mtl v0.0.0-20190408044501-666a987793e9/go.mod h1:H6x//7gZCb22OMCxBHrMx7a5I7Hp++hsVxbQ4BYO7hU=
gioui.org v0.0.0-20210308172011-57750fc8a0a6/go.mod h1:RSH6KIUZ0p2xy5zHDxgAM4zumjgTw83q2ge/PI+yyw8=
git.sr.ht/~sbinet/gg v0.3.1/go.mod h1:KGYtlADtqsqANL9ueOFkWymvzUvLMQllU5Ixo+8v3pc=
git.sr.ht/~sbinet/gg v0.5.0 h1:6V43j30HM623V329xA9Ntq+WJrMjDxRjuAB1LFWF5m8=
git.sr.ht/~sbinet/gg v0.5.0/go.mod h1:G2C0eRESqlKhS7ErsNey6HHrqU1PwsnCQlekFi9Q2Oo=
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
github.com/BurntSushi/xgb v0.0.0-20160522181843-27f122750802/go.mod h1:IVnqGOEym/WlBOVXweHU+Q+/VP0lqqI8lqeDx9IjBqo=
github.com/JohnCGriffin/overflow v0.0.0-20211019200055-46fa312c352c/go.mod h1:X0CRv0ky0k6m906ixxpzmDRLvX58TFUKS2eePweuyxk=
@@ -783,6 +785,7 @@ github.com/airbusgeo/osio v0.1.3/go.mod h1:d4qlaDSgsMilFoi+/62vwFZStuYIHIDovYzIR
github.com/ajstarks/deck v0.0.0-20200831202436-30c9fc6549a9/go.mod h1:JynElWSGnm/4RlzPXRlREEwqTHAN3T56Bv2ITsFT3gY=
github.com/ajstarks/deck/generate v0.0.0-20210309230005-c3f852c02e19/go.mod h1:T13YZdzov6OU0A1+RfKZiZN9ca6VeKdBdyDV+BY97Tk=
github.com/ajstarks/svgo v0.0.0-20180226025133-644b8db467af/go.mod h1:K08gAheRH3/J6wwsYMMT4xOr94bZjxIelGM0+d/wbFw=
github.com/ajstarks/svgo v0.0.0-20211024235047-1546f124cd8b h1:slYM766cy2nI3BwyRiyQj/Ud48djTMtMebDqepE95rw=
github.com/ajstarks/svgo v0.0.0-20211024235047-1546f124cd8b/go.mod h1:1KcenG0jGWcpt8ov532z81sp/kMMUG485J2InIOyADM=
github.com/andybalholm/brotli v1.0.4/go.mod h1:fO7iG3H7G2nSZ7m0zPUDn85XEX2GTukHGRSepvi9Eig=
github.com/antihax/optional v1.0.0/go.mod h1:uupD/76wgC+ih3iEmQUL+0Ugr19nfwCT1kdvxnR2qWY=
@@ -809,6 +812,8 @@ github.com/bgentry/speakeasy v0.1.0/go.mod h1:+zsyZBPWlz7T6j88CTgSN5bM796AkVf0kB
github.com/bketelsen/crypt v0.0.4/go.mod h1:aI6NrJ0pMGgvZKL1iVgXLnfIFJtfV+bKCoqOes/6LfM=
github.com/boombuler/barcode v1.0.0/go.mod h1:paBWMcWSl3LHKBqUq+rly7CNSldXjb2rDl3JlRe0mD8=
github.com/boombuler/barcode v1.0.1/go.mod h1:paBWMcWSl3LHKBqUq+rly7CNSldXjb2rDl3JlRe0mD8=
github.com/campoy/embedmd v1.0.0 h1:V4kI2qTJJLf4J29RzI/MAt2c3Bl4dQSYPuflzwFH2hY=
github.com/campoy/embedmd v1.0.0/go.mod h1:oxyr9RCiSXg0M3VJ3ks0UGfp98BpSSGr0kpiX3MzVl8=
github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU=
github.com/census-instrumentation/opencensus-proto v0.3.0/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU=
github.com/census-instrumentation/opencensus-proto v0.4.1/go.mod h1:4T9NM4+4Vw91VeyqjLS6ao50K5bOcLKN6Q42XnYaRYw=
@@ -881,20 +886,27 @@ github.com/go-fonts/dejavu v0.1.0/go.mod h1:4Wt4I4OU2Nq9asgDCteaAaWZOV24E+0/Pwo0
github.com/go-fonts/latin-modern v0.2.0/go.mod h1:rQVLdDMK+mK1xscDwsqM5J8U2jrRa3T0ecnM9pNujks=
github.com/go-fonts/liberation v0.1.1/go.mod h1:K6qoJYypsmfVjWg8KOVDQhLc8UDgIK2HYqyqAO9z7GY=
github.com/go-fonts/liberation v0.2.0/go.mod h1:K6qoJYypsmfVjWg8KOVDQhLc8UDgIK2HYqyqAO9z7GY=
github.com/go-fonts/liberation v0.3.2 h1:XuwG0vGHFBPRRI8Qwbi5tIvR3cku9LUfZGq/Ar16wlQ=
github.com/go-fonts/liberation v0.3.2/go.mod h1:N0QsDLVUQPy3UYg9XAc3Uh3UDMp2Z7M1o4+X98dXkmI=
github.com/go-fonts/stix v0.1.0/go.mod h1:w/c1f0ldAUlJmLBvlbkvVXLAD+tAMqobIIQpmnUIzUY=
github.com/go-gl/glfw v0.0.0-20190409004039-e6da0acd62b1/go.mod h1:vR7hzQXu2zJy9AVAgeJqvqgH9Q5CA+iKCZ2gyEVpxRU=
github.com/go-gl/glfw/v3.3/glfw v0.0.0-20191125211704-12ad95a8df72/go.mod h1:tQ2UAYgL5IevRw8kRxooKSPJfGvJ9fJQFa0TUsXzTg8=
github.com/go-gl/glfw/v3.3/glfw v0.0.0-20200222043503-6f7a984d4dc4/go.mod h1:tQ2UAYgL5IevRw8kRxooKSPJfGvJ9fJQFa0TUsXzTg8=
github.com/go-latex/latex v0.0.0-20210118124228-b3d85cf34e07/go.mod h1:CO1AlKB2CSIqUrmQPqA0gdRIlnLEY0gK5JGjh37zN5U=
github.com/go-latex/latex v0.0.0-20210823091927-c0d11ff05a81/go.mod h1:SX0U8uGpxhq9o2S/CELCSUxEWWAuoCUcVCQWv7G2OCk=
github.com/go-latex/latex v0.0.0-20231108140139-5c1ce85aa4ea h1:DfZQkvEbdmOe+JK2TMtBM+0I9GSdzE2y/L1/AmD8xKc=
github.com/go-latex/latex v0.0.0-20231108140139-5c1ce85aa4ea/go.mod h1:Y7Vld91/HRbTBm7JwoI7HejdDB0u+e9AUBO9MB7yuZk=
github.com/go-logr/logr v1.4.1 h1:pKouT5E8xu9zeFC39JXRDukb6JFQPXM5p5I91188VAQ=
github.com/go-logr/stdr v1.2.2 h1:hSWxHoqTgW2S2qGc0LTAI563KZ5YKYRhT3MFKZMbjag=
github.com/go-pdf/fpdf v0.5.0/go.mod h1:HzcnA+A23uwogo0tp9yU+l3V+KXhiESpt1PMayhOh5M=
github.com/go-pdf/fpdf v0.6.0/go.mod h1:HzcnA+A23uwogo0tp9yU+l3V+KXhiESpt1PMayhOh5M=
github.com/go-pdf/fpdf v0.9.0 h1:PPvSaUuo1iMi9KkaAn90NuKi+P4gwMedWPHhj8YlJQw=
github.com/go-pdf/fpdf v0.9.0/go.mod h1:oO8N111TkmKb9D7VvWGLvLJlaZUQVPM+6V42pp3iV4Y=
github.com/go-task/slim-sprig v0.0.0-20210107165309-348f09dbbbc0/go.mod h1:fyg7847qk6SyHyPtNmDHnmrv/HOrqktSC+C9fM+CJOE=
github.com/goccy/go-json v0.9.11/go.mod h1:6MelG93GURQebXPDq3khkgXZkazVtN9CRI+MGFi0w8I=
github.com/godbus/dbus/v5 v5.0.4/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA=
github.com/gogo/protobuf v1.3.2/go.mod h1:P1XiOD3dCwIKUDQYPy72D8LYyHL2YPYrpS2s69NZV8Q=
github.com/golang/freetype v0.0.0-20170609003504-e2365dfdc4a0 h1:DACJavvAHhabrF08vX0COfcOBJRhZ8lUbR+ZWIs0Y5g=
github.com/golang/freetype v0.0.0-20170609003504-e2365dfdc4a0/go.mod h1:E/TSTwGwJL78qG/PmXZO1EjYhfJinVAhrmmHX6Z8B9k=
github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b/go.mod h1:SBH7ygxi8pfUlaOkMMuAQtPIUF8ecWP5IEl/CR7VP2Q=
github.com/golang/glog v1.0.0/go.mod h1:EWib/APOK0SL3dFbYqvxE3UYd8E6s1ouQ7iEp/0LWV4=
@@ -1152,6 +1164,7 @@ github.com/pkg/sftp v1.10.1/go.mod h1:lYOWFsE0bwd1+KfKJaKeuokY15vzFx25BLbzYYoAxZ
github.com/pkg/sftp v1.13.1/go.mod h1:3HaPG6Dq1ILlpPZRO0HVMrsydcdLt6HRDccSgb87qRg=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 h1:Jamvg5psRIccs7FGNTlIRMkT8wgtp5eCXdBlqhYGL6U=
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/posener/complete v1.1.1/go.mod h1:em0nMJCgc9GFtwrmVmEMR/ZL6WyhyjMBndrE9hABlRI=
github.com/prometheus/client_model v0.0.0-20190812154241-14fe0d1b01d4/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA=
github.com/prometheus/client_model v0.2.0/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA=
@@ -1322,6 +1335,8 @@ golang.org/x/image v0.0.0-20210607152325-775e3b0c77b9/go.mod h1:023OzeP/+EPmXeap
golang.org/x/image v0.0.0-20210628002857-a66eb6448b8d/go.mod h1:023OzeP/+EPmXeapQh35lcL3II3LrY8Ic+EFFKVhULM=
golang.org/x/image v0.0.0-20211028202545-6944b10bf410/go.mod h1:023OzeP/+EPmXeapQh35lcL3II3LrY8Ic+EFFKVhULM=
golang.org/x/image v0.0.0-20220302094943-723b81ca9867/go.mod h1:023OzeP/+EPmXeapQh35lcL3II3LrY8Ic+EFFKVhULM=
golang.org/x/image v0.14.0 h1:tNgSxAFe3jC4uYqvZdTr84SZoM1KfwdC9SKIFrLjFn4=
golang.org/x/image v0.14.0/go.mod h1:HUYqC05R2ZcZ3ejNQsIHQDQiwWM4JBqmm6MKANTp4LE=
golang.org/x/lint v0.0.0-20181026193005-c67002cb31c3/go.mod h1:UVdnD1Gm6xHRNCYTkRU2/jEulfH38KcIWyp/GAMgvoE=
golang.org/x/lint v0.0.0-20190227174305-5b3e6a55c961/go.mod h1:wehouNa3lNwaWXcvxsM5YxQ5yQlVC4a0KAMCusXpPoU=
golang.org/x/lint v0.0.0-20190301231843-5614ed5bae6f/go.mod h1:UVdnD1Gm6xHRNCYTkRU2/jEulfH38KcIWyp/GAMgvoE=
@@ -1707,6 +1722,8 @@ gonum.org/v1/netlib v0.0.0-20190313105609-8cb42192e0e0/go.mod h1:wa6Ws7BG/ESfp6d
gonum.org/v1/plot v0.0.0-20190515093506-e2840ee46a6b/go.mod h1:Wt8AAjI+ypCyYX3nZBvf6cAIx93T+c/OS2HFAYskSZc=
gonum.org/v1/plot v0.9.0/go.mod h1:3Pcqqmp6RHvJI72kgb8fThyUnav364FOsdDo2aGW5lY=
gonum.org/v1/plot v0.10.1/go.mod h1:VZW5OlhkL1mysU9vaqNHnsy86inf6Ot+jB3r+BczCEo=
gonum.org/v1/plot v0.14.0 h1:+LBDVFYwFe4LHhdP8coW6296MBEY4nQ+Y4vuUpJopcE=
gonum.org/v1/plot v0.14.0/go.mod h1:MLdR9424SJed+5VqC6MsouEpig9pZX2VZ57H9ko2bXU=
google.golang.org/api v0.4.0/go.mod h1:8k5glujaEP+g9n7WNsDg8QP6cUVNI86fCNMcbazEtwE=
google.golang.org/api v0.7.0/go.mod h1:WtwebWUNSVBH/HAw79HIFXZNqEvBhG+Ra+ax0hx3E3M=
google.golang.org/api v0.8.0/go.mod h1:o4eAsZoiT+ibD93RtjEohWalFOjRDx6CVaqeizhEnKg=

View File

@@ -9,7 +9,7 @@ import (
func (r *Registrator) DoRRC() error {
logrus.Println("try to do RRC...")
tablePAN, err := rrc.LoadGrayTableMatrix("data/rrc/pan_gray_table.dat")
lutPAN, err := rrc.LoadLUT("data/rrc/B0.LUT", 9344)
if err != nil {
logrus.Error("load pan gray table failed")
return err
@@ -17,13 +17,13 @@ func (r *Registrator) DoRRC() error {
for y := 0; y < r.PanImage.Rows(); y++ {
for x := 0; x < r.PanImage.Cols(); x++ {
newGray := tablePAN.At(x, int(uint16(r.PanImage.GetShortAt(y, x))))
newGray := lutPAN[x][int(uint16(r.PanImage.GetShortAt(y, x)))]
r.PanImage.SetShortAt(y, x, int16(newGray))
}
}
for i := 0; i < 4; i++ {
tableMSS, err := rrc.LoadGrayTableMatrix(fmt.Sprintf("data/rrc/mss%d_gray_table.dat", i+1))
lutMSS, err := rrc.LoadLUT(fmt.Sprintf("data/rrc/B%d.LUT", i+1), 2336)
if err != nil {
logrus.Error("load mss gray table failed")
return err
@@ -31,7 +31,7 @@ func (r *Registrator) DoRRC() error {
for y := 0; y < r.MssImages[i].Rows(); y++ {
for x := 0; x < r.MssImages[i].Cols(); x++ {
newGray := tableMSS.At(x, int(uint16(r.MssImages[i].GetShortAt(y, x))))
newGray := lutMSS[x][int(uint16(r.MssImages[i].GetShortAt(y, x)))]
r.MssImages[i].SetShortAt(y, x, int16(newGray))
}
}

26
pkg/rrc/helper.go Normal file
View File

@@ -0,0 +1,26 @@
package rrc
func searchVL(V []float64, Sik float64) int {
left, right := 0, len(V)-2
// if Sik < V[0] || Sik > V[len(V)-1] {
// return -1
// }
for left <= right {
mid := left + (right-left)/2
// Check if Sik is between V[mid] and V[mid+1]
if V[mid]-1e-6 <= Sik && Sik <= V[mid+1]+1e-6 {
return mid
} else if Sik < V[mid] {
right = mid - 1
} else if Sik == V[mid] {
return mid
} else {
left = mid + 1
}
}
// Return -1 if no such position is found
return -1
}

View File

@@ -1,14 +1,18 @@
package rrc
import (
"bytes"
"encoding/binary"
"fmt"
"math"
"os"
"github.com/sirupsen/logrus"
log "github.com/sirupsen/logrus"
"gocv.io/x/gocv"
"gonum.org/v1/gonum/mat"
"gonum.org/v1/plot"
"gonum.org/v1/plot/plotter"
"gonum.org/v1/plot/plotutil"
"gonum.org/v1/plot/vg"
)
type ProbeHistogram struct {
@@ -21,7 +25,7 @@ type ProbeHistogram struct {
P_l []float64 // P_l = m_l/M 所有探元的期望直方图灰度等级为l的概率密度
S_ik [][]float64 // S_ik = sum(p_ij),j=0..k 第i个探元直方图灰度等级k的累积概率密度
V_l []float64 // V_l = sum(P_j),j=0..l // 期望直方图灰度级l对应的累积概率密度
Tmat *mat.Dense // 第i个像元的j灰度等级对应的新的灰度值用于修正图像
Tmat [][]uint16 // 第i个像元的j灰度等级对应的新的灰度值用于修正图像
}
func (hist *ProbeHistogram) init(width int) {
@@ -34,19 +38,30 @@ func (hist *ProbeHistogram) init(width int) {
hist.P_l = make([]float64, MaxGrayLevel)
hist.S_ik = make([][]float64, width)
hist.V_l = make([]float64, MaxGrayLevel)
hist.Tmat = make([][]uint16, width)
for i := 0; i < width; i++ {
hist.n_ik[i] = make([]int64, MaxGrayLevel)
hist.p_ik[i] = make([]float64, MaxGrayLevel)
hist.S_ik[i] = make([]float64, MaxGrayLevel)
hist.Tmat[i] = make([]uint16, MaxGrayLevel)
}
}
hist.Tmat = mat.NewDense(width, MaxGrayLevel, nil)
func (hist *ProbeHistogram) init0(width int) {
hist.probes = width
hist.M = 0
hist.N_i = make([]int64, width)
hist.n_ik = make([][]int64, width)
hist.m_l = make([]int64, MaxGrayLevel)
for i := 0; i < width; i++ {
hist.n_ik[i] = make([]int64, MaxGrayLevel)
}
}
func (hist *ProbeHistogram) statistical(img gocv.Mat) error {
hist.M += int64(img.Rows() * img.Cols())
fmt.Println("Hist.M:", hist.M)
// 探元i像素总数
for i := 0; i < hist.probes; i++ {
@@ -72,87 +87,118 @@ func (hist *ProbeHistogram) statistical(img gocv.Mat) error {
}
func (hist *ProbeHistogram) compute() {
// 探元i灰度概率密度
// 探元i灰度k概率密度
log.Info("computing p_ik")
for i := 0; i < hist.probes; i++ {
for k := 0; k < MaxGrayLevel; k++ {
hist.p_ik[i][k] = float64(hist.n_ik[i][k]) / float64(hist.N_i[i])
}
}
points := plotter.XYs{}
var maxY int64
// 所有探元的期望直方图灰度等级为l的概率密度
log.Info("computing p_l")
for gray := 0; gray < MaxGrayLevel; gray++ {
hist.P_l[gray] = float64(hist.m_l[gray]) / float64(hist.M)
points = append(points, plotter.XY{X: float64(gray), Y: float64(hist.m_l[gray])})
if hist.m_l[gray] > maxY {
maxY = hist.m_l[gray]
}
}
plt := plot.New()
plt.Title.Text = "Gray Level Histogram"
plt.X.Label.Text = "Gray Level"
plt.Y.Label.Text = "Pixels"
plt.Y.Min, plt.X.Min, plt.Y.Max, plt.X.Max = 0, 0, float64(maxY), 65536
plotutil.AddLines(plt, points)
plt.Save(5*vg.Inch, 5*vg.Inch, "data/rrc/01-gray-level-histogram.png")
// 第i个探元直方图灰度等级k的累积概率密度
log.Info("computing s_ik")
for i := 0; i < hist.probes; i++ {
for k := 0; k < MaxGrayLevel; k++ {
hist.S_ik[i][k] = 0
for j := 0; j <= k; j++ {
hist.S_ik[i][k] += hist.p_ik[i][j]
if k == 0 {
hist.S_ik[i][k] = hist.p_ik[i][0]
} else {
hist.S_ik[i][k] = hist.S_ik[i][k-1] + hist.p_ik[i][k]
}
}
}
// 期望直方图灰度级l对应的累积概率密度
log.Info("computing v_l")
for gray := 0; gray < MaxGrayLevel; gray++ {
hist.V_l[gray] = 0
for j := 0; j <= gray; j++ {
hist.V_l[gray] += hist.P_l[j]
if gray == 0 {
hist.V_l[gray] = hist.P_l[0]
} else {
hist.V_l[gray] = hist.V_l[gray-1] + hist.P_l[gray]
}
}
// 生成查找表
nT := 0
log.Info("computing Tij table")
var nT int64
for i := 0; i < hist.probes; i++ {
for k := 0; k < MaxGrayLevel; k++ {
for l := 0; l < MaxGrayLevel-1; l++ {
if hist.S_ik[i][k] >= hist.V_l[l] && hist.S_ik[i][k] <= hist.V_l[l+1] {
nT += 1
if math.Abs(hist.S_ik[i][k]-hist.V_l[l]) <= math.Abs(hist.S_ik[i][k]-hist.V_l[l+1]) {
hist.Tmat.Set(i, k, float64(l))
} else {
hist.Tmat.Set(i, k, float64(l+1))
}
}
l := searchVL(hist.V_l, hist.S_ik[i][k])
if l == -1 {
continue
}
nT++
if math.Abs(hist.S_ik[i][k]-hist.V_l[l]) <= math.Abs(hist.S_ik[i][k]-hist.V_l[l+1]) {
hist.Tmat[i][k] = uint16(l)
} else {
hist.Tmat[i][k] = uint16(l + 1)
}
}
}
if nT != hist.probes*MaxGrayLevel {
logrus.Error("error in computing Tij table, some values are not satisfied")
log.Info("total Tij table entries:", nT)
if nT != int64(hist.probes*MaxGrayLevel) {
log.Warn("error in computing Tij table, some values are not satisfied")
}
}
func (hist *ProbeHistogram) save(matrixFile string) error {
log.Printf("total pixels: %d", hist.M)
f, err := os.OpenFile(matrixFile, os.O_TRUNC|os.O_WRONLY|os.O_CREATE, 0644)
func (hist *ProbeHistogram) saveLUT(fLUT string) error {
file, err := os.Create(fLUT)
if err != nil {
return err
}
defer f.Close()
_, err = hist.Tmat.MarshalBinaryTo(f)
if err != nil {
return err
defer file.Close()
for i := 0; i < hist.probes; i++ {
binary.Write(file, binary.LittleEndian, hist.Tmat[i])
}
return nil
}
func LoadGrayTableMatrix(matrixFile string) (*mat.Dense, error) {
f, err := os.OpenFile(matrixFile, os.O_RDONLY, 0644)
const (
CheckPointProbe = 1000
CheckPointGray = 15000
)
func LoadLUT(fLUT string, probes int) ([][]uint16, error) {
data, err := os.ReadFile(fLUT)
if err != nil {
return nil, err
}
defer f.Close()
matrix := mat.Dense{}
if _, err := matrix.UnmarshalBinaryFrom(f); err != nil {
return nil, err
Tmat := make([][]uint16, probes)
for i := 0; i < probes; i++ {
Tmat[i] = make([]uint16, MaxGrayLevel)
binary.Read(bytes.NewReader(data[i*MaxGrayLevel*2:i*MaxGrayLevel*2+MaxGrayLevel*2]), binary.LittleEndian, &Tmat[i])
if i == CheckPointProbe {
log.Infof("Probes[%d], LUT check point [%d][%d]: %d", probes, i, CheckPointGray, Tmat[i][CheckPointGray])
}
}
return &matrix, nil
return Tmat, nil
}
func (hist *ProbeHistogram) sum(hists []*ProbeHistogram) {
@@ -168,4 +214,7 @@ func (hist *ProbeHistogram) sum(hists []*ProbeHistogram) {
hist.m_l[gray] += h.m_l[gray]
}
}
fmt.Println("Hist.M:", hist.M)
fmt.Println("Hist.probes:", hist.probes)
}

View File

@@ -4,6 +4,7 @@ import (
"bufio"
"fmt"
"os"
"sync"
log "github.com/sirupsen/logrus"
"gocv.io/x/gocv"
@@ -52,30 +53,57 @@ func (rrc *RRC) StatisticalPAN(dsfile string) error {
defer f.Close()
scanner := bufio.NewScanner(f)
var files []string
for scanner.Scan() {
l0 := scanner.Text()
log.Println("statistical PAN RAW: ", l0)
data, err := os.ReadFile(l0)
if err != nil {
log.Error("Error reading file: ", err)
continue
}
height := len(data) / (PANCameraProbeNum * 2)
img, err := gocv.NewMatFromBytes(height, PANCameraProbeNum, gocv.MatTypeCV16U, data)
if err != nil {
log.Error("Error creating Mat from bytes: ", err)
return err
}
rrc.Histograms[0].statistical(img)
img.Close()
files = append(files, scanner.Text())
}
// 并发处理
var wg sync.WaitGroup
jobs := make(chan struct{}, 5)
var hists []*ProbeHistogram
var mutex sync.Mutex
for _, file := range files {
wg.Add(1)
go func(l0 string) {
defer wg.Done()
jobs <- struct{}{}
defer func() { <-jobs }()
log.Println("statistical PAN RAW: ", l0)
data, err := os.ReadFile(l0)
if err != nil {
log.Error("Error reading file: ", err)
return
}
height := len(data) / (PANCameraProbeNum * 2)
img, err := gocv.NewMatFromBytes(height, PANCameraProbeNum, gocv.MatTypeCV16U, data)
if err != nil {
log.Error("Error creating Mat from bytes: ", err)
return
}
var hist ProbeHistogram
hist.init(PANCameraProbeNum)
hist.statistical(img)
img.Close()
mutex.Lock()
hists = append(hists, &hist)
mutex.Unlock()
}(file)
}
wg.Wait()
log.Println("sum PAN histogram...")
rrc.Histograms[0].sum(hists)
log.Println("compute PAN histogram...")
rrc.Histograms[0].compute()
log.Println("save PAN gray table matrix.")
rrc.Histograms[0].save("data/rrc/pan_gray_table.dat")
rrc.Histograms[0].saveLUT("data/rrc/B0.LUT")
return nil
}
@@ -88,43 +116,68 @@ func (rrc *RRC) StatisticalMSS(dsfile string) error {
defer f.Close()
scanner := bufio.NewScanner(f)
var files []string
for scanner.Scan() {
l0 := scanner.Text()
log.Println("statistical MSS RAW: ", l0)
data, err := os.ReadFile(l0)
if err != nil {
log.Error("Error reading file: ", err)
continue
}
width := MSSCameraProbeNum
height := len(data) / (width * 2)
mssData := make([][]byte, 4)
for h := 0; h < height; h++ {
row := data[h*width*4*2 : (h+1)*width*4*2]
for i := 0; i < 4; i++ {
mssData[i] = append(mssData[i], row[i*width*2:(i+1)*width*2]...)
}
}
var mssImages [4]gocv.Mat
for i := 0; i < 4; i++ {
mssImages[i], err = gocv.NewMatFromBytes(height, width, gocv.MatTypeCV16U, mssData[i])
if err != nil {
log.Error("Error creating Mat from bytes: ", err)
return err
}
rrc.Histograms[i+1].statistical(mssImages[i])
mssImages[i].Close()
}
files = append(files, scanner.Text())
}
var wg sync.WaitGroup
jobs := make(chan struct{}, 5)
var hists [4][]*ProbeHistogram
var mutex sync.Mutex
for _, file := range files {
wg.Add(1)
go func(l0 string) {
defer wg.Done()
jobs <- struct{}{}
defer func() { <-jobs }()
log.Println("statistical MSS RAW: ", l0)
data, err := os.ReadFile(l0)
if err != nil {
log.Error("Error reading file: ", err)
return
}
width := MSSCameraProbeNum
height := len(data) / (width * 2)
mssData := make([][]byte, 4)
for h := 0; h < height; h++ {
row := data[h*width*4*2 : (h+1)*width*4*2]
for i := 0; i < 4; i++ {
mssData[i] = append(mssData[i], row[i*width*2:(i+1)*width*2]...)
}
}
var mssImages [4]gocv.Mat
for i := 0; i < 4; i++ {
mssImages[i], err = gocv.NewMatFromBytes(height, width, gocv.MatTypeCV16U, mssData[i])
if err != nil {
log.Error("Error creating Mat from bytes: ", err)
return
}
var hist ProbeHistogram
hist.init(PANCameraProbeNum)
hist.statistical(mssImages[i])
mssImages[i].Close()
mutex.Lock()
hists[i] = append(hists[i], &hist)
mutex.Unlock()
}
}(file)
}
wg.Wait()
for i := 1; i < 5; i++ {
log.Println("sum MSS histogram...")
rrc.Histograms[i].sum(hists[i-1])
log.Println("compute MSS histogram...")
rrc.Histograms[i].compute()
log.Println("save MSS gray table matrix.")
rrc.Histograms[i].save(fmt.Sprintf("data/rrc/mss%d_gray_table.dat", i))
rrc.Histograms[i].saveLUT(fmt.Sprintf("data/rrc/B%d.LUT", i))
}
return nil