{"cells":[{"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\n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n\n# Input data files are available in the read-only \"../input/\" directory\n# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\n\nimport os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))\n        break\n        \n\n# You can write up to 5GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using \"Save & Run All\" \n# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","trusted":true},"cell_type":"code","source":"import cv2\nimport matplotlib.pyplot as plt\n%matplotlib inline","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"import pandas as pd","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"trainpath = '/kaggle/input/siim-isic-melanoma-classification/train.csv'\ntraindf = pd.read_csv(trainpath)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"traindf.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"traindf['age_approx'].describe()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"JPEG_PATH = '/kaggle/input/siim-isic-melanoma-classification/jpeg/'\nDICOM_PATH = '/kaggle/input/siim-isic-melanoma-classification/'","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"traindf['target'].plot(kind='hist')\nplt.xlabel(\"Category\")\nplt.ylabel(\"Images\")","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"traindf['target'].value_counts()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"traindf['benign_malignant'].value_counts()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"malignDF = traindf[traindf['target'] == 1]\nmalignDF.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"import pydicom as dicom\nimport imageio\nimport matplotlib.pylab as plt\n\n\nmalignImagPath = os.path.join(DICOM_PATH+'train/', 'ISIC_0149568.dcm')\nmalignImagPath2 = os.path.join(DICOM_PATH+'train/', 'ISIC_0188432.dcm')\nmalignImagPath3 = os.path.join(DICOM_PATH+'train/', 'ISIC_0207268.dcm')\nmalignImagPath4 = os.path.join(DICOM_PATH+'train/', 'ISIC_0247330.dcm')\n\nds = dicom.dcmread(malignImagPath)\nds2 = dicom.dcmread(malignImagPath2)\nds3 = dicom.dcmread(malignImagPath3)\nds4 = dicom.dcmread(malignImagPath4)\n\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"\nbenignImagPath = os.path.join(DICOM_PATH+'train/', 'ISIC_2637011.dcm')\nbenignImagPath2 = os.path.join(DICOM_PATH+'train/', 'ISIC_0015719.dcm')\nbenignImagPath3 = os.path.join(DICOM_PATH+'train/', 'ISIC_0068279.dcm')\nbenignImagPath4 = os.path.join(DICOM_PATH+'train/', 'ISIC_0074268.dcm')\n\nbs = dicom.dcmread(benignImagPath)\nbs2 = dicom.dcmread(benignImagPath2)\nbs3 = dicom.dcmread(benignImagPath3)\nbs4 = dicom.dcmread(benignImagPath4)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"\nf, (plot1, plot2 ) = plt.subplots(1, 2)\nplot1.imshow(bs.pixel_array)\nplot2.imshow(bs2.pixel_array)\n\n\nf, (plot1, plot2 ) = plt.subplots(1, 2)\nplot1.imshow(bs3.pixel_array)\nplot2.imshow(bs4.pixel_array)\nplt.title('Normal Images')\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"\nf, (plot1, plot2 ) = plt.subplots(1, 2)\nplot1.imshow(ds.pixel_array)\nplot2.imshow(ds2.pixel_array)\n\n\nf, (plot1, plot2 ) = plt.subplots(1, 2)\nplot1.imshow(ds3.pixel_array)\nplot2.imshow(ds4.pixel_array)\nplt.title('AbNormal Images')\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"ds.pixel_array.shape","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"ds.pixel_array.min()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"ds.pixel_array.max()","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"Lets visualize it into RGB","execution_count":null},{"metadata":{"trusted":true},"cell_type":"code","source":"from pydicom.pixel_data_handlers.util import convert_color_space ","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"rgbNormal = convert_color_space(bs.pixel_array, 'YBR_FULL_422', 'RGB')\nrgbNormal2 = convert_color_space(bs2.pixel_array, 'YBR_FULL_422', 'RGB')\nrgbNormal3 = convert_color_space(bs3.pixel_array, 'YBR_FULL_422', 'RGB')\nrgbNormal4 = convert_color_space(bs4.pixel_array, 'YBR_FULL_422', 'RGB')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"f, (plot1, plot2 ) = plt.subplots(1, 2)\nplot1.imshow(rgbNormal)\nplot2.imshow(rgbNormal2)\n\n\nf, (plot1, plot2 ) = plt.subplots(1, 2)\nplot1.imshow(rgbNormal3)\nplot2.imshow(rgbNormal4)\nplt.title('Normal Images')\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"rgbAbNormal = convert_color_space(ds.pixel_array, 'YBR_FULL_422', 'RGB')\nrgbAbNormal2 = convert_color_space(ds2.pixel_array, 'YBR_FULL_422', 'RGB')\nrgbAbNormal3 = convert_color_space(ds3.pixel_array, 'YBR_FULL_422', 'RGB')\nrgbAbNormal4 = convert_color_space(ds4.pixel_array, 'YBR_FULL_422', 'RGB')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"f, (plot1, plot2 ) = plt.subplots(1, 2)\nplot1.imshow(rgbAbNormal)\nplot2.imshow(rgbAbNormal2)\n\n\nf, (plot1, plot2 ) = plt.subplots(1, 2)\nplot1.imshow(rgbAbNormal3)\nplot2.imshow(rgbAbNormal4)\nplt.title('AbNormal Images')\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"orig_normal = [bs.pixel_array, bs2.pixel_array, bs3.pixel_array, bs4.pixel_array]\norig_abnormal = [ds.pixel_array, ds2.pixel_array, ds3.pixel_array, ds4.pixel_array]\nrgbnormal_list = [rgbNormal, rgbNormal2, rgbNormal3, rgbNormal4]\nrgbabnormal_list = [rgbAbNormal, rgbAbNormal2, rgbAbNormal3, rgbAbNormal4]","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"Coverting RGB into gray","execution_count":null},{"metadata":{"trusted":true},"cell_type":"code","source":"gray_normal = list(map(lambda image: cv2.cvtColor(image,cv2.COLOR_RGB2GRAY), rgbnormal_list))\ngray_abnormal = list(map(lambda image: cv2.cvtColor(image,cv2.COLOR_RGB2GRAY), rgbabnormal_list))\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"f, (plot1, plot2 ) = plt.subplots(1, 2)\nplot1.imshow(gray_normal[0])\nplot2.imshow(gray_normal[1])\n\nf, (plot1, plot2 ) = plt.subplots(1, 2)\nplot1.imshow(gray_normal[2])\nplot2.imshow(gray_normal[3])\nplt.title('Normal Images')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"f, (plot1, plot2 ) = plt.subplots(1, 2)\nplot1.imshow(gray_abnormal[0])\nplot2.imshow(gray_abnormal[1])\n\nf, (plot1, plot2 ) = plt.subplots(1, 2)\nplot1.imshow(gray_abnormal[2])\nplot2.imshow(gray_abnormal[3])\nplt.title('AbNormal Images')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"markdown","source":"### Histograms\n\n- Histograms: count number of pixels at each intensity value.\n- Implemented in scipy.ndimage \n  - higher-dimensional arrays \n  - masked data\n- Advanced techniques and functionality in scikit-image\n\nPlotting the **histogram** of normal/abnormal images****","execution_count":null},{"metadata":{"trusted":true},"cell_type":"code","source":"import scipy.ndimage as ndi\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"columns = 3\n# rows = len(os.listdir(DATA)) - 10\nrows = 4\nfor i in range(len(rgbnormal_list)):    \n    #histograms\n    hist_original = ndi.histogram(orig_normal[i], min=0, max=255, bins=256)\n    hist_rgb = ndi.histogram(rgbnormal_list[i], min=0, max=255, bins=256)\n    hist_gray = ndi.histogram(gray_normal[i], min=0, max=255, bins=256)\n    \n    \n    fig=plt.figure(figsize=(16, 16))\n    fig.add_subplot(rows, columns, i+1)    \n    plt.plot(hist_original)\n    \n    fig.add_subplot(rows, columns, i+1)   \n    plt.plot(hist_rgb)\n    \n    fig.add_subplot(rows, columns, i+1)  \n    plt.plot(hist_gray)\n    plt.title(\"Normal \"+str(i))\n\nplt.show()\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"columns = 3\n# rows = len(os.listdir(DATA)) - 10\nrows = 4\nfor i in range(len(rgbabnormal_list)):    \n    #histograms\n    hist_original = ndi.histogram(orig_abnormal[i], min=0, max=255, bins=256)\n    hist_rgb = ndi.histogram(rgbabnormal_list[i], min=0, max=255, bins=256)\n    hist_gray = ndi.histogram(gray_abnormal[i], min=0, max=255, bins=256)\n    \n    \n    fig=plt.figure(figsize=(16, 16))\n    fig.add_subplot(rows, columns, i+1)    \n    plt.plot(hist_original)\n    \n    fig.add_subplot(rows, columns, i+1)   \n    plt.plot(hist_rgb)\n    \n    fig.add_subplot(rows, columns, i+1)  \n    plt.plot(hist_gray)\n    plt.title(\"AbNormal \"+str(i))\n\nplt.show()\n","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"### Equalization\n\n- Distributions often skewed toward low intensities (background values).\n- **Equalization**: redistribute values to optimize full intensity range\n- **Cumulative distribution function**: (CDF) shows proportion of pixels in range.","execution_count":null},{"metadata":{"trusted":true},"cell_type":"code","source":"normal = [bs.pixel_array, bs2.pixel_array, bs3.pixel_array, bs4.pixel_array]\nabnormal = [ds.pixel_array, ds2.pixel_array, ds3.pixel_array, ds4.pixel_array]\nnormal_hists = [histNormal, histNormal2, histNormal3, histNormal4]\nabnormal_hists = [histAbNormal, histAbNormal2, histAbNormal3, histAbNormal4]","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"Plotting the before-aftert images after applying equalizer","execution_count":null},{"metadata":{},"cell_type":"markdown","source":"Equalizer on normal images","execution_count":null},{"metadata":{"trusted":true},"cell_type":"code","source":"for i in range(len(normal)):\n    im = normal[i]\n    hist = normal_hists[i]\n    cdf = hist.cumsum() / hist.sum()\n    im_equalized = cdf[im] * 255\n    fig, axes = plt.subplots(1, 2)\n    axes[0].imshow(im)\n    axes[1].imshow(im_equalized)\n    plt.show()","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"Equalizer on abnormal images","execution_count":null},{"metadata":{"trusted":true},"cell_type":"code","source":"for i in range(len(abnormal)):\n    im = abnormal[i]\n    hist = abnormal_hists[i]\n    cdf = hist.cumsum() / hist.sum()\n    im_equalized = cdf[im] * 255\n    fig, axes = plt.subplots(1, 2)\n    axes[0].imshow(im)\n    axes[1].imshow(im_equalized)\n    plt.show()","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}