{"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":"# Thresholding, Binarization & Adaptive Thresholding","metadata":{}},{"cell_type":"markdown","source":"Learn more here about [Thresholding on CV2 site](https://docs.opencv.org/3.4/d7/d1b/group__imgproc__misc.html#gaa9e58d2860d4afa658ef70a9b1115576)\n\nI thought I'd make a quick gist in case you want to incorporate this into your code. [Great notebook here for learning CV2](https://www.kaggle.com/bulentsiyah/learn-opencv-by-examples-with-python).","metadata":{}},{"cell_type":"code","source":"import numpy as np\nimport matplotlib.pyplot as plt\nimport cv2","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-02-16T17:02:37.812278Z","iopub.execute_input":"2022-02-16T17:02:37.812915Z","iopub.status.idle":"2022-02-16T17:02:38.195317Z","shell.execute_reply.started":"2022-02-16T17:02:37.812777Z","shell.execute_reply":"2022-02-16T17:02:38.194642Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Thresholding comes in three forms:**\n\n1. Basic thresholding works extremely well in controlled lighting conditions where you can ensure high contrast between the foreground and background of the image.\n\n2. Otsu’s thresholding attempts to be more dynamic and automatically compute the optimal threshold value based on the input image.\n\n3. Adaptive thresholding breaks the image down into smaller pieces and thresholds each of these pieces separately and individually.\n","metadata":{}},{"cell_type":"code","source":"# Load our new image\nimage = cv2.imread('../input/happy-whale-and-dolphin/train_images/000562241d384d.jpg', 0)\n\nplt.figure(figsize=(30, 30))\nplt.subplot(3, 2, 1)\nplt.title(\"Original\")\nplt.imshow(image)\n\n# Values below 127 goes to 0 (black, everything above goes to 255 (white)\nret,thresh1 = cv2.threshold(image, 127, 255, cv2.THRESH_BINARY)\n\nplt.subplot(3, 2, 2)\nplt.title(\"Threshold Binary\")\nplt.imshow(thresh1)\n\n\n# It's good practice to blur images as it removes noise\nimage = cv2.GaussianBlur(image, (3, 3), 0)\n\n# Using adaptiveThreshold\nthresh = cv2.adaptiveThreshold(image, 255, cv2.ADAPTIVE_THRESH_MEAN_C, cv2.THRESH_BINARY, 3, 5) \n\nplt.subplot(3, 2, 3)\nplt.title(\"Adaptive Mean Thresholding\")\nplt.imshow(thresh)\n\n\n_, th2 = cv2.threshold(image, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)\n\nplt.subplot(3, 2, 4)\nplt.title(\"Otsu's Thresholding\")\nplt.imshow(th2)\n\n\nplt.subplot(3, 2, 5)\n# Otsu's thresholding after Gaussian filtering\nblur = cv2.GaussianBlur(image, (5,5), 0)\n_, th3 = cv2.threshold(blur, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)\nplt.title(\"Guassian Otsu's Thresholding\")\nplt.imshow(th3)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-02-16T17:02:38.196672Z","iopub.execute_input":"2022-02-16T17:02:38.197092Z","iopub.status.idle":"2022-02-16T17:02:43.640203Z","shell.execute_reply.started":"2022-02-16T17:02:38.197046Z","shell.execute_reply":"2022-02-16T17:02:43.639500Z"},"trusted":true},"execution_count":null,"outputs":[]}]}