{"cells":[{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"cell_type":"code","source":"import numpy as np\nimport pandas as pd\nfrom skimage.color import rgb2gray\nimport matplotlib.pyplot as plt\n%matplotlib inline","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"Lets show some images that will be used for training"},{"metadata":{"trusted":true},"cell_type":"code","source":"img_list = ['d527d08861e41c7b.jpg', '82f4f279c9a96ee8.jpg', '309b0f22a3f7efe0.jpg', 'a57366e38050b227.jpg']\nfig = plt.figure(figsize=(16, 16))\nfor i in range(4):\n    x = fig.add_subplot(2, 2, i+1)\n    image = plt.imread('/kaggle/input/test/'+img_list[i])\n    x.set_title(\"{image} ({shape[0]},{shape[1]})\".format(image=img_list[i],shape=image.shape))\n    plt.imshow(image)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"Convert it into grayscale, that allows using simple treshold processing "},{"metadata":{"trusted":true},"cell_type":"code","source":"fig = plt.figure(figsize=(16, 16))\nfor i in range(4):\n    x = fig.add_subplot(2, 2, i+1)\n    image = plt.imread('/kaggle/input/test/'+img_list[i])\n    gray = rgb2gray(image)\n    x.set_title(\"{image} ({shape[0]},{shape[1]})\".format(image=img_list[i],shape=gray.shape))\n    plt.imshow(gray, cmap='gray')","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"We will take the mean of the pixel values and use that as a threshold. If the pixel value is more than threshold, we can say that it belongs to an object. If the pixel value is less than the threshold, it will be treated as the background:"},{"metadata":{"trusted":true},"cell_type":"code","source":"def to_2regions(image):\n    gray = rgb2gray(image)\n    m = gray.mean()\n    for i in range(image.shape[0]):\n        for j in range(image.shape[1]):    \n            gray[i,j] = int(gray[i,j] > m)\n    return gray","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"fig = plt.figure(figsize=(16, 16))\nfor i in range(4):\n    x = fig.add_subplot(2, 2, i+1)\n    image = plt.imread('/kaggle/input/test/'+img_list[i])\n    r2_gray = to_2regions(image)\n    x.set_title(\"{image} ({shape[0]},{shape[1]})\".format(image=img_list[i],shape=r2_gray.shape))\n    plt.imshow(r2_gray, cmap='gray')","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"Use thresholds to detect multiple objects:"},{"metadata":{"trusted":true},"cell_type":"code","source":"def to_4regions(image):\n    g_image = rgb2gray(image)\n    m = g_image.mean()\n    m1 = 0.5 * m\n    m2 = 0.25 * m\n    for i in range(image.shape[0]):\n        for j in range(image.shape[1]):\n            pxl = g_image[i,j]\n            if pxl > m:\n                g_image[i,j] = 3\n            elif pxl > m1:\n                g_image[i,j] = 2\n            elif pxl > m2:\n                g_image[i,j] = 1\n            else:\n                g_image[i,j] = 0            \n    return g_image","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"Lets see what can we get"},{"metadata":{"trusted":true},"cell_type":"code","source":"fig = plt.figure(figsize=(16, 16))\nfor i in range(4):\n    x = fig.add_subplot(2, 2, i+1)\n    image = plt.imread('/kaggle/input/test/'+img_list[i])\n    r2_gray = to_4regions(image)\n    x.set_title(\"{image} ({shape[0]},{shape[1]})\".format(image=img_list[i],shape=r2_gray.shape))\n    plt.imshow(r2_gray, cmap='gray')","execution_count":null,"outputs":[]}],"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"nbformat":4,"nbformat_minor":1}