{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.7.6","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"none","dataSources":[{"sourceId":20270,"databundleVersionId":1222630,"sourceType":"competition"}],"dockerImageVersionId":29974,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"## Import Libraries","metadata":{}},{"cell_type":"code","source":"import os\nimport numpy as np\nimport cv2\nimport matplotlib.pyplot as plt\nimport pandas as pd","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2024-01-26T11:19:49.474250Z","iopub.execute_input":"2024-01-26T11:19:49.474578Z","iopub.status.idle":"2024-01-26T11:19:49.720538Z","shell.execute_reply.started":"2024-01-26T11:19:49.474545Z","shell.execute_reply":"2024-01-26T11:19:49.719288Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"BASE_PATH = '../input/siim-isic-melanoma-classification'\nhair_images =['ISIC_0078712','ISIC_0080817','ISIC_0082348','ISIC_0109869','ISIC_0155012','ISIC_0159568','ISIC_0164145','ISIC_0194550','ISIC_0194914','ISIC_0202023']\nwithout_hair_images = ['ISIC_0015719','ISIC_0074268','ISIC_0075914','ISIC_0084395','ISIC_0085718','ISIC_0081956']","metadata":{"execution":{"iopub.status.busy":"2024-01-26T11:19:49.722445Z","iopub.execute_input":"2024-01-26T11:19:49.722804Z","iopub.status.idle":"2024-01-26T11:19:49.727162Z","shell.execute_reply.started":"2024-01-26T11:19:49.722771Z","shell.execute_reply":"2024-01-26T11:19:49.726576Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"l = len(hair_images[:8])\n\nfig = plt.figure(figsize=(20,30))\n\nfor i,image_name in enumerate(hair_images[:8]):\n    \n    \n    image = cv2.imread(BASE_PATH + '/jpeg/train/' + image_name + '.jpg')\n    image_resize = cv2.resize(image,(1024,1024))\n    plt.subplot(l, 5, (i*5)+1)\n    # Convert the original image to grayscale\n    plt.imshow(cv2.cvtColor(image_resize, cv2.COLOR_BGR2RGB))\n    plt.axis('off')\n    plt.title('Original : '+ image_name)\n    \n    grayScale = cv2.cvtColor(image_resize, cv2.COLOR_RGB2GRAY)\n    plt.subplot(l, 5, (i*5)+2)\n    plt.imshow(grayScale)\n    plt.axis('off')\n    plt.title('GrayScale : '+ image_name)\n    \n    # Kernel for the morphological filtering\n    kernel = cv2.getStructuringElement(1,(17,17))\n    \n    # Perform the blackHat filtering on the grayscale image to find the hair countours\n    blackhat = cv2.morphologyEx(grayScale, cv2.MORPH_BLACKHAT, kernel)\n    plt.subplot(l, 5, (i*5)+3)\n    plt.imshow(blackhat)\n    plt.axis('off')\n    plt.title('blackhat : '+ image_name)\n    \n    # intensify the hair countours in preparation for the inpainting \n    ret,threshold = cv2.threshold(blackhat,10,255,cv2.THRESH_BINARY)\n    plt.subplot(l, 5, (i*5)+4)\n    plt.imshow(threshold)\n    plt.axis('off')\n    plt.title('threshold : '+ image_name)\n    \n    # inpaint the original image depending on the mask\n    final_image = cv2.inpaint(image_resize,threshold,1,cv2.INPAINT_TELEA)\n    plt.subplot(l, 5, (i*5)+5)\n    plt.imshow(cv2.cvtColor(final_image, cv2.COLOR_BGR2RGB))\n    plt.axis('off')\n    plt.title('final_image : '+ image_name)\n       \nplt.plot()","metadata":{"execution":{"iopub.status.busy":"2024-01-26T11:19:49.727976Z","iopub.execute_input":"2024-01-26T11:19:49.728317Z","iopub.status.idle":"2024-01-26T11:21:31.780741Z","shell.execute_reply.started":"2024-01-26T11:19:49.728294Z","shell.execute_reply":"2024-01-26T11:21:31.777788Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def hair_remove(image):\n    # convert image to grayScale\n    grayScale = cv2.cvtColor(image, cv2.COLOR_RGB2GRAY)\n    \n    # kernel for morphologyEx\n    kernel = cv2.getStructuringElement(1,(17,17))\n    \n    # apply MORPH_BLACKHAT to grayScale image\n    blackhat = cv2.morphologyEx(grayScale, cv2.MORPH_BLACKHAT, kernel)\n    \n    # apply thresholding to blackhat\n    _,threshold = cv2.threshold(blackhat,10,255,cv2.THRESH_BINARY)\n    \n    # inpaint with original image and threshold image\n    final_image = cv2.inpaint(image,threshold,1,cv2.INPAINT_TELEA)\n    \n    return final_image","metadata":{"execution":{"iopub.status.busy":"2024-01-26T11:21:31.782278Z","iopub.execute_input":"2024-01-26T11:21:31.782579Z","iopub.status.idle":"2024-01-26T11:21:31.789012Z","shell.execute_reply.started":"2024-01-26T11:21:31.782548Z","shell.execute_reply":"2024-01-26T11:21:31.787873Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for i,image_name in enumerate(hair_images):\n    \n    fig = plt.figure(figsize=(10,5))\n    \n    image = cv2.imread(BASE_PATH + '/jpeg/train/' + image_name + '.jpg')\n    image_resize = cv2.resize(image,(1024,1024))\n    plt.subplot(1, 2, 1)\n    plt.imshow(cv2.cvtColor(image_resize, cv2.COLOR_BGR2RGB))\n    plt.axis('off')\n    plt.title('Original : '+ image_name)\n    \n    final_image = hair_remove(image_resize)\n    plt.subplot(1, 2, 2)\n    plt.imshow(cv2.cvtColor(final_image, cv2.COLOR_BGR2RGB))\n    plt.axis('off')\n    plt.title('Hair Removed : '+ image_name)\n    \n    plt.plot()","metadata":{"execution":{"iopub.status.busy":"2024-01-26T11:21:31.791013Z","iopub.execute_input":"2024-01-26T11:21:31.791446Z","iopub.status.idle":"2024-01-26T11:23:55.206166Z","shell.execute_reply.started":"2024-01-26T11:21:31.791407Z","shell.execute_reply":"2024-01-26T11:23:55.204919Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Time taken by one image (CPU)","metadata":{}},{"cell_type":"code","source":"image = cv2.imread(BASE_PATH + '/jpeg/train/' + 'ISIC_0109869' + '.jpg')\n","metadata":{"execution":{"iopub.status.busy":"2024-01-26T11:23:55.208062Z","iopub.execute_input":"2024-01-26T11:23:55.208341Z","iopub.status.idle":"2024-01-26T11:23:55.456546Z","shell.execute_reply.started":"2024-01-26T11:23:55.208313Z","shell.execute_reply":"2024-01-26T11:23:55.455958Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%%time\n## for 256,256 image\nimage_resize = cv2.resize(image,(256,256))\nfinal_image = hair_remove(image_resize)","metadata":{"execution":{"iopub.status.busy":"2024-01-26T11:23:55.457478Z","iopub.execute_input":"2024-01-26T11:23:55.457722Z","iopub.status.idle":"2024-01-26T11:23:55.476577Z","shell.execute_reply.started":"2024-01-26T11:23:55.457677Z","shell.execute_reply":"2024-01-26T11:23:55.475414Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%%time\n## for 512,512 image\nimage_resize = cv2.resize(image,(512,512))\nfinal_image = hair_remove(image_resize)","metadata":{"execution":{"iopub.status.busy":"2024-01-26T11:23:55.477662Z","iopub.execute_input":"2024-01-26T11:23:55.477900Z","iopub.status.idle":"2024-01-26T11:23:55.632034Z","shell.execute_reply.started":"2024-01-26T11:23:55.477877Z","shell.execute_reply":"2024-01-26T11:23:55.630630Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%%time\n## for 1024,1024 image\nimage_resize = cv2.resize(image,(1024,1024))\nfinal_image = hair_remove(image_resize)","metadata":{"execution":{"iopub.status.busy":"2024-01-26T11:23:55.633314Z","iopub.execute_input":"2024-01-26T11:23:55.633659Z","iopub.status.idle":"2024-01-26T11:23:57.422924Z","shell.execute_reply.started":"2024-01-26T11:23:55.633622Z","shell.execute_reply":"2024-01-26T11:23:57.421391Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## apply this method on without hair Images","metadata":{}},{"cell_type":"code","source":"l = len(without_hair_images)\n\nfig = plt.figure(figsize=(20,30))\n\nfor i,image_name in enumerate(without_hair_images):\n    \n    \n    image = cv2.imread(BASE_PATH + '/jpeg/train/' + image_name + '.jpg')\n    image_resize = cv2.resize(image,(1024,1024))\n    plt.subplot(l, 5, (i*5)+1)\n    # Convert the original image to grayscale\n    plt.imshow(cv2.cvtColor(image_resize, cv2.COLOR_BGR2RGB))\n    plt.axis('off')\n    plt.title('Original : '+ image_name)\n    \n    grayScale = cv2.cvtColor(image_resize, cv2.COLOR_RGB2GRAY)\n    plt.subplot(l, 5, (i*5)+2)\n    plt.imshow(grayScale)\n    plt.axis('off')\n    plt.title('GrayScale : '+ image_name)\n    \n    # Kernel for the morphological filtering\n    kernel = cv2.getStructuringElement(1,(17,17))\n    \n    # Perform the blackHat filtering on the grayscale image to find the hair countours\n    blackhat = cv2.morphologyEx(grayScale, cv2.MORPH_BLACKHAT, kernel)\n    plt.subplot(l, 5, (i*5)+3)\n    plt.imshow(blackhat)\n    plt.axis('off')\n    plt.title('blackhat : '+ image_name)\n    \n    # intensify the hair countours in preparation for the inpainting \n    ret,threshold = cv2.threshold(blackhat,10,255,cv2.THRESH_BINARY)\n    plt.subplot(l, 5, (i*5)+4)\n    plt.imshow(threshold)\n    plt.axis('off')\n    plt.title('threshold : '+ image_name)\n    \n    # inpaint the original image depending on the mask\n    final_image = cv2.inpaint(image_resize,threshold,1,cv2.INPAINT_TELEA)\n    plt.subplot(l, 5, (i*5)+5)\n    plt.imshow(cv2.cvtColor(final_image, cv2.COLOR_BGR2RGB))\n    plt.axis('off')\n    plt.title('final_image : '+ image_name)\n       \nplt.plot()","metadata":{"execution":{"iopub.status.busy":"2024-01-26T11:23:57.424295Z","iopub.execute_input":"2024-01-26T11:23:57.424579Z","iopub.status.idle":"2024-01-26T11:25:16.904078Z","shell.execute_reply.started":"2024-01-26T11:23:57.424550Z","shell.execute_reply":"2024-01-26T11:25:16.903006Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for i,image_name in enumerate(without_hair_images):\n    \n    fig = plt.figure(figsize=(10,5))\n    \n    image = cv2.imread(BASE_PATH + '/jpeg/train/' + image_name + '.jpg')\n    image_resize = cv2.resize(image,(1024,1024))\n    plt.subplot(1, 2, 1)\n    plt.imshow(cv2.cvtColor(image_resize, cv2.COLOR_BGR2RGB))\n    plt.axis('off')\n    plt.title('Original : '+ image_name)\n    \n    final_image = hair_remove(image_resize)\n    plt.subplot(1, 2, 2)\n    plt.imshow(cv2.cvtColor(final_image, cv2.COLOR_BGR2RGB))\n    plt.axis('off')\n    plt.title('Hair Removed : '+ image_name)\n    \n    plt.plot()","metadata":{"execution":{"iopub.status.busy":"2024-01-26T11:25:16.905290Z","iopub.execute_input":"2024-01-26T11:25:16.905553Z","iopub.status.idle":"2024-01-26T11:26:32.987823Z","shell.execute_reply.started":"2024-01-26T11:25:16.905525Z","shell.execute_reply":"2024-01-26T11:26:32.987209Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}