{
  "cells": [
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "d027b608-c2b8-d49a-e771-a2e3e2b8aaf3"
      },
      "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 copy\n",
        "import cv2\n",
        "import matplotlib.pyplot as plt\n",
        "import os\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": "624a4191-b5ac-020a-fee1-8158dcde0f36"
      },
      "outputs": [],
      "source": [
        "DATA_PATH = \"../input\"\n",
        "TRAIN_PATH = os.path.join(DATA_PATH, 'Train')\n",
        "\n",
        "for i in range(6):\n",
        "    img = cv2.imread(TRAIN_PATH+'/'+str(i)+'.jpg')\n",
        "    \n",
        "\n",
        "    img = cv2.resize(img, (512,512))\n",
        "    img2 = copy.deepcopy(img)\n",
        "    img_hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)\n",
        "    h,s,v = cv2.split(img_hsv)\n",
        "\n",
        "    lower_green = np.array([0,125,0])\n",
        "    upper_green = np.array([255,200,255])\n",
        "    img4 = cv2.inRange(img2,lower_green,upper_green)\n",
        "\n",
        "    no_seals = np.where(v < 150)\n",
        "    img2[no_seals] = 0\n",
        "\n",
        "    others = np.where(s < 40)\n",
        "\n",
        "    img2[others] = 0\n",
        "    \n",
        "    img3 = copy.deepcopy(img2)\n",
        "    lower_blue = np.array([102,127,150])\n",
        "    upper_blue = np.array([163,185,201])\n",
        "    img3 = cv2.inRange(img3,lower_blue,upper_blue)\n",
        "\n",
        "    blue_values = np.where(img3==0)\n",
        "    img2[blue_values] = 0\n",
        "    \n",
        "# ----------------------------------------------------------------------------#\n",
        "    \n",
        "    img = cv2.cvtColor(img,cv2.COLOR_BGR2RGB)\n",
        "    img3 = copy.deepcopy(img)\n",
        "\n",
        "    img4 = copy.deepcopy(img2)\n",
        "    img4[img4 > 0]=255\n",
        "\n",
        "    img4 = cv2.cvtColor(img4, cv2.COLOR_BGR2GRAY)\n",
        "    kernel = np.ones((3, 3), np.uint8)\n",
        "    closing = cv2.dilate(img4,kernel,iterations=2)\n",
        "\n",
        "\n",
        "    _,contours, _= cv2.findContours(closing,cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE)\n",
        "    for cnt in contours:\n",
        "        area = cv2.contourArea(cnt)\n",
        "        perimeter = cv2.arcLength(cnt,True)\n",
        "        x,y,w,h = cv2.boundingRect(cnt)\n",
        "        cv2.rectangle(img3,(x,y),(x+w,y+h),(0,255,0),2)\n",
        "\n",
        "    img2 = cv2.cvtColor(img2, cv2.COLOR_BGR2RGB)\n",
        "    #res = np.hstack((img,img2)) \n",
        "    \n",
        "    f, ax = plt.subplots(1, 2, figsize=(12,8))\n",
        "    (ax1, ax2) = ax.flatten()\n",
        "\n",
        "    ax1.imshow(img)\n",
        "    ax1.set_title(\"Original\")\n",
        "    ax2.imshow(img2)\n",
        "    ax2.set_title(\"Segmented\")\n",
        "    plt.show()\n",
        "     \n",
        "    f, ax = plt.subplots(figsize=(8,8))   \n",
        "    ax.imshow(img3)\n",
        "    ax.set_title(\"Proposed\")\n",
        "    plt.show()"
      ]
    }
  ],
  "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
}