|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "markdown", |
| 5 | + "metadata": {}, |
| 6 | + "source": [ |
| 7 | + "# Gradients and Edge Detection" |
| 8 | + ] |
| 9 | + }, |
| 10 | + { |
| 11 | + "cell_type": "markdown", |
| 12 | + "metadata": {}, |
| 13 | + "source": [ |
| 14 | + "## Gradients of an Image" |
| 15 | + ] |
| 16 | + }, |
| 17 | + { |
| 18 | + "cell_type": "code", |
| 19 | + "execution_count": 1, |
| 20 | + "metadata": {}, |
| 21 | + "outputs": [], |
| 22 | + "source": [ |
| 23 | + "import numpy as np\n", |
| 24 | + "import cv2" |
| 25 | + ] |
| 26 | + }, |
| 27 | + { |
| 28 | + "cell_type": "code", |
| 29 | + "execution_count": 2, |
| 30 | + "metadata": {}, |
| 31 | + "outputs": [], |
| 32 | + "source": [ |
| 33 | + "image = cv2.imread('sample_image.jpg')\n", |
| 34 | + "cv2.imshow(\"Original\", image)\n", |
| 35 | + "cv2.waitKey(0)\n", |
| 36 | + "cv2.destroyAllWindows()" |
| 37 | + ] |
| 38 | + }, |
| 39 | + { |
| 40 | + "cell_type": "code", |
| 41 | + "execution_count": 3, |
| 42 | + "metadata": {}, |
| 43 | + "outputs": [], |
| 44 | + "source": [ |
| 45 | + "gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)\n", |
| 46 | + "cv2.imshow(\"Grayscaled Image\", gray_image)\n", |
| 47 | + "cv2.waitKey(0)\n", |
| 48 | + "cv2.destroyAllWindows()" |
| 49 | + ] |
| 50 | + }, |
| 51 | + { |
| 52 | + "cell_type": "markdown", |
| 53 | + "metadata": {}, |
| 54 | + "source": [ |
| 55 | + "### Laplacian Gradient" |
| 56 | + ] |
| 57 | + }, |
| 58 | + { |
| 59 | + "cell_type": "code", |
| 60 | + "execution_count": 4, |
| 61 | + "metadata": {}, |
| 62 | + "outputs": [], |
| 63 | + "source": [ |
| 64 | + "lap_gradient = cv2.Laplacian(gray_image, cv2.CV_64F)\n", |
| 65 | + "lap_gradient = np.uint8(np.absolute(lap_gradient))\n", |
| 66 | + "cv2.imshow(\"Laplacian Gradient\", lap_gradient)\n", |
| 67 | + "cv2.waitKey(0)\n", |
| 68 | + "cv2.destroyAllWindows()" |
| 69 | + ] |
| 70 | + }, |
| 71 | + { |
| 72 | + "cell_type": "markdown", |
| 73 | + "metadata": {}, |
| 74 | + "source": [ |
| 75 | + "### Sobel Gradient" |
| 76 | + ] |
| 77 | + }, |
| 78 | + { |
| 79 | + "cell_type": "code", |
| 80 | + "execution_count": 5, |
| 81 | + "metadata": {}, |
| 82 | + "outputs": [], |
| 83 | + "source": [ |
| 84 | + "sobelX = cv2.Sobel(gray_image, cv2.CV_64F, 1, 0) # For Vertical Edge Features\n", |
| 85 | + "sobelY = cv2.Sobel(gray_image, cv2.CV_64F, 0, 1) # For Horizontal Edge Features\n", |
| 86 | + "sobelX = np.uint8(np.absolute(sobelX))\n", |
| 87 | + "sobelY = np.uint8(np.absolute(sobelY))\n", |
| 88 | + "sobelCombined = cv2.bitwise_or(sobelX, sobelY)\n", |
| 89 | + "cv2.imshow(\"SobelX Gradient\", sobelX)\n", |
| 90 | + "cv2.imshow(\"SobelY Gradient\", sobelY)\n", |
| 91 | + "cv2.imshow(\"Sobel Combined Gradient\", sobelCombined)\n", |
| 92 | + "cv2.waitKey(0)\n", |
| 93 | + "cv2.destroyAllWindows()" |
| 94 | + ] |
| 95 | + }, |
| 96 | + { |
| 97 | + "cell_type": "markdown", |
| 98 | + "metadata": {}, |
| 99 | + "source": [ |
| 100 | + "## Canny Edge Detection" |
| 101 | + ] |
| 102 | + }, |
| 103 | + { |
| 104 | + "cell_type": "code", |
| 105 | + "execution_count": 7, |
| 106 | + "metadata": {}, |
| 107 | + "outputs": [], |
| 108 | + "source": [ |
| 109 | + "blurred_image = cv2.GaussianBlur(gray_image, (5, 5), 0)\n", |
| 110 | + "cv2.imshow(\"Blurred Image\", blurred_image)\n", |
| 111 | + "\n", |
| 112 | + "canny_edge_image = cv2.Canny(blurred_image, 30, 150)\n", |
| 113 | + "cv2.imshow(\"Canny Edge Detection\", canny_edge_image)\n", |
| 114 | + "\n", |
| 115 | + "cv2.waitKey(0)\n", |
| 116 | + "cv2.destroyAllWindows()" |
| 117 | + ] |
| 118 | + } |
| 119 | + ], |
| 120 | + "metadata": { |
| 121 | + "kernelspec": { |
| 122 | + "display_name": "Python 3", |
| 123 | + "language": "python", |
| 124 | + "name": "python3" |
| 125 | + }, |
| 126 | + "language_info": { |
| 127 | + "codemirror_mode": { |
| 128 | + "name": "ipython", |
| 129 | + "version": 3 |
| 130 | + }, |
| 131 | + "file_extension": ".py", |
| 132 | + "mimetype": "text/x-python", |
| 133 | + "name": "python", |
| 134 | + "nbconvert_exporter": "python", |
| 135 | + "pygments_lexer": "ipython3", |
| 136 | + "version": "3.5.2" |
| 137 | + } |
| 138 | + }, |
| 139 | + "nbformat": 4, |
| 140 | + "nbformat_minor": 2 |
| 141 | +} |
0 commit comments