the following code in python detects edge using sobel operator in horizontal as well as vertical direction
import cv2
import numpy as np
img = cv2.imread('image.bmp', cv2.IMREAD_GRAYSCALE)
rows, cols = img.shape
sobel_horizontal = cv2.Sobel(img, cv2.CV_64F, 1, 0, ksize=5)
sobel_vertical = cv2.Sobel(img, cv2.CV_64F, 0, 1, ksize=5)
cv2.imshow('Original', img)
cv2.imshow('Sobel horizontal', sobel_horizontal)
cv2.imshow('Sobel vertical', sobel_vertical)
cv2.waitKey(0)
is there any logic to detect the edge from left to right and vice versa?
When you use double (CV_64F) as a destination type, you can distinguish between left/right (or up/down) edges by the sign of pixel value in the output image (remember that sobel is a smoothed numerical approximation of derivative, so this is quite natural)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With