Skip to content

Commit

Permalink
Merge pull request #13 from okieraised/hotfix/get-slice-index
Browse files Browse the repository at this point in the history
Hotfix/get slice index
  • Loading branch information
okieraised authored Apr 3, 2023
2 parents f3f14a6 + f9cdf50 commit fa087bb
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 4 deletions.
46 changes: 46 additions & 0 deletions annotation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"github.com/stretchr/testify/assert"
"io/ioutil"
"os"
"strings"
"testing"
)

Expand Down Expand Up @@ -190,3 +191,48 @@ func TestSegmentation_Annotation2(t *testing.T) {

fmt.Println(len(b))
}

func TestSegmentation_Annotation3(t *testing.T) {

y := []float64{1, 3, 4, 6, 8, 2, 532, 6234, 5, 75, 75, 656}
var res string
for _, e := range y {
res += fmt.Sprintf("%d ", int(e))
}

fmt.Println("res", strings.TrimSpace(res))
}

func TestSegmentation_Annotation4(t *testing.T) {
assert := assert.New(t)

filePath := "/home/tripg/Downloads/annot_import/CT_Abdo.1680080052.TSK-7.seg.nii.gz"
rd, err := NewNiiReader(WithReadImageFile(filePath), WithReadRetainHeader(true))
assert.NoError(err)
err = rd.Parse()
assert.NoError(err)

voxels := rd.GetNiiData().GetVoxels()
//voxels.FlipX()
//voxels.FlipY()
//voxels.FlipZ()
err = rd.GetNiiData().SetVoxelToRawVolume(voxels)
assert.NoError(err)

segments, err := rd.GetNiiData().GetVoxels().ImportAsRLE()
assert.NoError(err)

voxels, err = rd.GetNiiData().GetVoxels().ExportSingleFromRLE(segments)
assert.NoError(err)

err = rd.GetNiiData().SetVoxelToRawVolume(voxels)
assert.NoError(err)

writer, err := NewNiiWriter("/home/tripg/Downloads/reexport.nii.gz",
WithWriteNIfTIData(rd.GetNiiData()),
WithWriteCompression(true),
)
err = writer.WriteToFile()
assert.NoError(err)

}
10 changes: 6 additions & 4 deletions pkg/nifti/voxel.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func NewVoxels(dimX, dimY, dimZ, dimT int64, datatype int32) *Voxels {
}

// Flip flips the image along the specified axes
func (v *Voxels) Flip(flipX, flipY, flipZ bool) {
func (v *Voxels) Flip(flipX, flipY, flipZ bool) *Voxels {
if flipX {
v.FlipX()
}
Expand All @@ -36,6 +36,8 @@ func (v *Voxels) Flip(flipX, flipY, flipZ bool) {
if flipZ {
v.FlipZ()
}

return v
}

// FlipSagittal flips the image along Sagittal-axis (Y-Z)
Expand Down Expand Up @@ -141,10 +143,10 @@ func (v *Voxels) GetDimT() int64 {

// GetSlice returns the values of voxel as a 1-D slice of float64 calculated from z, t input
func (v *Voxels) GetSlice(z, t int64) []float64 {
res := make([]float64, 0)
res := make([]float64, v.dimX*v.dimY)
for x := int64(0); x < v.dimX; x++ {
for y := int64(0); y < v.dimY; y++ {
res = append(res, v.Get(x, y, z, t))
res[y*v.dimX+x] = v.Get(x, y, z, t)
}
}
return res
Expand Down Expand Up @@ -196,7 +198,6 @@ func (v *Voxels) Histogram(bins int) (utils.Histogram, error) {

// RLEEncode encodes the 1-D float64 array using the RLE encoding
func (v *Voxels) RLEEncode() ([]float64, error) {
//v.voxel = []float64{1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 11, 11, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}
return RLEEncode(v.voxel)
}

Expand All @@ -222,6 +223,7 @@ func (v *Voxels) ImportAsRLE() ([]SegmentRLE, error) {
continue
}
keyArr := make([]float64, len(sliceData))

for idx, voxVal := range sliceData {
if voxVal == key {
keyArr[idx] = key
Expand Down

0 comments on commit fa087bb

Please sign in to comment.