{"cells":[{"metadata":{},"cell_type":"markdown","source":"# Hacking the Kidney: Displaying a Polygon"},{"metadata":{},"cell_type":"markdown","source":"This is a notebook meant for CV2 beginners. The goal of this is to show how to display a polygon on an image using CV2. "},{"metadata":{},"cell_type":"markdown","source":"### Imports"},{"metadata":{"trusted":true},"cell_type":"code","source":"import numpy as np\nimport matplotlib.pyplot as plt \nimport pandas as pd\nimport cv2\nfrom osgeo import gdal \nimport json\n\n%matplotlib inline","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"### Load an Image"},{"metadata":{"trusted":true},"cell_type":"code","source":"dataset = gdal.Open(r'/kaggle/input/hubmap-kidney-segmentation/train/54f2eec69.tiff')\n \nband1 = dataset.GetRasterBand(1) # Red channel \nband2 = dataset.GetRasterBand(2) # Green channel \nband3 = dataset.GetRasterBand(3) # Blue channel\n\nb1 = band1.ReadAsArray() \nb2 = band2.ReadAsArray() \nb3 = band3.ReadAsArray() \n\nimg = np.dstack((b1, b2, b3)) \n","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"### Scale it so the CPU doesn't get overwhelmed"},{"metadata":{"trusted":true},"cell_type":"code","source":"img_small = cv2.resize(img, None,fx=0.5, fy=0.5)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"### Get Polygon Dimensions"},{"metadata":{"trusted":true},"cell_type":"code","source":"def getJsonObj(jsonFile):\n    with open(jsonFile, 'r') as myfile:\n        data=myfile.read()\n\n    # parse file\n    obj = json.loads(data)\n    \n    return obj","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"obj1 = getJsonObj('/kaggle/input/hubmap-kidney-segmentation/train/54f2eec69-anatomical-structure.json')\npoly_coords = np.array(obj1[0]['geometry']['coordinates'][0], np.int32)\npoly_coords2 = np.array(obj1[1]['geometry']['coordinates'][0], np.int32)\n\n# scale the polygon to keep proportions (with scaled image)\ntemp_small = np.array(poly_coords/2, np.int32) \ntemp_small2 = np.array(poly_coords2/2, np.int32)\n\n#pts = temp.reshape((-1, 1, 2)) \npts = temp_small.reshape((-1, 1, 2)) \npts2 = temp_small2.reshape((-1, 1, 2)) ","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"### Draw Polygon"},{"metadata":{"trusted":true},"cell_type":"code","source":"img_poly = cv2.polylines(img_small, [pts], True, (0,255,0), thickness=50) ","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"### Display Image"},{"metadata":{"trusted":true},"cell_type":"code","source":"plt.imshow(img_poly) ","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"### Draw Another Polygon"},{"metadata":{"trusted":true},"cell_type":"code","source":"img_poly2 = cv2.polylines(img_poly, [pts2], True, (0,255,0), thickness=50) ","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"### Display Image (Updated)"},{"metadata":{"trusted":true},"cell_type":"code","source":"plt.imshow(img_poly2) ","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# Conclusion"},{"metadata":{},"cell_type":"markdown","source":"Hopefully this notebook was helpful!"}],"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":4,"nbformat_minor":4}