{"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":"# What are .tif files and why do we use them? \n\nI have seen .tif OR .tiff files so often these days, but never got curious to know more about them. So this competition finally allowed me a chance to explore and learn about them :)\n\nTIFF stands for \"tag image file format\" are a loss less form of compression which captures extra image info used by graphic designers, medical experts to save designs, photographs and scans in a High quality and universal compatibility.\n\nHence for this reason we are using .tiff file format in this competition.","metadata":{}},{"cell_type":"markdown","source":"### Pros and Cons of .tif files (:o)\nPROS :)\n1. perfect for high-quality professional photos.\n2. TIFFs are universal and adaptable as a file format, so can be used with the major operating systems. \n3. The file \"can\" also work as a container for smaller-sized JPEGs, storing multiple images in one master raster graphic.\n\nCons :(\n1. Detail and resolution lead to TIFFs being quite large files, which takes up valuable space on your drive.\n2. Their high-quality makes them a poor choice for website design, since detailed images can slow a website’s loading speed. Hence we use .JPEG format.","metadata":{}},{"cell_type":"markdown","source":"## How to use .tiff files\n\nLets start to use .tiff files and see what images are given to us !\n\nThere are two Python libraries you can use to read .tiff files -\n1. Rasterio\n2. Tiffile\n\nI will be using RASTERIO in this NB","metadata":{}},{"cell_type":"code","source":"from torchvision.transforms import ToTensor\nimport rasterio\nfrom rasterio.plot import show\nimport numpy as np\nimport matplotlib.pyplot as plt","metadata":{"execution":{"iopub.status.busy":"2022-10-09T07:23:35.097051Z","iopub.execute_input":"2022-10-09T07:23:35.097510Z","iopub.status.idle":"2022-10-09T07:23:35.103335Z","shell.execute_reply.started":"2022-10-09T07:23:35.097448Z","shell.execute_reply":"2022-10-09T07:23:35.102167Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"PATH = '../input/hubmap-kidney-segmentation/train/aaa6a05cc.tiff'","metadata":{"execution":{"iopub.status.busy":"2022-10-09T07:11:50.331447Z","iopub.execute_input":"2022-10-09T07:11:50.332148Z","iopub.status.idle":"2022-10-09T07:11:50.338443Z","shell.execute_reply.started":"2022-10-09T07:11:50.332113Z","shell.execute_reply":"2022-10-09T07:11:50.336793Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"So i didn't looked at the size these images were, and simply started to read the image!!\n\nTo my surprise 😲😲 these files are HUGGEEEEEEE almost every file is above 200Mb (some files in GB's)😲😲\n\n","metadata":{}},{"cell_type":"code","source":"%%timeit\n# so lets see some samples\n\nwith rasterio.open(PATH) as image:\n    image_array = image.read()","metadata":{"execution":{"iopub.status.busy":"2022-10-09T08:11:47.640970Z","iopub.execute_input":"2022-10-09T08:11:47.641821Z","iopub.status.idle":"2022-10-09T08:13:18.497799Z","shell.execute_reply.started":"2022-10-09T08:11:47.641781Z","shell.execute_reply":"2022-10-09T08:13:18.496416Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#### Why not use openCV or Pillow libraries??\n\nA very genuine question, to answer this i would recommend you to copy this notebook and run the two cells below :\n\nThe image size is toooo LARGEE to be read by Pillow, so we end up getting error for image size \n\nimage read by openCV is a washed off image(very bad quality)","metadata":{}},{"cell_type":"code","source":"from PIL import Image\npil = Image.open(PATH)\n## PIL will raise Error, for the size of the image being VERY LARGE !","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%%timeit\nimport cv2\ncv2image = cv2.imread(PATH)\n# cv2 will read the image but lower quality than RASTERIO","metadata":{"execution":{"iopub.status.busy":"2022-10-09T08:13:18.500323Z","iopub.execute_input":"2022-10-09T08:13:18.500703Z","iopub.status.idle":"2022-10-09T08:13:54.251763Z","shell.execute_reply.started":"2022-10-09T08:13:18.500669Z","shell.execute_reply":"2022-10-09T08:13:54.250587Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.imshow(cv2image)","metadata":{"execution":{"iopub.status.busy":"2022-10-09T08:14:00.787434Z","iopub.execute_input":"2022-10-09T08:14:00.788224Z","iopub.status.idle":"2022-10-09T08:14:28.969362Z","shell.execute_reply.started":"2022-10-09T08:14:00.788185Z","shell.execute_reply":"2022-10-09T08:14:28.968364Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"show(image_array)","metadata":{"execution":{"iopub.status.busy":"2022-10-08T23:25:52.717181Z","iopub.execute_input":"2022-10-08T23:25:52.718109Z","iopub.status.idle":"2022-10-08T23:26:13.209111Z","shell.execute_reply.started":"2022-10-08T23:25:52.718058Z","shell.execute_reply":"2022-10-08T23:26:13.207893Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"image2 = rasterio.open('../input/hubmap-kidney-segmentation/train/b2dc8411c.tiff')","metadata":{"execution":{"iopub.status.busy":"2022-10-09T08:24:27.739331Z","iopub.execute_input":"2022-10-09T08:24:27.739778Z","iopub.status.idle":"2022-10-09T08:24:27.759252Z","shell.execute_reply.started":"2022-10-09T08:24:27.739729Z","shell.execute_reply":"2022-10-09T08:24:27.758210Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"show(image2)","metadata":{"execution":{"iopub.status.busy":"2022-10-09T08:25:20.669151Z","iopub.execute_input":"2022-10-09T08:25:20.669476Z","iopub.status.idle":"2022-10-09T08:26:21.945207Z","shell.execute_reply.started":"2022-10-09T08:25:20.669433Z","shell.execute_reply":"2022-10-09T08:26:21.944115Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"show((image2,1),cmap='viridis')","metadata":{"execution":{"iopub.status.busy":"2022-10-09T08:26:21.947179Z","iopub.execute_input":"2022-10-09T08:26:21.947544Z","iopub.status.idle":"2022-10-09T08:26:41.076069Z","shell.execute_reply.started":"2022-10-09T08:26:21.947510Z","shell.execute_reply":"2022-10-09T08:26:41.074906Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(image_array)","metadata":{"execution":{"iopub.status.busy":"2022-10-09T07:49:10.655056Z","iopub.execute_input":"2022-10-09T07:49:10.655553Z","iopub.status.idle":"2022-10-09T07:49:10.661603Z","shell.execute_reply.started":"2022-10-09T07:49:10.655513Z","shell.execute_reply":"2022-10-09T07:49:10.660741Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"torch_image = ToTensor()(image_array)\n\nprint(torch_image.shape)","metadata":{"execution":{"iopub.status.busy":"2022-10-09T07:13:23.222411Z","iopub.execute_input":"2022-10-09T07:13:23.223412Z","iopub.status.idle":"2022-10-09T07:13:23.229676Z","shell.execute_reply.started":"2022-10-09T07:13:23.223370Z","shell.execute_reply":"2022-10-09T07:13:23.228502Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### An image has pixel values, so lets see what are the central tendencies of those values !","metadata":{}},{"cell_type":"code","source":"# the len(image_array) is 3 hence we find 'mean'&'median' for the images\n\ncentral_vals = []\n\nfor image in image_array:\n    central_vals.append(\n    {\n        'mean':image.mean(),\n        'median':np.median(image)\n    }\n    )\nprint(central_vals)","metadata":{"execution":{"iopub.status.busy":"2022-10-09T07:57:27.122494Z","iopub.execute_input":"2022-10-09T07:57:27.122968Z","iopub.status.idle":"2022-10-09T07:57:33.168446Z","shell.execute_reply.started":"2022-10-09T07:57:27.122931Z","shell.execute_reply":"2022-10-09T07:57:33.167194Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# the len(image_array) is 3 hence we find 'mean'&'median' for the images\n\ncentral_vals = []\n\nfor image in image2.read():\n    central_vals.append(\n    {\n        'mean':image.mean(),\n        'median':np.median(image)\n    }\n    )\nprint(central_vals)","metadata":{"execution":{"iopub.status.busy":"2022-10-09T08:28:11.539212Z","iopub.execute_input":"2022-10-09T08:28:11.539641Z","iopub.status.idle":"2022-10-09T08:28:42.432534Z","shell.execute_reply.started":"2022-10-09T08:28:11.539606Z","shell.execute_reply":"2022-10-09T08:28:42.431257Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"lets have a look at the distribution of the image pixel values 🙂","metadata":{}},{"cell_type":"code","source":"from rasterio.plot import show_hist\nshow_hist(image_array, bins=50,lw=0.0, stacked=False, alpha=0.3, histtype='barstacked', title=\"Histogram\",)","metadata":{"execution":{"iopub.status.busy":"2022-10-09T07:41:54.118478Z","iopub.execute_input":"2022-10-09T07:41:54.118867Z","iopub.status.idle":"2022-10-09T07:42:22.269333Z","shell.execute_reply.started":"2022-10-09T07:41:54.118835Z","shell.execute_reply":"2022-10-09T07:42:22.268480Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from rasterio.plot import show_hist\nshow_hist(image2.read(), bins=50,lw=0.0, stacked=False, alpha=0.3, histtype='barstacked', title=\"Histogram\",)","metadata":{"execution":{"iopub.status.busy":"2022-10-09T08:29:31.753868Z","iopub.execute_input":"2022-10-09T08:29:31.755045Z","iopub.status.idle":"2022-10-09T08:30:41.083035Z","shell.execute_reply.started":"2022-10-09T08:29:31.754998Z","shell.execute_reply":"2022-10-09T08:30:41.081649Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### I will be ending this notebook here only  \n\n<h3 style='text-align:center'>DO UPVOTE if you found it useful :)</h3>\n\nIDK how to plot masks coordinates and use them on image :_\n\n\nso next time i will learn how to plot masks and make a very much detailed notebook on that ","metadata":{}},{"cell_type":"markdown","source":"I referred @yassinealouini notebook and adobe website to learn about .tiff files ","metadata":{}},{"cell_type":"markdown","source":"## see y'all later :o)\nTHANKS FOR READING","metadata":{}}]}