Skip to content

Commit

Permalink
hu api
Browse files Browse the repository at this point in the history
  • Loading branch information
mohsenhariri committed Nov 9, 2023
1 parent 1cf9753 commit 8437dd0
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 2 deletions.
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1.1.6
1.1.7
5 changes: 5 additions & 0 deletions api/preprocessing/hu.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import medviz as viz

image = "/storage/sync/git/mohsen/medviz/test_local/dataset/Patient 1/CT/I1000000.dcm"

viz.get_hu(image, save_path="/path/to/save")
1 change: 1 addition & 0 deletions medviz/preprocess/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@
from .match_image_mask import match_image_masks
from .resampling import resample
from .metadata import metadata
from .hu import get_hu
74 changes: 74 additions & 0 deletions medviz/preprocess/hu.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
import numpy as np
import pydicom

from ..utils import path2loader, path_in, save_path_file


# return pydicom.tag.Tag(int(group, 16), int(element, 16))
def convert_tag(tag_str):
if "|" in tag_str:
group, element = tag_str.split("|")
return pydicom.tag.Tag(int(group, 16), int(element, 16))
else:
return None # Return None for strings that don't contain "|"


def get_hu(file_path, save_path=None):
file_path = path_in(file_path)

dicom_file = pydicom.dcmread(file_path)
pixel_array = dicom_file.pixel_array

rescale_slope = dicom_file.RescaleSlope
rescale_intercept = dicom_file.RescaleIntercept

hu_pixel_array = pixel_array * rescale_slope + rescale_intercept
hu_pixel_array = hu_pixel_array.astype(np.int16)
dicom_file.PixelData = hu_pixel_array.tobytes()
if save_path is not None:
name = f"{file_path.stem}_hu"
save_path = save_path + "/" + name

save_path = save_path_file(save_path, suffix=".dcm")
dicom_file.save_as(save_path)


# def get_hu(path):
# image = path2loader(path)
# image_and_properties, reader, image_and_properties_itk = image

# if reader == "pydicom":
# ds = image_and_properties


# else:
# image = image_and_properties_itk

# metadata_keys = image.GetMetaDataKeys()

# metadata = {
# pydicom.datadict.keyword_for_tag(convert_tag(key)): image.GetMetaData(key)
# for key in metadata_keys
# if convert_tag(key) is not None
# and pydicom.datadict.keyword_for_tag(
# convert_tag(key)
# ) # Only include known tags with "|"
# }


# rescale_intercept = (
# float(image.GetMetaData("0028|1052"))
# if "0028|1052" in metadata_keys
# else metadata.get("scl_inter", None)
# )

# rescale_slope = (
# float(image.GetMetaData("0028|1053"))
# if "0028|1053" in metadata_keys
# else metadata.get("scl_slope", None)
# )


# metadata["itk_HU"] = rescale_intercept == -1024 and rescale_slope == 1

# return metadata["itk_HU"]
2 changes: 1 addition & 1 deletion medviz/utils/helper_reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ def path2loader(path: PathMedicalImage) -> PathMedicalImage:
if isinstance(path, MedicalImage):
return path

path_obj = Path(path)
path_obj = path_in(path)
suffixes = path_obj.suffixes

try:
Expand Down

0 comments on commit 8437dd0

Please sign in to comment.