{"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 cv2\nimport numpy as np\nimport pydicom\nimport matplotlib.pyplot as plt","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2023-01-16T07:54:27.298533Z","iopub.execute_input":"2023-01-16T07:54:27.298947Z","iopub.status.idle":"2023-01-16T07:54:27.305207Z","shell.execute_reply.started":"2023-01-16T07:54:27.298909Z","shell.execute_reply":"2023-01-16T07:54:27.303662Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def extract_roi(image):\n    if len(image.shape) > 2:\n        gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY).astype(np.uint8)\n    else:\n        gray = image.astype(np.uint8)\n\n    ret, gray = cv2.threshold(gray, 50, 255, cv2.THRESH_BINARY_INV)\n    gray = cv2.morphologyEx(gray, cv2.MORPH_OPEN, cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (128, 128)))\n    gray = cv2.bitwise_not(gray)\n    \n    contours, hierarchy = cv2.findContours(gray, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)\n    contour = max(contours, key = cv2.contourArea)\n    \n    x, y, w, h = cv2.boundingRect(contour)\n    roi = image[y: y + h, x: x + w]\n\n    return roi","metadata":{"execution":{"iopub.status.busy":"2023-01-16T07:54:27.307545Z","iopub.execute_input":"2023-01-16T07:54:27.307993Z","iopub.status.idle":"2023-01-16T07:54:27.317274Z","shell.execute_reply.started":"2023-01-16T07:54:27.307946Z","shell.execute_reply":"2023-01-16T07:54:27.31627Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def resize_image(image, width=None, height=None, inter=cv2.INTER_LINEAR):\n    (h, w) = image.shape[:2]\n\n    if width is None and height is None:\n        return image\n\n    if width is None:\n        r = height / float(h)\n        dim = (int(w * r), height)    \n    else:\n        r = width / float(w)\n        dim = (width, int(h * r))\n\n    image = cv2.resize(image, dim, interpolation=inter)\n\n    return image","metadata":{"execution":{"iopub.status.busy":"2023-01-16T07:54:27.318548Z","iopub.execute_input":"2023-01-16T07:54:27.31949Z","iopub.status.idle":"2023-01-16T07:54:27.333602Z","shell.execute_reply.started":"2023-01-16T07:54:27.319455Z","shell.execute_reply":"2023-01-16T07:54:27.332589Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def read_dicom(path, image_size=None, voi_lut=True, fix_monochrome=True, keep_aspect_ratio=True, crop_roi=True):\n    dicom = pydicom.dcmread(path)\n    data = dicom.pixel_array\n\n    if voi_lut:\n        data = pydicom.pixel_data_handlers.util.apply_voi_lut(dicom.pixel_array, dicom)\n\n    if fix_monochrome and dicom.PhotometricInterpretation == 'MONOCHROME1':\n        data = np.amax(data) - data\n    \n    if crop_roi:\n        data = extract_roi(data)\n\n    if image_size:\n        if keep_aspect_ratio:\n            h, w = data.shape\n\n            if w > h:\n                 data = resize_image(data, width=image_size)\n            else:\n                 data = resize_image(data, height=image_size)\n        else:\n            data = cv2.resize(data, (image_size, image_size))\n    \n    data = data - np.min(data)\n    data = data / np.max(data)\n    data = (data * 255).astype(np.uint8)\n\n    return data","metadata":{"execution":{"iopub.status.busy":"2023-01-16T07:54:27.335622Z","iopub.execute_input":"2023-01-16T07:54:27.335931Z","iopub.status.idle":"2023-01-16T07:54:27.344788Z","shell.execute_reply.started":"2023-01-16T07:54:27.335903Z","shell.execute_reply":"2023-01-16T07:54:27.343594Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"image_size = 1024\nvoi_lut = True\nfix_monochrome = True\nkeep_aspect_ratio = True\ncrop_roi = True","metadata":{"execution":{"iopub.status.busy":"2023-01-16T07:54:27.346052Z","iopub.execute_input":"2023-01-16T07:54:27.346452Z","iopub.status.idle":"2023-01-16T07:54:27.355526Z","shell.execute_reply.started":"2023-01-16T07:54:27.346366Z","shell.execute_reply":"2023-01-16T07:54:27.354648Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"image = read_dicom('/kaggle/input/rsna-breast-cancer-detection/test_images/10008/68070693.dcm', image_size, voi_lut, fix_monochrome, keep_aspect_ratio, crop_roi)\nprint('Image shape: ', image.shape)\nplt.imshow(image)","metadata":{"execution":{"iopub.status.busy":"2023-01-16T08:00:26.534463Z","iopub.execute_input":"2023-01-16T08:00:26.534946Z","iopub.status.idle":"2023-01-16T08:00:30.963589Z","shell.execute_reply.started":"2023-01-16T08:00:26.534904Z","shell.execute_reply":"2023-01-16T08:00:30.962219Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}