{
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {
        "_cell_guid": "745cd9fd-1aef-3302-8012-70d479bbfba8"
      },
      "source": [
        "By now we have realized that the images and dotted images have a fair number of discrepancies.. Here's a simple script for approximating counts of dots of each color in dotted images."
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "4158f402-991b-837a-d7a8-e953291d1c84"
      },
      "outputs": [],
      "source": [
        "import pandas as pd\n",
        "import numpy as np\n",
        "import cv2\n",
        "import matplotlib.pyplot as plt"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "_cell_guid": "1615ba12-74db-e314-b7a2-0930e377779a"
      },
      "source": [
        "I manually sampled some RGB ranges for each color of the dots.."
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "b157f0a5-8454-2a22-29fb-d7364a604cc2"
      },
      "outputs": [],
      "source": [
        "#Define RGB ranges\n",
        "col_min = {\"red\": np.array([160, 0, 0]),\n",
        "           \"magenta\": np.array([200, 0, 200]),\n",
        "           \"brown\": np.array([75, 40, 10]),\n",
        "           \"blue\": np.array([0, 40, 150]),\n",
        "           \"green\": np.array([40, 140, 25])\n",
        "           }\n",
        "\n",
        "col_max = {\"red\": np.array([255, 50, 50]),\n",
        "           \"magenta\": np.array([255, 55, 255]),\n",
        "           \"brown\": np.array([130, 55, 20]),\n",
        "           \"blue\": np.array([40, 80, 255]),\n",
        "           \"green\": np.array([65, 255, 50])\n",
        "           }\n",
        "\n",
        "col_avg = {\"red\": 28,\n",
        "           \"magenta\": 20,\n",
        "           \"brown\": 12,\n",
        "           \"blue\": 13,\n",
        "           \"green\": 15\n",
        "           }\n",
        "\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "1a008334-1727-da71-1a4b-7b337b0d5e0c"
      },
      "outputs": [],
      "source": [
        "#Function to count pixels in RGB range\n",
        "def _count_dots(img, colors = ['red', 'magenta', 'brown', 'blue', 'green']):\n",
        "    cnt = []\n",
        "    for col in colors:\n",
        "        cmsk = cv2.inRange(img, col_min[col], col_max[col])\n",
        "        num = int(np.sum(cmsk > 0) / col_avg[col])\n",
        "        cnt.append(num)\n",
        "        print(col + ': ' + str(num))\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "778b2ffa-1536-1ca2-cc22-7a2006fb1485"
      },
      "outputs": [],
      "source": [
        "#Test with image 0\n",
        "id = 0\n",
        "img = cv2.cvtColor(cv2.imread(\"../input/TrainDotted/\" + str(id) + \".jpg\"), cv2.COLOR_BGR2RGB)\n",
        "\n",
        "#First crop\n",
        "img_crop = img[1200:1800, 2200:2800]\n",
        "plt.imshow(img_crop)\n",
        "_count_dots(img_crop)\n",
        "\n",
        "print(\"actual counts should be 4, 1, 10, 1, 10\")"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "7ba22bce-a589-5fe3-758d-4a844b148855"
      },
      "outputs": [],
      "source": [
        "#Another crop\n",
        "img_crop = img[1800:2400, 2800:3400]\n",
        "plt.imshow(img_crop)\n",
        "_count_dots(img_crop)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "4064f66e-7cac-7390-b955-1fad388f6a64"
      },
      "outputs": [],
      "source": [
        "#Full image and compare to actual in train.csv\n",
        "train = pd.read_csv(\"../input/Train/train.csv\")\n",
        "print(train.loc[id,:])\n",
        "_count_dots(img)"
      ]
    }
  ],
  "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
}