{
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {
        "_cell_guid": "f2465081-db3f-96f9-6f42-53c44f06372e"
      },
      "source": [
        "This script crops all images based on the dots"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "27e294e6-7873-ebfe-9ef5-d594097a6d6f"
      },
      "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",
        "\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": "5c78b773-4cfb-7308-b4a5-311fb4b84a4b"
      },
      "outputs": [],
      "source": [
        "x = check_output([\"ls\", \"../input/TrainDotted\"]).decode(\"utf8\").split(\"\\n\")\n",
        "np.shape(x)[0]\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "e7011e9b-40aa-d742-5ba2-50df217e6022"
      },
      "outputs": [],
      "source": [
        "import numpy as np\n",
        "import cv2\n",
        "import matplotlib.pyplot as plt\n",
        "\n",
        "img = cv2.cvtColor(cv2.imread(\"../input/TrainDotted/10.jpg\"), cv2.COLOR_BGR2RGB)\n",
        "plt.imshow(img)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "74ee6dc3-8617-373f-0a32-07d3a2416aaf"
      },
      "outputs": [],
      "source": [
        "img2 = cv2.resize(img, (300, 400))\n",
        "plt.imshow(img2)\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "8620be65-a30f-d64a-98ab-b76cfa60a679"
      },
      "outputs": [],
      "source": [
        "# Create search color masks for given circle colors\n",
        "red = cv2.inRange(img, np.array([160, 0, 0]), np.array([255, 50, 50]))\n",
        "magenta = cv2.inRange(img, np.array([128, 0, 128]), np.array([255, 0, 255]))\n",
        "brown = cv2.inRange(img, np.array([139, 69, 16]), np.array([222,184,135]))\n",
        "blue = cv2.inRange(img, np.array([0, 0, 128]), np.array([50, 50, 255]))\n",
        "green = cv2.inRange(img, np.array([0, 128, 0]), np.array([50, 255, 50]))\n",
        "\n",
        "colors = [red, magenta, brown, blue, green]"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "45c97e51-a5ad-8101-22f6-87083c055b90"
      },
      "outputs": [],
      "source": [
        "coord = np.zeros((2, np.shape(circles)[1])) # Array containing seal boundaries for image cropping\n",
        "resizer = np.zeros((1,4))\n",
        "cropCoordinates = np.zeros((1,4))\n",
        "margin = 150 # makes sure border seals are not deleted\n",
        "\n",
        "# Get images\n",
        "imgList = check_output([\"ls\", \"../input/TrainDotted\"]).decode(\"utf8\").split(\"\\n\")\n",
        "\n",
        "# Loop over images and crop them\n",
        "for k in range(0, np.shape(imgList)[0]):\n",
        "    \n",
        "    img = cv2.cvtColor(cv2.imread(imgList[k]), cv2.COLOR_BGR2RGB)\n",
        "    \n",
        "    # Loop over color masks to find seal boundaries\n",
        "    for j in range(0, np.shape(circles)[0]):\n",
        "        cmsk = colors[j]\n",
        "        circles = cv2.HoughCircles(cmsk,cv2.HOUGH_GRADIENT,1,50, \n",
        "                                   param1=40,param2=1,minRadius=0,maxRadius=25)\n",
        "        for i in range(0, np.shape(circles)[1]):\n",
        "            coord[0,i] = circles[0][i][0]\n",
        "            coord[1,i] = circles[0][i][1]\n",
        "\n",
        "        # Find limits in x and y for ONE color\n",
        "        ymin_color = int(round(np.min(coord[1,:])))\n",
        "        ymax_color = int(round(np.max(coord[1,:])))\n",
        "        xmin_color = int(round(np.min(coord[0,:])))\n",
        "        xmax_color = int(round(np.max(coord[0,:]))) \n",
        "        \n",
        "    if ymin_color \n",
        "    croppedImage = img[ymin - margin:ymax + margin,\n",
        "                      xmin - margin:xmax + margin]\n",
        "        \n",
        "    # Find largest and smallest pictures for resizing\n",
        "    yDiff = ymax-ymin\n",
        "    xDiff = xmax-xmin\n",
        "\n",
        "    # Give largest and smallest height and width of cropped image\n",
        "    if yDiff > resizer[0][0]:\n",
        "        resizer[0][0] = yDiff\n",
        "    if yDiff < resizer[0][1]:\n",
        "        resizer[0][1] = yDiff\n",
        "    if xDiff > resizer[0][2]:\n",
        "        resizer[0][2] = xDiff\n",
        "    if xDiff > resizer[0][3]:\n",
        "        resizer[0][3] = xDiff\n",
        "    \n",
        "    \n",
        "    \n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "a83ca6e5-7dee-c27e-d4d7-fc48a1203df7"
      },
      "outputs": [],
      "source": [
        "cropCoordinates = img[int(round(np.min(coord[1,:]))) - margin:\n",
        "           int(round(np.max(coord[1,:]))) + margin, \n",
        "           int(round(np.min(coord[0,:]))) - margin:\n",
        "           int(round(np.max(coord[0,:]))) + margin]"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "fbfcccb7-4079-93ad-126d-154c751e91db"
      },
      "outputs": [],
      "source": [
        "plt.imshow(crop)"
      ]
    }
  ],
  "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
}