{"cells":[{"metadata":{},"cell_type":"markdown","source":"## Import Libraries","execution_count":null},{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"cell_type":"code","source":"import os\nimport numpy as np\nimport cv2\nimport matplotlib.pyplot as plt\nimport pandas as pd\n\nimport seaborn as sns\nsns.set(style=\"whitegrid\")","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"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']\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"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()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"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","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"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()","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"## Time taken by one image (CPU)","execution_count":null},{"metadata":{"trusted":true},"cell_type":"code","source":"image = cv2.imread(BASE_PATH + '/jpeg/train/' + 'ISIC_0109869' + '.jpg')\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"%%time\n## for 256,256 image\nimage_resize = cv2.resize(image,(256,256))\nfinal_image = hair_remove(image_resize)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"%%time\n## for 512,512 image\nimage_resize = cv2.resize(image,(512,512))\nfinal_image = hair_remove(image_resize)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"%%time\n## for 1024,1024 image\nimage_resize = cv2.resize(image,(1024,1024))\nfinal_image = hair_remove(image_resize)","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}