{
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {
        "_cell_guid": "153be8ea-1748-9156-57d5-dd41b554ecfc"
      },
      "source": [
        "## The segmentation is based on @chattob's reference to \"Greenspan et al., Automatic detection of anatomical landmarks in uterine cervix images. IEEE transaction on medical imaging, 2009.\" in his/her kernel.\n",
        "\n",
        "# Loading images (vfdev's kernel)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "5f00e019-2117-028a-ceb9-7e42db54d402"
      },
      "outputs": [],
      "source": [
        "# This Python 3 environment comes with many helpful analytics libraries installed\n",
        "# It is defined by the kaggle/python docker image: https://github.com/kaggle/docker-python\n",
        "# For example, here's several helpful packages to load in \n",
        "\n",
        "import numpy as np # linear algebra\n",
        "import pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n",
        "import cv2\n",
        "\n",
        "#additional imports\n",
        "import matplotlib.pylab as plt\n",
        "import math\n",
        "from sklearn import mixture\n",
        "from sklearn.utils import shuffle\n",
        "\n",
        "# Input data files are available in the \"../input/\" directory.\n",
        "# For example, running this (by clicking run or pressing Shift+Enter) will list the files in the input directory\n",
        "\n",
        "from subprocess import check_output\n",
        "print(check_output([\"ls\", \"../input\"]).decode(\"utf8\"))\n",
        "\n",
        "# Any results you write to the current directory are saved as output."
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "e3f5aec9-d1d8-c1d8-098d-ccc602d57fbf"
      },
      "outputs": [],
      "source": [
        "import os\n",
        "from glob import glob\n",
        "TRAIN_DATA = \"../input/train\"\n",
        "type_1_files = glob(os.path.join(TRAIN_DATA, \"Type_1\", \"*.jpg\"))\n",
        "type_1_ids = np.array([s[len(os.path.join(TRAIN_DATA, \"Type_1\"))+1:-4] for s in type_1_files])\n",
        "type_2_files = glob(os.path.join(TRAIN_DATA, \"Type_2\", \"*.jpg\"))\n",
        "type_2_ids = np.array([s[len(os.path.join(TRAIN_DATA, \"Type_2\"))+1:-4] for s in type_2_files])\n",
        "type_3_files = glob(os.path.join(TRAIN_DATA, \"Type_3\", \"*.jpg\"))\n",
        "type_3_ids = np.array([s[len(os.path.join(TRAIN_DATA, \"Type_3\"))+1:-4] for s in type_3_files])\n",
        "\n",
        "print(len(type_1_files), len(type_2_files), len(type_3_files))\n",
        "print(\"Type 1\", type_1_ids[:10])\n",
        "print(\"Type 2\", type_2_ids[:10])\n",
        "print(\"Type 3\", type_3_ids[:10])"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "bfd761da-c8b6-eb05-14c4-8e7625554200"
      },
      "outputs": [],
      "source": [
        "TEST_DATA = \"../input/test\"\n",
        "test_files = glob(os.path.join(TEST_DATA, \"*.jpg\"))\n",
        "test_ids = np.array([s[len(TEST_DATA)+1:-4] for s in test_files])\n",
        "print(len(test_ids))\n",
        "print(test_ids[:10])"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "9eb06c2e-cd06-6343-22ef-841d02345a43"
      },
      "outputs": [],
      "source": [
        "ADDITIONAL_DATA = \"../input/additional\"\n",
        "additional_type_1_files = glob(os.path.join(ADDITIONAL_DATA, \"Type_1\", \"*.jpg\"))\n",
        "additional_type_1_ids = np.array([s[len(os.path.join(ADDITIONAL_DATA, \"Type_1\"))+1:-4] for s in additional_type_1_files])\n",
        "additional_type_2_files = glob(os.path.join(ADDITIONAL_DATA, \"Type_2\", \"*.jpg\"))\n",
        "additional_type_2_ids = np.array([s[len(os.path.join(ADDITIONAL_DATA, \"Type_2\"))+1:-4] for s in additional_type_2_files])\n",
        "additional_type_3_files = glob(os.path.join(ADDITIONAL_DATA, \"Type_3\", \"*.jpg\"))\n",
        "additional_type_3_ids = np.array([s[len(os.path.join(ADDITIONAL_DATA, \"Type_3\"))+1:-4] for s in additional_type_3_files])\n",
        "\n",
        "print(len(additional_type_1_files), len(additional_type_2_files), len(additional_type_2_files))\n",
        "print(\"Type 1\", additional_type_1_ids[:10])\n",
        "print(\"Type 2\", additional_type_2_ids[:10])\n",
        "print(\"Type 3\", additional_type_3_ids[:10])"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "17652ecd-2a34-178d-3f25-c769cb72e565"
      },
      "outputs": [],
      "source": [
        "def get_filename(image_id, image_type):\n",
        "    \"\"\"\n",
        "    Method to get image file path from its id and type   \n",
        "    \"\"\"\n",
        "    if image_type == \"Type_1\" or \\\n",
        "        image_type == \"Type_2\" or \\\n",
        "        image_type == \"Type_3\":\n",
        "        data_path = os.path.join(TRAIN_DATA, image_type)\n",
        "    elif image_type == \"Test\":\n",
        "        data_path = TEST_DATA\n",
        "    elif image_type == \"AType_1\" or \\\n",
        "          image_type == \"AType_2\" or \\\n",
        "          image_type == \"AType_3\":\n",
        "        data_path = os.path.join(ADDITIONAL_DATA, image_type[1:])\n",
        "    else:\n",
        "        raise Exception(\"Image type '%s' is not recognized\" % image_type)\n",
        "\n",
        "    ext = 'jpg'\n",
        "    return os.path.join(data_path, \"{}.{}\".format(image_id, ext))\n",
        "\n",
        "\n",
        "def get_image_data(image_id, image_type):\n",
        "    \"\"\"\n",
        "    Method to get image data as np.array specifying image id and type\n",
        "    \"\"\"\n",
        "    fname = get_filename(image_id, image_type)\n",
        "    img = cv2.imread(fname)\n",
        "    assert img is not None, \"Failed to read image : %s, %s\" % (image_id, image_type)\n",
        "    img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)\n",
        "    return img"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "_cell_guid": "e4ed312b-9380-0ae4-4c1e-db58212ac74d"
      },
      "source": [
        "## Creating an R, a array\n",
        "\n",
        "# based on @chattob's \"cervix segmentation GMM\" kernel"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "21e644e9-a82b-d47d-fed0-2f201ae49718"
      },
      "outputs": [],
      "source": [
        "# defining the color of pixels outside of the roi in RGB\n",
        "mask_color = [0, 0, 0]"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "de340980-bda6-8321-c1ad-8b40ab2b821e"
      },
      "outputs": [],
      "source": [
        "def Ra_space(img, Ra_ratio, a_threshold):\n",
        "    imgLab = cv2.cvtColor(img, cv2.COLOR_RGB2LAB);\n",
        "    w = img.shape[0]\n",
        "    h = img.shape[1]\n",
        "    Ra = np.zeros((w*h, 2))\n",
        "    for i in range(w):\n",
        "        for j in range(h):\n",
        "            R = math.sqrt((w/2-i)*(w/2-i) + (h/2-j)*(h/2-j))\n",
        "            Ra[i*h+j, 0] = R\n",
        "            Ra[i*h+j, 1] = min(imgLab[i][j][1], a_threshold)\n",
        "            \n",
        "    Ra[:,0] /= max(Ra[:,0])\n",
        "    Ra[:,0] *= Ra_ratio\n",
        "    Ra[:,1] /= max(Ra[:,1])\n",
        "\n",
        "    return Ra"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "_cell_guid": "8156cc97-5c22-a50d-1157-7d0f2ad65316"
      },
      "source": [
        "### Clustering pixels to 2 groups"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "1b5a7b84-f739-cc4c-ceab-49c81a73bba1"
      },
      "outputs": [],
      "source": [
        "# sample images\n",
        "img_1 = get_image_data('7', 'Type_1')\n",
        "img_2 = get_image_data('35', 'Type_1')\n",
        "img_3 = get_image_data('77', 'Type_2')\n",
        "img_4 = get_image_data('178', 'Type_2')\n",
        "img_5 = get_image_data('11', 'Type_3')\n",
        "img_6 = get_image_data('212', 'Type_3')\n",
        "\n",
        "test_1 = get_image_data('81', 'Type_1')\n",
        "test_2 = get_image_data('31', 'Type_2')\n",
        "test_3 = get_image_data('62', 'Type_3')\n",
        "\n",
        "sample_images = [img_1, img_2, img_3, img_4, img_5, img_6]\n",
        "sample_labels = ['Type_1', 'Type_1', 'Type_2', 'Type_2', 'Type_3', 'Type_3']\n",
        "sample_test_images = [test_1, test_2, test_3]\n",
        "sample_test_labels = ['Type_1', 'Type_2', 'Type_3']\n",
        "\n",
        "#image reduction for computational speed\n",
        "tile_size = (256, 256)\n",
        "\n",
        "small_sample_images = []\n",
        "small_sample_test_images = []\n",
        "\n",
        "for image in sample_images:\n",
        "    small_sample_images.append(cv2.resize(image, dsize=tile_size))\n",
        "    \n",
        "for image in sample_test_images:\n",
        "    small_sample_test_images.append(cv2.resize(image, dsize=tile_size))   "
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "_cell_guid": "65386f99-7288-0848-5bb3-fb41497650e5"
      },
      "source": [
        "SVM experimentation"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "e4970dad-e6aa-378c-cc5b-dae5265eb1ea"
      },
      "outputs": [],
      "source": [
        "def crop_roi(image):\n",
        "    \n",
        "    # a channel saturation threshold\n",
        "    a_threshold = 300\n",
        "    \n",
        "    # creating the R-a feature for the image\n",
        "    Ra_array = Ra_space(image, 1.0, a_threshold)\n",
        "    \n",
        "    # k-means gaussian mixture model\n",
        "    g = mixture.GaussianMixture(n_components = 2, covariance_type = 'diag', random_state = 0, init_params = 'kmeans')\n",
        "    image_array_sample = shuffle(Ra_array, random_state=0)[:1000]\n",
        "    g.fit(image_array_sample)\n",
        "    labels = g.predict(Ra_array)\n",
        "    \n",
        "    # creating the mask array and assign the correct cluster label\n",
        "    boolean_image_mask = np.array(labels).reshape(image.shape[0], image.shape[1])\n",
        "    outer_cluster_label = boolean_image_mask[0,0]\n",
        "    \n",
        "    new_image = image.copy()\n",
        "    \n",
        "    for i in range(boolean_image_mask.shape[0]):\n",
        "        for j in range(boolean_image_mask.shape[1]):\n",
        "            if boolean_image_mask[i, j] == outer_cluster_label:\n",
        "                new_image[i, j] = mask_color\n",
        "    \n",
        "    plt.figure(figsize=(10,10))\n",
        "    \n",
        "    plt.subplot(221)\n",
        "    plt.title(\"Original image\")    \n",
        "    plt.imshow(image)\n",
        "    \n",
        "    plt.subplot(222)\n",
        "    plt.title(\"Region of interest\")\n",
        "    plt.imshow(new_image)\n",
        "    \n",
        "    a_channel = np.reshape(Ra_array[:,1], (image.shape[0], image.shape[1]))\n",
        "    plt.subplot(223)\n",
        "    plt.title(\"a channel\")\n",
        "    plt.imshow(a_channel)\n",
        "  \n",
        "    plt.subplot(224)\n",
        "    plt.title(\"Gaussiam mixture scatter plot\")    \n",
        "    plt.scatter(Ra_array[:,0], Ra_array[:,1], c=boolean_image_mask)\n",
        "    plt.show()\n",
        "    \n",
        "    return new_image"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "b1f2924e-3ac7-cc11-3cbe-b1e554d373ca"
      },
      "outputs": [],
      "source": [
        "def mean_redness(image):\n",
        "    total = 0\n",
        "    count = 0\n",
        "    \n",
        "    for i in range(image.shape[0]):\n",
        "        for j in range(image.shape[1]):\n",
        "            #print(sum(image[i,j]))\n",
        "            #print(sum(mask_color))\n",
        "            if sum(image[i, j]) != sum(mask_color):\n",
        "                total += image[i, j, 0]\n",
        "                count += 1\n",
        "    mean_red = total / count\n",
        "    \n",
        "    return mean_red"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "14297b86-2aab-831c-1bbe-6e603810ffdb"
      },
      "outputs": [],
      "source": [
        "initial_roi_images = []\n",
        "\n",
        "for image in small_sample_images:\n",
        "    initial_roi_images.append(crop_roi(image)) \n",
        "\n",
        "mean_red_function = []\n",
        "\n",
        "for image in initial_roi_images:\n",
        "    mean_red_function.append(mean_redness(image)) "
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "ecd43acb-6d1d-c664-295f-236930f0b519"
      },
      "outputs": [],
      "source": [
        "initial_roi_test_images = []\n",
        "test_mean_red_function = []\n",
        "\n",
        "for image in small_sample_test_images:\n",
        "    initial_roi_test_images.append(crop_roi(image)) \n",
        "    \n",
        "for image in initial_roi_test_images:\n",
        "    test_mean_red_function.append(mean_redness(image)) "
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "88d17549-2c2f-2879-8dc4-80124cdaa5ed"
      },
      "outputs": [],
      "source": [
        "from sklearn import svm\n",
        "\n",
        "clf = svm.SVC()\n",
        "clf.fit(np.array(mean_red_function).reshape(-1,1), sample_labels)\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "9751c8ee-716d-890e-c36b-c66190857b6c"
      },
      "outputs": [],
      "source": [
        "print(clf.predict(np.array(test_mean_red_function[0]).reshape(-1,1)))\n",
        "print(clf.predict(np.array(test_mean_red_function[1]).reshape(-1,1)))\n",
        "print(clf.predict(np.array(test_mean_red_function[2]).reshape(-1,1)))\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "_cell_guid": "f67fa570-c057-1d63-7188-45b2bb6d4d39"
      },
      "source": [
        "Plotting the data"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "_cell_guid": "d2674a68-d113-112e-ef8d-db2fdcee05cb"
      },
      "source": [
        "to be continued"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "b3d15129-668f-408b-6ba0-3e3d0c33044e"
      },
      "outputs": [],
      "source": [
        ""
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "264d5aa1-9bc7-8548-9b67-b7eb6cd37328"
      },
      "outputs": [],
      "source": [
        ""
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "2f15350e-43e2-1588-0c65-e03ebc201f17"
      },
      "outputs": [],
      "source": [
        ""
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "83b0880c-6a99-e727-aeb9-a36c1e9dc13b"
      },
      "outputs": [],
      "source": [
        ""
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "dc3047f5-659f-3337-d1d4-0d4deb4ac84f"
      },
      "outputs": [],
      "source": [
        ""
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "b75df00e-e736-3d59-b05f-ab34c5c9871f"
      },
      "outputs": [],
      "source": [
        ""
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "e6e51e1b-1904-b8ba-097a-26623881d891"
      },
      "outputs": [],
      "source": [
        ""
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "f3f1a7b9-baf9-a524-b816-5eccf7ba55ec"
      },
      "outputs": [],
      "source": [
        ""
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "6050bb6b-0b24-3b29-bda9-dbde75e71e90"
      },
      "outputs": [],
      "source": [
        ""
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "f3c41d9f-5b0e-cfae-2ad7-1f7a9e8c8afa"
      },
      "outputs": [],
      "source": [
        ""
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "64991bbd-5c10-22ff-d037-b3d513b20ca5"
      },
      "outputs": [],
      "source": [
        ""
      ]
    }
  ],
  "metadata": {
    "_change_revision": 0,
    "_is_fork": false,
    "kernelspec": {
      "display_name": "Python 3",
      "language": "python",
      "name": "python3"
    },
    "language_info": {
      "codemirror_mode": {
        "name": "ipython",
        "version": 3
      },
      "file_extension": ".py",
      "mimetype": "text/x-python",
      "name": "python",
      "nbconvert_exporter": "python",
      "pygments_lexer": "ipython3",
      "version": "3.6.0"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 0
}