def gen_output(im, result, info, win_show,  cour_list1, cour_list2, recist_list1, recist_list2):
    im = windowing_rev(im, cfg.INPUT.WINDOWING)
    im = windowing(im, win_show).astype('uint8')
    im = cv2.cvtColor(im, cv2.COLOR_GRAY2RGB)

    scale = cfg.TEST.VISUALIZE.SHOW_SCALE
    im = cv2.resize(im, None, None, fx=scale, fy=scale, interpolation=cv2.INTER_LINEAR)

    pred = result.bbox.cpu().numpy()
    labels = result.get_field('labels').cpu().numpy()
    scores = result.get_field('scores').cpu().numpy()
    tag_scores = result.get_field('tag_scores').cpu().numpy()
    tag_predictions = result.get_field('tag_predictions').cpu().numpy()

    mm2pix = info['im_scale'] / info['spacing'] * scale
    contours = result.get_field('contour_mm').cpu().numpy() * mm2pix
    contours = [c[c[:, 0] > 0, :] for c in contours]
    contours = [c+1*scale for c in contours]  # there seems to be a small offset in the mask?
    recists = result.get_field('recist_mm').cpu().numpy() * mm2pix
    recists += 1*scale   # there seems to be a small offset in the mask?
    diameters = result.get_field('diameter_mm').cpu().numpy()

    pred *= scale
    #contour_list = contour_list
    #recist_list = recist_list
    overlay, msgs = draw_results(im, pred,   cour_list1, cour_list2, recist_list1, recist_list2, labels, scores, tag_predictions=tag_predictions, tag_scores=tag_scores,
                                 contours=contours, recists=recists, diameters=diameters)
    overlay = print_msg_on_img(overlay, msgs)
    return overlay, msgs
Beispiel #2
0
def vis_all_detection(im, detections, class_names, scale):
    """
    visualize all detections in one image
    :param im_array: [b=1 c h w] in rgb
    :param detections: [ numpy.ndarray([[x1 y1 x2 y2 score]]) for j in classes ]
    :param class_names: list of names in imdb
    :param scale: visualize the scaled image
    :return:
    """
    im = windowing_rev(im + cfg.INPUT.PIXEL_MEAN, cfg.INPUT.WINDOWING)
    im = windowing(im, [-175, 275]).astype('uint8')
    plt.imshow(im, cmap='gray')
    for j, name in enumerate(class_names):
        if name == '__background__':
            continue
        elif name == 'gt':
            color = (0, 1, 0)
        else:
            color = (1, 1, 0)
        # color = (random.random(), random.random(), random.random())  # generate a random color
        dets = detections[j]
        for det in dets:
            bbox = det[:4] * scale
            rect = plt.Rectangle((bbox[0], bbox[1]),
                                 bbox[2] - bbox[0],
                                 bbox[3] - bbox[1],
                                 fill=False,
                                 edgecolor=color,
                                 linewidth=2)
            plt.gca().add_patch(rect)
            if det.shape[0] > 4:
                score = det[-1]
                plt.gca().text(
                    bbox[0],
                    bbox[1] - 2,
                    '{:s} {:.3f}'.format(name, score),
                    # bbox=dict(facecolor=color, alpha=0.5),
                    fontsize=12,
                    color=color)
    plt.show()
Beispiel #3
0
def visualize(im, target, result, info, masker):
    """Visualize gt and predicted box, tag, mask, and RECIST"""
    print('\n lesion_idxs', info['lesion_idxs'], ':', info['image_fn'])
    if 'tags' in info.keys():
        print('gt tags:')
        for tags in target.get_field('tags'):
            idx = torch.nonzero(tags > 0)
            msg = ', '.join([cfg.runtime_info.tag_list[idx1] for idx1 in idx])
            print(msg)
    if 'diameters' in info.keys():
        print('gt diameters:')
        print(info['diameters'])

    im = windowing_rev(im + cfg.INPUT.PIXEL_MEAN, cfg.INPUT.WINDOWING)
    im = windowing(im, info['window']).astype('uint8')
    im = cv2.cvtColor(im, cv2.COLOR_GRAY2RGB)

    gt = target.bbox.cpu().numpy()
    gt_recists = info['recists']

    scale = cfg.TEST.VISUALIZE.SHOW_SCALE
    im = cv2.resize(im,
                    None,
                    None,
                    fx=scale,
                    fy=scale,
                    interpolation=cv2.INTER_LINEAR)
    gt *= scale
    gt_recists *= scale
    overlay, msgs = draw_results(im,
                                 gt,
                                 recists=gt_recists,
                                 colors=(0, 255, 0),
                                 thickness=(2, 1, 1))

    pred = result.bbox.cpu().numpy()
    labels = result.get_field('labels').cpu().numpy()
    scores = result.get_field('scores').cpu().numpy()
    tag_scores = result.get_field('tag_scores').cpu().numpy()
    tag_predictions = result.get_field('tag_predictions').cpu().numpy()

    # if cfg.MODEL.MASK_ON and len(result.get_field('contour_mm')) > 0:
    mm2pix = info['im_scale'] / info['spacing'] * scale
    contours = result.get_field('contour_mm').cpu().numpy() * mm2pix
    contours = [c[c[:, 0] > 0, :] for c in contours]
    contours = [c + 1 * scale for c in contours
                ]  # there seems to be a small offset in the mask?
    recists = result.get_field('recist_mm').cpu().numpy() * mm2pix
    recists += 1 * scale  # there seems to be a small offset in the mask?
    diameters = result.get_field('diameter_mm').cpu().numpy()

    pred *= scale
    overlay, msgs = draw_results(overlay,
                                 pred,
                                 labels,
                                 scores,
                                 tag_predictions=tag_predictions,
                                 tag_scores=tag_scores,
                                 contours=contours,
                                 recists=recists,
                                 diameters=diameters)
    plt.figure(1)
    plt.imshow(overlay)
    for msg in msgs:
        print(msg)

    if cfg.TEST.VISUALIZE.SHOW_MASK_HEATMAPS and 'mask' in result.extra_fields.keys(
    ):
        plt.figure(2)
        mask = result.get_field('mask')
        mask = masker([mask], [result])[0]
        mask = mask.numpy()
        mask0 = cv2.resize(mask[0, 0], None, None, fx=scale, fy=scale)
        mask1 = np.empty(
            (mask.shape[0], mask.shape[1], mask0.shape[0], mask0.shape[1]),
            dtype=mask.dtype)
        for i1 in range(mask.shape[0]):
            for i2 in range(mask.shape[1]):
                mask1[i1, i2] = cv2.resize(mask[i1, i2],
                                           None,
                                           None,
                                           fx=scale,
                                           fy=scale,
                                           interpolation=cv2.INTER_NEAREST)

        pred *= scale
        heatmap = np.sum(mask1, (0, 1)).astype('uint8')
        # heatmap = cv2.applyColorMap(heatmap, cv2.COLORMAP_JET)
        plt.imshow(heatmap, cmap='gray')
    plt.show()