|
| 1 | +""" |
| 2 | +A few shortcut functions |
| 3 | +""" |
| 4 | +from typing import Dict, Union |
| 5 | + |
| 6 | +def display2D( |
| 7 | + image:Union[Image, ProcessObject]=None, |
| 8 | + segmentation:Union[Image, ProcessObject]=None, |
| 9 | + lines:Union[Mesh, ProcessObject]=None, |
| 10 | + vertices: Union[Mesh, ProcessObject]=None, |
| 11 | + intensityLevel:float=None, |
| 12 | + intensityWindow:float=None, |
| 13 | + segmentationColors:Dict[int, Color]=None, |
| 14 | + segmentationOpacity:float=0.5, |
| 15 | + segmentationBorderOpacity:float=None, |
| 16 | + segmentationBorderRadius:int=1, |
| 17 | + lineWidth:float=1, |
| 18 | + lineColor:Color=Color.Green(), |
| 19 | + vertexSize:float=10.0, |
| 20 | + vertexSizeIsInPixels:bool=True, |
| 21 | + vertexMinSize:float=1.0, |
| 22 | + vertexColor:Color=Color.Null(), |
| 23 | + vertexOpacity:float=1.0, |
| 24 | + bgcolor:Color=Color.White(), |
| 25 | + width:int=None, |
| 26 | + height:int=None, |
| 27 | + timeout:int=None, |
| 28 | + renderToImage:bool=False, |
| 29 | + returnWindow:bool=False, |
| 30 | + ): |
| 31 | + """ |
| 32 | + @brief Shortcut for displaying image, segmentation and meshes using SimpleWindow2D |
| 33 | +
|
| 34 | + :param image: Image to display |
| 35 | + :param segmentation: Segmentation to display |
| 36 | + :param lines: Lines to display |
| 37 | + :param vertices: Vertices to display |
| 38 | + :param intensityLevel: Intensity level for image rendering |
| 39 | + :param intensityWindow: Intensity window for image rendering |
| 40 | + :param segmentationColors: Colors to use for segmentation |
| 41 | + :param segmentationOpacity: Opacity of segmentation |
| 42 | + :param segmentationBorderOpacity: Border opacity of segmentation |
| 43 | + :param segmentationBorderRadius: Size of segmentation border |
| 44 | + :param lineWidth: Width of line |
| 45 | + :param lineColor: Color of line |
| 46 | + :param vertexSize Vertex point size (can be in pixels or millimeters, see sizeIsInPixels parameter) |
| 47 | + :param vertexSizeIsInPixels Whether size is given in pixels or millimeters |
| 48 | + :param vertexMinSize Minimum size in pixels, used when sizeInPixels = false |
| 49 | + :param vertexColor Override color stored for each vertex |
| 50 | + :param vertexOpacity Opacity of vertices: 1 = no transparency, 0 = fully transparent |
| 51 | + :param bgcolor: Background color |
| 52 | + :param width: Width of window |
| 53 | + :param height: Height of window |
| 54 | + :param timeout: If set to a number, the window will auto close after this many milliseconds |
| 55 | + :param renderToImage: Use RenderToImage instead of SimpleWindow and return the resulting image |
| 56 | + :param returnWindow: Whether to return the window object, or to run it |
| 57 | + :return: |
| 58 | + """ |
| 59 | + |
| 60 | + if image is None and segmentation is None and lines is None and vertices is None: |
| 61 | + raise ValueError('No data was given to display2D') |
| 62 | + |
| 63 | + def set_default_value(x, default): |
| 64 | + return default if x is None else x |
| 65 | + |
| 66 | + width = set_default_value(width, 0) |
| 67 | + height = set_default_value(height, 0) |
| 68 | + intensityLevel = set_default_value(intensityLevel, -1) |
| 69 | + intensityWindow = set_default_value(intensityWindow, -1) |
| 70 | + segmentationColors = set_default_value(segmentationColors, LabelColors()) |
| 71 | + segmentationBorderOpacity = set_default_value(segmentationBorderOpacity, -1) |
| 72 | + |
| 73 | + renderers = [] |
| 74 | + |
| 75 | + renderer = ImageRenderer.create( |
| 76 | + level=intensityLevel, |
| 77 | + window=intensityWindow |
| 78 | + ).connect(image) |
| 79 | + renderers.append(renderer) |
| 80 | + |
| 81 | + if segmentation is not None: |
| 82 | + renderer = SegmentationRenderer.create( |
| 83 | + colors=segmentationColors, |
| 84 | + opacity=segmentationOpacity, |
| 85 | + borderOpacity=segmentationBorderOpacity, |
| 86 | + borderRadius=segmentationBorderRadius |
| 87 | + ).connect(segmentation) |
| 88 | + renderers.append(renderer) |
| 89 | + |
| 90 | + if lines is not None: |
| 91 | + renderer = LineRenderer.create( |
| 92 | + lineWidth=lineWidth, |
| 93 | + color=lineColor |
| 94 | + ).connect(lines) |
| 95 | + renderers.append(renderer) |
| 96 | + |
| 97 | + if vertices is not None: |
| 98 | + renderer = VertexRenderer.create( |
| 99 | + size=vertexSize, |
| 100 | + sizeIsInPixels=vertexSizeIsInPixels, |
| 101 | + minSize=vertexMinSize, |
| 102 | + color=vertexColor, |
| 103 | + opacity=vertexOpacity, |
| 104 | + ).connect(vertices) |
| 105 | + renderers.append(renderer) |
| 106 | + |
| 107 | + if renderToImage: |
| 108 | + render = RenderToImage.create( |
| 109 | + bgcolor=bgcolor, |
| 110 | + width=width, |
| 111 | + height=height |
| 112 | + ).connect(renderers) |
| 113 | + return render.runAndGetOutputData() |
| 114 | + else: |
| 115 | + window = SimpleWindow2D.create( |
| 116 | + bgcolor=bgcolor, |
| 117 | + width=width, |
| 118 | + height=height |
| 119 | + ).connect(renderers) |
| 120 | + if timeout: |
| 121 | + window.setTimeout(timeout) |
| 122 | + if returnWindow: |
| 123 | + return window |
| 124 | + else: |
| 125 | + window.run() |
0 commit comments