{"cells":[{"cell_type":"markdown","metadata":{"_cell_guid":"3740a841-3ae6-e1c5-e09c-20c8cf305b02"},"source":""},{"cell_type":"code","execution_count":null,"metadata":{"_cell_guid":"13914f63-f825-c319-346e-f7dca1aef15f"},"outputs":[],"source":"import matplotlib.pyplot as plt\nimport numpy as np\nimport pandas as pd\nimport cv2\n\ntarget_size = (400,400)\n\ndef show(img):\n    plt.imshow(img)\n    plt.show()\n\ndef get_img(path, color_space=cv2.COLOR_BGR2RGB):\n    img = cv2.imread(path)\n    if target_size:\n        img = cv2.resize(img, target_size)\n    img = cv2.cvtColor(img, color_space)\n    return img\n\ndef select_channel(img, ch):\n    return np.stack((img[:,:,ch],img[:,:,ch],img[:,:,ch])).transpose((1,2,0))"},{"cell_type":"code","execution_count":null,"metadata":{"_cell_guid":"52bc2102-90d4-475c-4a97-298881311a56"},"outputs":[],"source":"from skimage.segmentation import slic\nfrom skimage.segmentation import mark_boundaries\nfrom skimage.util import img_as_float\nfrom skimage import io\nimport glob\n\nn_segments = 3\nsigma = 5\nfor i, filepath in enumerate(glob.glob('../input/train/Type_1/*.jpg')):\n    if i != 6:\n        continue\n    img = get_img(filepath, color_space=cv2.COLOR_BGR2RGB)\n\n    R = select_channel(img, 0)\n    G = select_channel(img, 1)\n    B = select_channel(img, 2)\n    segmentsR = slic(R, n_segments=n_segments, sigma=sigma)\n    #segmentsG = slic(G, n_segments=n_segments, sigma=sigma)\n    #segmentsB = slic(B, n_segments=n_segments, sigma=sigma)\n\n    mark_segments = get_img(filepath, color_space=cv2.COLOR_BGR2RGB)\n    mark_segments = mark_boundaries(mark_segments, segmentsR)\n    #mark_segments = mark_boundaries(mark_segments, segmentsG)\n    #mark_segments = mark_boundaries(mark_segments, segmentsB)\n\n    show(mark_segments)"},{"cell_type":"code","execution_count":null,"metadata":{"_cell_guid":"9b8257f2-15d4-e8a6-845c-2c9687ea8d69"},"outputs":[],"source":"roi = img[100:300,:350,:]\nshow(roi)\n\nscjB = slic(B[100:300,:350,:], n_segments=4, sigma=3)\nscjG = slic(G[100:300,:350,:], n_segments=4, sigma=3)\nmark_segments = roi.copy()\nmark_segments = mark_boundaries(roi, scjB)\nmark_segments = mark_boundaries(roi, scjG)\nshow(mark_segments)"},{"cell_type":"code","execution_count":null,"metadata":{"_cell_guid":"3eab473b-8f14-4230-8e06-6149b9740e8c"},"outputs":[],"source":""}],"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}