TissueImageAnalytics / tiatoolbox

Computational Pathology Toolbox developed by TIA Centre, University of Warwick.
https://warwick.ac.uk/tia
Other
374 stars 77 forks source link

Registration ValueError: Mismatch of shape between image and its corresponding mask. #863

Open sm-ak-r33 opened 1 week ago

sm-ak-r33 commented 1 week ago

Description

I tried to register a couple of images with rigid registration, img1.size = 1011600 and img2.size= 1322400

What I Did

I did all the masking and prerequisites and they worked quite fine, but when I began to register with the following code,

DFBR RIGID REGISTRATION

dfbr_fixed_image = np.repeat(np.expand_dims(fixed_image, axis=2), 3, axis=2) dfbr_moving_image = np.repeat(np.expand_dims(moving_image, axis=2), 3, axis=2)

dfbr = DFBRegister() dfbr_transform = dfbr.register( dfbr_fixed_image, dfbr_moving_image, fixed_mask, moving_mask, )

Visualization

original_moving = cv2.warpAffine( moving_image, np.eye(2, 3), fixed_image.shape[:2][::-1], ) dfbr_registered_image = cv2.warpAffine( moving_image, dfbr_transform[0:-1], fixed_image.shape[:2][::-1], ) dfbr_registered_mask = cv2.warpAffine( moving_mask, dfbr_transform[0:-1], fixed_image.shape[:2][::-1], )

before_overlay = np.dstack((original_moving, fixed_image, original_moving)) dfbr_overlay = np.dstack((dfbr_registered_image, fixed_image, dfbr_registered_image))

_, axs = plt.subplots(1, 2, figsize=(15, 10)) axs[0].imshow(before_overlay, cmap="gray") axs[0].set_title("Overlay Before Registration") axs[1].imshow(dfbr_overlay, cmap="gray") axs[1].set_title("Overlay After DFBR") plt.show()



The registration failed and I got the following error:

Downloading: "https://download.pytorch.org/models/vgg16-397923af.pth" to /root/.cache/torch/hub/checkpoints/vgg16-397923af.pth
100%|██████████| 528M/528M [00:06<00:00, 85.1MB/s]
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
[<ipython-input-15-d10e6d004d1e>](https://localhost:8080/#) in <cell line: 6>()
      4 
      5 dfbr = DFBRegister()
----> 6 dfbr_transform = dfbr.register(
      7     dfbr_fixed_image,
      8     dfbr_moving_image,

2 frames
[/usr/local/lib/python3.10/dist-packages/tiatoolbox/tools/registration/wsi_registration.py](https://localhost:8080/#) in register(self, fixed_img, moving_img, fixed_mask, moving_mask, transform_initializer)
   1124         # Perform Pre-alignment
   1125         if transform_initializer is None:
-> 1126             transform_initializer, moving_img, moving_mask, before_dice = prealignment(
   1127                 fixed_img,
   1128                 moving_img,

[/usr/local/lib/python3.10/dist-packages/tiatoolbox/tools/registration/wsi_registration.py](https://localhost:8080/#) in prealignment(fixed_img, moving_img, fixed_mask, moving_mask, dice_overlap, rotation_step)
    182     fixed_img = np.squeeze(fixed_img)
    183     moving_img = np.squeeze(moving_img)
--> 184     fixed_img, moving_img = _check_dims(fixed_img, moving_img, fixed_mask, moving_mask)
    185 
    186     if rotation_step < 10 or rotation_step > 20:  # noqa: PLR2004

[/usr/local/lib/python3.10/dist-packages/tiatoolbox/tools/registration/wsi_registration.py](https://localhost:8080/#) in _check_dims(fixed_img, moving_img, fixed_mask, moving_mask)
     66     ):
     67         msg = "Mismatch of shape between image and its corresponding mask."
---> 68         raise ValueError(msg)
     69 
     70     if len(fixed_img.shape) == RGB_IMAGE_DIM:

ValueError: Mismatch of shape between image and its corresponding mask.
Downloading: "https://download.pytorch.org/models/vgg16-397923af.pth" to /root/.cache/torch/hub/checkpoints/vgg16-397923af.pth
100%|██████████| 528M/528M [00:06<00:00, 85.1MB/s]
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
[<ipython-input-15-d10e6d004d1e>](https://localhost:8080/#) in <cell line: 6>()
      4 
      5 dfbr = DFBRegister()
----> 6 dfbr_transform = dfbr.register(
      7     dfbr_fixed_image,
      8     dfbr_moving_image,

2 frames
[/usr/local/lib/python3.10/dist-packages/tiatoolbox/tools/registration/wsi_registration.py](https://localhost:8080/#) in register(self, fixed_img, moving_img, fixed_mask, moving_mask, transform_initializer)
   1124         # Perform Pre-alignment
   1125         if transform_initializer is None:
-> 1126             transform_initializer, moving_img, moving_mask, before_dice = prealignment(
   1127                 fixed_img,
   1128                 moving_img,

[/usr/local/lib/python3.10/dist-packages/tiatoolbox/tools/registration/wsi_registration.py](https://localhost:8080/#) in prealignment(fixed_img, moving_img, fixed_mask, moving_mask, dice_overlap, rotation_step)
    182     fixed_img = np.squeeze(fixed_img)
    183     moving_img = np.squeeze(moving_img)
--> 184     fixed_img, moving_img = _check_dims(fixed_img, moving_img, fixed_mask, moving_mask)
    185 
    186     if rotation_step < 10 or rotation_step > 20:  # noqa: PLR2004

[/usr/local/lib/python3.10/dist-packages/tiatoolbox/tools/registration/wsi_registration.py](https://localhost:8080/#) in _check_dims(fixed_img, moving_img, fixed_mask, moving_mask)
     66     ):
     67         msg = "Mismatch of shape between image and its corresponding mask."
---> 68         raise ValueError(msg)
     69 
     70     if len(fixed_img.shape) == RGB_IMAGE_DIM:

ValueError: Mismatch of shape between image and its corresponding mask.Downloading: "https://download.pytorch.org/models/vgg16-397923af.pth" to /root/.cache/torch/hub/checkpoints/vgg16-397923af.pth
100%|██████████| 528M/528M [00:06<00:00, 85.1MB/s]
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
[<ipython-input-15-d10e6d004d1e>](https://localhost:8080/#) in <cell line: 6>()
      4 
      5 dfbr = DFBRegister()
----> 6 dfbr_transform = dfbr.register(
      7     dfbr_fixed_image,
      8     dfbr_moving_image,

2 frames
[/usr/local/lib/python3.10/dist-packages/tiatoolbox/tools/registration/wsi_registration.py](https://localhost:8080/#) in register(self, fixed_img, moving_img, fixed_mask, moving_mask, transform_initializer)
   1124         # Perform Pre-alignment
   1125         if transform_initializer is None:
-> 1126             transform_initializer, moving_img, moving_mask, before_dice = prealignment(
   1127                 fixed_img,
   1128                 moving_img,

[/usr/local/lib/python3.10/dist-packages/tiatoolbox/tools/registration/wsi_registration.py](https://localhost:8080/#) in prealignment(fixed_img, moving_img, fixed_mask, moving_mask, dice_overlap, rotation_step)
    182     fixed_img = np.squeeze(fixed_img)
    183     moving_img = np.squeeze(moving_img)
--> 184     fixed_img, moving_img = _check_dims(fixed_img, moving_img, fixed_mask, moving_mask)
    185 
    186     if rotation_step < 10 or rotation_step > 20:  # noqa: PLR2004

[/usr/local/lib/python3.10/dist-packages/tiatoolbox/tools/registration/wsi_registration.py](https://localhost:8080/#) in _check_dims(fixed_img, moving_img, fixed_mask, moving_mask)
     66     ):
     67         msg = "Mismatch of shape between image and its corresponding mask."
---> 68         raise ValueError(msg)
     69 
     70     if len(fixed_img.shape) == RGB_IMAGE_DIM:

ValueError: Mismatch of shape between image and its corresponding mask.
sm-ak-r33 commented 1 week ago

Note: it is working for other images, just not for this specific image.