histomicstk.annotations_and_masks.annotation_and_mask_utils

Created on Sun Aug 11 22:30:06 2019.

@author: tageldim

histomicstk.annotations_and_masks.annotation_and_mask_utils.create_mask_from_coords(coords)[source]

Create a binary mask from given vertices coordinates.

Source: This is modified from code by Juan Carlos from David Gutman Lab.

Parameters

coords (np arrray) – must be in the form (e.g. ([x1,y1],[x2,y2],[x3,y3],…..,[xn,yn])), where xn and yn corresponds to the nth vertix coordinate.

Returns

binary mask

Return type

np array

histomicstk.annotations_and_masks.annotation_and_mask_utils.delete_annotations_in_slide(gc, slide_id)[source]

Delete all annotations in a slide.

histomicstk.annotations_and_masks.annotation_and_mask_utils.get_bboxes_from_slide_annotations(slide_annotations)[source]

Given a slide annotation list, gets information on bounding boxes.

Parameters

slide_annotations (list of dicts) – response from server request

Returns

The columns annidx and elementidx encode the dict index of annotation document and element, respectively, in the original slide_annotations list of dictionaries

Return type

Pandas DataFrame

histomicstk.annotations_and_masks.annotation_and_mask_utils.get_idxs_for_annots_overlapping_roi_by_bbox(element_infos, idx_for_roi, iou_thresh=0.0)[source]

Find indices of potentially included annoations within the ROI.

We say “potentially” because this uses the IoU of roi and annotation as a fast indicator of potential inclusion. This helps dramatically scale down the number of annotations to look through. Later on, a detailed look at whether the annotation polygons actually overlap the ROI can be done.

Parameters
  • element_infos (pandas DataFrame) – result from running get_bboxes_from_slide_annotations()

  • idx_for_roi (int) – index for roi annotation within the element_infos DF

  • iou_thresh (float) – overlap threshold to be considered within ROI

Returns

indices relative to element_infos

Return type

list

histomicstk.annotations_and_masks.annotation_and_mask_utils.get_image_from_htk_response(resp)[source]

Given a histomicsTK girder response, get np array image.

Parameters

resp (object) – response from server request

Returns

a pillow Image object of the image

Return type

Pillow Image object

histomicstk.annotations_and_masks.annotation_and_mask_utils.get_rotated_rectangular_coords(roi_center, roi_width, roi_height, roi_rotation=0)[source]

Given data on rectangular ROI center/width/height/rotation.

Get the unrotated abounding box coordinates around rotated ROI. This of course is applicable to any rotated rectangular annotation.

Parameters
Returns

includes roi corners (x, y) and bounds

Return type

dict

histomicstk.annotations_and_masks.annotation_and_mask_utils.get_scale_factor_and_appendStr(gc, slide_id, MPP=None, MAG=None)[source]

Get how much is request region smaller than base.

This also gets the string to append to server request for getting rgb.

Parameters
  • gc (Girder client instance) – gc should be authoenticated.

  • slide_id (str) – girder id of slide

  • MPP (float or None) – Microns-per-pixel – best use this as it’s more well-defined than magnification which is more scanner/manufacturer specific. MPP of 0.25 often roughly translates to 40x

  • MAG (float or None) – If you prefer to use whatever magnification is reported in slide. If neither MPP or MAG is provided, everything is retrieved without scaling at base (scan) magnification.

Returns

  • float – how much smaller (0.1 means 10x smaller) is requested image compared to scan magnification (slide coordinates)

  • str – string to appnd to server request for getting slide region

histomicstk.annotations_and_masks.annotation_and_mask_utils.np_vec_no_jit_iou(bboxes1, bboxes2)[source]

Fast, vectorized IoU.

Source: https://medium.com/@venuktan/vectorized-intersection-over-union

-iou-in-numpy-and-tensor-flow-4fa16231b63d

Parameters
  • bboxes1 (np array) – columns encode bounding box corners xmin, ymin, xmax, ymax

  • bboxes2 (np array) – same as bboxes 1

Returns

IoU values for each pair from bboxes1 & bboxes2

Return type

np array

histomicstk.annotations_and_masks.annotation_and_mask_utils.parse_slide_annotations_into_tables(slide_annotations, cropping_bounds=None, cropping_polygon_vertices=None, use_shapely=False)[source]

Given a slide annotation list, parse into convenient tabular format.

If the annotation is a point, then it is just treated as if it is a rectangle with zero area (i.e. xmin=xmax). Rotated rectangles are treated as polygons for simplicity.

Parameters
  • slide_annotations (list of dicts) – response from server request

  • cropping_bounds (dict or None) – if given, must have keys XMIN, XMAX, YMIN, YMAX. These are the bounds to which the polygons may be cropped using shapely, if the param use_shapely is True. Otherwise, the polygon coordinates are just shifted relative to these bounds without actually cropping.

  • cropping_polygon_vertices (nd array or None) – if given, is an (m, 2) nd array of vertices to crop bounds. if the param use_shapely is True. Otherwise, the polygon coordinates are just shifted relative to these bounds without actually cropping.

  • use_shapely (bool) – see cropping_bounds description.

Returns

  • Pandas DataFrame – Summary of key properties of the annotation documents. It has the following columns: - annotation_girder_id - _modelType - _version - itemId - created - creatorId - public - updated - updatedId - groups - element_count - element_details

  • Pandas DataFrame – The individual annotation elements (polygons, points, rectangles). The columns annidx and elementidx encode the dict index of annotation document and element, respectively, in the original slide_annotations list of dictionaries. It has the following columns:

    • annidx

    • annotation_girder_id

    • elementidx

    • element_girder_id

    • type

    • group

    • label

    • color

    • xmin

    • xmax

    • ymin

    • ymax

    • bbox_area

    • coords_x

    • coords_y

histomicstk.annotations_and_masks.annotation_and_mask_utils.rotate_point_list(point_list, rotation, center=(0, 0))[source]

Rotate a certain point list around a central point. Modified from.

javascript version at: https://github.com/girder/large_image/blob/master/ … web_client/annotations/rotate.js.

Parameters
  • point_list (list of tuples) – (x,y) coordinates

  • rotation (float) – degrees (in radians)

  • center (tuple of ints) – central point coordinates

Returns

Return type

list of tuples

histomicstk.annotations_and_masks.annotation_and_mask_utils.scale_slide_annotations(slide_annotations, sf)[source]

Scales up or down annotations in a slide.

Works in place, but returns slide_annotations anyways.

Parameters
  • slide_annotations (list of dicts) – response from server request

  • sf (float) – scale factor to multiply coordinates

Returns

Return type

list of dicts