{"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":"markdown","source":"# Reading TIFF","metadata":{}},{"cell_type":"markdown","source":"Let's start using the rasterio library to read TIFF files.","metadata":{}},{"cell_type":"code","source":"from torchvision.transforms import ToTensor\nimport rasterio\nfrom rasterio.plot import show\nimport numpy as np\n\npath = \"/kaggle/input/hubmap-organ-segmentation/train_images/28189.tiff\"\n\nwith rasterio.open(path) as image:\n    image_array = image.read()\n\n    \n\n\ntorch_image = ToTensor()(image_array)\nprint(torch_image.shape)","metadata":{"execution":{"iopub.status.busy":"2022-08-27T17:21:06.022526Z","iopub.execute_input":"2022-08-27T17:21:06.023248Z","iopub.status.idle":"2022-08-27T17:21:09.589361Z","shell.execute_reply.started":"2022-08-27T17:21:06.023142Z","shell.execute_reply":"2022-08-27T17:21:09.587895Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"You can also read the TIFF files using PIL and OpenCV.","metadata":{}},{"cell_type":"code","source":"from PIL import Image\npil_image = Image.open(path)\n","metadata":{"execution":{"iopub.status.busy":"2022-08-27T17:21:09.591760Z","iopub.execute_input":"2022-08-27T17:21:09.592481Z","iopub.status.idle":"2022-08-27T17:21:09.616537Z","shell.execute_reply.started":"2022-08-27T17:21:09.592423Z","shell.execute_reply":"2022-08-27T17:21:09.614811Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import cv2\ncv2_img = cv2.imread(path)","metadata":{"execution":{"iopub.status.busy":"2022-08-27T17:21:09.618369Z","iopub.execute_input":"2022-08-27T17:21:09.619719Z","iopub.status.idle":"2022-08-27T17:21:09.925131Z","shell.execute_reply.started":"2022-08-27T17:21:09.619669Z","shell.execute_reply":"2022-08-27T17:21:09.923750Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Let's compare the execution times for the 3 different methods:","metadata":{}},{"cell_type":"code","source":"%timeit rasterio.open(path).read()","metadata":{"execution":{"iopub.status.busy":"2022-08-27T17:21:09.928039Z","iopub.execute_input":"2022-08-27T17:21:09.928454Z","iopub.status.idle":"2022-08-27T17:21:12.765926Z","shell.execute_reply.started":"2022-08-27T17:21:09.928420Z","shell.execute_reply":"2022-08-27T17:21:12.764452Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%timeit np.array(Image.open(path))","metadata":{"execution":{"iopub.status.busy":"2022-08-27T17:21:12.769557Z","iopub.execute_input":"2022-08-27T17:21:12.771677Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%timeit cv2.imread(path)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"It looks like rasterio is the fastest method.","metadata":{}},{"cell_type":"markdown","source":"# Plotting TIFF","metadata":{}},{"cell_type":"markdown","source":"Using rasterio again: ","metadata":{}},{"cell_type":"code","source":"show(image_array)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ds = rasterio.open(path)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"show((ds, 1), cmap='Reds')","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"show((ds, 2), cmap='Greens')","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"show((ds, 3), cmap='Blues')","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"show((ds, 2), cmap='viridis')","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"You can also show the  PIL image by just having a cell display it:","metadata":{}},{"cell_type":"code","source":"pil_image","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Or even OpenCV (we need matplotlib as well to plot):","metadata":{}},{"cell_type":"code","source":"import matplotlib.pylab as plt\nplt.imshow(cv2_img)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Some Metadata","metadata":{}},{"cell_type":"markdown","source":"Notice that we can compute some statistics about the image \"bands\" (here the bands are the RGB channels).","metadata":{}},{"cell_type":"code","source":"# From here: https://automating-gis-processes.github.io/CSC18/lessons/L6/reading-raster.html\n\nstats = []\n\nfor band in image_array:\n    stats.append({\n    'min': band.min(),\n    'mean': band.mean(),\n    'median': np.median(band),\n    'max': band.max()})","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"stats","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Plotting Histograms","metadata":{}},{"cell_type":"markdown","source":"In addition to simple image plots, you can also get the histogram of the TIFF file.","metadata":{"execution":{"iopub.status.busy":"2022-06-23T16:23:26.553518Z","iopub.execute_input":"2022-06-23T16:23:26.554728Z","iopub.status.idle":"2022-06-23T16:23:26.583734Z","shell.execute_reply.started":"2022-06-23T16:23:26.55463Z","shell.execute_reply":"2022-06-23T16:23:26.582469Z"}}},{"cell_type":"code","source":"# TODO: Improve the name...\nfrom rasterio.plot import show_hist\nshow_hist(ds, bins=50, lw=0.0, stacked=False, alpha=0.3, histtype='stepfilled', title=\"Histogram\")","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Some technical details about TIFF","metadata":{}},{"cell_type":"markdown","source":"Here are some TIFF details:\n    \n- TIFF stands for Tage Image File Format\n- It is often used for medical imaging\n- It is used for scanning devices more generally\n\nThe most popular Python libraries to work with TIFF files are:\n\n- [rasterio](https://pypi.org/project/rasterio/): this is a library that is useful for GIS processing in general\n- [tiffile](https://pypi.org/project/tifffile/): more focused on TIFF file processing","metadata":{}},{"cell_type":"markdown","source":"Some more details from Wikipedia:\n    \n> Tag Image File Format, abbreviated TIFF or TIF, is an image file format for storing raster graphics images, popular among graphic artists, the publishing industry,[1] and photographers. TIFF is widely supported by scanning, faxing, word processing, optical character recognition, image manipulation, desktop publishing, and page-layout applications.[2] The format was created by the Aldus Corporation for use in desktop publishing. It published the latest version 6.0 in 1992, subsequently updated with an Adobe Systems copyright after the latter acquired Aldus in 1994. Several Aldus or Adobe technical notes have been published with minor extensions to the format, and several specifications have been based on TIFF 6.0, including TIFF/EP (ISO 12234-2), TIFF/IT (ISO 12639),[3][4][5] TIFF-F (RFC 2306) and TIFF-FX (RFC 3949).[6]","metadata":{}},{"cell_type":"markdown","source":"That's it for this time, I hope you find this useful.","metadata":{}}]}