{
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {
        "_cell_guid": "7777d9a4-3741-606b-af50-12cadc4c5717"
      },
      "source": [
        "Using pattern matching to find the dotted sea lions. With the current settings it finds most of the dots, but not all of them."
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "_cell_guid": "9b8cbb31-b7a2-929c-3ff5-e0bd808f28f0"
      },
      "source": [
        "Filter out the colored pixels. Then finding circles."
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "1140cf35-22c3-43fa-8de9-f5c4fcd7b121"
      },
      "outputs": [],
      "source": [
        "import numpy as np\n",
        "import cv2\n",
        "import matplotlib.pyplot as plt\n",
        "\n",
        "# Filter ranges for each dot color.\n",
        "redRange = [np.array([160, 0, 0]), np.array([255, 50, 50])]\n",
        "magnetaRange = [np.array([160, 0, 160]), np.array([255, 50, 255])]\n",
        "brownRange = [np.array([76, 39, 5]), np.array([94, 53, 22])]\n",
        "blueRange = [np.array([0, 0, 160]), np.array([56, 56, 255])]\n",
        "greenRange = [np.array([0, 160, 0]), np.array([56, 255, 56])]\n",
        "colorRanges = [redRange, magnetaRange, brownRange, blueRange, greenRange ]\n",
        "\n",
        "red = (255, 0, 0)\n",
        "magneta = (255, 0, 255)\n",
        "brown = (78, 42, 8)\n",
        "blue = (0, 0, 255)\n",
        "green = (0, 255, 0)\n",
        "colors = [red, magneta, brown, blue, green]\n",
        "\n",
        "img = cv2.cvtColor(cv2.imread(\"../input/TrainDotted/1.jpg\"), cv2.COLOR_BGR2RGB)\n",
        "imgPaint = img.copy()"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "50e7b13a-af58-9e2e-edab-cec89fa5875e"
      },
      "outputs": [],
      "source": [
        "counts = np.zeros(5)\n",
        "for color in range(0,5):\n",
        "    cmsk = cv2.inRange(img, colorRanges[color][0], colorRanges[color][1])\n",
        "    circles = cv2.HoughCircles(cmsk,cv2.HOUGH_GRADIENT,1,50, param1=40,param2=1,minRadius=2,maxRadius=6)\n",
        "\n",
        "    if circles is not None:\n",
        "        circles = np.uint16(np.around(circles))\n",
        "        counts[color] = len(circles[0,:])\n",
        "        for i in circles[0,:]:\n",
        "            cv2.rectangle(imgPaint, (i[0] - 50, i[1] - 50), (i[0] + 50, i[1] + 50), colors[color], 3)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "5a725813-303e-aeb6-44fb-1656ed362b2e"
      },
      "outputs": [],
      "source": [
        "print(counts)\n",
        "plt.imshow(imgPaint, cmap = 'gray', interpolation = 'bicubic')"
      ]
    }
  ],
  "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
}