{"cells":[{"cell_type":"code","execution_count":null,"metadata":{"_cell_guid":"df0e4a2a-341a-96d8-04fa-2cbff60047cc"},"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\nimport numpy as np # linear algebra\nimport 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\nfrom subprocess import check_output\nprint(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":"625c281c-effe-14d9-2235-708bde4fc3b3"},"outputs":[],"source":"## libraries\n\nimport numpy as np\nimport pandas as pd\nimport os\nimport cv2\nimport matplotlib.pyplot as plt\nimport skimage.feature\n%matplotlib inline\n\n\nfile_names = os.listdir(\"../input/Train/\")\n#test = pd.read_csv(\"Test.csv\")\n#train = pd.read_csv(\"Train.csv\")\nfile_names[0:2]"},{"cell_type":"code","execution_count":null,"metadata":{"_cell_guid":"34a3477d-161f-f741-9e4a-d6acc2bbc8d5"},"outputs":[],"source":"\nfilename = file_names[0]\nimage_0_tr = cv2.imread(\"../input/TrainDotted/\" + filename)\nimage_0 = cv2.imread(\"../input/Train/\" + filename)"},{"cell_type":"code","execution_count":null,"metadata":{"_cell_guid":"c4666657-8608-70b3-7263-f4d6a84ed6ef"},"outputs":[],"source":"mask_0 = cv2.cvtColor(image_0_tr, cv2.COLOR_BGR2GRAY)\n#mask_0[mask_0 < 20] = 0\n#mask_0[mask_0 > 0] = 255\n#mask_1\n#cv2.imwrite('mask_0',mask_0)\n#cv2.imshow('mask_0',mask_0)\n\nfrom PIL import Image\n#img = Image.fromarray(image_0_tr, 'RGB')\n#img.save('my.png')\n#img\ntype(mask_0)\n#Image.fromarray(mask_0)\n(mask_0)"},{"cell_type":"code","execution_count":null,"metadata":{"_cell_guid":"e3bc8b9f-e527-a980-40f6-78e65640d363"},"outputs":[],"source":"#image_0_df = image_0 - image_0_tr\nimage_0_df = cv2.absdiff(image_0,image_0_tr)\nimage_0_df = Image.fromarray(image_0_df, 'RGB')\nimage_0_df\n#img.save('my.png')\n#blobs = skimage.feature.blob_log(image_0_df, min_sigma=3, max_sigma=4, num_sigma=1, threshold=0.02)\n\n#red: adult males\n#magenta: subadult males\n#brown: adult females\n#blue: juveniles\n#green: pups"},{"cell_type":"code","execution_count":null,"metadata":{"_cell_guid":"df60b02c-f488-fd4e-2b90-61ca37f0f3bf"},"outputs":[],"source":"#### code based on the web: http://www.pyimagesearch.com/2014/08/04/opencv-python-color-detection/\n# import the necessary packages\nimport numpy as np\n#import argparse\nimport cv2\nimport os\nfrom PIL import Image\n\n# construct the argument parse and parse the arguments\n#ap = argparse.ArgumentParser()\n#ap.add_argument(\"-i\", \"--image\", help = \"path to the image\")\n#args = vars(ap.parse_args())\n \n# load the image\n#image = cv2.imread(args[\"image\"])\nfile_names = os.listdir(\"../input/Train/\")\nfilename = file_names[0]\nimage_0_tr = cv2.imread(\"../input/TrainDotted/\" + filename)\nimage_0 = cv2.imread(\"../input/Train/\" + filename)\nimage_0_df = cv2.absdiff(image_0,image_0_tr)\n# define the list of boundaries, red, blue, yellow = [25, 146, 190], [62, 174, 250], and gray= [103, 86, 65], [145, 133, 128], \n#BGR, mangeta RGB: 255,0,255, brown : 139,69,19\nboundaries = [\n\t([17, 15, 100], [50, 56, 200]),\n\t([86, 31, 4], [220, 88, 50]),\n\t([17, 90, 10], [88, 220, 50]),\n\t([80, 10, 90], [220, 88, 220]),\n    ([3, 10, 65], [50, 80, 150])\n]\n# loop over the boundaries\nfor (lower, upper) in boundaries:\n\t# create NumPy arrays from the boundaries\n\tlower = np.array(lower, dtype = \"uint8\")\n\tupper = np.array(upper, dtype = \"uint8\")\n \n\t# find the colors within the specified boundaries and apply\n\t# the mask\n\tmask = cv2.inRange(image_0_tr, lower, upper)\n\toutput = cv2.bitwise_and(image_0_tr, image_0_tr, mask = mask)\n    # show the images\n    #image_0_out = Image.fromarray(output, 'RGB')\n    #image_0_out"},{"cell_type":"code","execution_count":null,"metadata":{"_cell_guid":"17f0dc0b-33ef-b9e7-3a9c-988110784ecb"},"outputs":[],"source":"image_0_out = Image.fromarray(output, 'RGB')\n#image_0_out\n\n## every image_0_out has only these values detected but then i can create a mask from the difference \n# the images.\n\n\nimage_0_df = Image.fromarray(image_0_df, 'RGB')\nimage_0_df\n"},{"cell_type":"code","execution_count":null,"metadata":{"_cell_guid":"71df6cdd-df48-0e3c-7a8e-85f2a7df6ac7"},"outputs":[],"source":"image_0_df = cv2.absdiff(image_0,image_0_tr)\nmask_0_df = cv2.cvtColor(image_0_df, cv2.COLOR_BGR2GRAY)\n#mask_0_df = Image.fromarray(mask_0_df)\nmask_0_df"},{"cell_type":"code","execution_count":null,"metadata":{"_cell_guid":"2ab4c8b6-9f30-a604-16d5-cecdca929f52"},"outputs":[],"source":"im_bw = cv2.threshold(mask_0_df, 127, 255, cv2.THRESH_BINARY)[1]\n#im_bw = Image.fromarray(im_bw)\n#im_bw"},{"cell_type":"code","execution_count":null,"metadata":{"_cell_guid":"ce20b400-87ef-bea2-725b-ace88ff642a6"},"outputs":[],"source":"image_out0 = cv2.bitwise_or(output, output, mask=im_bw)\nimage_out0 = Image.fromarray(image_out0)\nimage_out0"}],"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}