{"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":"**Canny Edge Detection is a popular edge detection algorithm.**\n\nLearn more here: https://docs.opencv.org/3.4/da/d22/tutorial_py_canny.html\n\nI thought I'd make a quick gist in case you want to incorporate this into your code. \n[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":{"execution":{"iopub.status.busy":"2022-02-15T22:47:06.445603Z","iopub.execute_input":"2022-02-15T22:47:06.445990Z","iopub.status.idle":"2022-02-15T22:47:06.493926Z","shell.execute_reply.started":"2022-02-15T22:47:06.445882Z","shell.execute_reply":"2022-02-15T22:47:06.493021Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Here are the docs for canny:\nhttps://docs.opencv.org/3.4/dd/d1a/group__imgproc__feature.html#ga04723e007ed888ddf11d9ba04e2232de\n\nBelow is a very basic implementation.\n\n**Canny Edge Detection in OpenCV**\nOpenCV puts all the above in single function, cv.Canny(). \n\nFirst argument is our input image. \n\nSecond and third arguments are our minVal and maxVal respectively. \n\nFourth argument is aperture_size. It is the size of Sobel kernel used for find image gradients. By default it is 3. \n\nLast argument is L2gradient which specifies the equation for finding gradient magnitude. If it is True, it uses the equation mentioned above which is more accurate, otherwise it uses this function: Edge_Gradient(G)=|Gx|+|Gy|. By default, it is False.","metadata":{}},{"cell_type":"code","source":"image = cv2.imread('../input/happy-whale-and-dolphin/train_images/00021adfb725ed.jpg')\nimage = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)\n\nheight, width,_ = image.shape\n\n# Extract Sobel Edges\nsobel_x = cv2.Sobel(image, cv2.CV_64F, 0, 1, ksize=5)\nsobel_y = cv2.Sobel(image, cv2.CV_64F, 1, 0, ksize=5)\n\nplt.figure(figsize=(20, 20))\n\nplt.subplot(3, 2, 1)\nplt.title(\"Original\")\nplt.imshow(image)\n\nplt.subplot(3, 2, 2)\nplt.title(\"Sobel X\")\nplt.imshow(sobel_x)\n\n\nplt.subplot(3, 2, 3)\nplt.title(\"Sobel Y\")\nplt.imshow(sobel_y)\n\nsobel_OR = cv2.bitwise_or(sobel_x, sobel_y)\n\nplt.subplot(3, 2, 4)\nplt.title(\"sobel_OR\")\nplt.imshow(sobel_OR)\n\nlaplacian = cv2.Laplacian(image, cv2.CV_64F)\n\nplt.subplot(3, 2, 5)\nplt.title(\"Laplacian\")\nplt.imshow(laplacian)\n\n\n##  Then, we need to provide two values: threshold1 and threshold2. Any gradient value larger than threshold2\n# is considered to be an edge. Any value below threshold1 is considered not to be an edge. \n#Values in between threshold1 and threshold2 are either classiﬁed as edges or non-edges based on how their \n#intensities are “connected”. In this case, any gradient values below 60 are considered non-edges\n#whereas any values above 120 are considered edges.\n\n\n# Canny Edge Detection uses gradient values as thresholds\n# The first threshold gradient\ncanny = cv2.Canny(image, 50, 120)\n\nplt.subplot(3, 2, 6)\nplt.title(\"Canny\")\nplt.imshow(canny)","metadata":{"execution":{"iopub.status.busy":"2022-02-15T22:47:09.400264Z","iopub.execute_input":"2022-02-15T22:47:09.401351Z","iopub.status.idle":"2022-02-15T22:47:13.664364Z","shell.execute_reply.started":"2022-02-15T22:47:09.401290Z","shell.execute_reply":"2022-02-15T22:47:13.663669Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}