{"cells": [{"metadata": {"_uuid": "d700d559a5a85b7324feeccbebf3f60463dbe776", "_cell_guid": "3eefe6c7-c947-4128-a3f0-ad181c550cbe"}, "cell_type": "markdown", "source": ["# Introduction\n", "This example shows that how you could use OpenCV to detect and count white areas in an image. "]}, {"metadata": {"_uuid": "b28c9a2f4675a9c6b07cd648cf0ef16a6c1c95c1", "_cell_guid": "434a57d6-1ab9-4ff6-8e5c-94c71613042f"}, "cell_type": "markdown", "source": ["# 1. Pick a random image"]}, {"outputs": [], "metadata": {"_kg_hide-input": false, "_uuid": "847cd7ec90b1f4b843ce1fee1517039385684472", "collapsed": true, "_cell_guid": "594fda91-c757-447d-8543-38ecdd10218c"}, "cell_type": "code", "source": ["import numpy as np\n", "import matplotlib.pyplot as plt\n", "import cv2\n", "%matplotlib inline\n", "\n", "ID = '0a7d30b252359a10fd298b638b90cb9ada3acced4e0c0e5a3692013f432ee4e9'\n", "FILE = \"../input/stage1_train/{}/images/{}.png\".format(ID,ID)"], "execution_count": null}, {"metadata": {"_uuid": "5d4e9dcc7b4311da8a9f6b31a2f324e59a78ea07", "_cell_guid": "2d13909e-ab29-4dd7-809f-6f722256b3f2"}, "cell_type": "markdown", "source": ["# 2. Read the image and find appropriate filter settings"]}, {"outputs": [], "metadata": {"_kg_hide-input": false, "_uuid": "c9493c474d1144c68bdc18b05d526b6a80ef5869", "collapsed": true, "_cell_guid": "4f0e07a3-fc45-48a9-b1a2-33b466979fc7"}, "cell_type": "code", "source": ["img = cv2.imread(FILE,0)\n", "\n", "# Otsu's thresholding after Gaussian filtering\n", "blur = cv2.GaussianBlur(img,(5,5),0)\n", "ret3,th3 = cv2.threshold(blur,0,255,cv2.THRESH_BINARY+cv2.THRESH_OTSU)"], "execution_count": null}, {"metadata": {"_uuid": "a0c799c63cf07129e99cf1df3b2c92d498bfa48d", "_cell_guid": "97eab948-9c2f-4555-97a9-824da0b725d7"}, "cell_type": "markdown", "source": ["# 3. Plot the filtering process"]}, {"outputs": [], "metadata": {"_kg_hide-input": false, "_uuid": "56e99aa694741c1257eb1ac05bab0fb3f29c6642", "collapsed": true, "_cell_guid": "0fd2ea08-76d5-4d5e-9cf4-8493093f1ab3"}, "cell_type": "code", "source": ["# Plot Here\n", "plt.figure(figsize=(15,5))\n", "images = [blur, 0, th3]\n", "titles = ['Gaussian filtered Image','Histogram',\"Otsu's Thresholding\"]\n", "plt.subplot(1,3,1),plt.imshow(images[0],'gray')\n", "plt.title(titles[0]), plt.xticks([]), plt.yticks([])\n", "plt.subplot(1,3,2),plt.hist(images[0].ravel(),256)\n", "plt.title(titles[1]), plt.xticks([]), plt.yticks([])\n", "plt.subplot(1,3,3),plt.imshow(images[2],'gray')\n", "plt.title(titles[2]), plt.xticks([]), plt.yticks([])"], "execution_count": null}, {"metadata": {"_uuid": "7e18e436f0e66c098e2071182621568b7bb85e80", "_cell_guid": "7cad62f3-95a5-46a1-8d3e-c9d9c7cba3b9"}, "cell_type": "markdown", "source": ["# 4. Count the number of contours in the filtered image"]}, {"outputs": [], "metadata": {"_kg_hide-input": false, "_uuid": "6d7f5ca760aae450876a1d601f1083f004df181b", "collapsed": true, "_cell_guid": "e5bf98e4-b2c1-4bcc-a102-290728bf33a2"}, "cell_type": "code", "source": ["im = cv2.imread(FILE)\n", "imgray = cv2.cvtColor(im,cv2.COLOR_BGR2GRAY)\n", "ret,thresh = cv2.threshold(img,0,255,cv2.THRESH_BINARY+cv2.THRESH_OTSU)\n", "image, contours, hierarchy = cv2.findContours(thresh,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)\n", "img = cv2.drawContours(img, contours, -1, (255,255,255), 3)\n", "plt.figure(figsize=(10,10))\n", "plt.subplot(1,2,1),plt.title('Original Image'),plt.imshow(im)#,'red')\n", "plt.subplot(1,2,2),plt.title('OpenCV.findContours'),plt.imshow(img,'gray')#,'red')\n", "\n", "print('number of detected contours: ',len(contours))"], "execution_count": null}, {"metadata": {"_uuid": "daba862d82f6fd5d4c4ed3f25d9ffd026804e961", "_cell_guid": "64a7c20e-ff6d-448e-91de-53e7e3e5c282"}, "cell_type": "markdown", "source": ["*Interested in OpenCV? Checkout [OpenCV-Python-Toturials](http://opencv-python-tutroals.readthedocs.io/en/latest/py_tutorials/py_tutorials.html)*"]}], "metadata": {"kernelspec": {"display_name": "Python 3", "language": "python", "name": "python3"}, "language_info": {"nbconvert_exporter": "python", "version": "3.6.4", "codemirror_mode": {"name": "ipython", "version": 3}, "pygments_lexer": "ipython3", "name": "python", "file_extension": ".py", "mimetype": "text/x-python"}}, "nbformat": 4, "nbformat_minor": 1}