| from PIL import ImageDraw, ImageFont, Image |
| import numpy as np |
| import cv2 |
|
|
|
|
| def _get_text_dimensions(draw, text, font): |
| """ |
| Calculate the width and height of text in pixels. |
| |
| Args: |
| draw: ImageDraw object |
| text: String of text to measure |
| font: ImageFont object |
| |
| Returns: |
| tuple: (width, height) in pixels |
| """ |
| |
| bbox = draw.textbbox((0, 0), text, font=font) |
|
|
| |
| |
| width = bbox[2] - bbox[0] |
| height = bbox[3] - bbox[1] |
|
|
| return width, height |
|
|
|
|
| def add_landmarks_and_line_overlay(pil_img, p1_coords, p2_coords, line_color="#00FF00", point_color="#FF0000"): |
| """ |
| Add landmarks (points) and a line connecting them to an image. |
| |
| Args: |
| pil_img: PIL Image in RGB or RGBA format |
| p1_coords: List of [dim0, dim1] coordinates for the first point |
| p2_coords: List of [dim0, dim1] coordinates for the second point |
| |
| Returns: |
| PIL Image with the landmarks and connecting line overlay |
| """ |
| |
| draw = ImageDraw.Draw(pil_img) |
|
|
| |
| |
| x1, y1 = p1_coords[1], p1_coords[0] |
| x2, y2 = p2_coords[1], p2_coords[0] |
|
|
| |
| draw.line([(x1, y1), (x2, y2)], fill=line_color, width=2) |
|
|
| |
| point_radius = 3 |
| draw.ellipse( |
| [ |
| (x1 - point_radius, y1 - point_radius), |
| (x1 + point_radius, y1 + point_radius), |
| ], |
| fill=point_color, |
| ) |
| draw.ellipse( |
| [ |
| (x2 - point_radius, y2 - point_radius), |
| (x2 + point_radius, y2 + point_radius), |
| ], |
| fill=point_color, |
| ) |
|
|
| return pil_img |
|
|
|
|
| def add_bbox_overlay(pil_img, bbox_min_coords, bbox_max_coords): |
| """ |
| Add a bounding box overlay to an image. |
| |
| Args: |
| pil_img: PIL Image in RGB or RGBA format |
| bbox_min_coords: List of [dim0_min, dim1_min] coordinates for the top-left corner of the bounding box |
| bbox_max_coords: List of [dim0_max, dim1_max] coordinates for the bottom-right corner of the bounding box |
| |
| NOTE: For the coordinate definition in the MedVision dataset, please refer to the |
| `medvision_ds.utils.benchmark_planner.MedVision_BenchmarkPlannerDetection._find_bounding_boxes_2D` |
| |
| Returns: |
| PIL Image with the bounding box overlay |
| """ |
| |
| |
| |
| |
| |
|
|
| |
| x_min = bbox_min_coords[1] |
| y_min = bbox_min_coords[0] |
| x_max = bbox_max_coords[1] |
| y_max = bbox_max_coords[0] |
|
|
| |
| draw = ImageDraw.Draw(pil_img) |
| draw.rectangle([(x_min, y_min), (x_max, y_max)], outline="#00FF00", width=2) |
| return pil_img |
|
|
|
|
| def add_bbox_overlay_solid(pil_img, bbox_min_coords, bbox_max_coords): |
| """ |
| Add a semi-transparent solid bounding box overlay to an image. |
| |
| Args: |
| pil_img: PIL Image in RGB or RGBA format |
| bbox_min_coords: List of [dim0_min, dim1_min] coordinates for the top-left corner of the bounding box |
| bbox_max_coords: List of [dim0_max, dim1_max] coordinates for the bottom-right corner of the bounding box |
| |
| NOTE: For the coordinate definition in the MedVision dataset, please refer to the |
| `medvision_ds.utils.benchmark_planner.MedVision_BenchmarkPlannerDetection._find_bounding_boxes_2D` |
| |
| Returns: |
| PIL Image with the solid bounding box overlay |
| """ |
| |
| |
| |
| |
| |
|
|
| |
| x_min = bbox_min_coords[1] |
| y_min = bbox_min_coords[0] |
| x_max = bbox_max_coords[1] |
| y_max = bbox_max_coords[0] |
|
|
| |
| pil_img = pil_img.convert("RGBA") |
| |
| |
| draw = ImageDraw.Draw(pil_img) |
| |
| |
| draw.rectangle([(x_min, y_min), (x_max, y_max)], fill=(0, 255, 0, 64)) |
| |
| return pil_img |
|
|
|
|
| def add_mask_overlay_contour(pil_img, mask_2d_binary): |
| """ |
| Add a green contour outline to an image based on a binary mask. |
| |
| Args: |
| pil_img: PIL Image in RGB or RGBA format |
| mask_2d_binary: Binary numpy array representing the mask |
| |
| Returns: |
| PIL Image with the mask contour overlaid in green |
| """ |
|
|
| |
| img_np = np.array(pil_img) |
|
|
| |
| mask = mask_2d_binary.astype(np.uint8) |
| if mask.shape[:2] != img_np.shape[:2]: |
| mask = cv2.resize( |
| mask, (img_np.shape[1], img_np.shape[0]), interpolation=cv2.INTER_NEAREST |
| ) |
|
|
| |
| contours, _ = cv2.findContours(mask, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) |
|
|
| |
| img_with_contour = img_np.copy() |
|
|
| |
| cv2.drawContours(img_with_contour, contours, -1, (0, 255, 0), 2) |
|
|
| |
| return Image.fromarray(img_with_contour) |
|
|
|
|
| def add_mask_overlay_solid(pil_img, mask_2d_binary): |
| """ |
| Add a semi-transparent green overlay to an image based on a binary mask. |
| |
| Args: |
| pil_img: PIL Image in RGB or RGBA format |
| mask_2d_binary: Binary numpy array representing the mask |
| |
| Returns: |
| PIL Image with the mask overlaid in green |
| """ |
| |
| overlay = Image.new("RGBA", pil_img.size, (0, 255, 0, 0)) |
|
|
| |
| mask_pil = Image.fromarray((mask_2d_binary * 64).astype(np.uint8), mode="L") |
| if mask_pil.size != pil_img.size: |
| mask_pil = mask_pil.resize(pil_img.size) |
|
|
| |
| overlay.putalpha(mask_pil) |
|
|
| |
| pil_img = pil_img.convert("RGBA") |
|
|
| |
| pil_img = Image.alpha_composite(pil_img, overlay) |
|
|
| |
| pil_img = pil_img.convert("RGB") |
|
|
| return pil_img |
|
|
|
|
| def add_scale_label(pil_img, pixel_sizes, slice_dim): |
| """Add scale label to image.""" |
| draw = ImageDraw.Draw(pil_img) |
|
|
| |
| img_width, img_height = pil_img.size |
|
|
| |
| class ScaleCalculator: |
| def _get_appropriate_scale(self, pixel_size, img_size, init_scale=10): |
| scales = [1, 2, 5, 10, 15, 20, 25, 30, 40, 50, 60, 70, 80, 90, 100] |
| scale_pixels_num = int(init_scale / pixel_size) |
| min_pixels = img_size * 0.05 |
| max_pixels = img_size * 0.25 |
|
|
| if scale_pixels_num < min_pixels: |
| for scale in scales: |
| if scale > init_scale: |
| return self._get_appropriate_scale(pixel_size, img_size, scale) |
| elif scale_pixels_num > max_pixels: |
| for scale in reversed(scales): |
| if scale < init_scale: |
| return self._get_appropriate_scale(pixel_size, img_size, scale) |
|
|
| return init_scale, scale_pixels_num |
|
|
| scale_calculator = ScaleCalculator() |
|
|
| |
| |
| |
| |
| if img_height < img_width: |
| pixel_size_min = pixel_sizes[0] |
| image_dim_min = img_height |
| else: |
| pixel_size_min = pixel_sizes[1] |
| image_dim_min = img_width |
|
|
| |
| scale_mm, scale_pixels_min = scale_calculator._get_appropriate_scale( |
| pixel_size_min, image_dim_min, init_scale=10 |
| ) |
|
|
| |
| if img_height < img_width: |
| scale_pixels_height = scale_pixels_min |
| scale_pixels_width = int(scale_mm / pixel_sizes[1]) |
| else: |
| scale_pixels_width = scale_pixels_min |
| scale_pixels_height = int(scale_mm / pixel_sizes[0]) |
|
|
| |
| start_x, start_y = int(img_width * 0.05), int(img_height * 0.05) |
| end_x, end_y = start_x + scale_pixels_width, start_y + scale_pixels_height |
|
|
| |
| default_line_width = 2 |
| default_fontsize = 10 |
| line_width = default_line_width |
| font = ImageFont.load_default().font_variant(size=default_fontsize) |
|
|
| |
| draw.line( |
| [(start_x, start_y), (end_x, start_y)], |
| fill=(255, 255, 255), |
| width=line_width, |
| ) |
| |
| draw.line( |
| [(start_x, start_y), (start_x, end_y)], |
| fill=(255, 255, 255), |
| width=line_width, |
| ) |
| |
| draw.text( |
| (start_x + 5, start_y + 5), f"{scale_mm} mm", fill=(255, 255, 255), font=font |
| ) |
|
|
| return pil_img |
|
|
|
|
| def add_scale_label_v2(pil_img, pixel_sizes, slice_dim): |
| """Add scale label to image.""" |
| draw = ImageDraw.Draw(pil_img) |
|
|
| |
| img_width, img_height = pil_img.size |
|
|
| |
| class ScaleCalculator: |
| def _get_appropriate_scale(self, pixel_size, img_size, init_scale=10): |
| scales = [1, 2, 5, 10, 15, 20, 25, 30, 40, 50, 60, 70, 80, 90, 100] |
| scale_pixels_num = int(init_scale / pixel_size) |
| min_pixels = img_size * 0.05 |
| max_pixels = img_size * 0.25 |
|
|
| if scale_pixels_num < min_pixels: |
| for scale in scales: |
| if scale > init_scale: |
| return self._get_appropriate_scale(pixel_size, img_size, scale) |
| elif scale_pixels_num > max_pixels: |
| for scale in reversed(scales): |
| if scale < init_scale: |
| return self._get_appropriate_scale(pixel_size, img_size, scale) |
|
|
| return init_scale, scale_pixels_num |
|
|
| scale_calculator = ScaleCalculator() |
|
|
| |
| |
| |
| |
| if img_height < img_width: |
| pixel_size_min = pixel_sizes[0] |
| image_dim_min = img_height |
| else: |
| pixel_size_min = pixel_sizes[1] |
| image_dim_min = img_width |
|
|
| |
| scale_mm, scale_pixels_min = scale_calculator._get_appropriate_scale( |
| pixel_size_min, image_dim_min, init_scale=10 |
| ) |
|
|
| |
| if img_height < img_width: |
| scale_pixels_height = scale_pixels_min |
| scale_pixels_width = int(scale_mm / pixel_sizes[1]) |
| else: |
| scale_pixels_width = scale_pixels_min |
| scale_pixels_height = int(scale_mm / pixel_sizes[0]) |
|
|
| |
| start_x, start_y = int(img_width * 0.05), int(img_height * 0.05) |
| end_x, end_y = start_x + scale_pixels_width, start_y + scale_pixels_height |
|
|
| |
| default_line_width = 2 |
| default_fontsize = 10 |
| line_width = default_line_width |
| font = ImageFont.load_default().font_variant(size=default_fontsize) |
|
|
| |
| draw.line( |
| [(start_x, start_y), (end_x, start_y)], |
| fill=(255, 255, 255), |
| width=line_width, |
| ) |
| |
| draw.line( |
| [(start_x, start_y), (start_x, end_y)], |
| fill=(255, 255, 255), |
| width=line_width, |
| ) |
| |
| draw.text( |
| (start_x + 5, start_y + 5), f"{scale_mm} mm", fill=(255, 255, 255), font=font |
| ) |
|
|
| return pil_img, scale_mm |
|
|
|
|
| def add_scale_label_autoGreen(pil_img, pixel_sizes, slice_dim): |
| """Add scale label to image.""" |
| draw = ImageDraw.Draw(pil_img) |
|
|
| |
| img_width, img_height = pil_img.size |
|
|
| |
| class ScaleCalculator: |
| def _get_appropriate_scale(self, pixel_size, img_size, init_scale=10): |
| scales = [1, 2, 5, 10, 15, 20, 25, 30, 40, 50, 60, 70, 80, 90, 100] |
| scale_pixels_num = int(init_scale / pixel_size) |
| min_pixels = img_size * 0.05 |
| max_pixels = img_size * 0.25 |
|
|
| if scale_pixels_num < min_pixels: |
| for scale in scales: |
| if scale > init_scale: |
| return self._get_appropriate_scale(pixel_size, img_size, scale) |
| elif scale_pixels_num > max_pixels: |
| for scale in reversed(scales): |
| if scale < init_scale: |
| return self._get_appropriate_scale(pixel_size, img_size, scale) |
|
|
| return init_scale, scale_pixels_num |
|
|
| scale_calculator = ScaleCalculator() |
|
|
| |
| |
| |
| |
| if img_height < img_width: |
| pixel_size_min = pixel_sizes[0] |
| image_dim_min = img_height |
| else: |
| pixel_size_min = pixel_sizes[1] |
| image_dim_min = img_width |
|
|
| |
| scale_mm, scale_pixels_min = scale_calculator._get_appropriate_scale( |
| pixel_size_min, image_dim_min, init_scale=10 |
| ) |
|
|
| |
| if img_height < img_width: |
| scale_pixels_height = scale_pixels_min |
| scale_pixels_width = int(scale_mm / pixel_sizes[1]) |
| else: |
| scale_pixels_width = scale_pixels_min |
| scale_pixels_height = int(scale_mm / pixel_sizes[0]) |
|
|
| |
| start_x, start_y = int(img_width * 0.05), int(img_height * 0.05) |
| end_x, end_y = start_x + scale_pixels_width, start_y + scale_pixels_height |
|
|
| |
| default_line_width = 2 |
| default_fontsize = 10 |
| line_width = default_line_width |
| font = ImageFont.load_default().font_variant(size=default_fontsize) |
| text = f"{scale_mm} mm" |
| _, tmp_text_height = _get_text_dimensions(draw, text, font) |
| if tmp_text_height < img_height * 0.05: |
| fontsize = int(default_fontsize * (img_height * 0.05) / tmp_text_height) |
| font = ImageFont.load_default().font_variant(size=fontsize) |
| if (img_height * 0.05) / tmp_text_height > 5: |
| line_width = int( |
| default_line_width * (img_height * 0.05) / tmp_text_height / 5 |
| ) |
|
|
| |
| draw.text((start_x + 5, start_y + 5), text, fill=(0, 255, 0), font=font) |
| |
| draw.line( |
| [(start_x, start_y), (end_x, start_y)], |
| fill=(0, 255, 0), |
| width=line_width, |
| ) |
| |
| draw.line( |
| [(start_x, start_y), (start_x, end_y)], |
| fill=(0, 255, 0), |
| width=line_width, |
| ) |
|
|
| return pil_img |
|
|
|
|
| def add_scale_and_orientation_label(pil_img, pixel_sizes, slice_dim): |
| """Add scale bar and orientation labels to image.""" |
| draw = ImageDraw.Draw(pil_img) |
|
|
| |
| img_width, img_height = pil_img.size |
|
|
| |
| class ScaleCalculator: |
| def _get_appropriate_scale(self, pixel_size, img_size, init_scale=10): |
| scales = [1, 2, 5, 10, 15, 20, 25, 30, 40, 50, 60, 70, 80, 90, 100] |
| scale_pixels_num = int(init_scale / pixel_size) |
| min_pixels = img_size * 0.05 |
| max_pixels = img_size * 0.25 |
|
|
| if scale_pixels_num < min_pixels: |
| for scale in scales: |
| if scale > init_scale: |
| return self._get_appropriate_scale(pixel_size, img_size, scale) |
| elif scale_pixels_num > max_pixels: |
| for scale in reversed(scales): |
| if scale < init_scale: |
| return self._get_appropriate_scale(pixel_size, img_size, scale) |
|
|
| return init_scale, scale_pixels_num |
|
|
| scale_calculator = ScaleCalculator() |
|
|
| |
| |
| |
| |
| if img_height < img_width: |
| pixel_size_min = pixel_sizes[0] |
| image_dim_min = img_height |
| else: |
| pixel_size_min = pixel_sizes[1] |
| image_dim_min = img_width |
|
|
| |
| scale_mm, scale_pixels_min = scale_calculator._get_appropriate_scale( |
| pixel_size_min, image_dim_min, init_scale=10 |
| ) |
|
|
| |
| if img_height < img_width: |
| scale_pixels_height = scale_pixels_min |
| scale_pixels_width = int(scale_mm / pixel_sizes[1]) |
| else: |
| scale_pixels_width = scale_pixels_min |
| scale_pixels_height = int(scale_mm / pixel_sizes[0]) |
|
|
| |
| start_x, start_y = int(img_width * 0.05), int(img_height * 0.05) |
| end_x, end_y = start_x + scale_pixels_width, start_y + scale_pixels_height |
|
|
| |
| default_line_width = 2 |
| default_fontsize = 10 |
| line_width = default_line_width |
| font = ImageFont.load_default().font_variant(size=default_fontsize) |
|
|
| |
| draw.line( |
| [(start_x, start_y), (end_x, start_y)], |
| fill=(255, 255, 255), |
| width=line_width, |
| ) |
| |
| draw.line( |
| [(start_x, start_y), (start_x, end_y)], |
| fill=(255, 255, 255), |
| width=line_width, |
| ) |
| |
| draw.text( |
| (start_x + 5, start_y + 5), f"{scale_mm} mm", fill=(255, 255, 255), font=font |
| ) |
|
|
| |
| label_padding = 10 |
| if slice_dim == 0: |
| draw.text((start_x, end_y + 5), "Anterior", fill=(255, 255, 255), font=font) |
| draw.text((end_x + 5, start_y), "Superior", fill=(255, 255, 255), font=font) |
| elif slice_dim == 1: |
| draw.text((start_x, end_y + 5), "Right", fill=(255, 255, 255), font=font) |
| draw.text((end_x + 5, start_y), "Superior", fill=(255, 255, 255), font=font) |
| else: |
| draw.text((start_x, end_y + 5), "Right", fill=(255, 255, 255), font=font) |
| draw.text((end_x + 5, start_y), "Anterior", fill=(255, 255, 255), font=font) |
|
|
| return pil_img |
|
|