{"cells":[{"metadata":{},"cell_type":"markdown","source":"## <center> <span style=\"color:#aaa\"> Prostate cANcer graDe Assessment (PANDA) Challenge </span></center>\n\n\n# <center> Looking into the Effect of  </center>\n# <center>  <font size=\"6\"> <span style=\"color:#ffa7b6\"> IMAGE ENHANCEMENT </span> </font></center>   "},{"metadata":{},"cell_type":"markdown","source":"##  <span style=\"color:pink\">  In this notebook: </span>\n\n### 1. First, applied a contrast enhancement filter to remove gray areas in Radboud samples.\n\n### 2. Experimented with other filters:\n \n   a. Histogram equalization: did not work!\n\n   b. Unsharp masking: results are good!\n\n### <center> I will add more filters if Kagglers like this notebook. Please upvote if it is useful!"},{"metadata":{},"cell_type":"markdown","source":"##  <span style=\"color:pink\">  Import Packages </span>"},{"metadata":{"_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","trusted":true},"cell_type":"code","source":"import numpy as np\nimport pandas as pd\nimport matplotlib.pyplot as plt\nimport skimage.io\nfrom tqdm.notebook import tqdm\nimport random\nimport cv2\nplt.rcParams['figure.figsize'] = [15,8]","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"## <span style=\"color:pink\">  Data I/O"},{"metadata":{"_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","trusted":true},"cell_type":"code","source":"DATA_DIR = '/kaggle/input/prostate-cancer-grade-assessment/'\ndata = pd.read_csv(DATA_DIR + 'train.csv')\ndata_karolinska = data[data.data_provider==\"karolinska\"].reset_index().drop(columns=['index'])\ndata_radboud = data[data.data_provider==\"radboud\"].reset_index().drop(columns=['index'])","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"##  <span style=\"color:pink\">  Utilities"},{"metadata":{"trusted":true},"cell_type":"code","source":"def show_grid(dataframe):\n    N = len(dataframe)\n    for i in range(3):\n        for j in range(3):\n            plt.subplot(3,3,j+3*i+1)\n            img = skimage.io.MultiImage(DATA_DIR + 'train_images/' + dataframe.image_id[int(N*random.random())] + '.tiff')\n            plt.imshow(img[2])\n            plt.axis('off')","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"##  <span style=\"color:pink\">  Let's Look at Radboud and Karolinska Samples </span>\n\nWe randomly sample ```Radboud``` and ```Karolinska``` data for visualization.\n\n- If you look carefully, you will notice that in the ```Radboud``` samples, there is a distinct gray thick boundary around the tissue border due to transparent slides used in the experiment. \n\n- This is not visible as predominently in the ```Karolinska``` data.\n\n### <span style=\"color:orange\"> This region can cause issue in segmenting forground from the background! </span>"},{"metadata":{"trusted":true},"cell_type":"code","source":"print('Radboud Samples')\nshow_grid(data_radboud)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"print('Karolinska Samples')\nshow_grid(data_karolinska)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"---\n##  Filter 1: <span style=\"color:pink\">  Removing Gray Borders in Radboud Samples using </span> <span style=\"color:orange\">Contrast Enhancement </span>\n---\n\nFor contrast enhancement we will use OpenCV function ```addWeighted``` for this purpose.\n\n```\nimg_enhanced = cv2.addWeighted( img, contrast, img, 0, brightness)\n```\n\nThe same enhancement can also be safely applied to ```Karolinska``` samples, as I observed from my limited experiments. See the following figures for comparison.\n\n#### Reference: https://stackoverflow.com/questions/39308030/how-do-i-increase-the-contrast-of-an-image-in-python-opencv\n"},{"metadata":{"trusted":true},"cell_type":"code","source":"def enhance_image(image, contrast=1, brightness=15):\n    \"\"\"\n    Enhance constrast and brightness of images\n    \"\"\"\n    img_enhanced = cv2.addWeighted(image, contrast, image, 0, brightness)\n    return img_enhanced\n\nfactor = 4 \nchannel = 1# using 2nd channel\nimg = skimage.io.MultiImage(DATA_DIR + 'train_images/' + data_radboud.image_id[50] + '.tiff')[channel][factor*0:factor*300,factor*0:factor*300]\nenhnaced_img = enhance_image(img)\nimg_concat = np.concatenate((img, enhnaced_img), axis=1)\nplt.imshow(img_concat);plt.axis('off');\nplt.title('A RADBOUD SAMPLE \\n\\n [Left] Before Image Enhancement, [Right] After Image Enhancement')\nplt.show();\n\nimg = skimage.io.MultiImage(DATA_DIR + 'train_images/' + data_karolinska.image_id[50] + '.tiff')[channel][factor*0:factor*1300,factor*0:factor*1300]\nenhnaced_img = enhance_image(img)\nimg_concat = np.concatenate((img, enhnaced_img), axis=1)\nplt.imshow(img_concat);plt.axis('off');\nplt.title('A KAROLINSKA SAMPLE \\n\\n [Left] Before Image Enhancement, [Right] After Image Enhancement')\nplt.show();","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"#  <span style=\"color:#aaa\">  Further Enhancements</span>"},{"metadata":{},"cell_type":"markdown","source":"---\n## Filter 2: <span style=\"color:pink\"> Histogram Equalization </span>\n---\n\nNote: We are applying histogram equalization to already contrast enhanced images.\n\n<span style=\"color:orange\"> The result is not good. So we cannot use it for this competitions. Please let me know if you made it work, I will be happy to update here! </span>\n\nWe can use opencv ```equalizeHist``` function."},{"metadata":{"trusted":true},"cell_type":"code","source":"def RGB_histogram_equalization(img):\n    \"\"\" Histogram Equalization of 3-Channel images\"\"\"\n    equalized_image1 = cv2.equalizeHist(img[:,:,0])\n    equalized_image2 = cv2.equalizeHist(img[:,:,1])\n    equalized_image3 = cv2.equalizeHist(img[:,:,2])\n    return cv2.merge((equalized_image1,equalized_image2,equalized_image3))\n\nfactor = 4 \nchannel = 1# using 2nd channel\nimg = skimage.io.MultiImage(DATA_DIR + 'train_images/' + data_radboud.image_id[50] + '.tiff')[channel][factor*0:factor*300,factor*0:factor*300]\nimg = enhance_image(img)\nequalized_image = RGB_histogram_equalization(img)\nimg_concat = np.concatenate((img, equalized_image),axis=1)\nplt.imshow(img_concat);plt.axis('off');\nplt.title('A RADBOUD SAMPLE \\n\\n [Left] Before Histogram Equalization, [Right] After Histogram Equalization')\nplt.show();\n\nimg = skimage.io.MultiImage(DATA_DIR + 'train_images/' + data_karolinska.image_id[50] + '.tiff')[1][factor*0:factor*1300,factor*0:factor*1300]\nimg = enhance_image(img)\nequalized_image = RGB_histogram_equalization(img)\nimg_concat = np.concatenate((img, equalized_image),axis=1)\nplt.imshow(img_concat);plt.axis('off');\nplt.title('A KAROLINSKA SAMPLE \\n\\n [Left] Before Histogram Equalization, [Right] After Histogram Equalization')\nplt.show();","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"---\n## Filter 3: <span style=\"color:pink\"> Unsharp Masking </span>\n---\n\nUnsharp masking enhances the high frequency details. This can be implemented using a gaussian filter and subtracting it from the original image.\n\n### <span style=\"color:orange\"> You can see that this filter enhance the details! </span>"},{"metadata":{"trusted":true},"cell_type":"code","source":"def unsharp_masking(img):\n    \"\"\" Unsharp masking of an RGB image\"\"\"\n    img_gaussian = cv2.GaussianBlur(img, (21,21), 10.0)\n    return cv2.addWeighted(img, 1.8, img_gaussian, -0.8, 0, img)\n\nfactor = 4\nimg = skimage.io.MultiImage(DATA_DIR + 'train_images/' + data_radboud.image_id[500] + '.tiff')[1][factor*150:factor*250,factor*150:factor*250]\nimg = enhance_image(img)\nunsharp_image = unsharp_masking(img.copy())\nimg_concat = np.concatenate((img, unsharp_image),axis=1)\nplt.imshow(img_concat);plt.axis('off');\nplt.title('A RADBOUD SAMPLE \\n\\n [Left] Before Unsharp Masking, [Right] After Unsharp Masking')\nplt.show();\n\nimg = skimage.io.MultiImage(DATA_DIR + 'train_images/' + data_karolinska.image_id[500] + '.tiff')[1][factor*600:factor*1000,factor*600:factor*1000]\nimg = enhance_image(img)\nunsharp_image = unsharp_masking(img.copy())\nimg_concat = np.concatenate((img, unsharp_image),axis=1)\nplt.imshow(img_concat);plt.axis('off');\nplt.title('A KAROLINSKA SAMPLE \\n\\n [Left] Before Unsharp Masking, [Right] Unsharp Masking')\nplt.show();","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# <span style=\"color:orange\"> More filters coming if Kagglers show interest!\n  "}],"metadata":{"kernelspec":{"display_name":"Python 3","language":"python","name":"python3"},"language_info":{"codemirror_mode":{"name":"ipython","version":3},"file_extension":".py","mimetype":"text/x-python","name":"python","nbconvert_exporter":"python","pygments_lexer":"ipython3","version":"3.7.6"}},"nbformat":4,"nbformat_minor":4}