{"cells":[{"metadata":{},"cell_type":"markdown","source":"# During the exploration phase I found that the black areas around the images could be a problem.\n# So I looked for some ways to normalized this and found stretching works very well.\n# My initial idea was to transform the Circle to a Rectangle and I searched for some 2D morph libraries using maped triangles. This looks promissing to morphing faces but not for this task.\n# So I found a simple line-by-line approximation worked pretty well with this images. The idea is remove the left and right black portion of images stretching the non-black area to the rectangle limits.\n# This preprocessing showed to be very stable and improved our scores by 0.01+ :)"},{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"cell_type":"code","source":"import numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\nimport os\nimport cv2\nfrom matplotlib import pyplot as plt\nfrom joblib import Parallel, delayed\nimport glob\n\nfilenames = glob.glob( '../input/aptos2019-blindness-detection/train_images/*' )","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","trusted":true},"cell_type":"code","source":"def morph_func( img, subgauss=False ):\n    img = img.astype( np.uint8 )\n\n    findblack = np.sum( img, axis=2 )\n    findblack = findblack - np.min(findblack)\n    findblack[findblack > np.mean(findblack)] = np.mean(findblack)\n    findblack = findblack / np.max(findblack)\n    borders = 2+np.sum( findblack[:,:int(img.shape[1]/2)]<0.33, axis=1 )\n\n    for i in range( img.shape[0] ):\n        k = borders[i]\n        img[i] = img[i, np.linspace(k,img.shape[1]-k,num=img.shape[1]).astype(int),: ]   \n        \n    img = cv2.resize( img, (512,512) )\n    if subgauss==True:\n        img = cv2.addWeighted(img,4, cv2.GaussianBlur(img, (0,0) , sigmaX=13) ,-4 ,127)\n        \n    return img\n\ndef print_preprocessing( fn ):\n    img_circle = cv2.imread( fn )\n    img_circle = cv2.cvtColor(img_circle, cv2.COLOR_BGR2RGB)\n    img_circle = cv2.resize( img_circle, (512,512) )\n\n    img_rect = morph_func(img_circle)\n\n    plt.imshow( np.hstack((img_circle, img_rect))  )","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"print_preprocessing( filenames[0] )","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"print_preprocessing( filenames[11] )","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"print_preprocessing( filenames[111] )","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"print_preprocessing( filenames[1111] )","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"print_preprocessing( filenames[2222] )","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"print_preprocessing( filenames[3333] )","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"print_preprocessing( filenames[2019] )","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"print_preprocessing( filenames[908] )","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"print_preprocessing( filenames[5] )","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"print_preprocessing( filenames[6] )","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"print_preprocessing( filenames[7] )","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"print_preprocessing( filenames[9] )","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"","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}