image_output_2.png image_output_1.png
from glob import glob

import cv2

import matplotlib.pyplot as plt

import numpy as np

import pandas as pd

from glob import glob
basepath = '../input/train/'
all_cervix_images = []
for path in glob(basepath + '*'):
    cervix_type = path.split('/')[-1]
    cervix_images = glob(basepath + cervix_type + '/*')
    all_cervix_images = all_cervix_images + cervix_images
all_cervix_images = pd.DataFrame({'imagepath': all_cervix_images})
all_cervix_images['filetype'] = all_cervix_images.apply(lambda row: row.imagepath.split('.')[-1], axis=1)
all_cervix_images['type'] = all_cervix_images.apply(lambda row: row.imagepath.split('/')[-2], axis=1)

i = 1
fig = plt.figure(figsize=(12, 8))
for t in all_cervix_images['type'].unique()[:1]:
    ax = fig.add_subplot(1, 3, i)
    f = all_cervix_images[all_cervix_images['type'] == t]['imagepath'].values[0]
    img = cv2.imread(f)
    Z = img.reshape((-1, 3))
    Z = np.float32(Z)
    criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 10, 1.0)
    '\n    Right now the mask has an either in or out policy \n    '
    K = 8
    ret, label, center = cv2.kmeans(Z, K, None, criteria, 10, cv2.KMEANS_RANDOM_CENTERS)
    center = np.uint8(center)
    res = center[label.flatten()]
    res2 = res.reshape(img.shape)
    plt.imshow(res2, cmap='gray')
    plt.show()
    plt.title('sample for cervix {}'.format(t))
    "\n    screen_res = 1280, 720\n    scale_width = screen_res[0] / img.shape[1]\n    scale_height = screen_res[1] / img.shape[0]\n    scale = min(scale_width, scale_height)\n    window_width = int(img.shape[1] * scale)\n    window_height = int(img.shape[0] * scale)\n    cv2.namedWindow('dst_rt', cv2.WINDOW_NORMAL)\n    cv2.resizeWindow('dst_rt', window_width, window_height)\n    cv2.imshow('dst_rt', res2)\n    cv2.waitKey(0)\n    cv2.destroyAllWindows()\n    "