Repository: Project-MONAI/MONAI

Issue:
ScaleIntensityRangePercentile bug
**Describe the bug**
In ScaleIntensityRangePercentile, the output is converted back to input dtype and ignored the `dtype` input. If input is uint8,  the scaled image is wrong. 
https://github.com/Project-MONAI/MONAI/blob/59a7211070538586369afd4a01eca0a7fe2e742e/monai/transforms/intensity/array.py#L1414

In ScaleIntensityRange, the implementation is correct
https://github.com/Project-MONAI/MONAI/blob/59a7211070538586369afd4a01eca0a7fe2e742e/monai/transforms/intensity/array.py#L1006


Discussion:
KumoLiu: /build
KumoLiu: /build
KumoLiu: /build
KumoLiu: /build

Patch:
diff --git a/monai/transforms/intensity/array.py b/monai/transforms/intensity/array.py
index 3b813809..20000c52 100644
--- a/monai/transforms/intensity/array.py
+++ b/monai/transforms/intensity/array.py
@@ -1411,7 +1411,7 @@ class ScaleIntensityRangePercentiles(Transform):
         else:
             img_t = self._normalize(img=img_t)
 
-        return convert_to_dst_type(img_t, dst=img)[0]
+        return convert_to_dst_type(img_t, dst=img, dtype=self.dtype)[0]
 
 
 class MaskIntensity(Transform):


Test patch:
diff --git a/tests/test_scale_intensity_range_percentiles.py b/tests/test_scale_intensity_range_percentiles.py
index 7c3a684a..a7390efe 100644
--- a/tests/test_scale_intensity_range_percentiles.py
+++ b/tests/test_scale_intensity_range_percentiles.py
@@ -14,6 +14,7 @@ from __future__ import annotations
 import unittest
 
 import numpy as np
+import torch
 
 from monai.transforms.intensity.array import ScaleIntensityRangePercentiles
 from tests.utils import TEST_NDARRAYS, NumpyImageTestCase2D, assert_allclose
@@ -34,6 +35,7 @@ class TestScaleIntensityRangePercentiles(NumpyImageTestCase2D):
         scaler = ScaleIntensityRangePercentiles(lower=lower, upper=upper, b_min=b_min, b_max=b_max, dtype=np.uint8)
         for p in TEST_NDARRAYS:
             result = scaler(p(img))
+            self.assertEqual(result.dtype, torch.uint8)
             assert_allclose(result, p(expected), type_test="tensor", rtol=1e-4)
 
     def test_relative_scaling(self):