image = cv2.imread('images.jpg', cv2.IMREAD_GRAYSCALE) 'images.jpg': • This is the file path of the image you want to read. cv2.IMREAD_GRAYSCALE: • This is a flag used with cv2.imread() to specify how the image should be read.
• cv2.IMREAD_GRAYSCALE indicates that the image
should be read as a grayscale image. Note:
Original Image Detected edges
Image gradient • Image gradient is defined as a directional change in image intensity. • Gradient measures the change in pixel intensity in a given direction. • By estimating the direction or orientation along with the magnitude (i.e. how strong the change in direction is), we are able to detect regions of an image that look like edges. • Image gradients are estimated using kernels or filters.
• The first derivative of an image measures the rate of
change of pixel intensity. • It is useful for detecting edges because edges are locations in the image where the intensity changes rapidly.
• The first derivative can be approximated using gradient
operators like the Sobel. • sobel_x = cv2.Sobel(image, cv2.CV_64F, 1, 0, ksize=3) image • The input image (grayscale or color) on which the Sobel operation will be applied. cv2.CV_64F • The desired depth of the output image. CV_64F specifies a 64-bit floating-point output. 1 The order of the derivative in the x-direction (Sobel kernel used for horizontal gradient). 0 The order of the derivative in the y-direction (Sobel kernel used for vertical gradient). ksize : Size of the kernel Example of Sobel Edge Detection • sobel_y = cv2.Sobel(image, cv2.CV_64F, 0, 1, ksize=3) image • The input image (grayscale or color) on which the Sobel operation will be applied. cv2.CV_64F • The desired depth of the output image. CV_64F specifies a 64-bit floating-point output. 0 The order of the derivative in the x-direction (Sobel kernel used for horizontal gradient). 1 The order of the derivative in the y-direction (Sobel kernel used for vertical gradient). ksize : Size of the kernel sobel_edge = cv2.magnitude(sobel_x, sobel_y)
• Computes the magnitude of the gradient for each
pixel in the image. sobel_x: Gradient image in the x-direction (horizontal gradient).
sobel_y: Gradient image in the y-direction (vertical
gradient). laplacian_edge = cv2.Laplacian(image, cv2.CV_64F) • Laplacian is a second order derivative filter. • Laplacian function applies the Laplacian operator to the input image (image) to compute the second derivative of the image intensity.
cv2.CV_64F: Specifies the output image data type
(64-bit floating point) to handle negative Laplacian values correctly. # Apply Gaussian blur for texture extraction gaussian_blur = cv2.GaussianBlur(image, (5, 5), 0) Gaussian Blur Syntax: cv2. GaussianBlur(image, kernelsize, sigmaX ) Image The image you need to blur kernelsize • This parameter specifies the size of the Gaussian kernel • It should be a tuple (width, height) specifying the kernel size. • Both width and height should be odd and positive numbers. 5 x 5 Kernel sigmaX
• The Gaussian kernel standard deviation which is the
default set to 0. cv2.imshow('Original Image', image) cv2.imshow('Sobel Edge Detection', np.uint8(sobel_edge)) cv2.imshow('Laplacian Edge Detection', np.uint8(laplacian_edge))