{"cells":[{"metadata":{},"cell_type":"markdown","source":"This kernel based on the [this kernerl](https://www.kaggle.com/seriousran/image-pre-processing-for-wild-images)\n\nIt introduce image pre-precessing for wild images using CLAHE and AWB"},{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"cell_type":"code","source":"import os\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n!pip install opencv-contrib-python\nimport cv2\nprint(os.listdir(\"../input\"))\nimport matplotlib.pyplot as plt\n%matplotlib inline ","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"!ls /kaggle/input/","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","trusted":true},"cell_type":"code","source":"train_dir = '../input/iwildcam-2020-fgvc7/train'\ntrain_imgs = ['../input/iwildcam-2020-fgvc7/train/{}'.format(i) for i in os.listdir(train_dir)]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"num_show = 15\ncolumns = 5","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"plt.figure(figsize=(25,12))\nfor idx, train_img in enumerate(train_imgs):\n    if idx >= num_show:\n        break\n    \n    temp_img = cv2.imread(train_img, cv2.IMREAD_COLOR)\n    \n    plt.subplot(10 / columns + 1, columns, idx + 1)\n    plt.imshow(temp_img)\n    if idx % 5 == 4:\n        plt.show()\n        plt.figure(figsize=(25,12))","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# CLAHE(Contrast Limited Adaptive Histogram Equalization)\nref: https://docs.opencv.org/3.1.0/d5/daf/tutorial_py_histogram_equalization.html"},{"metadata":{"trusted":true},"cell_type":"code","source":"clahe = cv2.createCLAHE(clipLimit=2.0, tileGridSize=(16, 16))\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"plt.figure(figsize=(25,12))\nfor idx, train_img in enumerate(train_imgs):\n    if idx >= num_show:\n        break\n    \n    temp_img = cv2.imread(train_img, cv2.IMREAD_COLOR)        \n    img_lab = cv2.cvtColor(temp_img, cv2.COLOR_BGR2Lab)\n\n    l, a, b = cv2.split(img_lab)\n    img_l = clahe.apply(l)\n    img_clahe = cv2.merge((img_l, a, b))\n\n    img_clahe = cv2.cvtColor(img_clahe, cv2.COLOR_Lab2BGR)\n    \n    plt.subplot(10 / columns + 1, columns, idx + 1)\n    plt.imshow(img_clahe)\n    if idx % 5 == 4:\n        plt.show()\n        plt.figure(figsize=(25,12))","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# Automatic White Balance\n- Simple\n- Grayworld\n- Learning-based\nTheses require xphoto in 'opencv-contrib-python'"},{"metadata":{"trusted":true},"cell_type":"code","source":"wb = cv2.xphoto.createSimpleWB()\nwb.setP(0.4)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"plt.figure(figsize=(25,12))\nfor idx, train_img in enumerate(train_imgs):\n    if idx >= num_show:\n        break\n    \n    temp_img = cv2.imread(train_img, cv2.IMREAD_COLOR)        \n    \n    img_wb = wb.balanceWhite(temp_img)\n    \n    plt.subplot(10 / columns + 1, columns, idx + 1)\n    plt.imshow(img_wb)\n    if idx % 5 == 4:\n        plt.show()\n        plt.figure(figsize=(25,12))","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# CLAHE & Simple WB"},{"metadata":{"trusted":true},"cell_type":"code","source":"plt.figure(figsize=(25,12))\nfor idx, train_img in enumerate(train_imgs):\n    if idx >= num_show:\n        break\n    \n    temp_img = cv2.imread(train_img, cv2.IMREAD_COLOR)        \n    \n    img_wb = wb.balanceWhite(temp_img)\n\n    img_lab = cv2.cvtColor(img_wb, cv2.COLOR_BGR2Lab)\n\n    l, a, b = cv2.split(img_lab)\n    res_l = clahe.apply(l)\n    res = cv2.merge((res_l, a, b))\n\n    res = cv2.cvtColor(res, cv2.COLOR_Lab2BGR)\n\n    plt.subplot(10 / columns + 1, columns, idx + 1)\n    plt.imshow(res)\n    if idx % 5 == 4:\n        plt.show()\n        plt.figure(figsize=(25,12))","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# Compare Original and Pre-processed with CLAHE & SimpleWB"},{"metadata":{"trusted":true},"cell_type":"code","source":"fig=plt.figure(figsize=(32, 128))\nfor idx, train_img in enumerate(train_imgs):\n    if idx >= num_show:\n        break\n    \n    temp_img = cv2.imread(train_img, cv2.IMREAD_COLOR)        \n    \n    img_wb = wb.balanceWhite(temp_img)\n\n    img_lab = cv2.cvtColor(img_wb, cv2.COLOR_BGR2Lab)\n\n    l, a, b = cv2.split(img_lab)\n    res_l = clahe.apply(l)\n    res = cv2.merge((res_l, a, b))\n\n    res = cv2.cvtColor(res, cv2.COLOR_Lab2BGR)\n    fig.add_subplot(15, 2, 2 * idx + 1)\n    plt.imshow(temp_img)\n    fig.add_subplot(15, 2, 2 * idx + 2)\n    plt.imshow(res)\nplt.show()","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":4}