{"cells":[{"cell_type":"code","execution_count":null,"metadata":{"_cell_guid":"c4aa1a1c-b515-ab02-6170-6dace90a5b3d"},"outputs":[],"source":"import numpy as np\nimport pandas as pd\nimport os\nimport cv2\nimport matplotlib.pyplot as plt\nimport skimage.feature\nfrom sklearn.model_selection import train_test_split\nfrom sklearn.preprocessing import LabelBinarizer\n%matplotlib inline\n"},{"cell_type":"code","execution_count":null,"metadata":{"_cell_guid":"d6840436-932d-2fd2-5a42-b48e23ed7133"},"outputs":[],"source":"file_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:20]"},{"cell_type":"code","execution_count":null,"metadata":{"_cell_guid":"59c9d80e-2b5c-95e2-37d7-61d6730ac42a"},"outputs":[],"source":"# dataframe to store coordinate results in\nclasses = [\"adult_males\", \"subadult_males\", \"adult_females\", \"juveniles\", \"pups\"]\ncoordinates_df = pd.DataFrame(index=file_names, columns=classes)"},{"cell_type":"code","execution_count":null,"metadata":{"_cell_guid":"79b0513e-341c-180d-b593-d59150371cf8"},"outputs":[],"source":"file_names[2]"},{"cell_type":"code","execution_count":null,"metadata":{"_cell_guid":"fb54feb1-1161-3f03-0b8a-c298aa757ab2"},"outputs":[],"source":"test_image = cv2.imread(\"../input/TrainDotted/\" + file_names[2])\ntest_image_2 = cv2.imread(\"../input/Train/\" + file_names[2])\nimage_3 = cv2.absdiff(test_image,test_image_2)\nplt.imshow(test_image, interpolation='none')\n"},{"cell_type":"code","execution_count":null,"metadata":{"_cell_guid":"c6fb8da7-dac9-f298-5854-3f1ac4b48a99"},"outputs":[],"source":"plt.imshow(test_image_2[2500:2600, 800:900,:], interpolation='none')"},{"cell_type":"code","execution_count":null,"metadata":{"_cell_guid":"f68137f5-149f-59b2-7e32-d513a5f7659c"},"outputs":[],"source":"for filename in {file_names[2]}:\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_3 = cv2.bitwise_or(image_3, image_3, mask=mask_1)\n    image_3 = cv2.bitwise_or(image_3, image_3, mask=mask_2) \n    \n    # convert to grayscale to be accepted by skimage.feature.blob_log\n    image_3 = cv2.cvtColor(image_3, cv2.COLOR_BGR2GRAY)\n    \n    # detect blobs\n    blobs = skimage.feature.blob_log(image_3, min_sigma=3, max_sigma=4, num_sigma=1, threshold=0.02)\n    \n    adult_males = []\n    subadult_males = []\n    pups = []\n    juveniles = []\n    adult_females = [] \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        g,b,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 g < 50 and b < 50: # RED\n            adult_males.append((int(x),int(y)))        \n        elif r > 200 and g > 200 and b < 50: # MAGENTA\n            subadult_males.append((int(x),int(y)))         \n        elif r < 100 and g < 100 and 150 < b < 200: # GREEN\n            pups.append((int(x),int(y)))\n        elif r < 100 and  100 < g and b < 100: # BLUE\n            juveniles.append((int(x),int(y))) \n        elif r < 150 and g < 50 and b < 100:  # BROWN\n            adult_females.append((int(x),int(y)))\n            \n    coordinates_df[\"adult_males\"][filename] = adult_males\n    coordinates_df[\"subadult_males\"][filename] = subadult_males\n    coordinates_df[\"adult_females\"][filename] = adult_females\n    coordinates_df[\"juveniles\"][filename] = juveniles\n    coordinates_df[\"pups\"][filename] = pups"},{"cell_type":"code","execution_count":null,"metadata":{"_cell_guid":"e7cc76ac-75bb-affa-56cb-19787b6b23ae"},"outputs":[],"source":"\ncoordinates_df"},{"cell_type":"code","execution_count":null,"metadata":{"_cell_guid":"83e358b3-01e9-eb84-3418-dffd2cc9ead8"},"outputs":[],"source":"adult_males"},{"cell_type":"code","execution_count":null,"metadata":{"_cell_guid":"a456670e-571d-5987-43bd-198f55ced218"},"outputs":[],"source":""}],"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}