{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Color thresholding semantic segmentation\n", "\n", "Whole-slide images often contain artifacts like marker or acellular regions that\n", "need to be avoided during analysis. In this example we show how HistomicsTK can\n", "be used to develop saliency detection algorithms that segment the slide at low\n", "magnification to generate a map to guide higher magnification analyses. Here we\n", "show how how colorspace analysis can detect various elements such as inking\n", "or blood, as well as dense cellular regions, to improve the quality of\n", "subsequent image analysis tasks.\n", "\n", "This uses a thresholding and stain unmixing based pipeline to detect\n", "highly-cellular regions in a slide. The `run()` method of the\n", "`CDT_single_tissue_piece()` class has the key steps of the pipeline.\n", "\n", "Additional functionality includes contour extraction to get the final segmentation boundaries and to visualize them in DSA using one's preferred styles.\n", "\n", "**Here are some sample results:**\n", "\n", "![saliency_results](https://user-images.githubusercontent.com/22067552/80079317-1bcaa580-851e-11ea-9353-a435a2afc6eb.jpg)\n", "\n", "**Where to look?**\n", "\n", "```\n", " |_ histomicstk/\n", " |_saliency/\n", " |_cellularity_detection_thresholding.py \n", " |_tests/\n", " |_test_saliency.py\n", "```" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "import tempfile\n", "import girder_client\n", "import numpy as np\n", "from pandas import read_csv\n", "from histomicstk.annotations_and_masks.annotation_and_mask_utils import (\n", " delete_annotations_in_slide)\n", "from histomicstk.saliency.cellularity_detection_thresholding import (\n", " Cellularity_detector_thresholding)\n", "\n", "import matplotlib.pylab as plt\n", "from matplotlib.colors import ListedColormap\n", "%matplotlib inline" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Prepwork" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "APIURL = 'http://candygram.neurology.emory.edu:8080/api/v1/'\n", "SAMPLE_SLIDE_ID = '5d8c296cbd4404c6b1fa5572'\n", "\n", "gc = girder_client.GirderClient(apiUrl=APIURL)\n", "gc.authenticate(apiKey='kri19nTIGOkWH01TbzRqfohaaDWb6kPecRqGmemb')\n", "\n", "# This is where the run logs will be saved\n", "logging_savepath = tempfile.mkdtemp()\n", "\n", "# read GT codes dataframe\n", "GTcodes = read_csv('../../histomicstk/saliency/tests/saliency_GTcodes.csv')" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [], "source": [ "# deleting existing annotations in target slide (if any)\n", "delete_annotations_in_slide(gc, SAMPLE_SLIDE_ID)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Let's explore the GTcodes dataframe" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " | group | \n", "overlay_order | \n", "GT_code | \n", "is_roi | \n", "is_background_class | \n", "color | \n", "comments | \n", "
---|---|---|---|---|---|---|---|
0 | \n", "outside_tissue | \n", "-1 | \n", "255 | \n", "0 | \n", "0 | \n", "rgb(40,40,40) | \n", "NaN | \n", "
1 | \n", "roi | \n", "0 | \n", "254 | \n", "0 | \n", "0 | \n", "rgb(0,0,0) | \n", "NaN | \n", "
2 | \n", "not_specified | \n", "0 | \n", "253 | \n", "0 | \n", "1 | \n", "rgb(255,50,255) | \n", "NaN | \n", "
3 | \n", "blue_sharpie | \n", "1 | \n", "6 | \n", "0 | \n", "0 | \n", "rgb(0,224,255) | \n", "NaN | \n", "
4 | \n", "blood | \n", "2 | \n", "7 | \n", "0 | \n", "0 | \n", "rgb(255,255,0) | \n", "NaN | \n", "
5 | \n", "whitespace | \n", "3 | \n", "8 | \n", "0 | \n", "0 | \n", "rgb(70,70,70) | \n", "NaN | \n", "
6 | \n", "maybe_cellular | \n", "4 | \n", "9 | \n", "0 | \n", "0 | \n", "rgb(145,109,189) | \n", "NaN | \n", "
7 | \n", "top_cellular | \n", "5 | \n", "10 | \n", "0 | \n", "0 | \n", "rgb(50,250,20) | \n", "NaN | \n", "