def image_with_bbox(image, bbox, bbox_ids=None, colors=None, thickness=1, width=None, level=None): rgb_image = med2rgb(image, width=width, level=level) if bbox_ids is not None and len(bbox_ids) != len(bbox): raise ValueError('bbox_ids should have same length as bbox') bbox_ids = bbox_ids or list(range(len(bbox))) colors = colors or generate_rand_color(n_color=np.max(bbox_ids)+1) for i, (x, y, w, h) in enumerate(bbox): rgb_image = cv2.rectangle(rgb_image, pt1=(x, y), pt2=(x+w, y+h), color=colors[bbox_ids[i]], thickness=thickness) return rgb_image
def image_with_heatmap(image, heatmap, alpha=0.8, beta=0.3, colormap=cv2.COLORMAP_JET, width=None, level=None): rgb_image = med2rgb(image, width=width, level=level) uint8_heatmap = np.uint8((heatmap - np.min(heatmap)) / (np.max(heatmap) - np.min(heatmap) + 1e-5) * 255) color_heatmap = cv2.applyColorMap(src=uint8_heatmap, colormap=colormap) return cv2.addWeighted(src1=rgb_image, alpha=alpha, src2=color_heatmap, beta=beta, gamma=0)
def image_with_mask(image, mask, alpha=0.5, colors=None, width=None, level=None): rgb_image = med2rgb(image, width=width, level=level) mask = np.squeeze(mask) if len(np.shape(mask)) != 2: raise ValueError('mask image should have rank 2, but received input shape: %s' % len(np.shape(mask))) mask_unique = np.unique(mask) mask_labels = mask_unique[np.nonzero(mask_unique)] if np.size(mask_labels) < 1: raise ValueError('mask must have nonzero label') colors = colors or generate_rand_color(n_color=np.size(mask_labels)) for i, label in enumerate(mask_labels): for c in range(3): rgb_image[:, :, c] = np.where(mask == label, rgb_image[:, :, c] * (1 - alpha) + colors[i][c] * alpha, rgb_image[:, :, c]) return rgb_image
def image_with_contours(image, mask, colors=None, thickness=1, width=None, level=None): rgb_image = med2rgb(image, width=width, level=level) mask = np.squeeze(mask) if len(np.shape(mask)) != 2: raise ValueError('mask image should have rank 2, but received input shape: %s' % len(np.shape(mask))) mask_unique = np.unique(mask) mask_labels = mask_unique[np.nonzero(mask_unique)] if np.size(mask_labels) < 1: raise ValueError('mask must have nonzero label') colors = colors or generate_rand_color(n_color=np.size(mask_labels)) for i, label in enumerate(mask_labels): zero_mask = np.zeros_like(mask, dtype=np.uint8) zero_mask[mask == label] = 1 if is_cv3_version: _, contours, _ = cv2.findContours(zero_mask, mode=cv2.RETR_TREE, method=cv2.CHAIN_APPROX_NONE) rgb_image = cv2.drawContours(rgb_image, contours, contourIdx=-1, color=colors[i], thickness=thickness) return rgb_image
# !/usr/bin/env python # -*- coding:utf-8 -*- import numpy as np import matplotlib.pyplot as plt from medcv.tools.transform import med2rgb from medcv.data import chest_dcm, chest_heatmap from medcv.visualize.cls import image_with_heatmap # load chest dcm image and its heatmap chest_dcm_image = chest_dcm() chest_heatmap_image = chest_heatmap() # dcm to rgb image with image window width and level chest_rgb_image = med2rgb(chest_dcm_image, width=22135, level=12209) # dcm with heatmap chest_image_with_heatmap = image_with_heatmap(chest_dcm_image, chest_heatmap_image, width=22135, level=12209) # plot visualize plt.figure(figsize=(15, 5)) plt.subplot(1, 4, 1) plt.title('origin image') plt.imshow(np.squeeze(chest_dcm_image)) plt.subplot(1, 4, 2) plt.title('rgb image') plt.imshow(chest_rgb_image) plt.subplot(1, 4, 3)
# !/usr/bin/env python # -*- coding:utf-8 -*- import matplotlib.pyplot as plt from medcv.data import chest_dcm from medcv.tools.transform import med2rgb # load chest dcm image chest_dcm_image = chest_dcm() # width=50000, level=20000 rgb_image1 = med2rgb(chest_dcm_image, width=50000, level=20000) # width=20000, level=15000 rgb_image2 = med2rgb(chest_dcm_image, width=20000, level=15000) # width=20000, level=15000, invert=True rgb_image3 = med2rgb(chest_dcm_image, width=20000, level=15000, invert=True) # plot visualize plt.figure(figsize=(15, 5)) plt.subplot(1, 4, 1) plt.title('chest origin image') plt.imshow(med2rgb(chest_dcm_image)) plt.subplot(1, 4, 2) plt.title('width=70000,level=20000') plt.imshow(rgb_image1) plt.subplot(1, 4, 3) plt.title('width=20000,level=15000') plt.imshow(rgb_image2) plt.subplot(1, 4, 4)