ANTsX / ANTsR

R interface to the ANTs biomedical image processing library
https://antsx.github.io/ANTsR
Apache License 2.0
128 stars 35 forks source link

Bug: Cropping corrupts the image #28

Closed dorianps closed 9 years ago

dorianps commented 9 years ago

I am using cropImage: predlabel = cropImage(predlabel, predlabel, max(predlabel))

At this stage predlabel can be plotted, but it is already corrupt:

"> predlabel antsImage Pixel Type : float Pixel Size :
Dimensions : 32x49x38 Voxel Spacing: 2x2x2 Origin : -93 138.1172 -82 Direction : 1 0 0 0 -1 0 0 0 1 > predlabel[15,15,15] index not inside the image : [14, 14, 14] [1] NA"

After cropping, resampleImage is performed (within mrvnrfs, but I tried manually as well). There is no error but resampleImage produces a corrupt image, it cannot be plotted and getNeirghborhoodInMask is a matrix with 0 columns.

Cropping works fine if I use traditional code:

ttt2 = as.antsImage(as.array(predlabel)[20:80, 30:100, 1:84]) > ttt2[15,15,15] , , 1

 [,1]

[1,] 0

plot(ttt2) NULL ttt3=resampleImage(ttt2, rep(4,3), 0, 1) plot(ttt3) NULL

stnava commented 9 years ago

as usual - need a reproducible example

brian

On Mon, Mar 16, 2015 at 10:52 AM, dorianps notifications@github.com wrote:

I am using cropImage: predlabel = cropImage(predlabel, predlabel, max(predlabel)) At this stage predlabel can be plotted, but there is already corrupt:

predlabel[15,15,15] index not inside the image : [14, 14, 14] [1] NA

After cropping, resampleImage is performed (within mrvnrfs, but I tried manually as well). There is no error but resampleImage produces a corrupt image, it cannot be plotted and getNeirghborhoodInMask is a matrix with 0 columns.

The image, however, works fine if I use traditional code:

ttt2 = as.antsImage(as.array(predlabel)[20:80, 30:100, 1:84])

ttt2[15,15,15] , , 1

[,1]

[1,] 0

plot(ttt2) NULL ttt3=resampleImage(ttt2, rep(4,3), 0, 1) plot(ttt3) NULL

— Reply to this email directly or view it on GitHub https://github.com/stnava/ANTsR/issues/28.

dorianps commented 9 years ago

https://www.dropbox.com/s/umhio3bp1npu1x5/predlabel.nii.gz?dl=1

predlabel=antsImageRead('predlabel.nii.gz', 3) predlabel = cropImage(predlabel, predlabel, max(predlabel)) predlabel[15,15,15]

stnava commented 9 years ago

here is an example

fi <- antsImageRead(getANTsRData("r16"), 2)

 cropped <- cropImage(fi)

cropped[1,1]

cropped[128,128]

this is the expected behavior because 1,1 is not in the image anymore ... after resampling, though, there is an issue but it's not clear how to resolve this "perfectly" b/c indices and physical space dont match exactly. what needs to be done is that the function SetOutputStartIndex in ResampleImageFilter

http://www.itk.org/Doxygen316/html/classitk_1_1ResampleImageFilter.html#a1f09112c98d174394f8a9dba8d2e1c0

needs to be set correctly ... not sure how to resolve that. So, this needs to be dealt with here:

https://github.com/stnava/ANTs/blob/master/Examples/ResampleImage.cxx#L244

@ntustison ... any thoughts re: how to define this?

stnava commented 9 years ago

i've learned more about this ... no resolution but here is an interesting example:

fi <- antsImageRead( getANTsRData("r16") ,2)
cropped <- cropImage( fi )
rcropped<-resampleImage( cropped, dim(cropped)/2, 1 , 0 )  # not correct ( i think )
antsImageWrite( cropped , '/tmp/crop.nii.gz')
cropread<-antsImageRead("/tmp/crop.nii.gz")
rcropped2<-resampleImage( cropread, dim(cropped)/2, 1 , 0 ) # correct

these produced different results but should not ... @jeffduda @ntustison - any insight / guesses?

bkandel commented 9 years ago

This behavior is somewhat confusing, as this is the only case I'm aware of where indexing an antsImage doesn't work like indexing an array, and as far as I can tell there's no way to tell where the indexing starts. Should we at least make it possible to see where the indexing begins?

2015-03-16 16:47 GMT-04:00 stnava notifications@github.com:

i've learned more about this ... no resolution but here is an interesting example:

fi <- antsImageRead( getANTsRData("r16") ,2) cropped <- cropImage( fi ) rcropped<-resampleImage( cropped, dim(cropped)/2, 1 , 0 ) # not correct ( i think ) antsImageWrite( cropped , '/tmp/crop.nii.gz') cropread<-antsImageRead("/tmp/crop.nii.gz") rcropped2<-resampleImage( cropread, dim(cropped)/2, 1 , 0 ) # correct

these produced different results but should not ... @jeffduda https://github.com/jeffduda @ntustison https://github.com/ntustison - any insight / guesses?

— Reply to this email directly or view it on GitHub https://github.com/stnava/ANTsR/issues/28#issuecomment-81928221.

stnava commented 9 years ago

that (albeit correct) behavior is a separate issue ... but one reason that this is difficult to debug is because i cant (or dont know how to) see the startindex for this image .... this can be accessed via:

image->GetLargestPossibleRegion().GetIndex()

or ( better but doesnt always work right )

image->GetRequestedRegion().GetIndex()

there probably should be an r interface implemented for this ...

the only reason this has not come up previously is that the StartIndex is usually zero for itk images ... but not always (obviously).

brian

On Mon, Mar 16, 2015 at 4:57 PM, bkandel notifications@github.com wrote:

This behavior is somewhat confusing, as this is the only case I'm aware of where indexing an antsImage doesn't work like indexing an array, and as far as I can tell there's no way to tell where the indexing starts. Should we at least make it possible to see where the indexing begins?

2015-03-16 16:47 GMT-04:00 stnava notifications@github.com:

i've learned more about this ... no resolution but here is an interesting example:

fi <- antsImageRead( getANTsRData("r16") ,2) cropped <- cropImage( fi ) rcropped<-resampleImage( cropped, dim(cropped)/2, 1 , 0 ) # not correct ( i think ) antsImageWrite( cropped , '/tmp/crop.nii.gz') cropread<-antsImageRead("/tmp/crop.nii.gz") rcropped2<-resampleImage( cropread, dim(cropped)/2, 1 , 0 ) # correct

these produced different results but should not ... @jeffduda

https://github.com/jeffduda @ntustison https://github.com/ntustison

any insight / guesses?

— Reply to this email directly or view it on GitHub https://github.com/stnava/ANTsR/issues/28#issuecomment-81928221.

— Reply to this email directly or view it on GitHub https://github.com/stnava/ANTsR/issues/28#issuecomment-81935510.

stnava commented 9 years ago

ok - i changed this behavior to use the origin rather than the index

fi <- antsImageRead( getANTsRData("r16") ,2)
cropped <- cropImage( fi )
d1<-decropImage( cropped , fi)
rcropped<-resampleImage( cropped, dim(cropped)/2, 1 , 0 )
upsam<-resampleImage( rcropped, dim(cropped), 1, 0 )
d2<-decropImage( upsam, fi )

the above shows "invertible" cropping resampling and decropping

not sure if this will fix other issues but it seems more consistent ....

bkandel commented 9 years ago

Relevant to this conversation, I enabled indexing of antsImages without explicit indices, so something like img[2, , ] will get the second slice of an image. This returns a matrix or array, not an antsImage, but if you just need the voxel values you can use it.

2015-03-16 17:52 GMT-04:00 stnava notifications@github.com:

ok - i changed this behavior to use the origin rather than the index

fi <- antsImageRead( getANTsRData("r16") ,2) cropped <- cropImage( fi ) d1<-decropImage( cropped , fi) rcropped<-resampleImage( cropped, dim(cropped)/2, 1 , 0 ) upsam<-resampleImage( rcropped, dim(cropped), 1, 0 ) d2<-decropImage( upsam, fi )

the above shows "invertible" cropping resampling and decropping

not sure if this will fix other issues but it seems more consistent ....

— Reply to this email directly or view it on GitHub https://github.com/stnava/ANTsR/issues/28#issuecomment-81957671.