def dcmmf2memmap(dcm_file, orientation): reader = gdcm.ImageReader() reader.SetFileName(dcm_file) reader.Read() image = reader.GetImage() xs, ys, zs = image.GetSpacing() pf = image.GetPixelFormat() samples_per_pixel = pf.GetSamplesPerPixel() np_image = converters.gdcm_to_numpy(image, pf.GetSamplesPerPixel() == 1) if samples_per_pixel == 3: np_image = image_normalize(rgb2gray(np_image), 0, 255) temp_file = tempfile.mktemp() matrix = numpy.memmap(temp_file, mode="w+", dtype="int16", shape=np_image.shape) z, y, x = np_image.shape if orientation == "CORONAL": spacing = xs, zs, ys matrix.shape = y, z, x for n in range(z): matrix[:, n, :] = np_image[n][::-1] elif orientation == "SAGITTAL": spacing = zs, ys, xs matrix.shape = y, x, z for n in range(z): matrix[:, :, n] = np_image[n][::-1] else: spacing = xs, ys, zs matrix[:] = np_image[:, ::-1, :] matrix.flush() scalar_range = matrix.min(), matrix.max() return matrix, scalar_range, spacing, temp_file
def create_dicom_thumbnails(image, window=None, level=None): pf = image.GetPixelFormat() np_image = converters.gdcm_to_numpy(image, pf.GetSamplesPerPixel() == 1) if window is None or level is None: _min, _max = np_image.min(), np_image.max() window = _max - _min level = _min + window / 2 if image.GetNumberOfDimensions() >= 3: thumbnail_paths = [] for i in range(np_image.shape[0]): thumb_image = zoom(np_image[i], 0.25) thumb_image = np.array( get_LUT_value_255(thumb_image, window, level), dtype=np.uint8 ) thumbnail_path = tempfile.mktemp(prefix="thumb_", suffix=".png") imageio.imsave(thumbnail_path, thumb_image) thumbnail_paths.append(thumbnail_path) return thumbnail_paths else: thumbnail_path = tempfile.mktemp(prefix="thumb_", suffix=".png") if pf.GetSamplesPerPixel() == 1: thumb_image = zoom(np_image, 0.25) thumb_image = np.array( get_LUT_value_255(thumb_image, window, level), dtype=np.uint8 ) else: thumb_image = zoom(np_image, (0.25, 0.25, 1)) imageio.imsave(thumbnail_path, thumb_image) return thumbnail_path
def dcmmf2memmap(dcm_file, orientation): reader = gdcm.ImageReader() reader.SetFileName(dcm_file) reader.Read() image = reader.GetImage() xs, ys, zs = image.GetSpacing() pf = image.GetPixelFormat() np_image = converters.gdcm_to_numpy(image, pf.GetSamplesPerPixel() == 1) temp_file = tempfile.mktemp() matrix = numpy.memmap(temp_file, mode='w+', dtype='int16', shape=np_image.shape) print("Number of dimensions", np_image.shape) z, y, x = np_image.shape if orientation == 'CORONAL': spacing = xs, zs, ys matrix.shape = y, z, x for n in range(z): matrix[:, n, :] = np_image[n][::-1] elif orientation == 'SAGITTAL': spacing = zs, ys, xs matrix.shape = y, x, z for n in range(z): matrix[:, :, n] = np_image[n][::-1] else: spacing = xs, ys, zs matrix[:] = np_image[:, ::-1, :] matrix.flush() scalar_range = matrix.min(), matrix.max() return matrix, scalar_range, spacing, temp_file
def read_dcm_slice_as_np2(filename, resolution_percentage=1.0): reader = gdcm.ImageReader() reader.SetFileName(filename) reader.Read() image = reader.GetImage() output = converters.gdcm_to_numpy(image) if resolution_percentage < 1.0: output = zoom(output, resolution_percentage) return output