Skip to content

Commit

Permalink
Update for image handling with odd dimensions
Browse files Browse the repository at this point in the history
Signed-off-by: Eric Kerfoot <[email protected]>
  • Loading branch information
ericspod committed May 24, 2024
1 parent c699efa commit 8aefe3b
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 8 deletions.
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ FROM alpine:3.20
RUN apk update && \
apk add py3-qt5 py3-numpy py3-pip py3-pillow ttf-freefont mesa-dri-gallium && \
rm -rf /var/cache/apk/* usr/lib/python3.12/EXTERNALLY-MANAGED &&\
pip3 install pydicom pyqtgraph
pip3 install pydicom pyqtgraph pylibjpeg


WORKDIR /dicombrowser
Expand Down
1 change: 0 additions & 1 deletion dicombrowser/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,5 @@
# with this program (LICENSE.txt). If not, see <http://www.gnu.org/licenses/>



from ._version import __version__, __author__, __copyright__
from .dicombrowser import mainargv
5 changes: 2 additions & 3 deletions dicombrowser/_version.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,8 @@
# with this program (LICENSE.txt). If not, see <http://www.gnu.org/licenses/>

__appname__ = "DicomBrowser"
__version_info__ = (1, 5, 0) # global application version, major/minor/patch
__version_info__ = (1, 5, 1) # global application version, major/minor/patch
__version__ = f"{__version_info__[0]}.{__version_info__[1]}.{__version_info__[2]}"
__author__ = "Eric Kerfoot"
__author_email__="[email protected]"
__author_email__ = "[email protected]"
__copyright__ = "Copyright (c) 2016-22 Eric Kerfoot, King's College London, all rights reserved. Licensed under the GPL (see LICENSE.txt)."

25 changes: 25 additions & 0 deletions dicombrowser/dicom.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
from concurrent.futures import ProcessPoolExecutor, as_completed
from io import BytesIO
from glob import glob
from warnings import warn

import numpy as np
from pydicom import dicomio, datadict, errors
Expand Down Expand Up @@ -65,6 +66,30 @@
FULL_NAME_MAP = {v: k for k, v in KEYWORD_NAME_MAP.items()} # maps full names to keywords


def get_2d_equivalent_image(img):
"""Given an array `img` of some arbitrary dimensions, attempt to choose a valid 2D gray/RGB/RGBA image from it."""
ndim = img.ndim
shape = img.shape
color_dim = ndim > 2 and shape[-1] in (1, 3, 4)

if ndim <= 2 or (ndim == 3 and color_dim): # 0D array, 1D array, 2D grayscale or 2D RGB(A)
if ndim < 2:
warn(f"Image has unusual shape {shape}, attempting to visualise")

return img
elif ndim == 3: # 3D grayscale
warn(f"Image is volume with shape {shape}, using mid-slice")
elif ndim == 4 and color_dim:
warn(f"Image is RGB(A) volume with shape {shape}, using mid-slice")
else:
warn(f"Image is unknown volume with shape {shape}, using mid-slices")

# attempt to slice the volume in every dimension that's not height, width, or the channels
stop = 3 if color_dim else 2 # dimensions to not slice in, ie. (height,width) or (height,width,channels)
slices = [s // 2 for s in shape[:-stop]] + [slice(None)] * stop
return img[tuple(slices)]


def get_scaled_image(dcm):
"""Return image data from `dcm` scaled using slope and intercept values."""
try:
Expand Down
7 changes: 4 additions & 3 deletions dicombrowser/dicombrowser.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@

from ._version import __version__
from . import res
from .dicom import load_dicom_dir, load_dicom_zip, SERIES_LIST_COLUMNS, ATTR_TREE_COLUMNS
from .dicom import load_dicom_dir, load_dicom_zip, get_2d_equivalent_image, SERIES_LIST_COLUMNS, ATTR_TREE_COLUMNS
from .models import AttrItemModel, SeriesTreeModel


Expand All @@ -49,6 +49,7 @@

class LoadWorker(QtCore.QRunnable):
"""Loads Dicom data in a separate thread, updating the UI through the given signals."""

def __init__(self, src, status_signal, update_signal):
super().__init__()
self.src = src
Expand Down Expand Up @@ -231,8 +232,8 @@ def set_series_image(self, i, auto_range=False):

if img is None: # if the image is None use the default "no image" object
img = self.noimg
# elif len(img.shape)==3: # multi-channel or multi-dimensional image, use average of dimensions
# img=np.mean(img,axis=2)

img = get_2d_equivalent_image(img) # get something renderable in 2D

self.image_view.setImage(img.T, autoRange=auto_range, autoLevels=self.autoLevelsCheck.isChecked())
self._fill_attr_view()
Expand Down
1 change: 1 addition & 0 deletions dicombrowser/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ def fill_attrs(self, dcm, columns, regex=None, maxValueSize=256):

class SeriesTreeModel(QtGui.QStandardItemModel):
"""Represents the tree of Dicom series organised under nodes for each source file/directory."""

def __init__(self, columns, data={}, parent=None):
super().__init__(parent)
self.columns = columns
Expand Down

0 comments on commit 8aefe3b

Please sign in to comment.