Skip to content

Commit

Permalink
fix a bug when ploting a dataset with images in a range other than 0-…
Browse files Browse the repository at this point in the history
…255 (#1884)

* fix a bug when ploting a dataset with images in a range other than 0-255

* fix

* fix

---------

Co-authored-by: Eugene Khvedchenya <ekhvedchenya@gmail.com>
  • Loading branch information
ofrimasad and BloodAxe committed Mar 4, 2024
1 parent 78beb29 commit 2b601b8
Showing 1 changed file with 13 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -449,6 +449,18 @@ def output_target_format(self):
target_format = transform.output_format
return target_format

@staticmethod
def _standardize_image(image):
# Normalize the image to have minimum of 0 and maximum of 1
image_min = image.min()
image_max = image.max()
normalized_image = (image - image_min) / (image_max - image_min + 1e-8)

# Rescale the normalized image to 0-255
standardized_image = (normalized_image * 255).astype(np.uint8)

return standardized_image

def plot(
self,
max_samples_per_plot: int = 16,
Expand Down Expand Up @@ -493,6 +505,7 @@ def plot(
if image.shape[0] in (1, 3): # (C, H, W) -> (H, W, C)
image = image.transpose((1, 2, 0))

image = self._standardize_image(image)
image = image.astype(np.uint8)
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB) # Detection dataset works with BGR images, so we have to convert to RGB

Expand Down

0 comments on commit 2b601b8

Please sign in to comment.