{"cells":[{"cell_type":"markdown","metadata":{"_cell_guid":"db62dbae-1266-baa7-aed8-d5bda56e90c9"},"source":"#**My 1st notebook for playing around with NOAA competition**\n\n* red: adult males\n* magenta: subadult males\n* brown: adult females\n* blue: juveniles\n* green: pups\n\nReferences :\n[Kevin Mader][1], \n[Philipp Schmidt][2] \n\n\n  [1]: https://www.kaggle.com/kmader/noaa-fisheries-steller-sea-lion-population-count/qbi-2017-single-object-analysis\n  [2]: https://www.kaggle.com/philschmidt/noaa-fisheries-steller-sea-lion-population-count/counting-sea-lions/run/1044523"},{"cell_type":"code","execution_count":null,"metadata":{"_cell_guid":"a7f96d34-5e94-a0c2-f1c6-7c86c96e8088"},"outputs":[],"source":"import numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\nfrom subprocess import check_output\nimport matplotlib.pyplot as plt\nfrom glob import glob\nimport seaborn as sns\nsns.set_style(\"whitegrid\", {'axes.grid': False})\nfrom scipy import stats\nfrom skimage.io import imread, imshow\nfrom skimage.util import crop\nimport os\nimport cv2\nfrom collections import namedtuple"},{"cell_type":"code","execution_count":null,"metadata":{"_cell_guid":"ef08fd57-5a1c-713d-a802-8210b31cf676"},"outputs":[],"source":"def get_dots_from_image(cropped_dotted, cropped_raw):\n    \"\"\"\n    # Get the markers only\n    There are also brown markers which are removed by our thresholding \n    and are also not very present in the difference image itself.\n    \"\"\"\n    y_max, x_max, _ = cropped_dotted.shape\n    diff = cv2.subtract(cropped_dotted, cropped_raw)\n    diff = diff/diff.max()\n    diff = cv2.absdiff(cropped_dotted, cropped_raw)\n    gray = cv2.cvtColor(diff, cv2.COLOR_RGB2GRAY)\n    ret,th1 = cv2.threshold(gray,0,255,cv2.THRESH_BINARY | cv2.THRESH_OTSU)\n    cnts = cv2.findContours(th1.copy(), cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE)[-2]\n    x, y = [], []\n    for loc in cnts:\n        x.append(loc[0][0][0])\n        y.append(loc[0][0][1])\n    x = np.array(x)\n    y = np.array(y)\n    return x,y\n\nlabimg=namedtuple('LabeledImage',['image','x','y'])\n\ndef load_image_and_labels(img_id):\n    \"\"\"\n    Read the images and compute the x,y coordinates of the sea lions\n    \"\"\"\n    temp_dotted = cv2.cvtColor(cv2.imread('../input/TrainDotted/{}'.format(img_id)), cv2.COLOR_BGR2RGB)\n    temp_raw = cv2.cvtColor(cv2.imread('../input/Train/{}'.format(img_id)), cv2.COLOR_BGR2RGB)\n    x,y = get_dots_from_image(temp_dotted,temp_raw)\n    return labimg(temp_raw,x,y)"},{"cell_type":"markdown","metadata":{"_cell_guid":"85706964-0c00-ef6d-3a1a-7942464c1f6c"},"source":"#**Sample Images**"},{"cell_type":"code","execution_count":null,"metadata":{"_cell_guid":"45531aa6-db12-055c-45d7-3964b49605b5"},"outputs":[],"source":"training_image_ids = [os.path.basename(c) for c in glob('../input/Train/*.jpg')]"},{"cell_type":"code","execution_count":null,"metadata":{"_cell_guid":"31c83191-4222-5866-6ba2-8bc6e73d4227"},"outputs":[],"source":"for img_id in training_image_ids:\n    temp_dotted = cv2.cvtColor(cv2.imread('../input/TrainDotted/{}'.format(img_id)), cv2.COLOR_BGR2RGB)\n    temp_raw = cv2.cvtColor(cv2.imread('../input/Train/{}'.format(img_id)), cv2.COLOR_BGR2RGB)\n    fig = plt.figure(figsize=(12,8))\n    fig.suptitle(img_id)\n    plt.subplot(121)\n    plt.imshow(temp_raw)\n    plt.xticks([]), plt.yticks([])\n    plt.subplot(122)\n    plt.imshow(temp_dotted)\n    plt.xticks([]), plt.yticks([])\n    plt.show()"},{"cell_type":"markdown","metadata":{"_cell_guid":"8a9a6541-2164-63f3-b181-8011eef612d6"},"source":"##Experiments"},{"cell_type":"code","execution_count":null,"metadata":{"_cell_guid":"88402695-0254-c6c4-c3f8-f5e6b13b7a98"},"outputs":[],"source":"img_exp=5\nexp_dotted = cv2.cvtColor(cv2.imread('../input/TrainDotted/{}.jpg'.format(img_exp)), cv2.COLOR_BGR2RGB)[1350:1900, 3000:3400]#[0:3230, 2800:3950]\nexp_raw = cv2.cvtColor(cv2.imread('../input/Train/{}.jpg'.format(img_exp)), cv2.COLOR_BGR2RGB)[1350:1900, 3000:3400]#[0:3230, 2800:3950]\nfig = plt.figure(figsize=(12,8))\nfig.suptitle(img_exp)\nplt.subplot(121)\nplt.imshow(exp_raw)\nplt.xticks([]), plt.yticks([])\nplt.subplot(122)\nplt.imshow(exp_dotted)\nplt.xticks([]), plt.yticks([])\nplt.show()"},{"cell_type":"code","execution_count":null,"metadata":{"_cell_guid":"90739d1c-e4ba-8d1d-d9f4-12b113538104"},"outputs":[],"source":"exp_raw.shape"},{"cell_type":"code","execution_count":null,"metadata":{"_cell_guid":"e45cfcc4-9fee-6d91-4855-0f0d3bcde807"},"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}