{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"base_folder = '../input/rsna-intracranial-hemorrhage-detection/rsna-intracranial-hemorrhage-detection/'","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"HEIGHT = 256\nWIDTH = 256\nCHANNELS = 3\n\nSHAPE = (HEIGHT, WIDTH, CHANNELS)\nimage_path = base_folder + 'stage_2_train/'","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# load image and perform windowing","metadata":{}},{"cell_type":"code","source":"import matplotlib.pyplot as plt\nimport pydicom\nimport cv2\n\ndef correct_dcm(dcm):\n    x = dcm.pixel_array + 1000\n    px_mode = 4096\n    x[x>=px_mode] = x[x>=px_mode] - px_mode\n    dcm.PixelData = x.tobytes()\n    dcm.RescaleIntercept = -1000\n\ndef window_image(dcm, window_center, window_width):    \n    if (dcm.BitsStored == 12) and (dcm.PixelRepresentation == 0) and (int(dcm.RescaleIntercept) > -100):\n        correct_dcm(dcm)\n    img = dcm.pixel_array * dcm.RescaleSlope + dcm.RescaleIntercept\n    \n    # Resize\n    img = cv2.resize(img, SHAPE[:2], interpolation = cv2.INTER_LINEAR)\n   \n    img_min = window_center - window_width // 2\n    img_max = window_center + window_width // 2\n    img = np.clip(img, img_min, img_max)\n    return img\n\ndef bsb_window(dcm):\n    brain_img = window_image(dcm, 40, 80)\n    subdural_img = window_image(dcm, 80, 200)\n    soft_img = window_image(dcm, 40, 380)\n    \n    brain_img = (brain_img - 0) / 80\n    subdural_img = (subdural_img - (-20)) / 200\n    soft_img = (soft_img - (-150)) / 380\n    bsb_img = np.array([brain_img, subdural_img, soft_img]).transpose(1,2,0)\n    return bsb_img\n\ndef _read(path, SHAPE):\n    dcm = pydicom.dcmread(path)\n    try:\n        image = bsb_window(dcm)\n    except:\n        image = np.zeros(SHAPE)\n    image -= image.min((0,1))\n    image = (255*image).astype(np.uint8)\n    image = cv2.resize(image, (256, 256))\n    return image","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# prepare csv","metadata":{}},{"cell_type":"code","source":"df = pd.read_csv(base_folder + 'stage_2_train.csv')\ndf[['ID', 'type']] = df['ID'].str.rsplit(\"_\", n=1, expand=True)\ndf.drop_duplicates(['ID', 'type'], inplace=True)\ndf = df.pivot('ID', 'type', 'Label')\ndf.reset_index(inplace=True)\ncount_row = df.shape[0]\nprint(count_row)\ndf.head()","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"index = []","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for image_id in df[\"ID\"].tolist():\n#     load image\n    image = _read(image_path + image_id + \".dcm\", SHAPE)\n\n#     calculate histograms\n    colors = ('r','g','b')\n    histograms = []\n    for k,color in enumerate(colors):\n        histogram = cv2.calcHist([image],[k],None,[256],[0,256])\n        histograms.append(histogram)\n\n    for histogram in histograms:\n        histogram_sum = np.sum(histogram[:250])\n        weighing_factor = [\n          (histogram[i] * (i)) / histogram_sum for i in range (250)\n        ]\n\n        if (sum(weighing_factor[:250]) == 0): \n            index.append(image_id)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"index = list(set(index))","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# visualizing some of the blank images\nimport random\n\nn = len(index) - 1\n\nfor i in range(3):\n    im = _read(image_path + index[random.randint(0,n)] + \".dcm\", SHAPE)\n    plt.imshow(im)\n    plt.show()","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"np.save(\"blank_images\", index)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}