{"cells": [{"outputs": [], "source": ["### Get dot coordinates using blob_log from skimage library"], "cell_type": "markdown", "execution_count": null, "metadata": {"_uuid": "41fb7a5b643bd2a170728357994455902bb837c6", "_cell_guid": "7daa5a37-ea6b-2274-4bc1-fadb0a2fc41a"}}, {"outputs": [], "source": ["import numpy as np\n", "import pandas as pd\n", "import os\n", "import cv2\n", "import matplotlib.pyplot as plt\n", "import skimage.feature\n", "%matplotlib inline"], "cell_type": "code", "execution_count": null, "metadata": {"_uuid": "384295b32b7e3933a1335f14d6c95a18355befe0", "_cell_guid": "35cabbd8-8e7b-5be7-bd00-61b1addc2d75", "trusted": false}}, {"outputs": [], "source": "classes = [\"adult_males\", \"subadult_males\", \"adult_females\", \"juveniles\", \"pups\", \"error\"]\n\nfile_names = os.listdir(\"../input/Train/\")\nfile_names = sorted(file_names, key=lambda \n                    item: (int(item.partition('.')[0]) if item[0].isdigit() else float('inf'), item)) \n\n# select a subset of files to run on\nfile_names = file_names[0:2]\n\n# dataframe to store results in\ncount_df = pd.DataFrame(index=file_names, columns=classes).fillna(0)", "cell_type": "code", "execution_count": null, "metadata": {"_uuid": "eb544278d2c226a56ac705ad1e94da9ba8c1c881", "_cell_guid": "3a6329c7-aa3b-b576-b6e1-d675e6e8fbf2", "trusted": false}}, {"outputs": [], "source": "for filename in file_names:\n    \n    # read the Train and Train Dotted images\n    image_1 = cv2.imread(\"../input/TrainDotted/\" + filename)\n    image_2 = cv2.imread(\"../input/Train/\" + filename)\n    \n    # absolute difference between Train and Train Dotted\n    image_3 = cv2.absdiff(image_1,image_2)\n    \n    # mask out blackened regions from Train Dotted\n    mask_1 = cv2.cvtColor(image_1, cv2.COLOR_BGR2GRAY)\n    mask_1[mask_1 < 20] = 0\n    mask_1[mask_1 > 0] = 255\n    \n    mask_2 = cv2.cvtColor(image_2, cv2.COLOR_BGR2GRAY)\n    mask_2[mask_2 < 20] = 0\n    mask_2[mask_2 > 0] = 255\n    \n    image_4 = cv2.bitwise_or(image_3, image_3, mask=mask_1)\n    image_5 = cv2.bitwise_or(image_4, image_4, mask=mask_2) \n    \n    # convert to grayscale to be accepted by skimage.feature.blob_log\n    image_6 = cv2.cvtColor(image_5, cv2.COLOR_BGR2GRAY)\n    \n    # detect blobs\n    blobs = skimage.feature.blob_log(image_6, min_sigma=3, max_sigma=4, num_sigma=1, threshold=0.02)\n    \n    # prepare the image to plot the results on\n    image_7 = cv2.cvtColor(image_6, cv2.COLOR_GRAY2BGR)\n    \n    for blob in blobs:\n        # get the coordinates for each blob\n        y, x, s = blob\n        # get the color of the pixel from Train Dotted in the center of the blob\n        b,g,r = image_1[int(y)][int(x)][:]\n        \n        # decision tree to pick the class of the blob by looking at the color in Train Dotted\n        if r > 200 and b < 50 and g < 50: # RED\n            count_df[\"adult_males\"][filename] += 1\n            cv2.circle(image_7, (int(x),int(y)), 8, (0,0,255), 2)            \n        elif r > 200 and b > 200 and g < 50: # MAGENTA\n            count_df[\"subadult_males\"][filename] += 1\n            cv2.circle(image_7, (int(x),int(y)), 8, (250,10,250), 2)            \n        elif r < 100 and b < 100 and 150 < g < 200: # GREEN\n            count_df[\"pups\"][filename] += 1\n            cv2.circle(image_7, (int(x),int(y)), 8, (20,180,35), 2) \n        elif r < 100 and  100 < b and g < 100: # BLUE\n            count_df[\"juveniles\"][filename] += 1 \n            cv2.circle(image_7, (int(x),int(y)), 8, (180,60,30), 2)\n        elif r < 150 and b < 50 and g < 100:  # BROWN\n            count_df[\"adult_females\"][filename] += 1\n            cv2.circle(image_7, (int(x),int(y)), 8, (0,42,84), 2)            \n        else:\n            count_df[\"error\"][filename] += 1\n            cv2.circle(image_7, (int(x),int(y)), 8, (255,255,155), 2)\n    \n    # output the results\n          \n    f, ax = plt.subplots(3,2,figsize=(10,16))\n    (ax1, ax2, ax3, ax4, ax5, ax6) = ax.flatten()\n    plt.title('%s'%filename)\n    \n    ax1.imshow(cv2.cvtColor(image_2[700:1200,2130:2639,:], cv2.COLOR_BGR2RGB))\n    ax1.set_title('Train')\n    ax2.imshow(cv2.cvtColor(image_1[700:1200,2130:2639,:], cv2.COLOR_BGR2RGB))\n    ax2.set_title('Train Dotted')\n    ax3.imshow(cv2.cvtColor(image_3[700:1200,2130:2639,:], cv2.COLOR_BGR2RGB))\n    ax3.set_title('Train Dotted - Train')\n    ax4.imshow(cv2.cvtColor(image_5[700:1200,2130:2639,:], cv2.COLOR_BGR2RGB))\n    ax4.set_title('Mask blackened areas of Train Dotted')\n    ax5.imshow(image_6[700:1200,2130:2639], cmap='gray')\n    ax5.set_title('Grayscale for input to blob_log')\n    ax6.imshow(cv2.cvtColor(image_7[700:1200,2130:2639,:], cv2.COLOR_BGR2RGB))\n    ax6.set_title('Result')\n\n    plt.show()", "cell_type": "code", "execution_count": null, "metadata": {"_uuid": "24cfb69c5e6ff93523b48a529964ebd19f4d2957", "_cell_guid": "7755c681-04df-368a-aca2-f099dd9ce805", "trusted": false}}, {"outputs": [], "source": "### Check count results", "cell_type": "markdown", "execution_count": null, "metadata": {"_uuid": "2183fb71ba45c4cb3df611d7168ac45532175218", "_cell_guid": "d03424c1-b12b-ae53-2fed-1dff86398164"}}, {"outputs": [], "source": "count_df", "cell_type": "code", "execution_count": null, "metadata": {"_uuid": "5556e8bcfce748ddf4528282a9eb2dd04e5e50fa", "_cell_guid": "c9549615-3a64-2ef2-2be0-1946e07c2ee2", "trusted": false}}, {"outputs": [], "source": "### Reference counts", "cell_type": "markdown", "execution_count": null, "metadata": {"_uuid": "edf75301df8e0ccbeeaaf9a5666aeea5e96ad1d0", "_cell_guid": "36b150cc-ffbb-ebb5-049f-58c48e5bde00"}}, {"outputs": [], "source": "reference = pd.read_csv('../input/Train/train.csv')\nreference.ix[0:1]", "cell_type": "code", "execution_count": null, "metadata": {"_uuid": "a86a298b03a14e1e68ad0005e7f8d824b454302c", "_cell_guid": "cf4ecf01-de99-b59e-d1d6-067e8f4478fe", "trusted": false}}], "nbformat": 4, "nbformat_minor": 0, "metadata": {"_change_revision": 0, "language_info": {"file_extension": ".py", "version": "3.6.1", "codemirror_mode": {"version": 3, "name": "ipython"}, "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "mimetype": "text/x-python", "name": "python"}, "_is_fork": false, "kernelspec": {"language": "python", "display_name": "Python 3", "name": "python3"}}}