{"cells":[{"cell_type":"markdown","metadata":{"_cell_guid":"ccc128d6-b369-cfbc-8a83-d49a138cc912"},"source":"Trying superpixels"},{"cell_type":"code","execution_count":null,"metadata":{"_cell_guid":"fb67719e-6367-0dac-9c81-85acc797b305"},"outputs":[],"source":"import matplotlib.pyplot as plt\n%matplotlib inline\nimport numpy as np\nimport pandas as pd\nimport cv2\nimport math\nfrom sklearn import mixture\nfrom sklearn.utils import shuffle\nfrom skimage import measure\nfrom glob import glob\nimport os\nfrom skimage.segmentation import slic\nfrom skimage.util import img_as_float\nfrom skimage.segmentation import mark_boundaries\nfrom skimage import io\n\n\nfrom subprocess import check_output\nprint(check_output([\"ls\", \"../input\"]).decode(\"utf8\"))\n\nTRAIN_DATA = \"../input/train\"\ntype_1_files = glob(os.path.join(TRAIN_DATA, \"Type_1\", \"*.jpg\"))\ntype_1_ids = np.array([s[len(os.path.join(TRAIN_DATA, \"Type_1\"))+1:-4] for s in type_1_files])\ntype_2_files = glob(os.path.join(TRAIN_DATA, \"Type_2\", \"*.jpg\"))\ntype_2_ids = np.array([s[len(os.path.join(TRAIN_DATA, \"Type_2\"))+1:-4] for s in type_2_files])\ntype_3_files = glob(os.path.join(TRAIN_DATA, \"Type_3\", \"*.jpg\"))\ntype_3_ids = np.array([s[len(os.path.join(TRAIN_DATA, \"Type_3\"))+1:-4] for s in type_3_files])\n\ntype_1_ids = type_1_ids[:30]\n\ndef get_filename(image_id, image_type):\n    \"\"\"\n    Method to get image file path from its id and type   \n    \"\"\"\n    if image_type == \"Type_1\" or \\\n        image_type == \"Type_2\" or \\\n        image_type == \"Type_3\":\n        data_path = os.path.join(TRAIN_DATA, image_type)\n    elif image_type == \"Test\":\n        data_path = TEST_DATA\n    elif image_type == \"AType_1\" or \\\n          image_type == \"AType_2\" or \\\n          image_type == \"AType_3\":\n        data_path = os.path.join(ADDITIONAL_DATA, image_type)\n    else:\n        raise Exception(\"Image type '%s' is not recognized\" % image_type)\n\n    ext = 'jpg'\n    return os.path.join(data_path, \"{}.{}\".format(image_id, ext))\n\ndef get_image_data(image_id, image_type):\n    \"\"\"\n    Method to get image data as np.array specifying image id and type\n    \"\"\"\n    fname = get_filename(image_id, image_type)\n    img = cv2.imread(fname)\n    assert img is not None, \"Failed to read image : %s, %s\" % (image_id, image_type)\n    img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)\n    return img"},{"cell_type":"code","execution_count":null,"metadata":{"_cell_guid":"f427b590-cfbc-a307-09ef-4ca41df3a8b1"},"outputs":[],"source":"for k, type_ids in enumerate([type_1_ids]):\n    print(\"Type_ids:\", type_ids)\n    print(\"k:\", k)\n    #m = len(type_ids)\n    m=10\n    train_ids = sorted(type_ids)\n    \n    \n    for i in range(m):                \n        image_id = train_ids[i] \n        #print(image_id)\n\n        img = get_image_data(image_id, 'Type_%i' % (k+1))\n        \n        if(img.shape[0] > img.shape[1]):\n            tile_size = (int(img.shape[1]*256/img.shape[0]),256)\n        else:\n            tile_size = (256, int(img.shape[0]*256/img.shape[1]))\n\n        img = cv2.resize(img, dsize=tile_size)\n        \n        numSegments = 20\n        segments = slic(img, n_segments = numSegments, sigma = 5)\n        \n        \n        plt.subplot(121)\n        plt.imshow(img)\n        plt.subplot(122)\n        plt.imshow(mark_boundaries(img, segments))\n        plt.show()\n        "},{"cell_type":"markdown","metadata":{"_cell_guid":"db79632d-8e7d-1289-2761-1f2b742750bb"},"source":"To do: \n\n - Incorrect segmentation in cervix close-up pictures. Identify those...\n - Feed cropped pictures to a CNN"}],"metadata":{"_change_revision":0,"_is_fork":false,"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.6.0"}},"nbformat":4,"nbformat_minor":0}