{"cells":[{"metadata":{},"cell_type":"markdown","source":"https://github.com/usuyama/pydata-medical-image/blob/master/diabetic_retinopathy/scripts/preprocess.py"},{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"cell_type":"code","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 \nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\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\nimport os\nprint(os.listdir(\"../input\"))\n# Any results you write to the current directory are saved as output.","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"from glob import glob\nimport cv2\nfrom matplotlib import pyplot as plt\n%matplotlib inline\npath_list = glob('../input/train_images/*.png')[0:16]","execution_count":null,"outputs":[]},{"metadata":{"_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","trusted":true},"cell_type":"code","source":"def estimate_radius(img):\n    mx = img[img.shape[0] // 2,:,:].sum(1)\n    rx = (mx > mx.mean() / 10).sum() / 2\n    my = img[:,img.shape[1] // 2,:].sum(1)\n    ry = (my > my.mean() / 10).sum() / 2\n    return (ry, rx)\n\ndef subtract_gaussian_blur(img,b=5):\n    gb_img = cv2.GaussianBlur(img, (0, 0), b)\n    return cv2.addWeighted(img, 4, gb_img, -4, 128)\n\ndef remove_outer_circle(a, p, r):\n    b = np.zeros(a.shape, dtype=np.uint8)\n    cv2.circle(b, (a.shape[1] // 2, a.shape[0] // 2), int(r * p), (1, 1, 1), -1, 8, 0)\n    return a * b + 128 * (1 - b)\n\ndef crop_img(img, h, w):\n        h_margin = (img.shape[0] - h) // 2 if img.shape[0] > h else 0\n        w_margin = (img.shape[1] - w) // 2 if img.shape[1] > w else 0\n        crop_img = img[h_margin:h + h_margin,w_margin:w + w_margin,:]\n        return crop_img\n\ndef place_in_square(img, r, h, w):\n    new_img = np.zeros((2 * r, 2 * r, 3), dtype=np.uint8)\n    new_img += 128\n    new_img[r - h // 2:r - h // 2 + img.shape[0], r - w // 2:r - w // 2 + img.shape[1]] = img\n    return new_img\nfrom skimage.color import rgb2gray,rgba2rgb\ndef preprocess(f, r, debug_plot=False):\n    img = cv2.imread(f)\n    ry, rx = estimate_radius(img)\n    resize_scale = r / max(rx, ry)\n    w = min(int(rx * resize_scale * 2), r * 2)\n    h = min(int(ry * resize_scale * 2), r * 2)\n    img = cv2.resize(img, (0,0), fx=resize_scale, fy=resize_scale)\n    img = crop_img(img, h, w)\n    if debug_plot:\n        plt.figure()\n        plt.imshow(img)\n    img = subtract_gaussian_blur(img)\n    img = remove_outer_circle(img, 0.9, r)\n    img_rgba = np.zeros([img.shape[0],img.shape[1],4])\n    for row in range(4):\n        img2 = subtract_gaussian_blur(img,(row+1)*5)\n        img_rgba[:,:,row] = rgb2gray(img2)\n    img = place_in_square(img_rgba, r, h, w)\n    if debug_plot:\n        plt.figure()\n        plt.imshow(img)\n    return img_rgba","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"size= 512\nr=size//2\npath0 = path_list[0]\nimg = cv2.imread(path0)\nplt.imshow(img)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"ry, rx = estimate_radius(img)\nresize_scale = r / max(rx, ry)\nw = min(int(rx * resize_scale * 2), r*2)\nh = min(int(ry * resize_scale * 2), r*2)\nimg = cv2.resize(img, (0,0), fx=resize_scale, fy=resize_scale)\nimg = crop_img(img, h, w)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"plt.figure(figsize=[20,5])\nfor row in range(6):\n    img2 = subtract_gaussian_blur(img,(row+1)*5)\n    plt.subplot(1,6,row+1)\n    plt.imshow(img2)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"#make rgba data for training\nfrom skimage.color import rgb2gray,rgba2rgb\nplt.figure(figsize=[10,10])\nimg_rgba = np.zeros([img.shape[0],img.shape[1],4])\nfor row in range(4):\n    img2 = subtract_gaussian_blur(img,(row+1)*5)\n    img_rgba[:,:,row] = rgb2gray(img2)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"plt.figure(figsize=[10,10])\nimg = remove_outer_circle(img_rgba, 0.9, r)\nplt.imshow(img)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"from skimage.color import rgb2gray,rgba2rgb\ndef prepare_rgba(path,r):\n    img = cv2.imread(path)\n    ry, rx = estimate_radius(img)\n    resize_scale = r / max(rx, ry)\n    w = min(int(rx * resize_scale * 2), r*2)\n    h = min(int(ry * resize_scale * 2), r*2)\n    img = cv2.resize(img, (0,0), fx=resize_scale, fy=resize_scale)\n    img = crop_img(img, h, w)\n    #make rgba data for training\n    img_rgba = np.zeros([img.shape[0],img.shape[1],4])\n    for row in range(4):\n        img2 = subtract_gaussian_blur(img,(row+1)*5)\n        img_rgba[:,:,row] = rgb2gray(img2)\n    img = remove_outer_circle(img_rgba, 0.9, r)\n    return img","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"plt.figure(figsize=[20,20])\nfor row in range(16):\n    plt.subplot(4,4,row+1)\n    plt.imshow(prepare_rgba(path_list[row],r))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"","execution_count":null,"outputs":[]}],"metadata":{"kernelspec":{"display_name":"Python 3","language":"python","name":"python3"},"language_info":{"name":"python","version":"3.6.4","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"}},"nbformat":4,"nbformat_minor":1}