{"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":"## Open CV\n* <a href=\"#display_image\"> Display image </a>\n* <a href=\"#split_color\">Split color channel </a>\n* <a href=\"#draw_line\">Draw Line</a>\n* <a href=\"#draw_rectangles\">Draw Rectangles</a>\n* <a href=\"#draw_circles\">Draw Cirlcles</a>\n* <a href=\"#draw_circles\">Draw Polygons</a>\n* <a href=\"#add_text\">Add Text in image</a>\n* <a href=\"#translations\">Translations</a>\n* <a href=\"#rotaions\">Rotations</a>\n* <a href=\"#resizing\">Image Re-sizing</a>\n* <a href=\"#image_pyraminds\">Image Pyraminds</a>\n* <a href=\"#cropping\">Cropping</a>\n* <a href=\"#increasing_brightness\">Increasing Brightness</a>\n* <a href=\"#decreasing_brightness\">Decreasing Brightness</a>\n* <a href=\"#bitwise_operation\">Bitwise Operation and Masking</a>\n* <a href=\"#bitwise_operation2\">Bitwise operations such as AND, OR, XOR and NOT</a>\n* <a href=\"#convolutions_blurring\">Convolutions, Blurring and sharpening </a>\n* <a href=\"#image_noising\">Image De-noising - Non-Local Means Denoising</a>\n* <a href=\"#sharpeing_image\">Sharpening Images</a>\n* <a href=\"#thresholding\">Thresholding, Binarization & Adaptive Thresholding</a>\n* <a href=\"#dilation\">Dilation, Erosion and Edge Detection</a>\n* <a href=\"#matching_contours\">Matching Contours</a>\n* <a href=\"#overlays\">Overlays with OpenCV</a>\n","metadata":{}},{"cell_type":"code","source":"# This Python 3 environment comes with many helpful analytics libraries installed\n# It is defined by the kaggle/python Docker image: https://github.com/kaggle/docker-python\n# For example, here's several helpful packages to load\n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n\n# Input data files are available in the read-only \"../input/\" directory\n# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\n\n\nimport os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))\n\n# You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using \"Save & Run All\" \n# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-06-10T02:52:56.164413Z","iopub.execute_input":"2022-06-10T02:52:56.165202Z","iopub.status.idle":"2022-06-10T02:52:56.212546Z","shell.execute_reply.started":"2022-06-10T02:52:56.165078Z","shell.execute_reply":"2022-06-10T02:52:56.211443Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import cv2\nimport matplotlib.pyplot as plt\nfrom zipfile import ZipFile","metadata":{"execution":{"iopub.status.busy":"2022-06-10T02:52:56.21409Z","iopub.execute_input":"2022-06-10T02:52:56.21432Z","iopub.status.idle":"2022-06-10T02:52:56.493285Z","shell.execute_reply.started":"2022-06-10T02:52:56.214291Z","shell.execute_reply":"2022-06-10T02:52:56.492631Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"with ZipFile('/kaggle/input/dogs-vs-cats-redux-kernels-edition/test.zip', 'r') as zip:\n    zip.extractall()\n    print('done')","metadata":{"execution":{"iopub.status.busy":"2022-06-10T02:52:56.495495Z","iopub.execute_input":"2022-06-10T02:52:56.495822Z","iopub.status.idle":"2022-06-10T02:53:04.695722Z","shell.execute_reply.started":"2022-06-10T02:52:56.495782Z","shell.execute_reply":"2022-06-10T02:53:04.694782Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"PATH = '/kaggle/working/test'\nfilename = os.listdir(PATH)","metadata":{"execution":{"iopub.status.busy":"2022-06-10T02:53:04.698066Z","iopub.execute_input":"2022-06-10T02:53:04.698457Z","iopub.status.idle":"2022-06-10T02:53:04.712202Z","shell.execute_reply.started":"2022-06-10T02:53:04.698411Z","shell.execute_reply":"2022-06-10T02:53:04.711196Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"type(filename)","metadata":{"execution":{"iopub.status.busy":"2022-06-10T02:53:04.713622Z","iopub.execute_input":"2022-06-10T02:53:04.714014Z","iopub.status.idle":"2022-06-10T02:53:04.722104Z","shell.execute_reply.started":"2022-06-10T02:53:04.713969Z","shell.execute_reply":"2022-06-10T02:53:04.721293Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(filename)","metadata":{"execution":{"iopub.status.busy":"2022-06-10T02:53:04.723406Z","iopub.execute_input":"2022-06-10T02:53:04.724126Z","iopub.status.idle":"2022-06-10T02:53:04.735955Z","shell.execute_reply.started":"2022-06-10T02:53:04.724083Z","shell.execute_reply":"2022-06-10T02:53:04.735241Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"filename[5:30]","metadata":{"execution":{"iopub.status.busy":"2022-06-10T02:53:04.736923Z","iopub.execute_input":"2022-06-10T02:53:04.737246Z","iopub.status.idle":"2022-06-10T02:53:04.748436Z","shell.execute_reply.started":"2022-06-10T02:53:04.73722Z","shell.execute_reply":"2022-06-10T02:53:04.747768Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<h5 id=\"display_image\"></h5>","metadata":{}},{"cell_type":"code","source":"def imshow(title = \"Image\", image=None, size=10):\n    w, h = image.shape[0], image.shape[1]\n    aspect_ratio = w/h\n    plt.figure(figsize=(size*aspect_ratio, size))\n    plt.imshow(cv2.cvtColor(image, cv2.COLOR_BGR2RGB))\n    plt.title(title)\n    plt.show()","metadata":{"execution":{"iopub.status.busy":"2022-06-10T02:53:04.749958Z","iopub.execute_input":"2022-06-10T02:53:04.75061Z","iopub.status.idle":"2022-06-10T02:53:04.759745Z","shell.execute_reply.started":"2022-06-10T02:53:04.750538Z","shell.execute_reply":"2022-06-10T02:53:04.75901Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"image = cv2.imread('/kaggle/working/test/5795.jpg')\nimshow(\"DOG\", image)","metadata":{"execution":{"iopub.status.busy":"2022-06-10T02:53:04.761021Z","iopub.execute_input":"2022-06-10T02:53:04.762738Z","iopub.status.idle":"2022-06-10T02:53:05.145307Z","shell.execute_reply.started":"2022-06-10T02:53:04.762694Z","shell.execute_reply":"2022-06-10T02:53:05.144629Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"image.shape","metadata":{"execution":{"iopub.status.busy":"2022-06-10T02:53:05.147862Z","iopub.execute_input":"2022-06-10T02:53:05.148264Z","iopub.status.idle":"2022-06-10T02:53:05.153796Z","shell.execute_reply.started":"2022-06-10T02:53:05.148218Z","shell.execute_reply":"2022-06-10T02:53:05.153081Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)\nimshow(\"Converted to grayscale\", gray_image)","metadata":{"execution":{"iopub.status.busy":"2022-06-10T02:53:05.15511Z","iopub.execute_input":"2022-06-10T02:53:05.155417Z","iopub.status.idle":"2022-06-10T02:53:05.430118Z","shell.execute_reply.started":"2022-06-10T02:53:05.155372Z","shell.execute_reply":"2022-06-10T02:53:05.429218Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(\"G\")","metadata":{"execution":{"iopub.status.busy":"2022-06-10T02:53:05.431506Z","iopub.execute_input":"2022-06-10T02:53:05.43187Z","iopub.status.idle":"2022-06-10T02:53:05.436608Z","shell.execute_reply.started":"2022-06-10T02:53:05.431833Z","shell.execute_reply":"2022-06-10T02:53:05.435843Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"img2 = cv2.imread('/kaggle/working/test/5355.jpg')","metadata":{"execution":{"iopub.status.busy":"2022-06-10T02:53:05.438105Z","iopub.execute_input":"2022-06-10T02:53:05.43852Z","iopub.status.idle":"2022-06-10T02:53:05.448516Z","shell.execute_reply.started":"2022-06-10T02:53:05.438474Z","shell.execute_reply":"2022-06-10T02:53:05.447767Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"B,G,R = cv2.split(img2)\nprint(B.shape)\nprint(G.shape)\nprint(R.shape)","metadata":{"execution":{"iopub.status.busy":"2022-06-10T02:53:05.449624Z","iopub.execute_input":"2022-06-10T02:53:05.449943Z","iopub.status.idle":"2022-06-10T02:53:05.474184Z","shell.execute_reply.started":"2022-06-10T02:53:05.449915Z","shell.execute_reply":"2022-06-10T02:53:05.473309Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"imshow(\"Blue channel only\", B)","metadata":{"execution":{"iopub.status.busy":"2022-06-10T02:53:05.475435Z","iopub.execute_input":"2022-06-10T02:53:05.476101Z","iopub.status.idle":"2022-06-10T02:53:05.807909Z","shell.execute_reply.started":"2022-06-10T02:53:05.476065Z","shell.execute_reply":"2022-06-10T02:53:05.807274Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"zeros = np.zeros(img2.shape[:2], dtype=\"uint8\")\n\nimshow(\"Red\", cv2.merge([zeros, zeros, R]))\nimshow(\"Green\", cv2.merge([zeros, G, zeros]))\nimshow(\"Blue\", cv2.merge([B, zeros, zeros]))\n","metadata":{"execution":{"iopub.status.busy":"2022-06-10T02:53:05.8092Z","iopub.execute_input":"2022-06-10T02:53:05.809629Z","iopub.status.idle":"2022-06-10T02:53:06.674504Z","shell.execute_reply.started":"2022-06-10T02:53:05.809596Z","shell.execute_reply":"2022-06-10T02:53:06.673863Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"B,G,R = cv2.split(img2)","metadata":{"execution":{"iopub.status.busy":"2022-06-10T02:53:06.675448Z","iopub.execute_input":"2022-06-10T02:53:06.675742Z","iopub.status.idle":"2022-06-10T02:53:06.68019Z","shell.execute_reply.started":"2022-06-10T02:53:06.675707Z","shell.execute_reply":"2022-06-10T02:53:06.679421Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"merged = cv2.merge([B,G,R])\nimshow(\"Merged\", merged)","metadata":{"execution":{"iopub.status.busy":"2022-06-10T02:53:06.6818Z","iopub.execute_input":"2022-06-10T02:53:06.682344Z","iopub.status.idle":"2022-06-10T02:53:07.065083Z","shell.execute_reply.started":"2022-06-10T02:53:06.682301Z","shell.execute_reply":"2022-06-10T02:53:07.064163Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"merged = cv2.merge([B, G,R+100])\nimshow(\"Blue Boost\", merged)","metadata":{"execution":{"iopub.status.busy":"2022-06-10T02:53:07.066253Z","iopub.execute_input":"2022-06-10T02:53:07.066483Z","iopub.status.idle":"2022-06-10T02:53:07.503537Z","shell.execute_reply.started":"2022-06-10T02:53:07.066454Z","shell.execute_reply":"2022-06-10T02:53:07.502636Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"hsv_image = cv2.cvtColor(img2, cv2.COLOR_BGR2HSV)\nimshow('HSV', hsv_image)","metadata":{"execution":{"iopub.status.busy":"2022-06-10T02:53:07.505086Z","iopub.execute_input":"2022-06-10T02:53:07.505394Z","iopub.status.idle":"2022-06-10T02:53:07.930653Z","shell.execute_reply.started":"2022-06-10T02:53:07.505356Z","shell.execute_reply":"2022-06-10T02:53:07.930035Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.imshow(cv2.cvtColor(hsv_image, cv2.COLOR_HSV2RGB))\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-06-10T02:53:07.931525Z","iopub.execute_input":"2022-06-10T02:53:07.932254Z","iopub.status.idle":"2022-06-10T02:53:08.150318Z","shell.execute_reply.started":"2022-06-10T02:53:07.932218Z","shell.execute_reply":"2022-06-10T02:53:08.149509Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"imshow(\"Hue\", hsv_image[:,:,0])\nimshow(\"Saturation\", hsv_image[:,:,1])\nimshow(\"Value\", hsv_image[:,:,2])","metadata":{"execution":{"iopub.status.busy":"2022-06-10T02:53:08.151548Z","iopub.execute_input":"2022-06-10T02:53:08.151865Z","iopub.status.idle":"2022-06-10T02:53:09.055431Z","shell.execute_reply.started":"2022-06-10T02:53:08.151833Z","shell.execute_reply":"2022-06-10T02:53:09.054871Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"img3 = np.zeros((512, 512, 3),np.uint8)","metadata":{"execution":{"iopub.status.busy":"2022-06-10T02:53:09.05632Z","iopub.execute_input":"2022-06-10T02:53:09.057093Z","iopub.status.idle":"2022-06-10T02:53:09.062036Z","shell.execute_reply.started":"2022-06-10T02:53:09.057045Z","shell.execute_reply":"2022-06-10T02:53:09.06115Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"image_gray = np.zeros((512, 512), np.uint8)","metadata":{"execution":{"iopub.status.busy":"2022-06-10T02:53:09.063421Z","iopub.execute_input":"2022-06-10T02:53:09.063727Z","iopub.status.idle":"2022-06-10T02:53:09.074213Z","shell.execute_reply.started":"2022-06-10T02:53:09.063688Z","shell.execute_reply":"2022-06-10T02:53:09.073645Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"imshow(\"Black Canvas - RGB Color\", img3)\nimshow(\"Black Canvas - Grayscale\", image_gray)","metadata":{"execution":{"iopub.status.busy":"2022-06-10T02:53:09.07543Z","iopub.execute_input":"2022-06-10T02:53:09.076271Z","iopub.status.idle":"2022-06-10T02:53:09.713877Z","shell.execute_reply.started":"2022-06-10T02:53:09.076229Z","shell.execute_reply":"2022-06-10T02:53:09.712985Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cv2.line(img3, (0,0), (511,511), (255, 127,0), 5)\nimshow(\"Black Canvas\",img3)","metadata":{"execution":{"iopub.status.busy":"2022-06-10T02:53:09.714992Z","iopub.execute_input":"2022-06-10T02:53:09.715283Z","iopub.status.idle":"2022-06-10T02:53:09.976336Z","shell.execute_reply.started":"2022-06-10T02:53:09.715251Z","shell.execute_reply":"2022-06-10T02:53:09.975519Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cv2.line(img3, (100,0), (511,511), (255, 127,0), 5)\ncv2.line(img3, (120,0), (511,511), (255, 127,0), 5)\ncv2.line(img3, (140,0), (511,511), (255, 127,0), 5)\ncv2.line(img3, (160,0), (511,511), (0, 0,255), 5)\ncv2.line(img3, (180,0), (511,511), (0, 255,0), 5)\nimshow(\"Black Canvas\",img3)","metadata":{"execution":{"iopub.status.busy":"2022-06-10T02:53:09.977404Z","iopub.execute_input":"2022-06-10T02:53:09.97762Z","iopub.status.idle":"2022-06-10T02:53:10.244588Z","shell.execute_reply.started":"2022-06-10T02:53:09.977594Z","shell.execute_reply":"2022-06-10T02:53:10.243659Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<h2 id=\"draw_rectangles\"> Drawing Rectangles </h2>\n> cv2.rectangle(image, starting vertex, opposite vertex, color, thickness)","metadata":{}},{"cell_type":"code","source":"img4 = np.zeros((512,512, 3), np.uint8)","metadata":{"execution":{"iopub.status.busy":"2022-06-10T02:53:10.249949Z","iopub.execute_input":"2022-06-10T02:53:10.250197Z","iopub.status.idle":"2022-06-10T02:53:10.255372Z","shell.execute_reply.started":"2022-06-10T02:53:10.250166Z","shell.execute_reply":"2022-06-10T02:53:10.254598Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cv2.rectangle(img4, (100,100), (300, 250), (0, 50, 127), 10)\nimshow(\"Rectangle\", img4)","metadata":{"execution":{"iopub.status.busy":"2022-06-10T02:53:10.256533Z","iopub.execute_input":"2022-06-10T02:53:10.256796Z","iopub.status.idle":"2022-06-10T02:53:10.513522Z","shell.execute_reply.started":"2022-06-10T02:53:10.25676Z","shell.execute_reply":"2022-06-10T02:53:10.512918Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cv2.rectangle(img4, (150,150), (300, 250), (0, 50, 127), 10)\nimshow(\"Rectangle\", img4)","metadata":{"execution":{"iopub.status.busy":"2022-06-10T02:53:10.514709Z","iopub.execute_input":"2022-06-10T02:53:10.515383Z","iopub.status.idle":"2022-06-10T02:53:10.774245Z","shell.execute_reply.started":"2022-06-10T02:53:10.515337Z","shell.execute_reply":"2022-06-10T02:53:10.77328Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cv2.rectangle(img4, (100,100), (350, 350), (0, 50, 127), 10)\nimshow(\"Rectangle\", img4)","metadata":{"execution":{"iopub.status.busy":"2022-06-10T02:53:10.775661Z","iopub.execute_input":"2022-06-10T02:53:10.775965Z","iopub.status.idle":"2022-06-10T02:53:11.02915Z","shell.execute_reply.started":"2022-06-10T02:53:10.775914Z","shell.execute_reply":"2022-06-10T02:53:11.028346Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cv2.rectangle(img4, (120,120), (350, 350), (0, 0, 127), 5)\nimshow(\"Rectangle\", img4)","metadata":{"execution":{"iopub.status.busy":"2022-06-10T02:53:11.030311Z","iopub.execute_input":"2022-06-10T02:53:11.030598Z","iopub.status.idle":"2022-06-10T02:53:11.28061Z","shell.execute_reply.started":"2022-06-10T02:53:11.030512Z","shell.execute_reply":"2022-06-10T02:53:11.279751Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<h2 id=\"draw_circles\"> Cirlcles </h2>\n> cv2.circle(imge,center, radius, color, fill)","metadata":{}},{"cell_type":"code","source":"img5 = np.zeros((500,500,3), np.uint8)","metadata":{"execution":{"iopub.status.busy":"2022-06-10T02:53:11.281886Z","iopub.execute_input":"2022-06-10T02:53:11.282107Z","iopub.status.idle":"2022-06-10T02:53:11.286119Z","shell.execute_reply.started":"2022-06-10T02:53:11.282079Z","shell.execute_reply":"2022-06-10T02:53:11.28536Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cv2.circle(img5, (300, 300), 100, (0, 0, 230), -1)\nimshow(\"Circle\", img5)","metadata":{"execution":{"iopub.status.busy":"2022-06-10T02:53:11.287166Z","iopub.execute_input":"2022-06-10T02:53:11.287704Z","iopub.status.idle":"2022-06-10T02:53:11.539546Z","shell.execute_reply.started":"2022-06-10T02:53:11.287641Z","shell.execute_reply":"2022-06-10T02:53:11.538766Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cv2.circle(img5, (320, 330), 80, (0, 0, 230), 3)\nimshow(\"Circle\", img5)","metadata":{"execution":{"iopub.status.busy":"2022-06-10T02:53:11.540716Z","iopub.execute_input":"2022-06-10T02:53:11.540962Z","iopub.status.idle":"2022-06-10T02:53:11.785172Z","shell.execute_reply.started":"2022-06-10T02:53:11.540933Z","shell.execute_reply":"2022-06-10T02:53:11.784282Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<h2 id=\"draw_polygons\"> Polygons </h2>\n\n> cv2.polylines(image, points, closed?, color, thickness)","metadata":{}},{"cell_type":"code","source":"img6 = np.zeros((550,550,3), np.uint8)\n\npts = np.array([[10,50], [400,50], [90, 200], [50, 500]], np.int32)\n\npts = pts.reshape(-1,1,2)","metadata":{"execution":{"iopub.status.busy":"2022-06-10T02:53:11.786438Z","iopub.execute_input":"2022-06-10T02:53:11.786666Z","iopub.status.idle":"2022-06-10T02:53:11.791672Z","shell.execute_reply.started":"2022-06-10T02:53:11.786638Z","shell.execute_reply":"2022-06-10T02:53:11.790942Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cv2.polylines(img6, [pts], True, (0,0,240),3)\nimshow(\"Polygon\", img6)","metadata":{"execution":{"iopub.status.busy":"2022-06-10T02:53:11.792926Z","iopub.execute_input":"2022-06-10T02:53:11.793136Z","iopub.status.idle":"2022-06-10T02:53:12.058508Z","shell.execute_reply.started":"2022-06-10T02:53:11.793111Z","shell.execute_reply":"2022-06-10T02:53:12.057591Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**NOTE** cv2.polylines requires our data be shaped in this way:","metadata":{}},{"cell_type":"code","source":"pts = np.array([[10,50], [400, 50],[90,200], [50, 500]], np.int32)\npts.shape","metadata":{"execution":{"iopub.status.busy":"2022-06-10T02:53:12.059873Z","iopub.execute_input":"2022-06-10T02:53:12.060074Z","iopub.status.idle":"2022-06-10T02:53:12.06687Z","shell.execute_reply.started":"2022-06-10T02:53:12.060049Z","shell.execute_reply":"2022-06-10T02:53:12.065979Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pts = pts.reshape((-1,1,2))\npts.shape","metadata":{"execution":{"iopub.status.busy":"2022-06-10T02:53:12.06825Z","iopub.execute_input":"2022-06-10T02:53:12.068944Z","iopub.status.idle":"2022-06-10T02:53:12.085205Z","shell.execute_reply.started":"2022-06-10T02:53:12.068899Z","shell.execute_reply":"2022-06-10T02:53:12.084415Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<h2 id=\"add_text\"> Adding Text </h2>\ncv2.putText(image, 'Text to Display', bottom left starting point , Font, Font Size, Color, Thickness)","metadata":{}},{"cell_type":"code","source":"img7 = np.zeros((500, 500,3), np.uint8)\nourString = \"Sense Tech\"\ncv2.putText(img7, ourString, (50, 100), cv2.FONT_HERSHEY_SIMPLEX,2, (10, 100, 230))\n\nimshow(\"Text Print\", img7)","metadata":{"execution":{"iopub.status.busy":"2022-06-10T02:53:12.086358Z","iopub.execute_input":"2022-06-10T02:53:12.087372Z","iopub.status.idle":"2022-06-10T02:53:12.335148Z","shell.execute_reply.started":"2022-06-10T02:53:12.087317Z","shell.execute_reply":"2022-06-10T02:53:12.334294Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<h2 id=\"translations\"> Translations  </h2>\ncv2.warpAffine","metadata":{}},{"cell_type":"code","source":"img = cv2.imread('/kaggle/working/test/7734.jpg')","metadata":{"execution":{"iopub.status.busy":"2022-06-10T02:53:12.336343Z","iopub.execute_input":"2022-06-10T02:53:12.336552Z","iopub.status.idle":"2022-06-10T02:53:12.343841Z","shell.execute_reply.started":"2022-06-10T02:53:12.336526Z","shell.execute_reply":"2022-06-10T02:53:12.343302Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"imshow('Original',img)","metadata":{"execution":{"iopub.status.busy":"2022-06-10T02:53:12.345744Z","iopub.execute_input":"2022-06-10T02:53:12.34605Z","iopub.status.idle":"2022-06-10T02:53:13.053786Z","shell.execute_reply.started":"2022-06-10T02:53:12.34601Z","shell.execute_reply":"2022-06-10T02:53:13.052965Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# This an affine transform that simply shifts the position of an image. (left or right)\n# cv2.warpAffine(image, T, (width, height))\n\nheight, width = img.shape[:2]\n\nquarter_height, quarter_width = height/4, width/4\n\nT = np.float32([[1, 0, quarter_width], [0, 1, quarter_height]])\n\nimg_translation = cv2.warpAffine(img, T, (width, height))\n\nimshow('Original',img)\nimshow(\"Translated\", img_translation)","metadata":{"execution":{"iopub.status.busy":"2022-06-10T02:53:13.054985Z","iopub.execute_input":"2022-06-10T02:53:13.055212Z","iopub.status.idle":"2022-06-10T02:53:13.552547Z","shell.execute_reply.started":"2022-06-10T02:53:13.055183Z","shell.execute_reply":"2022-06-10T02:53:13.551749Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<h2 id=\"rotaions\"> Rotations </h2>\n\ncv2.getRotationMatrix2D(rotation_center_x, rotation_center_y, angle of rotation, scale)","metadata":{}},{"cell_type":"code","source":"img =cv2.imread('/kaggle/working/test/7739.jpg')\nimshow(\"original image\", img)","metadata":{"execution":{"iopub.status.busy":"2022-06-10T02:53:13.553625Z","iopub.execute_input":"2022-06-10T02:53:13.553825Z","iopub.status.idle":"2022-06-10T02:53:13.865522Z","shell.execute_reply.started":"2022-06-10T02:53:13.553799Z","shell.execute_reply":"2022-06-10T02:53:13.864959Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"height, width = img.shape[:2]\n# image rototate \nrotation_matrix = cv2.getRotationMatrix2D((width/2, height/2), 85, 1)\n\nrotated_image = cv2.warpAffine(img, rotation_matrix, (width, height))\n\nimshow(\"Rotated 90 degrees with scale =1\", rotated_image)","metadata":{"execution":{"iopub.status.busy":"2022-06-10T02:53:13.866403Z","iopub.execute_input":"2022-06-10T02:53:13.866926Z","iopub.status.idle":"2022-06-10T02:53:14.168762Z","shell.execute_reply.started":"2022-06-10T02:53:13.866884Z","shell.execute_reply":"2022-06-10T02:53:14.168095Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"img =cv2.imread('/kaggle/working/test/7741.jpg')\nimshow(\"original image\", img)","metadata":{"execution":{"iopub.status.busy":"2022-06-10T02:53:14.169653Z","iopub.execute_input":"2022-06-10T02:53:14.170271Z","iopub.status.idle":"2022-06-10T02:53:14.417806Z","shell.execute_reply.started":"2022-06-10T02:53:14.17024Z","shell.execute_reply":"2022-06-10T02:53:14.417162Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"flipped = cv2.flip(img, 1)\nimshow(\"Horizontal Flip\", flipped)","metadata":{"execution":{"iopub.status.busy":"2022-06-10T02:53:14.418759Z","iopub.execute_input":"2022-06-10T02:53:14.419229Z","iopub.status.idle":"2022-06-10T02:53:14.666021Z","shell.execute_reply.started":"2022-06-10T02:53:14.419196Z","shell.execute_reply":"2022-06-10T02:53:14.665186Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<h2 id=\"resizing\"> Re-sizing </h2>","metadata":{"execution":{"iopub.status.busy":"2022-05-23T12:40:40.977404Z","iopub.execute_input":"2022-05-23T12:40:40.977893Z","iopub.status.idle":"2022-05-23T12:40:40.983351Z","shell.execute_reply.started":"2022-05-23T12:40:40.977843Z","shell.execute_reply":"2022-05-23T12:40:40.981984Z"}}},{"cell_type":"code","source":"# cv2 resize function\n# cv2.resize(image, dsize(output image size), x scale, y scale, interpolatio)\nimg =cv2.imread('/kaggle/working/test/7753.jpg')\n\nprint(img.shape)\nimshow(\"Original image\", img)\n","metadata":{"execution":{"iopub.status.busy":"2022-06-10T02:53:14.667152Z","iopub.execute_input":"2022-06-10T02:53:14.667383Z","iopub.status.idle":"2022-06-10T02:53:15.103218Z","shell.execute_reply.started":"2022-06-10T02:53:14.667354Z","shell.execute_reply":"2022-06-10T02:53:15.102643Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"image_scaled = cv2.resize(img, None, fx=0.75, fy=0.75)\nprint(image_scaled.shape)\nimshow(\"0.75x scaling - linear interpolation\", image_scaled)","metadata":{"execution":{"iopub.status.busy":"2022-06-10T02:53:15.104265Z","iopub.execute_input":"2022-06-10T02:53:15.104588Z","iopub.status.idle":"2022-06-10T02:53:15.560368Z","shell.execute_reply.started":"2022-06-10T02:53:15.104539Z","shell.execute_reply":"2022-06-10T02:53:15.559637Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"img_scaled2 = cv2.resize(img, None, fx=2, fy=2, interpolation = cv2.INTER_CUBIC )\nprint(img_scaled2.shape)\nimshow(\"2x Scaling - Inter Cubic\", img_scaled2)","metadata":{"execution":{"iopub.status.busy":"2022-06-10T02:53:15.561738Z","iopub.execute_input":"2022-06-10T02:53:15.562184Z","iopub.status.idle":"2022-06-10T02:53:16.236501Z","shell.execute_reply.started":"2022-06-10T02:53:15.562143Z","shell.execute_reply":"2022-06-10T02:53:16.235663Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"img_scaled3 = cv2.resize(img, None, fx = 2, fy=2, interpolation = cv2.INTER_NEAREST)\nprint(img_scaled3.shape)\nimshow(\"2x scaling- Inter Nearest\", img_scaled3)","metadata":{"execution":{"iopub.status.busy":"2022-06-10T02:53:16.237853Z","iopub.execute_input":"2022-06-10T02:53:16.238124Z","iopub.status.idle":"2022-06-10T02:53:16.753302Z","shell.execute_reply.started":"2022-06-10T02:53:16.238092Z","shell.execute_reply":"2022-06-10T02:53:16.75254Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Re-sizing by setting exact  dimensions\nimg_scaled4 = cv2.resize(img, (900, 400), interpolation = cv2.INTER_AREA)\nprint(img_scaled4.shape)\nimshow(\"Scaling- Inter Area\", img_scaled4)","metadata":{"execution":{"iopub.status.busy":"2022-06-10T02:53:16.754567Z","iopub.execute_input":"2022-06-10T02:53:16.75481Z","iopub.status.idle":"2022-06-10T02:53:16.977193Z","shell.execute_reply.started":"2022-06-10T02:53:16.754782Z","shell.execute_reply":"2022-06-10T02:53:16.976245Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<h2 id=\"image_pyraminds\"> Image Pyraminds </h2>","metadata":{}},{"cell_type":"code","source":"image = cv2.imread('/kaggle/working/test/7754.jpg')\n\nsmaller = cv2.pyrDown(image)\nlarger = cv2.pyrUp(smaller)\n\nimshow(\"Original\", image)\nimshow('Smaller', smaller)\nimshow('Larger', larger)\n\neven_smaller = cv2.pyrDown(smaller)\nimshow('Even Smaller', even_smaller)","metadata":{"execution":{"iopub.status.busy":"2022-06-10T02:53:16.978487Z","iopub.execute_input":"2022-06-10T02:53:16.978706Z","iopub.status.idle":"2022-06-10T02:53:18.349715Z","shell.execute_reply.started":"2022-06-10T02:53:16.978678Z","shell.execute_reply":"2022-06-10T02:53:18.348976Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<h2 id=\"cropping\"> Cropping </h2>","metadata":{}},{"cell_type":"code","source":"img = cv2.imread(\"/kaggle/working/test/7757.jpg\")\nimshow(\"Original Image\", img)","metadata":{"execution":{"iopub.status.busy":"2022-06-10T02:53:18.350894Z","iopub.execute_input":"2022-06-10T02:53:18.351093Z","iopub.status.idle":"2022-06-10T02:53:18.696291Z","shell.execute_reply.started":"2022-06-10T02:53:18.351068Z","shell.execute_reply":"2022-06-10T02:53:18.695484Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# get image dimensions\nheight, width = img.shape[:2]\n\nstart_row, start_col = int(height*.25), int(width*.25)\n\n# Let's get the ending pixel coordinates (bottom right)\nend_row, end_col = int(height*.75), int(width*.75)\n\ncropped = img[start_row:end_row, start_col:end_col]\n\ncopy = img.copy()\ncv2.rectangle(copy, (start_col, start_row), (end_col, end_row), (0, 255,255), 10)\n\nimshow(\"Area we are cropping\", copy)\n\nimshow(\"cropped image\", cropped)","metadata":{"execution":{"iopub.status.busy":"2022-06-10T02:53:18.697956Z","iopub.execute_input":"2022-06-10T02:53:18.698229Z","iopub.status.idle":"2022-06-10T02:53:19.332694Z","shell.execute_reply.started":"2022-06-10T02:53:18.698194Z","shell.execute_reply":"2022-06-10T02:53:19.332127Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Adding comma zero in cv2.imread loads image in grayscaled\nimg = cv2.imread(\"/kaggle/working/test/7739.jpg\", 0)\nprint(img.shape)\nimshow(\"Original\", img)","metadata":{"execution":{"iopub.status.busy":"2022-06-10T02:53:19.333643Z","iopub.execute_input":"2022-06-10T02:53:19.334362Z","iopub.status.idle":"2022-06-10T02:53:19.601149Z","shell.execute_reply.started":"2022-06-10T02:53:19.334328Z","shell.execute_reply":"2022-06-10T02:53:19.600305Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# create a matrix , then multiply by 100\nM = np.ones(img.shape, dtype=\"uint8\")*100","metadata":{"execution":{"iopub.status.busy":"2022-06-10T02:53:19.602506Z","iopub.execute_input":"2022-06-10T02:53:19.602994Z","iopub.status.idle":"2022-06-10T02:53:19.608105Z","shell.execute_reply.started":"2022-06-10T02:53:19.602952Z","shell.execute_reply":"2022-06-10T02:53:19.607329Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<h2 id=\"increasing_brightness\"> Increasing Brightness </h2>","metadata":{}},{"cell_type":"code","source":"# Add this matrix M\nadded = cv2.add(img, M)\nimshow(\"Increasing Brightness\", added)\n\n# Just added it\nadded2 = img+M\nimshow(\"Simple Numpy adding results\",added2)\n","metadata":{"execution":{"iopub.status.busy":"2022-06-10T02:53:19.609602Z","iopub.execute_input":"2022-06-10T02:53:19.610035Z","iopub.status.idle":"2022-06-10T02:53:20.139269Z","shell.execute_reply.started":"2022-06-10T02:53:19.609995Z","shell.execute_reply":"2022-06-10T02:53:20.138476Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<h2 id=\"decreasing_brightness\"> Decreasing Brightness </h2>","metadata":{}},{"cell_type":"code","source":"subtracted = cv2.subtract(img, M)\nimshow(\"Subtracted\", subtracted)","metadata":{"execution":{"iopub.status.busy":"2022-06-10T02:53:20.140764Z","iopub.execute_input":"2022-06-10T02:53:20.141436Z","iopub.status.idle":"2022-06-10T02:53:20.394837Z","shell.execute_reply.started":"2022-06-10T02:53:20.141395Z","shell.execute_reply":"2022-06-10T02:53:20.39428Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"subtracted2 = img-M\nimshow(\"Subtracted 2\", subtracted2)","metadata":{"execution":{"iopub.status.busy":"2022-06-10T02:53:20.395716Z","iopub.execute_input":"2022-06-10T02:53:20.396365Z","iopub.status.idle":"2022-06-10T02:53:20.661416Z","shell.execute_reply.started":"2022-06-10T02:53:20.396332Z","shell.execute_reply":"2022-06-10T02:53:20.66064Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<h2 id=\"bitwise_operation\"> Bitwise Operation and Masking </h2>","metadata":{}},{"cell_type":"code","source":"# Making a square\nsquare = np.zeros((300,300), np.uint8)\ncv2.rectangle(square, (50,50), (250,250), 255, -2)\nimshow(\"Square\", square)","metadata":{"execution":{"iopub.status.busy":"2022-06-10T02:53:20.662817Z","iopub.execute_input":"2022-06-10T02:53:20.663187Z","iopub.status.idle":"2022-06-10T02:53:20.900966Z","shell.execute_reply.started":"2022-06-10T02:53:20.663143Z","shell.execute_reply":"2022-06-10T02:53:20.900027Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Making a ellipse\nellipse = np.zeros((300,300), np.uint8)\ncv2.ellipse(ellipse, (150,150), (150, 150), 30, 0, 180, 255, -1)\nimshow(\"ellipse\", ellipse)","metadata":{"execution":{"iopub.status.busy":"2022-06-10T02:53:20.902167Z","iopub.execute_input":"2022-06-10T02:53:20.902373Z","iopub.status.idle":"2022-06-10T02:53:21.152486Z","shell.execute_reply.started":"2022-06-10T02:53:20.902347Z","shell.execute_reply":"2022-06-10T02:53:21.151656Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<h2 id=\"bitwise_operation2\"> Bitwise operations such as AND, OR, XOR and NOT </h2>","metadata":{}},{"cell_type":"code","source":"And = cv2.bitwise_and(square, ellipse)\nimshow(\"AND\", And)\n\nbitwiseOr = cv2.bitwise_or(square, ellipse)\nimshow(\"bitwiseOr\", bitwiseOr)\n\nbitwiseXor = cv2.bitwise_xor(square, ellipse)\nimshow(\"bitwiseXor\", bitwiseXor)\n\nbitwiseNot_sq = cv2.bitwise_not(square)\nimshow(\"bitwiseNot_sq\", bitwiseNot_sq)","metadata":{"execution":{"iopub.status.busy":"2022-06-10T02:53:21.154018Z","iopub.execute_input":"2022-06-10T02:53:21.154475Z","iopub.status.idle":"2022-06-10T02:53:22.264943Z","shell.execute_reply.started":"2022-06-10T02:53:21.154423Z","shell.execute_reply":"2022-06-10T02:53:22.264091Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<h2 id=\"convolutions_blurring\"> Convolutions, Blurring and sharpening </h2>\n1. Convolution Opertion\n2. Blurring\n3. Denoising\n4. Sharpening","metadata":{}},{"cell_type":"markdown","source":"### Blurring","metadata":{}},{"cell_type":"code","source":"img = cv2.imread(\"/kaggle/working/test/7739.jpg\")\nprint(img.shape)\nimshow(\"Original\", img)","metadata":{"execution":{"iopub.status.busy":"2022-06-10T02:53:22.266032Z","iopub.execute_input":"2022-06-10T02:53:22.266234Z","iopub.status.idle":"2022-06-10T02:53:22.579159Z","shell.execute_reply.started":"2022-06-10T02:53:22.266209Z","shell.execute_reply":"2022-06-10T02:53:22.578476Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Creating 3x3 Kernel\nkernel_3x3 = np.ones((3,3), np.float32)/9\n\n# Use the cv2.filter2D to conovlve the kernal with an image\nblurred = cv2.filter2D(img, -1, kernel_3x3)\nimshow(\"3x3 Kernel Blurring\", blurred)\n\n# Creating our 7x7 Kernel\nkernel_7x7 = np.ones((7,7), np.float32)/49\n\nblurred2 = cv2.filter2D(img, -1, kernel_7x7)\nimshow(\"7x7 Kernel Blurring\", blurred2)","metadata":{"execution":{"iopub.status.busy":"2022-06-10T02:53:22.580321Z","iopub.execute_input":"2022-06-10T02:53:22.58071Z","iopub.status.idle":"2022-06-10T02:53:23.224665Z","shell.execute_reply.started":"2022-06-10T02:53:22.580654Z","shell.execute_reply":"2022-06-10T02:53:23.223749Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<h2 id=\"image_noising\"> Blurring methods in OpenCV </h2>\n1. Regular Blurring\n1. Gaussian Blurring\n1. Median Blurring","metadata":{}},{"cell_type":"code","source":"# Averaging done by convolving the image with a normalized box filter.\n# Pixels under the box and replaces the central element\n# Box size needs to odd and positive\nblur = cv2.blur(img, (5,5))\nimshow('Averaging',blur)\n\n# Box filter, gaussian kernel\ngaussian = cv2.GaussianBlur(img, (5,5), 0)\nimshow(\"Gaussian Blurring\", gaussian)\n\n# median of all the pixels under kernel area and central\n# element is replaced with the median value\nmedian = cv2.medianBlur(img, 5)\nimshow(\"Median Blurring\", median)","metadata":{"execution":{"iopub.status.busy":"2022-06-10T02:53:23.226208Z","iopub.execute_input":"2022-06-10T02:53:23.226433Z","iopub.status.idle":"2022-06-10T02:53:24.179385Z","shell.execute_reply.started":"2022-06-10T02:53:23.226406Z","shell.execute_reply":"2022-06-10T02:53:24.178831Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Bilateral Filter\ndst = cv2.bilateralFilter(src, d, sigmaColor, sigmaSpace[, dst[, borderType]])","metadata":{}},{"cell_type":"code","source":"img = cv2.imread(\"/kaggle/working/test/7739.jpg\")\nprint(img.shape)\nimshow(\"Original image\", img)\n\n# Bilateral is very effective in noise removal while keeping edges sharp\nbilateral = cv2.bilateralFilter(img, 9, 75, 75)\nimshow(\"Bilateral Blurring\", bilateral)","metadata":{"execution":{"iopub.status.busy":"2022-06-10T02:53:24.180516Z","iopub.execute_input":"2022-06-10T02:53:24.180929Z","iopub.status.idle":"2022-06-10T02:53:24.822195Z","shell.execute_reply.started":"2022-06-10T02:53:24.180898Z","shell.execute_reply":"2022-06-10T02:53:24.821363Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<h2 id=\"image_noising\"> Image De-noising - Non-Local Means Denoising </h2>\n4 Variations of Non-Local Means Denoising:\n1. cv2.fastNlMeansDenoising()\n2. cv2.fastNlMeansDenoisingColored()\n3. cv2.fastNlMeansDenoisingMulti()\n4. cv2.fastNlMeansDenoisingColoredMulti","metadata":{}},{"cell_type":"code","source":"img = cv2.imread(\"/kaggle/working/test/7739.jpg\")\nprint(img.shape)\nimshow(\"Original image\", img)\n\ndst = cv2.fastNlMeansDenoisingColored(img, None, 6, 6, 7, 21)\nimshow(\"fastNlMeansDenoisingColored\", dst)","metadata":{"execution":{"iopub.status.busy":"2022-06-10T02:53:24.823389Z","iopub.execute_input":"2022-06-10T02:53:24.82364Z","iopub.status.idle":"2022-06-10T02:53:26.174421Z","shell.execute_reply.started":"2022-06-10T02:53:24.823607Z","shell.execute_reply":"2022-06-10T02:53:26.173639Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<h2 id=\"sharpeing_image\"> Sharpening Images </h2>","metadata":{}},{"cell_type":"code","source":"img = cv2.imread(\"/kaggle/working/test/7739.jpg\")\nprint(img.shape)\nimshow(\"Original image\", img)\n\n# Create our shapening kernel, remember it must sum to one\nkernel_sharpening = np.array([[-1, -1,-1],\n                             [-1, 9, -1],\n                             [-1, -1, -1]])\n\n# applying the sharpening kernel to the image\nsharpened = cv2.filter2D(img, -1, kernel_sharpening)\nimshow(\"sharpened Image\", sharpened)","metadata":{"execution":{"iopub.status.busy":"2022-06-10T02:53:26.175998Z","iopub.execute_input":"2022-06-10T02:53:26.176535Z","iopub.status.idle":"2022-06-10T02:53:26.773756Z","shell.execute_reply.started":"2022-06-10T02:53:26.176499Z","shell.execute_reply":"2022-06-10T02:53:26.773019Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<h2 id=\"thresholding\"> Thresholding, Binarization & Adaptive Thresholding </h2>\n\n1. Binarized Images\n1. Thresholiding Methods\n1. Adaptive Thresholding\n1. Skimage's Threshold Local","metadata":{}},{"cell_type":"code","source":"a1 = cv2.imread(\"/kaggle/input/image-processing/foomA.jpg\", 0)\na2 = cv2.imread(\"/kaggle/input/image-processing/rentagriment.jpg\", 0)\nimshow(\"Original Image a1\", a1) \nimshow(\"Original Image a2\", a2)","metadata":{"execution":{"iopub.status.busy":"2022-06-10T02:53:26.774996Z","iopub.execute_input":"2022-06-10T02:53:26.7752Z","iopub.status.idle":"2022-06-10T02:53:27.532658Z","shell.execute_reply.started":"2022-06-10T02:53:26.775174Z","shell.execute_reply":"2022-06-10T02:53:27.531655Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Values below 127 goes to 0 or black, everything above goes to 255 (white)\nret, thresh1 = cv2.threshold(a1, 127, 255, cv2.THRESH_BINARY)\nimshow('1 Threshold Binary @ 127', thresh1)","metadata":{"execution":{"iopub.status.busy":"2022-06-10T02:53:27.534624Z","iopub.execute_input":"2022-06-10T02:53:27.535072Z","iopub.status.idle":"2022-06-10T02:53:27.839729Z","shell.execute_reply.started":"2022-06-10T02:53:27.535038Z","shell.execute_reply":"2022-06-10T02:53:27.838877Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ret, thresh1 = cv2.threshold(a2, 127, 255, cv2.THRESH_BINARY)\nimshow('1 Threshold Binary @ 127', thresh1)","metadata":{"execution":{"iopub.status.busy":"2022-06-10T02:53:27.841118Z","iopub.execute_input":"2022-06-10T02:53:27.841549Z","iopub.status.idle":"2022-06-10T02:53:28.19304Z","shell.execute_reply.started":"2022-06-10T02:53:27.841511Z","shell.execute_reply":"2022-06-10T02:53:28.192433Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Values below 127 go to 255 and values above 127 go to 0 ( reverse of above)\nret, thresh2 = cv2.threshold(a1, 127, 255, cv2.THRESH_BINARY_INV)\nimshow('2 Thrshold Binary Inverse @127', thresh2)\nret, thresh2 = cv2.threshold(a2, 127, 255, cv2.THRESH_BINARY_INV)\nimshow('2 Thrshold Binary Inverse @127', thresh2)","metadata":{"execution":{"iopub.status.busy":"2022-06-10T02:53:28.194143Z","iopub.execute_input":"2022-06-10T02:53:28.194447Z","iopub.status.idle":"2022-06-10T02:53:28.850434Z","shell.execute_reply.started":"2022-06-10T02:53:28.194421Z","shell.execute_reply":"2022-06-10T02:53:28.849629Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Values above 127 are truncated (held) at 127 (the 255 argument is unused)\nret, thresh3 = cv2.threshold(a1, 127, 255, cv2.THRESH_TRUNC)\nimshow(\"3 Thresh Trunc @127\", thresh3)\nret, thresh3 = cv2.threshold(a2, 127, 255, cv2.THRESH_TRUNC)\nimshow(\"3 Thresh Trunc @127\", thresh3)","metadata":{"execution":{"iopub.status.busy":"2022-06-10T02:53:28.851682Z","iopub.execute_input":"2022-06-10T02:53:28.851888Z","iopub.status.idle":"2022-06-10T02:53:29.646523Z","shell.execute_reply.started":"2022-06-10T02:53:28.851862Z","shell.execute_reply":"2022-06-10T02:53:29.645755Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Values below 127 go to 0 above 127 are unchanged\nret, thresh4 = cv2.threshold(a1, 127, 255, cv2.THRESH_TOZERO)\nimshow(\"4 THRESH TO ZERO @ 127\", thresh4)\nret, thresh4 = cv2.threshold(a2, 127, 255, cv2.THRESH_TOZERO)\nimshow(\"4 THRESH TO ZERO @ 127\", thresh4)","metadata":{"execution":{"iopub.status.busy":"2022-06-10T02:53:29.647988Z","iopub.execute_input":"2022-06-10T02:53:29.648298Z","iopub.status.idle":"2022-06-10T02:53:30.350479Z","shell.execute_reply.started":"2022-06-10T02:53:29.648256Z","shell.execute_reply":"2022-06-10T02:53:30.349709Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Reverse of the above, below 127 is unchanged, above 127 goes to 0\nret, thresh5 = cv2.threshold(a1, 127, 255, cv2.THRESH_TOZERO_INV)\nimshow(\"5 THRESH TO ZERO INV @127\", thresh5)\nret, thresh5 = cv2.threshold(a2, 127, 255, cv2.THRESH_TOZERO_INV)\nimshow(\"5 THRESH TO ZERO INV @127\", thresh5)","metadata":{"execution":{"iopub.status.busy":"2022-06-10T02:53:30.351486Z","iopub.execute_input":"2022-06-10T02:53:30.351698Z","iopub.status.idle":"2022-06-10T02:53:30.995635Z","shell.execute_reply.started":"2022-06-10T02:53:30.351672Z","shell.execute_reply":"2022-06-10T02:53:30.994819Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Adaptive Thresholding\n1. ADAPTIVE_THRESH_MEAN_C\n1. THRESH_OTSU","metadata":{}},{"cell_type":"code","source":"# Blur images as it removes noise\n# image = cv2.GaussianBlur(image, (3,3),0)\n\n# Using adaptiveThreshold\nthresh = cv2.adaptiveThreshold(a1, 255, cv2.ADAPTIVE_THRESH_MEAN_C, cv2.THRESH_BINARY, 3, 5)\nimshow(\"Adaptive Mean Thresholding\", thresh)\n\nthresh = cv2.adaptiveThreshold(a2, 255, cv2.ADAPTIVE_THRESH_MEAN_C, cv2.THRESH_BINARY, 3, 5)\nimshow(\"Adaptive Mean Thresholding\", thresh)","metadata":{"execution":{"iopub.status.busy":"2022-06-10T02:53:30.996667Z","iopub.execute_input":"2022-06-10T02:53:30.996871Z","iopub.status.idle":"2022-06-10T02:53:31.661018Z","shell.execute_reply.started":"2022-06-10T02:53:30.996845Z","shell.execute_reply":"2022-06-10T02:53:31.657525Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"_, th2 = cv2.threshold(a1, 0, 255, cv2.THRESH_BINARY+cv2.THRESH_OTSU)\nimshow(\"Otsu's Thresholding\", th2)\n_, th2 = cv2.threshold(a2, 0, 255, cv2.THRESH_BINARY+cv2.THRESH_OTSU)\nimshow(\"Otsu's Thresholding\", th2)","metadata":{"execution":{"iopub.status.busy":"2022-06-10T02:53:31.661942Z","iopub.execute_input":"2022-06-10T02:53:31.662459Z","iopub.status.idle":"2022-06-10T02:53:32.300421Z","shell.execute_reply.started":"2022-06-10T02:53:31.662426Z","shell.execute_reply":"2022-06-10T02:53:32.29962Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Otsu's thresholding after Gaussian filtering\nblur = cv2.GaussianBlur(a1, (5,5), 0)\n_, th3 = cv2.threshold(blur, 0, 255, cv2.THRESH_BINARY+cv2.THRESH_OTSU)\nimshow(\"Gaussian Otsu's Thresholding\", th3)\n\nblur = cv2.GaussianBlur(a2, (5,5),0)\n_, th3 = cv2.threshold(blur, 0, 255, cv2.THRESH_BINARY+cv2.THRESH_OTSU)\nimshow(\"Gaussian Otsu's Thresholding\", th3)","metadata":{"execution":{"iopub.status.busy":"2022-06-10T02:53:32.301596Z","iopub.execute_input":"2022-06-10T02:53:32.301821Z","iopub.status.idle":"2022-06-10T02:53:32.96967Z","shell.execute_reply.started":"2022-06-10T02:53:32.301793Z","shell.execute_reply":"2022-06-10T02:53:32.968813Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Skimage Threshold Local\n\nthreshold_local(image, block_size, offset = 10)","metadata":{}},{"cell_type":"code","source":"from skimage.filters import threshold_local\n\na1 = cv2.imread(\"/kaggle/input/image-processing/foomA.jpg\")\n# get the value component from the HSV color space\n# then we apply adaptive thresholding to\nV = cv2.split(cv2.cvtColor(a1, cv2.COLOR_BGR2HSV))[2] \nT = threshold_local(V, 25, offset = 15, method = \"gaussian\")\n\n# Apply the threshold operation\nthresh = (V >T).astype(\"uint8\")*255\nimshow(\"threshold local\", thresh)","metadata":{"execution":{"iopub.status.busy":"2022-06-10T02:53:32.971227Z","iopub.execute_input":"2022-06-10T02:53:32.971715Z","iopub.status.idle":"2022-06-10T02:53:35.848001Z","shell.execute_reply.started":"2022-06-10T02:53:32.971671Z","shell.execute_reply":"2022-06-10T02:53:35.846965Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<h2 id=\"dilation\"> Dilation, Erosion and Edge Detection </h2>\n1. Dilation\n2. Erosion\n3. Opening\n4. Closing\n5. Canny Edge Detection\n\n**Dilation** - Adds pixels to the boundaries of objects in an image <br/>\n**Erosion** - Removes pixels at the boundaries of objects in an image <br/>\n**Opening** - Erosion followed by dilation <br/>\n**Closing** - Dilation followed by erosion","metadata":{}},{"cell_type":"code","source":"img = cv2.imread(\"/kaggle/input/image-processing/opencv_inv.png\", 0)\nimshow(\"Original image\", img)","metadata":{"execution":{"iopub.status.busy":"2022-06-10T02:53:35.849812Z","iopub.execute_input":"2022-06-10T02:53:35.850404Z","iopub.status.idle":"2022-06-10T02:53:36.194142Z","shell.execute_reply.started":"2022-06-10T02:53:35.850358Z","shell.execute_reply":"2022-06-10T02:53:36.193318Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#  Let's define our kernel size\nkernel = np.ones((5,5), np.uint8)\n\n# Now we erode\nerosion = cv2.erode(img, kernel, iterations = 1)\nimshow(\"Erosion\", erosion)\n\n# Dilate here\ndilation = cv2.dilate(img, kernel, iterations = 1)\nimshow(\"Dilation\", dilation)\n\n# Opening - Good for removing noise\nopening = cv2.morphologyEx(img, cv2.MORPH_OPEN, kernel)\nimshow(\"Opening\", opening)\n\n# Closing - Good for removing noise\nclosing = cv2.morphologyEx(img, cv2.MORPH_CLOSE, kernel)\nimshow(\"Closing\", closing)","metadata":{"execution":{"iopub.status.busy":"2022-06-10T02:53:36.203043Z","iopub.execute_input":"2022-06-10T02:53:36.203548Z","iopub.status.idle":"2022-06-10T02:53:36.964722Z","shell.execute_reply.started":"2022-06-10T02:53:36.2035Z","shell.execute_reply":"2022-06-10T02:53:36.963923Z"},"jupyter":{"source_hidden":true},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Canny Edge Detection","metadata":{}},{"cell_type":"code","source":"img  = cv2.imread(\"/kaggle/input/image-processing/flower.jpg\", 0)\nimshow(\"Original Image\", img)","metadata":{"execution":{"iopub.status.busy":"2022-06-10T02:53:36.965903Z","iopub.execute_input":"2022-06-10T02:53:36.966541Z","iopub.status.idle":"2022-06-10T02:53:37.677151Z","shell.execute_reply.started":"2022-06-10T02:53:36.966506Z","shell.execute_reply":"2022-06-10T02:53:37.676364Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Canny Edge Detection uses gradient values as thresholds\n# The first threshold gradient\ncanny = cv2.Canny(img, 50, 120)\nimshow('Canny 1', canny)\n\n# Wide edge thresholds expect lots of edges\ncanny = cv2.Canny(img, 10, 200)\nimshow('Canny wide', canny)\n\n# Narrow threshold, expect less edges\ncanny = cv2.Canny(img, 200, 240)\nimshow(\"Canny Narrow\", canny)\n\ncanny = cv2.Canny(img, 60, 110)\nimshow(\"Canny 4\", canny)","metadata":{"execution":{"iopub.status.busy":"2022-06-10T02:53:37.678486Z","iopub.execute_input":"2022-06-10T02:53:37.678792Z","iopub.status.idle":"2022-06-10T02:53:40.196979Z","shell.execute_reply.started":"2022-06-10T02:53:37.678752Z","shell.execute_reply":"2022-06-10T02:53:40.196076Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def autoCanny(image):\n    blurred_img = cv2.blur(img, ksize=(5,5))\n    med_val = np.median(img)\n    lower = int(max(0, 0.66*med_val))\n    upper = int(min(255, 1.33*med_val))\n    \n    edges = cv2.Canny(image=img, threshold1 =lower, threshold2=upper)\n    return edges\n\nauto_canny = autoCanny(img)\nimshow(\"auto canny\", auto_canny)","metadata":{"execution":{"iopub.status.busy":"2022-06-10T02:53:40.198229Z","iopub.execute_input":"2022-06-10T02:53:40.198502Z","iopub.status.idle":"2022-06-10T02:53:40.810678Z","shell.execute_reply.started":"2022-06-10T02:53:40.198467Z","shell.execute_reply":"2022-06-10T02:53:40.809925Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Contours?","metadata":{}},{"cell_type":"code","source":"# Load a license plate image\nimage = cv2.imread(\"/kaggle/input/image-processing/LP.jpg\")\nimshow(\"Original image\", image, 30)","metadata":{"execution":{"iopub.status.busy":"2022-06-10T02:53:40.811723Z","iopub.execute_input":"2022-06-10T02:53:40.81208Z","iopub.status.idle":"2022-06-10T02:53:40.946269Z","shell.execute_reply.started":"2022-06-10T02:53:40.812048Z","shell.execute_reply":"2022-06-10T02:53:40.945345Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### cv2.findContours()\ncv2.findContours(image, Retrieval mode, approximation method) <br/>\n#### Retriveval Model\n##### RETR_LIST\n##### RETR_EXTERNAL\n##### RETR_CCOMP\n##### RETR_TREE\n#### Approximation Method Options\n##### cv2.CHAIN_APPROX_NONE\n##### cv2.CHAIN_APPROX_SIMPLE\n","metadata":{}},{"cell_type":"code","source":"# Convert to grayscale\ngray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)\n\n_, th2 = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY+cv2.THRESH_OTSU)\nimshow(\"After thresholding\", th2, 30)\n\n# Finding Contours\n# Use a edged.copy(), since findContours alters the image\ncontours, hierarchy = cv2.findContours(th2, cv2.RETR_LIST, cv2.CHAIN_APPROX_NONE)\n\n# Draw contours, overwrites the input image (inplace)\n# '-1' as the 3rd parameter to draw all\n\ncv2.drawContours(image, contours, -1, (0, 255, 0), thickness=2)\nimshow(\"Contours overlaid\", image, 30)\n\nprint(\"Number of contours found =\" + str(len(contours)) )","metadata":{"execution":{"iopub.status.busy":"2022-06-10T02:53:40.947968Z","iopub.execute_input":"2022-06-10T02:53:40.948496Z","iopub.status.idle":"2022-06-10T02:53:41.305996Z","shell.execute_reply.started":"2022-06-10T02:53:40.948446Z","shell.execute_reply":"2022-06-10T02:53:41.305443Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Contours without threshold","metadata":{}},{"cell_type":"code","source":"image = cv2.imread(\"/kaggle/input/image-processing/LP.jpg\")\n# Convert to grayscale\n\ngray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)\nimshow(\"Original Image\", gray, 30)\n\n# Finding Contours\ncontours, hierarchy  = cv2.findContours(gray, cv2.RETR_LIST, cv2.CHAIN_APPROX_NONE)\n\nimshow(\"Contours overlaid\", image, 30)\n","metadata":{"execution":{"iopub.status.busy":"2022-06-10T02:53:41.306844Z","iopub.execute_input":"2022-06-10T02:53:41.307557Z","iopub.status.idle":"2022-06-10T02:53:41.658811Z","shell.execute_reply.started":"2022-06-10T02:53:41.307525Z","shell.execute_reply":"2022-06-10T02:53:41.658176Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"image = cv2.imread(\"/kaggle/input/image-processing/LP.jpg\")\n# Convert to grayscale\n\ngray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)\nimshow(\"Original Image\", gray, 30)\n\n# Canny Edges\nedged = cv2.Canny(gray, 30, 200)\nimshow(\"Canny Edges\", edged,30)\n\n# Finding Contours\ncontours, hierarchy = cv2.findContours(edged, cv2.RETR_LIST, cv2.CHAIN_APPROX_NONE)\n\ncv2.drawContours(image, contours, -1, (0, 255, 0), thickness= 2)\nimshow(\"Contours overlaid\", image,30)\nprint(\"Number of contours found=\"+ str(len(contours)))","metadata":{"execution":{"iopub.status.busy":"2022-06-10T02:53:41.660021Z","iopub.execute_input":"2022-06-10T02:53:41.660231Z","iopub.status.idle":"2022-06-10T02:53:42.178407Z","shell.execute_reply.started":"2022-06-10T02:53:41.660204Z","shell.execute_reply":"2022-06-10T02:53:42.177795Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"image = cv2.imread(\"/kaggle/input/image-processing/LP.jpg\")\n# Convert to grayscale\n\ngray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)\nimshow(\"Original Image\", gray, 30)\n\n_, th2 = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY+cv2.THRESH_OTSU)\nimshow(\"After thresholding\", th2, 30)\n\n#  copy of image edged.copy()\ncontours, hierarchy = cv2.findContours(th2, cv2.RETR_LIST, cv2.CHAIN_APPROX_NONE)\n\ncv2.drawContours(image, contours, -1, (0, 255, 0), thickness=2)\n\nimshow(\"Contours overlaid\", image, 30)\n","metadata":{"execution":{"iopub.status.busy":"2022-06-10T02:53:42.179703Z","iopub.execute_input":"2022-06-10T02:53:42.180102Z","iopub.status.idle":"2022-06-10T02:53:42.691785Z","shell.execute_reply.started":"2022-06-10T02:53:42.180063Z","shell.execute_reply":"2022-06-10T02:53:42.690934Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"image = cv2.imread(\"/kaggle/input/image-processing/LP.jpg\")\n# Convert to grayscale\n\ngray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)\nimshow(\"Original Image\", gray, 30)\n\n_, th2 = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY+cv2.THRESH_OTSU)\nimshow(\"After thresholding\", th2, 30)\n\n#  copy of image edged.copy()\ncontours, hierarchy = cv2.findContours(th2, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)\n\ncv2.drawContours(image, contours, -1, (0, 255, 0), thickness=2)\n\nimshow(\"Contours overlaid\", image, 30)","metadata":{"execution":{"iopub.status.busy":"2022-06-10T02:53:42.692822Z","iopub.execute_input":"2022-06-10T02:53:42.693024Z","iopub.status.idle":"2022-06-10T02:53:43.371248Z","shell.execute_reply.started":"2022-06-10T02:53:42.692999Z","shell.execute_reply":"2022-06-10T02:53:43.370324Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"image = cv2.imread(\"/kaggle/input/image-processing/LP.jpg\")\n# Convert to grayscale\n\ngray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)\nimshow(\"Original Image\", gray, 30)\n\n_, th2 = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY+cv2.THRESH_OTSU)\nimshow(\"After thresholding\", th2, 30)\n\n#  copy of image edged.copy()\ncontours, hierarchy = cv2.findContours(th2, cv2.RETR_CCOMP, cv2.CHAIN_APPROX_NONE)\n\ncv2.drawContours(image, contours, -1, (0, 255, 0), thickness=2)\n\nimshow(\"Contours overlaid\", image, 30)","metadata":{"execution":{"iopub.status.busy":"2022-06-10T02:53:43.372562Z","iopub.execute_input":"2022-06-10T02:53:43.372814Z","iopub.status.idle":"2022-06-10T02:53:43.898229Z","shell.execute_reply.started":"2022-06-10T02:53:43.372785Z","shell.execute_reply":"2022-06-10T02:53:43.897464Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"image = cv2.imread(\"/kaggle/input/image-processing/LP.jpg\")\n# Convert to grayscale\n\ngray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)\nimshow(\"Original Image\", gray, 30)\n\n_, th2 = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY+cv2.THRESH_OTSU)\nimshow(\"After thresholding\", th2, 30)\n\n#  copy of image edged.copy()\ncontours, hierarchy = cv2.findContours(th2, cv2.RETR_TREE, cv2.CHAIN_APPROX_NONE)\n\ncv2.drawContours(image, contours, -1, (0, 255, 0), thickness=2)\n\nimshow(\"Contours overlaid\", image, 30)","metadata":{"execution":{"iopub.status.busy":"2022-06-10T02:53:43.899409Z","iopub.execute_input":"2022-06-10T02:53:43.899652Z","iopub.status.idle":"2022-06-10T02:53:44.419149Z","shell.execute_reply.started":"2022-06-10T02:53:43.899623Z","shell.execute_reply":"2022-06-10T02:53:44.418329Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"image = cv2.imread(\"/kaggle/input/image-processing/LP.jpg\")\n# Convert to grayscale\n\ngray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)\nimshow(\"Original Image\", gray, 30)\n\n_, th2 = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY+cv2.THRESH_OTSU)\nimshow(\"After thresholding\", th2, 30)\n\n#  copy of image edged.copy()\ncontours, hierarchy = cv2.findContours(th2, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)\n\ncv2.drawContours(image, contours, -1, (0, 255, 0), thickness=2)\n\nimshow(\"Contours overlaid\", image, 30)","metadata":{"execution":{"iopub.status.busy":"2022-06-10T02:53:44.420468Z","iopub.execute_input":"2022-06-10T02:53:44.420709Z","iopub.status.idle":"2022-06-10T02:53:44.941463Z","shell.execute_reply.started":"2022-06-10T02:53:44.420679Z","shell.execute_reply":"2022-06-10T02:53:44.940901Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Finding Contours","metadata":{}},{"cell_type":"code","source":"# Load image\nimage = cv2.imread(\"/kaggle/input/image-processing/bunchofshapes.jpg\")\nimshow(\"Original image\", image, 30)\n\n# Grayscale our image\ngray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)\n\n# Find canny edges\nedged =cv2.Canny(gray, 50, 200)\nimshow('canny Edges', edged,30)\n\n# Finding contours and print how many were found\ncontours, hierarchy = cv2.findContours(edged.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)\nprint(\"Number of contours found=\", len(contours))\n\n# Draw all contours \ncv2.drawContours(image, contours, -1, (0, 255, 0), 3)\nimshow(\"All contours\", image,30)","metadata":{"execution":{"iopub.status.busy":"2022-06-10T02:53:44.942757Z","iopub.execute_input":"2022-06-10T02:53:44.943678Z","iopub.status.idle":"2022-06-10T02:53:45.936148Z","shell.execute_reply.started":"2022-06-10T02:53:44.943633Z","shell.execute_reply":"2022-06-10T02:53:45.935294Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### cv2.ContourArea and cv2.Moments","metadata":{}},{"cell_type":"code","source":"def get_contour_areas(contours):\n    all_areas = []\n    for cnt in contours:\n        area = cv2.contourArea(cnt)\n        all_areas.append(area)\n    return all_areas\n\n# Load our image\nimage = cv2.imread(\"/kaggle/input/image-processing/bunchofshapes.jpg\")\nprint(\"contor areas\")\nprint(get_contour_areas(contours))\nprint(sorted(get_contour_areas(contours), reverse=True))\n\n# Sort contours large to small by area\nsorted_contours = sorted(contours, key=cv2.contourArea, reverse=True)\nprint(\"Contor Area after sorting..\")\nprint(get_contour_areas(sorted_contours))\n\n# Iterate over our contours and draw one at a time\nfor (i, c) in enumerate(sorted_contours):\n    M = cv2.moments(c)\n    #print(M)\n    cx = int(M['m10']/M['m00'])\n    cy = int(M['m01']/M['m00'])\n    cv2.putText(image, str(i+1), (cx, cy), cv2.FONT_HERSHEY_SIMPLEX, 2, (0, 255, 0), 3)\n    cv2.drawContours(image, [c], -1, (255, 0, 0), 3)\n    \nimshow('Contours by area', image, 30)\n    ","metadata":{"execution":{"iopub.status.busy":"2022-06-10T02:53:45.93729Z","iopub.execute_input":"2022-06-10T02:53:45.937494Z","iopub.status.idle":"2022-06-10T02:53:46.286656Z","shell.execute_reply.started":"2022-06-10T02:53:45.937468Z","shell.execute_reply":"2022-06-10T02:53:46.285839Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Sorting by position\ndef x_cord_contour(contours):\n    \"\"\"Return the X cordinate for the cotour centroid\"\"\"\n    if cv2.contourArea(contours) > 10:\n        M = cv2.moments(contours)\n        return (int(M['m10']/M['m00']))\n    else:\n        pass\n    \ndef label_contour_center(image, c):\n    \"\"\"Pleces a red circle on the centers of contours\"\"\"\n    M = cv2.moments(c)\n    cx = int(M['m10']/M['m00'])\n    cy = int(M['m01']/M['m00'])\n    \n    # Draw the countour number on the image\n    cv2.circle(image, (cx, cy), 10, (0,0, 255), -1)\n    return image","metadata":{"execution":{"iopub.status.busy":"2022-06-10T02:53:46.287774Z","iopub.execute_input":"2022-06-10T02:53:46.288002Z","iopub.status.idle":"2022-06-10T02:53:46.295051Z","shell.execute_reply.started":"2022-06-10T02:53:46.287975Z","shell.execute_reply":"2022-06-10T02:53:46.294273Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**We use moments to calculate the center and then use the X cordinate to sort from left to right**","metadata":{}},{"cell_type":"code","source":"# Load image\nimage = cv2.imread(\"/kaggle/input/image-processing/bunchofshapes.jpg\")\nimshow(\"Original image\", image, 30)\noriginal_image =  image.copy()\n\n# Compute center of mass or centroids and draw them on our image\nfor (i, c) in enumerate(contours):\n    orig = label_contour_center(image, c)\n    \n# Showing the contour centers\nimshow(\"Sorting left to right\", image, 30)\n\n# Sort by left to right using our x_cord_contour function\ncontours_left_to_right = sorted(contours, key = x_cord_contour, reverse=False)\n\n# Labeling Contours left to right\nfor (i, c) in enumerate(contours_left_to_right):\n    cv2.drawContours(original_image, [c], -1, (0,0, 255), 3)\n    M = cv2.moments(c)\n    cx = int(M['m10']/ M['m00'])\n    cy = int(M['m01']/ M['m00'])\n    cv2.putText(original_image, str(i+1), (cx, cy), cv2.FONT_HERSHEY_SIMPLEX, 2, (0, 255, 0), 3)\n    (x, y, w, h) = cv2.boundingRect(c)\n    \nimshow(\"Sorting Left to Right\", original_image,30)","metadata":{"execution":{"iopub.status.busy":"2022-06-10T02:53:46.296206Z","iopub.execute_input":"2022-06-10T02:53:46.296415Z","iopub.status.idle":"2022-06-10T02:53:47.299072Z","shell.execute_reply.started":"2022-06-10T02:53:46.296387Z","shell.execute_reply":"2022-06-10T02:53:47.298146Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Load image \nimage = cv2.imread(\"/kaggle/input/image-processing/house.jpg\")\noriginal_image = image.copy()\nimshow(\"Original image\", original_image)\n\n# Grayscale and binarize\ngray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)\nret, thresh = cv2.threshold(gray, 127, 255, cv2.THRESH_BINARY_INV)\n\n# Find Contours\ncontours, hierarchy = cv2.findContours(thresh.copy(), cv2.RETR_LIST, cv2.CHAIN_APPROX_NONE)\ncopy = image.copy()\n\n# Iterate through each contour\nfor c in contours:\n    x,y,w,h = cv2.boundingRect(c)\n    cv2.rectangle(original_image, (x,y), (x+w, y+h), (0,0,255), 2)\n    cv2.drawContours(image, [c], 0, (0, 255, 0), 2)\n    \nimshow(\"Drawing of Contours\", image)\nimshow(\"Bounding Rectangles\", original_image)\n\n# Iterate through each contour and compute the approx contour\nfor c in contours:\n    # Calculate accuracy as a percent of the contour perimeter\n    accuracy = 0.03* cv2.arcLength(c, True)\n    approx = cv2.approxPolyDP(c, accuracy, True)\n    cv2.drawContours(copy, [approx], 0, (0, 255, 0), 2)\n    \nimshow(\"Approx Poly DP\", copy)","metadata":{"execution":{"iopub.status.busy":"2022-06-10T02:53:47.300654Z","iopub.execute_input":"2022-06-10T02:53:47.301216Z","iopub.status.idle":"2022-06-10T02:53:48.634184Z","shell.execute_reply.started":"2022-06-10T02:53:47.301183Z","shell.execute_reply":"2022-06-10T02:53:48.633405Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"image = cv2.imread(\"/kaggle/input/image-processing/hand.jpg\")\noriginal_image = image.copy()\ngray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)\n\nimshow(\"Original Image\", image)\n\n# Threshold the image\nret, thresh = cv2.threshold(gray, 176, 255,0)\n\n# Find contours\ncontours, hierarchy = cv2.findContours(thresh.copy(), cv2.RETR_LIST, cv2.CHAIN_APPROX_NONE)\ncv2.drawContours(image, contours, 0, (0, 255, 0), 2)\nimshow(\"Contours of Hand\", image)\n\n# Sort contours by area and then remove the largest frame contour\nn = len(contours)- 1\ncontours = sorted(contours, key = cv2.contourArea, reverse = False)[:n]\n\n# Iterate through contours and draw the convex hull\nfor c in contours:\n    hull = cv2.convexHull(c)\n    cv2.drawContours(original_image, [hull], 0, (0,255, 0), 2)\n    \nimshow(\"convex hull\", original_image)","metadata":{"execution":{"iopub.status.busy":"2022-06-10T02:53:48.635529Z","iopub.execute_input":"2022-06-10T02:53:48.636745Z","iopub.status.idle":"2022-06-10T02:53:49.409108Z","shell.execute_reply.started":"2022-06-10T02:53:48.636698Z","shell.execute_reply":"2022-06-10T02:53:49.408217Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<h2 id=\"matching_contours\"> Matching Contours <h2>\n**cv2.matchShapes(contour template, contour, method, method parameter)**","metadata":{}},{"cell_type":"code","source":"# Load the shape\nimage = cv2.imread(\"/kaggle/input/image-processing/4star.jpg\", 0)\nimshow(\"Template\", image)\n\n# Load the target image with the shapes we're trying to match\ntarget = cv2.imread('/kaggle/input/image-processing/shapestomatch.jpg')\ntarget_gray = cv2.cvtColor(target, cv2.COLOR_BGR2GRAY)\n\n# Threshold both images first before using cv2.findContours\nret, thresh1 = cv2.threshold(image, 127, 255, 0)\nret, thresh2 = cv2.threshold(target_gray, 127, 255, 0 )\n\n# Find contours in template\ncontours, hierarchy = cv2.findContours(thresh1, cv2.RETR_CCOMP, cv2.CHAIN_APPROX_NONE)\n\n# We need to sort the contours by area so that we can remove the largest\n# contour which is the image outline\n\nsorted_contours = sorted(contours, key=cv2.contourArea, reverse = True)\n\n# we extract the second largest contour which will be our template contour\n\ntemplate_contour = contours[1]\n\n# Extract contours from second target image\ncontours, hierarchy = cv2.findContours(thresh2, cv2.RETR_CCOMP, cv2.CHAIN_APPROX_NONE)\n\nfor c in contours:\n    # use cv2.matchShapes to compare contour shapes\n    match = cv2.matchShapes(template_contour, c, 3, 0.0)\n    print(match)\n    \n    if match < 0.15:\n        closest_contour = c\n    else:\n        closest_contour =[]\n        \ncv2.drawContours(target, [closest_contour], -1, (0,255, 0), 3)\nimshow(\"output\", target)","metadata":{"execution":{"iopub.status.busy":"2022-06-10T02:53:49.410448Z","iopub.execute_input":"2022-06-10T02:53:49.410689Z","iopub.status.idle":"2022-06-10T02:53:49.992615Z","shell.execute_reply.started":"2022-06-10T02:53:49.410661Z","shell.execute_reply":"2022-06-10T02:53:49.991797Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<h2  id=\"overlays\"> Overlays with OpenCV </h2>","metadata":{}},{"cell_type":"code","source":"# load image\nimage = cv2.imread('/kaggle/working/test/5795.jpg')\nimshow(\"Original image\", image)","metadata":{"execution":{"iopub.status.busy":"2022-06-10T02:59:03.261695Z","iopub.execute_input":"2022-06-10T02:59:03.261988Z","iopub.status.idle":"2022-06-10T02:59:03.574212Z","shell.execute_reply.started":"2022-06-10T02:59:03.261956Z","shell.execute_reply":"2022-06-10T02:59:03.573275Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"alpha_val = np.arange(0, 1.1, 0.1)[::-1]\nprint(alpha_val)","metadata":{"execution":{"iopub.status.busy":"2022-06-10T03:01:06.036546Z","iopub.execute_input":"2022-06-10T03:01:06.037264Z","iopub.status.idle":"2022-06-10T03:01:06.04241Z","shell.execute_reply.started":"2022-06-10T03:01:06.037227Z","shell.execute_reply":"2022-06-10T03:01:06.041802Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for alpha in alpha_val:\n    overlay = image.copy()\n    output= image.copy()\n    \n    cv2.rectangle(overlay, (420,205), (595,385), (0,0,255), -1)\n    \n    cv2.putText(overlay, \"Shiva Manhar : alpha={}\".format(alpha), (10,30), cv2.FONT_HERSHEY_SIMPLEX, 1.0, (0, 0, 255), 3)\n    \n    cv2.addWeighted(overlay, alpha, output, 1-alpha, 0, output)\n    print(\"alpha={}, beta={}\".format(alpha, 1 - alpha))\n    imshow(\"Output\", output)","metadata":{"execution":{"iopub.status.busy":"2022-06-10T03:06:24.300018Z","iopub.execute_input":"2022-06-10T03:06:24.300303Z","iopub.status.idle":"2022-06-10T03:06:27.588501Z","shell.execute_reply.started":"2022-06-10T03:06:24.30027Z","shell.execute_reply":"2022-06-10T03:06:27.587689Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}