{"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 torch\nimport matplotlib.pyplot as plt\nfrom IPython import display","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2023-06-12T11:57:46.49928Z","iopub.execute_input":"2023-06-12T11:57:46.499655Z","iopub.status.idle":"2023-06-12T11:57:46.504818Z","shell.execute_reply.started":"2023-06-12T11:57:46.499619Z","shell.execute_reply":"2023-06-12T11:57:46.503454Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def double_thresholds(image, high_threshold, low_threshold):\n    X = image > high_threshold\n    Y = (image > low_threshold) & (image <= high_threshold)\n    while True:\n        X_neighborhood = torch.nn.functional.max_pool2d(X.float(), kernel_size=3, stride=1, padding=1)\n        new_X = X | (Y & X_neighborhood.byte())\n        if torch.all(X == new_X):\n            break\n        X = new_X\n    result = X.float()\n    return result  # returns a binary image","metadata":{"execution":{"iopub.status.busy":"2023-06-12T11:57:46.506904Z","iopub.execute_input":"2023-06-12T11:57:46.507339Z","iopub.status.idle":"2023-06-12T11:57:46.51848Z","shell.execute_reply.started":"2023-06-12T11:57:46.5073Z","shell.execute_reply":"2023-06-12T11:57:46.51754Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"image = torch.Tensor([  # a sample image\n    [0.6, 0., 0., 0.9, 0.9, 0.6],\n    [0.6, 0., 0.6, 0., 0.6, 0.],\n    [0.6, 0.9, 0., 0., 0., 0.],\n    [0.9, 0.6, 0., 0.6, 0.2, 0.6],\n    [0., 0., 0.5, 0.4, 0.6, 0.],\n    [0., 0., 0., 0.5, 0., 0.],\n])\nimage = image.reshape(1, 1, 6, 6)  # [BatchSize, Channels, Height, Width]\nhigh_threshold = 0.85\nlow_threshold = 0.5\nprocessed = double_thresholds(image, high_threshold, low_threshold)","metadata":{"execution":{"iopub.status.busy":"2023-06-12T11:57:46.520733Z","iopub.execute_input":"2023-06-12T11:57:46.521075Z","iopub.status.idle":"2023-06-12T11:57:46.535902Z","shell.execute_reply.started":"2023-06-12T11:57:46.521048Z","shell.execute_reply":"2023-06-12T11:57:46.534607Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(10, 6))\nax = plt.subplot(1, 2, 1)\nax.imshow(image.squeeze().numpy(), interpolation='none')\nax.set_title('Image')\n\nax = plt.subplot(1, 2, 2)\nax.imshow(processed.squeeze().numpy(), interpolation='none')\nax.set_title('Processed')","metadata":{"execution":{"iopub.status.busy":"2023-06-12T11:57:46.537243Z","iopub.execute_input":"2023-06-12T11:57:46.537597Z","iopub.status.idle":"2023-06-12T11:57:47.122794Z","shell.execute_reply.started":"2023-06-12T11:57:46.537566Z","shell.execute_reply":"2023-06-12T11:57:47.121278Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}