Project-MONAI / tutorials

MONAI Tutorials
https://monai.io/started.html
Apache License 2.0
1.83k stars 678 forks source link

Inconsistency in ground truth boxes coordinates? #1114

Closed taborzbislaw closed 1 year ago

taborzbislaw commented 1 year ago

Hi,

I have successfully gone through the detection tutorial using your rescaled images in nifty format. Now I am trying to figure out what is the correspondence between the coordinates of the centers of the ground truth bounding boxes (as specified in dataset_fold*.json files) and the centers of nodules seen in CT images from Luna database.

To be specific, I focused on '1.3.6.1.4.1.14519.5.2.1.6279.6001.250397690690072950000431855143.nii.gz' file (resampled image downloaded from your google drive, ground truth boxes for this image are in dataset_fold0.json). I read the image using nibabel library:

import nibabel as nib im = nib.load(imName).get_fdata() aff = nib.load(imName).affine

The affinity matrix aff for this image is: [[ 0.703125 0. 0. -212.43840027] [ 0. 0.703125 0. -250.73840332] [ 0. 0. 1.25 -315.25 ] [ 0. 0. 0. 1. ]]

From the affinity matrix I calculate world coordinates of the image origin and voxel size: imageOrigin = [aff[0,3],aff[1,3],aff[2,3]] voxelSize = [aff[0,0],aff[1,1],aff[2,2]]

For this image there is a big nodule centered approximately at: noduleCenterVoxel = [159,321,136]

Given these coordinates, voxelSize and imageOrigin, I get world coordinates of the center of the nodule: noduleCenterWorldCoord = [o*p + s for o,p,s in zip(noduleCenterVoxel,voxelSize,imageOrigin)]

noduleCenterWorldCoord is equal to [-100.64152526855469, -25.0352783203125, -145.25]

However, in dataset_fold0.json there is no bounding box like this one. There is a bounding box centered at [100.0021827, 22.00611876, -147.0757546]: so the first and the second coordinate from the ground truth bounding box center must be multiplied by -1 to reproduce bounding box center calculated from the affinity matrix. The same holds for all the other bounding boxes in dataset_fold0.json: I have to multiply the first two coordinates by -1 to find center voxels of nodules seen in the image. Could you, please, explain this inconsistency?

Best regards, Zbisław

taborzbislaw commented 1 year ago

Hi,

after running some experiments with the data it appears that the coordinates of the ground truth boxes in dataset_fold*.json could be calculated directly from the pixel locations and affinity matrix of nifti files, according to the procedure from my previous post, and then correctly used in detector training.

For this purpose it is necessary to set affine_lps_to_ras flag to False when defining train_transforms and val_transforms in https://github.com/Project-MONAI/tutorials/blob/608795fd6578b84eef57b6c8f02dd7ce2a51a5c4/detection/luna16_training.py#L99 and https://github.com/Project-MONAI/tutorials/blob/608795fd6578b84eef57b6c8f02dd7ce2a51a5c4/detection/luna16_training.py#L109

Could you, please, confirm?

It appears that skipping multiplication of the first and the second coordinate of the center of a bounding box by (-1) in dataset_fold*.json and calculating these coordinates directly from the affinity matrix of a nifti file and pixel locations of a lesion will make ground truth preparation more clear.

Zbisław

wyli commented 1 year ago

cc @Can-Zhao