Skip to content

Commit

Permalink
COLMAP Points3D Binary format
Browse files Browse the repository at this point in the history
  • Loading branch information
EliCDavis committed Jan 11, 2024
1 parent 53fd0a5 commit 3a6690a
Show file tree
Hide file tree
Showing 4 changed files with 109 additions and 0 deletions.
27 changes: 27 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,33 @@

Structs for interacting with reconstruction data from different SFM programs

## COLMAP

Interact with COLMAPS's `points3D.bin` file output under the sparse reconstruction data.


```golang
package example

import (
"fmt"

"github.com/EliCDavis/sfm/colmap"
)

func main() {
points, err := colmap.ReadPoints3D("ColmapProject/sparse/0/points3D.bin")

if err != nil {
panic(err)
}


fmt.Printf("File contains %d points", len(points))
}

```

## Meshroom

Interact with Meshroom's `cameras.sfm` file output from the `StructureFromMotion` step.
Expand Down
73 changes: 73 additions & 0 deletions colmap/points.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package colmap

import (
"encoding/binary"
"image/color"
"os"

"github.com/EliCDavis/bitlib"
"github.com/EliCDavis/vector/vector3"
)

// Everything transcribed from
// https://github.com/colmap/colmap/blob/main/scripts/python/read_write_model.py
// https://docs.python.org/3/library/struct.html

type Point3DTrack struct {
ImageID int
Point2DID int
}

type Point3D struct {
ID uint64
Position vector3.Float64
Color color.RGBA
Error float64
Tracks []Point3DTrack // as (IMAGE_ID, POINT2D_IDX)
}

func ReadPoints3DBinary(filename string) ([]Point3D, error) {
f, err := os.Open(filename)
if err != nil {
return nil, err
}
defer f.Close()

reader := bitlib.NewReader(f, binary.LittleEndian)

numPoints := reader.UInt64()
points := make([]Point3D, numPoints)

for i := uint64(0); i < numPoints; i++ {
p := Point3D{}

p.ID = reader.UInt64()

p.Position = vector3.New(
reader.Float64(),
reader.Float64(),
reader.Float64(),
)

c := color.RGBA{A: 255}
c.R = reader.Byte()
c.G = reader.Byte()
c.B = reader.Byte()
p.Color = c

p.Error = reader.Float64()

numTracks := reader.UInt64()
p.Tracks = make([]Point3DTrack, numTracks)
for trackIndex := uint64(0); trackIndex < numTracks; trackIndex++ {
track := Point3DTrack{}
track.ImageID = int(reader.Int32())
track.Point2DID = int(reader.Int32())
p.Tracks[trackIndex] = track
}

points[i] = p
}

return points, reader.Error()
}
5 changes: 5 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
module github.com/EliCDavis/sfm

go 1.21.0

require (
github.com/EliCDavis/bitlib v1.0.0
github.com/EliCDavis/vector v1.5.0
)
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
github.com/EliCDavis/bitlib v1.0.0 h1:iMWUThiBzXnEQTAG3C5Mtjzfna8cpFhrmQylhWSJINM=
github.com/EliCDavis/bitlib v1.0.0/go.mod h1:B/mBEBsbOKSL85ELpHDmN1ncf1gO14Nrum184HWHcPQ=
github.com/EliCDavis/vector v1.5.0 h1:IE7Xco/V9S0inlIoIPu5J87Nnup9RJUEK/PiDLsueY8=
github.com/EliCDavis/vector v1.5.0/go.mod h1:A+Zf8tDKfocwQa9md33HsE53KclL7uayR/cKjKHUFSA=

0 comments on commit 3a6690a

Please sign in to comment.