{"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_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import numpy as np\nimport pandas as pd\nimport matplotlib.pyplot as plt\n\nimport cv2\nimport os\nimport seaborn as sns\n\nfrom ast import literal_eval\n\nplt.style.use('dark_background')","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-01-23T14:53:14.748877Z","iopub.execute_input":"2022-01-23T14:53:14.749714Z","iopub.status.idle":"2022-01-23T14:53:16.055367Z","shell.execute_reply.started":"2022-01-23T14:53:14.749598Z","shell.execute_reply":"2022-01-23T14:53:16.054462Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def singleScaleRetinex(img,variance):\n    retinex = np.log10(img) - np.log10(cv2.GaussianBlur(img, (0, 0), variance))\n    return retinex\n\ndef multiScaleRetinex(img, variance_list):\n    retinex = np.zeros_like(img)\n    for variance in variance_list:\n        retinex += singleScaleRetinex(img, variance)\n    retinex = retinex / len(variance_list)\n    return retinex\n\n   \n\ndef MSR(img, variance_list):\n    img = np.float64(img) + 1.0\n    img_retinex = multiScaleRetinex(img, variance_list)\n\n    for i in range(img_retinex.shape[2]):\n        unique, count = np.unique(np.int32(img_retinex[:, :, i] * 100), return_counts=True)\n        for u, c in zip(unique, count):\n            if u == 0:\n                zero_count = c\n                break            \n        low_val = unique[0] / 100.0\n        high_val = unique[-1] / 100.0\n        for u, c in zip(unique, count):\n            if u < 0 and c < zero_count * 0.1:\n                low_val = u / 100.0\n            if u > 0 and c < zero_count * 0.1:\n                high_val = u / 100.0\n                break            \n        img_retinex[:, :, i] = np.maximum(np.minimum(img_retinex[:, :, i], high_val), low_val)\n        \n        img_retinex[:, :, i] = (img_retinex[:, :, i] - np.min(img_retinex[:, :, i])) / \\\n                               (np.max(img_retinex[:, :, i]) - np.min(img_retinex[:, :, i])) \\\n                               * 255\n    img_retinex = np.uint8(img_retinex)        \n    return img_retinex\n\n\n\ndef SSR(img, variance):\n    img = np.float64(img) + 1.0\n    img_retinex = singleScaleRetinex(img, variance)\n    for i in range(img_retinex.shape[2]):\n        unique, count = np.unique(np.int32(img_retinex[:, :, i] * 100), return_counts=True)\n        for u, c in zip(unique, count):\n            if u == 0:\n                zero_count = c\n                break            \n        low_val = unique[0] / 100.0\n        high_val = unique[-1] / 100.0\n        for u, c in zip(unique, count):\n            if u < 0 and c < zero_count * 0.1:\n                low_val = u / 100.0\n            if u > 0 and c < zero_count * 0.1:\n                high_val = u / 100.0\n                break            \n        img_retinex[:, :, i] = np.maximum(np.minimum(img_retinex[:, :, i], high_val), low_val)\n        \n        img_retinex[:, :, i] = (img_retinex[:, :, i] - np.min(img_retinex[:, :, i])) / \\\n                               (np.max(img_retinex[:, :, i]) - np.min(img_retinex[:, :, i])) \\\n                               * 255\n    img_retinex = np.uint8(img_retinex)        \n    return img_retinex\n\n\nvariance_list=[15, 80, 30]\nvariance=300","metadata":{"execution":{"iopub.status.busy":"2022-01-23T14:53:16.057169Z","iopub.execute_input":"2022-01-23T14:53:16.057405Z","iopub.status.idle":"2022-01-23T14:53:16.078141Z","shell.execute_reply.started":"2022-01-23T14:53:16.057375Z","shell.execute_reply":"2022-01-23T14:53:16.077039Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def load_image(video_id, video_frame, image_dir, ssr=False):\n    img_path = f'{image_dir}/video_{video_id}/{video_frame}.jpg'\n    assert os.path.exists(img_path), f'{img_path} does not exist.'\n    img = cv2.imread(img_path)\n    \n    if ssr:\n        img = SSR(img, variance)\n    \n    return img\n\n\ndef decode_annotations(annotaitons_str):\n    \"\"\"decode annotations in string to list of dict\"\"\"\n    return literal_eval(annotaitons_str)\n\ndef load_image_with_annotations(video_id, video_frame, image_dir, annotaitons_str, ssr=False):\n    img = load_image(video_id, video_frame, image_dir, ssr=ssr)\n    annotations = decode_annotations(annotaitons_str)\n    \n    if len(annotations) > 0:\n        for ann in annotations:\n            cv2.rectangle(img, (ann['x'], ann['y']),\n                (ann['x'] + ann['width'], ann['y'] + ann['height']),\n                (0, 0, 255), thickness=2)\n            \n    return img\n","metadata":{"execution":{"iopub.status.busy":"2022-01-23T14:53:16.079666Z","iopub.execute_input":"2022-01-23T14:53:16.081189Z","iopub.status.idle":"2022-01-23T14:53:16.099044Z","shell.execute_reply.started":"2022-01-23T14:53:16.081061Z","shell.execute_reply":"2022-01-23T14:53:16.097905Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def visualize(**images):\n    n = len(images)\n    plt.figure(figsize=(30, 30))\n    for i, (name, image) in enumerate(images.items()):\n        plt.subplot(1, n, i + 1)\n        plt.xticks([])\n        plt.yticks([])\n        plt.title(' '.join(name.split('_')).title())\n        plt.imshow(image)\n    plt.show()","metadata":{"execution":{"iopub.status.busy":"2022-01-23T14:53:16.102565Z","iopub.execute_input":"2022-01-23T14:53:16.102965Z","iopub.status.idle":"2022-01-23T14:53:16.114051Z","shell.execute_reply.started":"2022-01-23T14:53:16.102893Z","shell.execute_reply":"2022-01-23T14:53:16.113138Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df = pd.read_csv('../input/tensorflow-great-barrier-reef/train.csv')\ndf.head()","metadata":{"execution":{"iopub.status.busy":"2022-01-23T14:53:16.116374Z","iopub.execute_input":"2022-01-23T14:53:16.117558Z","iopub.status.idle":"2022-01-23T14:53:16.227274Z","shell.execute_reply.started":"2022-01-23T14:53:16.117517Z","shell.execute_reply":"2022-01-23T14:53:16.225966Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"index = 30\nrow = df.iloc[index]\nvideo_id = row.video_id\nvideo_frame = row.video_frame\nannotations_str = row.annotations\nimage_dir = '../input/tensorflow-great-barrier-reef/train_images'\n\noriginal_img = load_image_with_annotations(video_id, video_frame, image_dir, annotations_str, ssr=False)\nssr_img = load_image_with_annotations(video_id, video_frame, image_dir, annotations_str, ssr=True)\n\nvisualize(\n    original=original_img[:, :, ::-1],\n    ssr=ssr_img[:, :, ::-1],\n)","metadata":{"execution":{"iopub.status.busy":"2022-01-23T14:53:16.229126Z","iopub.execute_input":"2022-01-23T14:53:16.229682Z","iopub.status.idle":"2022-01-23T14:53:28.852496Z","shell.execute_reply.started":"2022-01-23T14:53:16.229631Z","shell.execute_reply":"2022-01-23T14:53:28.848870Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"index = 45\nrow = df.iloc[index]\nvideo_id = row.video_id\nvideo_frame = row.video_frame\nannotations_str = row.annotations\nimage_dir = '../input/tensorflow-great-barrier-reef/train_images'\n\noriginal_img = load_image_with_annotations(video_id, video_frame, image_dir, annotations_str, ssr=False)\nssr_img = load_image_with_annotations(video_id, video_frame, image_dir, annotations_str, ssr=True)\n\nvisualize(\n    original=original_img[:, :, ::-1],\n    ssr=ssr_img[:, :, ::-1],\n)","metadata":{"execution":{"iopub.status.busy":"2022-01-23T14:53:28.854338Z","iopub.execute_input":"2022-01-23T14:53:28.854895Z","iopub.status.idle":"2022-01-23T14:53:41.313119Z","shell.execute_reply.started":"2022-01-23T14:53:28.854851Z","shell.execute_reply":"2022-01-23T14:53:41.312008Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"index = 60\nrow = df.iloc[index]\nvideo_id = row.video_id\nvideo_frame = row.video_frame\nannotations_str = row.annotations\nimage_dir = '../input/tensorflow-great-barrier-reef/train_images'\n\noriginal_img = load_image_with_annotations(video_id, video_frame, image_dir, annotations_str, ssr=False)\nssr_img = load_image_with_annotations(video_id, video_frame, image_dir, annotations_str, ssr=True)\n\nvisualize(\n    original=original_img[:, :, ::-1],\n    ssr=ssr_img[:, :, ::-1],\n)","metadata":{"execution":{"iopub.status.busy":"2022-01-23T14:53:41.314408Z","iopub.execute_input":"2022-01-23T14:53:41.314999Z","iopub.status.idle":"2022-01-23T14:53:53.950963Z","shell.execute_reply.started":"2022-01-23T14:53:41.314967Z","shell.execute_reply":"2022-01-23T14:53:53.949554Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(np.mean(original_img[..., 0]))\nprint(np.mean(ssr_img[..., 0]))","metadata":{"execution":{"iopub.status.busy":"2022-01-23T14:53:53.952635Z","iopub.execute_input":"2022-01-23T14:53:53.952864Z","iopub.status.idle":"2022-01-23T14:53:53.961816Z","shell.execute_reply.started":"2022-01-23T14:53:53.952833Z","shell.execute_reply":"2022-01-23T14:53:53.960762Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(np.mean(original_img[..., 1]))\nprint(np.mean(ssr_img[..., 1]))","metadata":{"execution":{"iopub.status.busy":"2022-01-23T14:53:53.965441Z","iopub.execute_input":"2022-01-23T14:53:53.966068Z","iopub.status.idle":"2022-01-23T14:53:53.975292Z","shell.execute_reply.started":"2022-01-23T14:53:53.966020Z","shell.execute_reply":"2022-01-23T14:53:53.974371Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(np.mean(original_img[..., 2]))\nprint(np.mean(ssr_img[..., 2]))","metadata":{"execution":{"iopub.status.busy":"2022-01-23T14:53:53.976994Z","iopub.execute_input":"2022-01-23T14:53:53.977417Z","iopub.status.idle":"2022-01-23T14:53:53.988363Z","shell.execute_reply.started":"2022-01-23T14:53:53.977367Z","shell.execute_reply":"2022-01-23T14:53:53.987414Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"colors = (\"red\", \"green\", \"blue\")\nchannel_ids = (0, 1, 2)\nimage = original_img\n\nplt.figure(figsize=(12, 8))\nplt.xlim([0, 256])\nfor channel_id, c in zip(channel_ids, colors):\n    histogram, bin_edges = np.histogram(image[:, :, channel_id], bins=256, range=(0, 256))\n    plt.plot(bin_edges[0:-1], histogram, color=c)\nplt.xlabel(\"Color value\")\nplt.ylabel(\"Pixels\")\n\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-01-23T14:53:53.989700Z","iopub.execute_input":"2022-01-23T14:53:53.990606Z","iopub.status.idle":"2022-01-23T14:53:54.285768Z","shell.execute_reply.started":"2022-01-23T14:53:53.990571Z","shell.execute_reply":"2022-01-23T14:53:54.284767Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"colors = (\"red\", \"green\", \"blue\")\nchannel_ids = (0, 1, 2)\nimage = ssr_img\n\nplt.figure(figsize=(12, 8))\nplt.xlim([0, 256])\nfor channel_id, c in zip(channel_ids, colors):\n    histogram, bin_edges = np.histogram(image[:, :, channel_id], bins=256, range=(0, 256))\n    plt.plot(bin_edges[0:-1], histogram, color=c)\nplt.xlabel(\"Color value\")\nplt.ylabel(\"Pixels\")\n\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-01-23T14:53:54.287267Z","iopub.execute_input":"2022-01-23T14:53:54.287540Z","iopub.status.idle":"2022-01-23T14:53:54.573883Z","shell.execute_reply.started":"2022-01-23T14:53:54.287507Z","shell.execute_reply":"2022-01-23T14:53:54.572861Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"image = original_img\n\nplt.figure(figsize=(12, 8))\nsns.histplot(image.ravel(), bins=255)\nplt.xlabel('Intensity Value')\nplt.ylabel('Count')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-01-23T14:53:54.575679Z","iopub.execute_input":"2022-01-23T14:53:54.575948Z","iopub.status.idle":"2022-01-23T14:53:57.567267Z","shell.execute_reply.started":"2022-01-23T14:53:54.575874Z","shell.execute_reply":"2022-01-23T14:53:57.566330Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"image = ssr_img\n\nplt.figure(figsize=(12, 8))\nsns.histplot(image.ravel(), bins=255)\nplt.xlabel('Intensity Value')\nplt.ylabel('Count')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-01-23T14:53:57.568652Z","iopub.execute_input":"2022-01-23T14:53:57.569170Z","iopub.status.idle":"2022-01-23T14:54:00.595087Z","shell.execute_reply.started":"2022-01-23T14:53:57.569135Z","shell.execute_reply":"2022-01-23T14:54:00.594245Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"visualize(\n    original=original_img[..., 0],\n    ssr=ssr_img[..., 0],\n)","metadata":{"execution":{"iopub.status.busy":"2022-01-23T14:54:00.596616Z","iopub.execute_input":"2022-01-23T14:54:00.596838Z","iopub.status.idle":"2022-01-23T14:54:01.275998Z","shell.execute_reply.started":"2022-01-23T14:54:00.596808Z","shell.execute_reply":"2022-01-23T14:54:01.274982Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"H = 720\nW = 1280\n\nvisualize(\n    original1=original_img[:H//2, :W//2, ::-1],\n    original2=original_img[:H//2, W//2:, ::-1],\n)\n\nvisualize(\n    original3=original_img[H//2:, :W//2, ::-1],\n    original4=original_img[H//2:, W//2:, ::-1],\n)","metadata":{"execution":{"iopub.status.busy":"2022-01-23T14:54:01.277437Z","iopub.execute_input":"2022-01-23T14:54:01.277663Z","iopub.status.idle":"2022-01-23T14:54:02.554664Z","shell.execute_reply.started":"2022-01-23T14:54:01.277633Z","shell.execute_reply":"2022-01-23T14:54:02.553137Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"H = 720\nW = 1280\n\nvisualize(\n    original1=ssr_img[:H//2, :W//2, ::-1],\n    original2=ssr_img[:H//2, W//2:, ::-1],\n)\n\nvisualize(\n    original3=ssr_img[H//2:, :W//2, ::-1],\n    original4=ssr_img[H//2:, W//2:, ::-1],\n)","metadata":{"execution":{"iopub.status.busy":"2022-01-23T14:54:02.556216Z","iopub.execute_input":"2022-01-23T14:54:02.556483Z","iopub.status.idle":"2022-01-23T14:54:03.843254Z","shell.execute_reply.started":"2022-01-23T14:54:02.556450Z","shell.execute_reply":"2022-01-23T14:54:03.842080Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}