Commit 53c78d4feebe95a7a78f3c65d2629904a425eda4
1 parent
08a3b8ee
Exists in
master
Resizing image when creating surface with low and medium quality
Showing
3 changed files
with
44 additions
and
29 deletions
Show diff stats
invesalius/data/imagedata_utils.py
... | ... | @@ -125,6 +125,16 @@ def resize_slice(im_array, resolution_percentage): |
125 | 125 | return out |
126 | 126 | |
127 | 127 | |
128 | +def resize_image_array(image, resolution_percentage, as_mmap=False): | |
129 | + out = zoom(image, resolution_percentage, image.dtype, order=2) | |
130 | + if as_mmap: | |
131 | + fname = tempfile.mktemp(suffix="_resized") | |
132 | + out_mmap = np.memmap(fname, shape=out.shape, dtype=out.dtype, mode='w+') | |
133 | + out_mmap[:] = out | |
134 | + return out_mmap | |
135 | + return out | |
136 | + | |
137 | + | |
128 | 138 | def read_dcm_slice_as_np2(filename, resolution_percentage=1.0): |
129 | 139 | reader = gdcm.ImageReader() |
130 | 140 | reader.SetFileName(filename) | ... | ... |
invesalius/data/surface.py
... | ... | @@ -598,6 +598,10 @@ class SurfaceManager(): |
598 | 598 | filename_img = slice_.matrix_filename |
599 | 599 | spacing = slice_.spacing |
600 | 600 | |
601 | + mask_temp_file = mask.temp_file | |
602 | + mask_shape = mask.matrix.shape | |
603 | + mask_dtype = mask.matrix.dtype | |
604 | + | |
601 | 605 | algorithm = surface_parameters['method']['algorithm'] |
602 | 606 | options = surface_parameters['method']['options'] |
603 | 607 | |
... | ... | @@ -608,6 +612,8 @@ class SurfaceManager(): |
608 | 612 | |
609 | 613 | fill_border_holes = surface_parameters['options'].get('fill_border_holes', True) |
610 | 614 | |
615 | + print(surface_parameters) | |
616 | + | |
611 | 617 | mode = 'CONTOUR' # 'GRAYSCALE' |
612 | 618 | min_value, max_value = mask.threshold_range |
613 | 619 | colour = mask.colour[:3] |
... | ... | @@ -641,6 +647,16 @@ class SurfaceManager(): |
641 | 647 | else: |
642 | 648 | flip_image = True |
643 | 649 | |
650 | + if imagedata_resolution > 0: | |
651 | + spacing = tuple([s * imagedata_resolution for s in spacing]) | |
652 | + matrix = iu.resize_image_array(matrix, 1.0/imagedata_resolution, True) | |
653 | + mask = iu.resize_image_array(mask.matrix, 1.0/imagedata_resolution, True) | |
654 | + | |
655 | + filename_img = matrix.filename | |
656 | + mask_temp_file = mask.filename | |
657 | + mask_shape = mask.shape | |
658 | + mask_dtype = mask.dtype | |
659 | + | |
644 | 660 | n_processors = multiprocessing.cpu_count() |
645 | 661 | |
646 | 662 | o_piece = 1 |
... | ... | @@ -653,6 +669,8 @@ class SurfaceManager(): |
653 | 669 | manager = multiprocessing.Manager() |
654 | 670 | msg_queue = manager.Queue(1) |
655 | 671 | |
672 | + print("Resolution", imagedata_resolution) | |
673 | + | |
656 | 674 | # If InVesalius is running without GUI |
657 | 675 | if wx.GetApp() is None: |
658 | 676 | for i in range(n_pieces): |
... | ... | @@ -662,8 +680,8 @@ class SurfaceManager(): |
662 | 680 | print("new_piece", roi) |
663 | 681 | f = pool.apply_async(surface_process.create_surface_piece, |
664 | 682 | args = (filename_img, matrix.shape, matrix.dtype, |
665 | - mask.temp_file, mask.matrix.shape, | |
666 | - mask.matrix.dtype, roi, spacing, mode, | |
683 | + mask_temp_file, mask_shape, | |
684 | + mask_dtype, roi, spacing, mode, | |
667 | 685 | min_value, max_value, decimate_reduction, |
668 | 686 | smooth_relaxation_factor, |
669 | 687 | smooth_iterations, language, flip_image, |
... | ... | @@ -720,31 +738,18 @@ class SurfaceManager(): |
720 | 738 | end = init + piece_size + o_piece |
721 | 739 | roi = slice(init, end) |
722 | 740 | print("new_piece", roi) |
723 | - try: | |
724 | - f = pool.apply_async(surface_process.create_surface_piece, | |
725 | - args = (filename_img, matrix.shape, matrix.dtype, | |
726 | - mask.temp_file, mask.matrix.shape, | |
727 | - mask.matrix.dtype, roi, spacing, mode, | |
728 | - min_value, max_value, decimate_reduction, | |
729 | - smooth_relaxation_factor, | |
730 | - smooth_iterations, language, flip_image, | |
731 | - algorithm != 'Default', algorithm, | |
732 | - imagedata_resolution, fill_border_holes), | |
733 | - callback=lambda x: filenames.append(x), | |
734 | - error_callback=functools.partial(self._on_callback_error, | |
735 | - dialog=sp)) | |
736 | - # python2 | |
737 | - except TypeError: | |
738 | - f = pool.apply_async(surface_process.create_surface_piece, | |
739 | - args = (filename_img, matrix.shape, matrix.dtype, | |
740 | - mask.temp_file, mask.matrix.shape, | |
741 | - mask.matrix.dtype, roi, spacing, mode, | |
742 | - min_value, max_value, decimate_reduction, | |
743 | - smooth_relaxation_factor, | |
744 | - smooth_iterations, language, flip_image, | |
745 | - algorithm != 'Default', algorithm, | |
746 | - imagedata_resolution, fill_border_holes), | |
747 | - callback=lambda x: filenames.append(x)) | |
741 | + f = pool.apply_async(surface_process.create_surface_piece, | |
742 | + args = (filename_img, matrix.shape, matrix.dtype, | |
743 | + mask_temp_file, mask_shape, | |
744 | + mask_dtype, roi, spacing, mode, | |
745 | + min_value, max_value, decimate_reduction, | |
746 | + smooth_relaxation_factor, | |
747 | + smooth_iterations, language, flip_image, | |
748 | + algorithm != 'Default', algorithm, | |
749 | + imagedata_resolution, fill_border_holes), | |
750 | + callback=lambda x: filenames.append(x), | |
751 | + error_callback=functools.partial(self._on_callback_error, | |
752 | + dialog=sp)) | |
748 | 753 | |
749 | 754 | while len(filenames) != n_pieces: |
750 | 755 | if sp.WasCancelled() or not sp.running: | ... | ... |
invesalius/data/surface_process.py
... | ... | @@ -131,8 +131,8 @@ def create_surface_piece(filename, shape, dtype, mask_filename, mask_shape, |
131 | 131 | image = converters.to_vtk(a_image, spacing, roi.start, "AXIAL", padding=padding) |
132 | 132 | del a_image |
133 | 133 | |
134 | - if imagedata_resolution: | |
135 | - image = ResampleImage3D(image, imagedata_resolution) | |
134 | + # if imagedata_resolution: | |
135 | + # image = ResampleImage3D(image, imagedata_resolution) | |
136 | 136 | |
137 | 137 | flip = vtk.vtkImageFlip() |
138 | 138 | flip.SetInputData(image) | ... | ... |