{"cells":[{"metadata":{},"cell_type":"markdown","source":"# VinBigData Chest X-ray Abnormalities Detection\nAutomatically localize and classify thoracic abnormalities from chest radiographs\n\n#### The aim of this notebook is to explore the histogram equalization technique and it's variant."},{"metadata":{},"cell_type":"markdown","source":"## Import libraries"},{"metadata":{"_kg_hide-input":true,"_kg_hide-output":true,"trusted":true},"cell_type":"code","source":"!pip install tensorflow_io","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"cell_type":"code","source":"import os\nimport pandas as pd\nimport numpy as np\nimport cv2\nimport matplotlib.pyplot as plt\nimport tensorflow as tf\nimport tensorflow_io as tfio","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"## Read DICOM images"},{"metadata":{"trusted":true},"cell_type":"code","source":"# Reading DICOM images\n\ndef read_dicom(path):\n    image_bytes = tf.io.read_file(path)\n    image = tfio.image.decode_dicom_image(\n        image_bytes, \n        dtype = tf.uint16\n    )\n    \n    image = tf.squeeze(image, axis = 0)\n    \n    image = tf.image.resize(\n        image, \n        (500, 500), \n        preserve_aspect_ratio = True\n    )\n    \n    image = image - tf.reduce_min(image)\n    image = image / tf.reduce_max(image)\n    image = tf.cast(image * 255, tf.uint8)\n    \n    return image","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"## Visualization"},{"metadata":{},"cell_type":"markdown","source":"### Original"},{"metadata":{"trusted":true},"cell_type":"code","source":"fpath = \"../input/vinbigdata-chest-xray-abnormalities-detection/train/000434271f63a053c4128a0ba6352c7f.dicom\"\nimage = read_dicom(fpath)\n\nplt.figure(figsize = (18, 4))\n\nplt.subplot(1, 2, 1)\nplt.axis(\"off\")\nplt.title(\"Original\")\nplt.imshow(image, cmap = \"gray\");\n\nplt.subplot(1, 2, 2)\nhist, bins = np.histogram(image.numpy().flatten(), 256, [0, 256])\ncdf = hist.cumsum()\ncdf_normalized = cdf * float(hist.max()) / cdf.max()\nplt.plot(cdf_normalized, color = 'b')\n\nplt.hist(image.numpy().flatten(), 256, [0, 256], color = 'r')\nplt.xlim([0, 256])\nplt.legend(('cdf', 'histogram'), loc = 'upper left')\nplt.title(\"Original\")\nplt.show();","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"### Histogram Equalization\nThis method usually increases the global contrast of images. This is done by redistributing pixel intensity."},{"metadata":{"trusted":true},"cell_type":"code","source":"plt.figure(figsize = (18, 4))\n\nplt.subplot(1, 2, 1)\nplt.axis(\"off\")\nplt.title(\"Histogram Equalization\")\n\nimg_he = cv2.equalizeHist(image.numpy())\n\nplt.imshow(img_he, cmap = \"gray\");\n\nplt.subplot(1, 2, 2)\nhist, bins = np.histogram(img_he.flatten(), 256, [0, 256])\ncdf = hist.cumsum()\ncdf_normalized = cdf * float(hist.max()) / cdf.max()\nplt.plot(cdf_normalized, color = 'b')\n\nplt.hist(img_he.flatten(), 256, [0, 256], color = 'r')\nplt.xlim([0, 256])\nplt.legend(('cdf', 'histogram'), loc = 'upper left')\nplt.title(\"Histogram Equalization\")\nplt.show();","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"A disadvantage of the method is that it is indiscriminate. It may increase the contrast of background noise."},{"metadata":{},"cell_type":"markdown","source":"### Contrast Limited Adaptive Histogram Equalization\nTechnique to improve the contrast of an image. It differs from ordinary histogram equalization in the respect that the adaptive method computes several histograms, each corresponding to a distinct section of the image, and uses them to redistribute the lightness values of the image. Additionally, it prevents over amplification of noise."},{"metadata":{"trusted":true},"cell_type":"code","source":"plt.figure(figsize = (18, 4))\n\nplt.subplot(1, 2, 1)\nplt.axis(\"off\")\nplt.title(\"Contrast Limited Adaptive Histogram Equalization\")\n\nclahe = cv2.createCLAHE(\n    clipLimit = 2., \n    tileGridSize = (10, 10)\n)\n    \nimg_clahe = clahe.apply(image.numpy()) \n\nplt.imshow(img_clahe, cmap = \"gray\");\n\nplt.subplot(1, 2, 2)\nhist, bins = np.histogram(img_clahe.flatten(), 256, [0, 256])\ncdf = hist.cumsum()\ncdf_normalized = cdf * float(hist.max()) / cdf.max()\nplt.plot(cdf_normalized, color = 'b')\n\nplt.hist(img_clahe.flatten(), 256, [0, 256], color = 'r')\nplt.xlim([0, 256])\nplt.legend(('cdf', 'histogram'), loc = 'upper left')\nplt.title(\"Contrast Limited Adaptive Histogram Equalization\")\nplt.show();","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"### Summary"},{"metadata":{"trusted":true},"cell_type":"code","source":"plt.figure(figsize = (18, 4))\n\nplt.subplot(1, 3, 1)\nplt.axis(\"off\")\nplt.title(\"Original\")\nplt.imshow(image, cmap = \"gray\");\n\nplt.subplot(1, 3, 2)\nplt.axis(\"off\")\nplt.title(\"Histogram Equalization\")\nplt.imshow(img_he, cmap = \"gray\");\n\nplt.subplot(1, 3, 3)\nplt.axis(\"off\")\nplt.title(\"Contrast Limited Adaptive Histogram Equalization\")\nplt.imshow(img_clahe, cmap = \"gray\");","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":4}