{"cells":[{"metadata":{"_uuid":"64ec0db637e09add973c4c6e77c309b230fcf57f"},"cell_type":"markdown","source":"# Seam Carving to reduce image size - an alternative to resampling?\n\n\n## Introduction\nThe most challenging part of this competition for a hardware shy Kaggler is the sheer size of the image (512x512) and the fact that the features  cover only a couple of pixels. On the other hand, there are lot of voids in these images.\n\nSo the question is, can we get rid of the voids and reduce the image size. Seam Carving might be the answer.\n\n## Seam Carving\n\nSeam carving (or liquid rescaling) is an algorithm for content-aware image resizing ([Avidan & Shamir, 2007](https://perso.crans.org/frenoy/matlab2012/seamcarving.pdf)).\n\nHere, I show it with a working example from the training set."},{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"cell_type":"code","source":"import numpy as np \nimport pandas as pd\nfrom matplotlib import pyplot as plt\nfrom skimage.filters import sobel,gaussian\nfrom skimage.transform import seam_carve \nfrom keras.preprocessing.image import load_img","execution_count":null,"outputs":[]},{"metadata":{"_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","trusted":true},"cell_type":"code","source":"trainset = pd.read_csv(\"../input/train.csv\", index_col=\"Id\")","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"43d048fdbb523e13d0d672ff11569cc086a47780"},"cell_type":"code","source":"ix = 200\nfilters = ['green','blue','red','yellow']","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"19db27c45b30203f7fa8cbdbaeb3b5120b4812ff"},"cell_type":"code","source":"image = np.sum([np.array(load_img('../input/train/{}_{}.png'.format(trainset.index[ix],k)))[:,:,0]/255 for k in filters],axis=0)","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"b8f7e0eb5e35b8a60e6af164b61bb175fb3ea5fa"},"cell_type":"markdown","source":"A fast `seam_carve` function available in `skimage.transforms` only works for seams along one direction. So, we need to call it twice.\n\nI have used here a `sobel` of the gaussian smoothed image as the energy map.\n"},{"metadata":{"trusted":true,"_uuid":"9aaadbf284abe0973c277c074cf5015817210655"},"cell_type":"code","source":"eimage = sobel(gaussian(image,4))\nimageh = seam_carve(image, eimage,'horizontal',100)\neimageh = sobel(gaussian(imageh,4))\nfinimage = seam_carve(imageh, eimageh,'vertical',100)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"30da70a437ec9666827f4468698b82e3afa2023c"},"cell_type":"code","source":"plt.figure(figsize=(20,10))\nplt.subplot(121)\nplt.imshow(image)\nplt.subplot(122)\nplt.imshow(finimage)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"472d2914e79a6ba1dfb0cdb7632a92ecccbbf0db"},"cell_type":"markdown","source":"It seems to work on certain images in the training set like the one above. \nAlthough, if you look carefully, the features seems to be slightly cropped in certain parts.  \n\nBut, with a better idea, I hope it can be used for all the images.  I think this would be far better way of reducing the image size than resampling due to the fine structure of the features."},{"metadata":{"trusted":true,"_uuid":"0274665f245b3df88241cc9d17b37e3183ae748a"},"cell_type":"code","source":"","execution_count":null,"outputs":[]}],"metadata":{"kernelspec":{"display_name":"Python 3","language":"python","name":"python3"},"language_info":{"name":"python","version":"3.6.6","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"}},"nbformat":4,"nbformat_minor":1}