{"cells":[{"metadata":{"trusted":true,"_uuid":"ed49f7b156cc37bd58bb4258db845bc7b552d1e7"},"cell_type":"code","source":"import cv2\nimport numpy as np\nfrom PIL import Image","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"64968b54b630fc6461eee79ca8f1065069c31fcc"},"cell_type":"code","source":"img_orig = cv2.imread('../input/train/0b2eb27b5.jpg')[:,:,::-1]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"403e7356be9013ea4144fce2788c91e6b7682ae2"},"cell_type":"code","source":"im=Image.fromarray(img_orig).convert(\"L\")","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"0de9a6302fea4b4fc4f9642c99fed018c9f4a865"},"cell_type":"code","source":"Image.fromarray(img_orig)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"97111a6362e80fb7a1cc67ac21e9f4d181b6b1e6"},"cell_type":"code","source":"img = cv2.threshold(np.array(im), 120, 255, cv2.THRESH_BINARY)[1]  # ensure binary","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"3b139f02f6f38e307cd9f63a02c60470b963ab6f"},"cell_type":"code","source":"Image.fromarray(img)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"d2964fdb81d8efaad111d3865363e371d98acdb9"},"cell_type":"code","source":"ret, labels = cv2.connectedComponents(img)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"e79856eec692f9632f0915829ccc9745dd301945"},"cell_type":"code","source":"def undesired_objects(image):\n    image = image.astype('uint8')\n    nb_components, output, stats, centroids = cv2.connectedComponentsWithStats(image, connectivity=4)\n    sizes = stats[:, -1]\n\n    max_label = 1\n    max_size = sizes[1]\n    for i in range(2, nb_components):\n        if sizes[i] > max_size:\n            max_label = i\n            max_size = sizes[i]\n\n    img2 = np.zeros(output.shape)\n    img2[output == max_label] = 255\n    return img2","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"4a14dd8ccefe80bbc2e0801d29a10ea35e268a18"},"cell_type":"code","source":"img2=1-(undesired_objects(img)/255)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"cbd6d023aceffdf6834c55a512d527c73016e999"},"cell_type":"code","source":"img2.shape[0]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"ce256b5ef1fff4b17e3d679f03156270e3364a4b"},"cell_type":"code","source":"img3=img_orig*(img2.reshape(img2.shape[0],img2.shape[1],1))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"scrolled":true,"_uuid":"e44fea58a1c8196b6966b893cb1e28e4e15b340e"},"cell_type":"code","source":"Image.fromarray(img3.astype(np.uint8))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"5048b2a989026e39142384c718f383336be160d7"},"cell_type":"code","source":"1-img2","execution_count":null,"outputs":[]},{"metadata":{"trusted":false,"_uuid":"c8bd81a407d100985dcabb1d0f5ce1146f444312"},"cell_type":"code","source":"","execution_count":null,"outputs":[]}],"metadata":{"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.5"}},"nbformat":4,"nbformat_minor":1}