{"cells": [{"metadata": {"_uuid": "1c99403cccd52a588926ee3e4a28d3c8b45cea5a", "collapsed": false, "_execution_state": "idle"}, "outputs": [], "cell_type": "markdown", "source": "Sorry for the click-bait title, but I was exploring simple background removal to help the next stage of my ML process and I wanted to share the results since they look so cool!  It may even be immediately useful to someone.", "execution_count": null}, {"metadata": {"_uuid": "94b3e9a4c2036d556d52941cbc93957c0d68625f", "collapsed": false, "_execution_state": "idle"}, "outputs": [], "cell_type": "markdown", "source": "First we'll load all the image names into a list of ids", "execution_count": null}, {"metadata": {"_uuid": "887db523c4a36286516711a9af45d85d673406e0", "collapsed": false, "trusted": false, "_execution_state": "idle"}, "outputs": [], "cell_type": "code", "source": "import os\n\nTRAIN_DIR = '../input/train'\nX_SHAPE = (1280, 1918, 3)\n\nid_list = []\nfor root, subdirs, files in os.walk(TRAIN_DIR):\n    for file in files:\n        id_list.append(file.split('.')[0])\nid_list.sort()\nprint(\"Num of images: \" + str(len(id_list)))\n", "execution_count": 1}, {"metadata": {"_uuid": "674780bf80b133c08ca865daecb169dd388392a1", "collapsed": false, "_execution_state": "idle"}, "outputs": [], "cell_type": "markdown", "source": "So here is the mask generator.  Basically, it takes all the images in the same id group and then finds the minimum and maximum pixel intensity for every pixel on every channel.  It then returns the difference between the largest and smallest values, averaged over each the channels.  Theoretically, the only pixels we expect to be changing are the ones on the car, the rotating platform, and unfortunately some random noise and jpg artifacts.", "execution_count": null}, {"metadata": {"_uuid": "20a42e567b94287c2123b7d5bc97f70ef1ab5027", "collapsed": false, "trusted": false, "_execution_state": "idle"}, "outputs": [], "cell_type": "code", "source": "#Import relevant packages\nimport numpy as np\nimport matplotlib.pylab as plt\nimport cv2\n\ndef make_mask(id):\n    id = id.split('_')[0]\n    min_img = np.full(X_SHAPE, 255)\n    max_img = np.zeros(X_SHAPE)\n    for i in range(16):\n        if i < 9:\n            full_id = id + '_0' + str(i+1)\n        else:\n            full_id = id + '_' + str(i+1)\n\n        path = TRAIN_DIR + '/' + full_id + '.jpg'\n        img = cv2.imread(path)\n\n        min_img = np.minimum(min_img, img)\n        max_img = np.maximum(max_img, img)\n\n    return np.mean(max_img - min_img, axis=2)", "execution_count": 2}, {"metadata": {"_uuid": "4578d2633435a038f6a0562e42b8832bf314a7d5", "collapsed": false, "_execution_state": "idle"}, "outputs": [], "cell_type": "markdown", "source": "And now let's visualize some of the cars!", "execution_count": null}, {"metadata": {"_uuid": "57b3a559230e813499d4a5d0b2c886c953cff550", "collapsed": false, "_execution_state": "idle"}, "outputs": [], "cell_type": "code", "source": "\ndiff_mag = make_mask(id_list[0*16])\nplt.figure(figsize=(15,10))\nplt.imshow(diff_mag, cmap='gray', vmin=0, vmax=255)", "execution_count": 3}, {"metadata": {"_uuid": "1f8f45839e0d87aebee8247a4d1c81f3cf23e2ce", "collapsed": false, "_execution_state": "idle"}, "outputs": [], "cell_type": "code", "source": "diff_mag = make_mask(id_list[1*16])\nplt.figure(figsize=(15,10))\nplt.imshow(diff_mag, cmap='gray', vmin=0, vmax=255)", "execution_count": 4}, {"metadata": {"_uuid": "1194395b34ade565451d48885604f6c2864892a5", "collapsed": false, "_execution_state": "idle"}, "outputs": [], "cell_type": "code", "source": "diff_mag = make_mask(id_list[2*16])\nplt.figure(figsize=(15,10))\nplt.imshow(diff_mag, cmap='gray', vmin=0, vmax=255)", "execution_count": 5}, {"metadata": {"_uuid": "974b7f288dbdd1a7df29b806f695b742d0dc7827", "collapsed": false, "_execution_state": "idle"}, "outputs": [], "cell_type": "code", "source": "diff_mag = make_mask(id_list[3*16])\nplt.figure(figsize=(15,10))\nplt.imshow(diff_mag, cmap='gray', vmin=0, vmax=255)", "execution_count": 6}, {"metadata": {"_uuid": "a739491035ef221ce627161dc3eef5e883f97a0c", "collapsed": false, "_execution_state": "idle"}, "outputs": [], "cell_type": "code", "source": "diff_mag = make_mask(id_list[4*16])\nplt.figure(figsize=(15,10))\nplt.imshow(diff_mag, cmap='gray', vmin=0, vmax=255)", "execution_count": 7}, {"metadata": {"_uuid": "b78b22c3df594d02b7097a3b69574b4407aa3064", "collapsed": false, "_execution_state": "idle"}, "outputs": [], "cell_type": "code", "source": "diff_mag = make_mask(id_list[22*16])\nplt.figure(figsize=(15,10))\nplt.imshow(diff_mag, cmap='gray', vmin=0, vmax=255)", "execution_count": 11}, {"metadata": {"_uuid": "0178453b16e1f09116e5b3d01ba2a93a04b21420", "collapsed": false, "_execution_state": "idle"}, "outputs": [], "cell_type": "markdown", "source": "Really cool right?\n\nAnyway, hope that helps someone, if not, it's still fun to look at. :)", "execution_count": null}], "nbformat_minor": 0, "metadata": {"language_info": {"mimetype": "text/x-python", "nbconvert_exporter": "python", "codemirror_mode": {"version": 3, "name": "ipython"}, "file_extension": ".py", "name": "python", "pygments_lexer": "ipython3", "version": "3.6.1"}, "kernelspec": {"display_name": "Python 3", "language": "python", "name": "python3"}}, "nbformat": 4}