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?
当您使用 double (CV_64F) 作为目标类型时,您可以通过输出图像中像素值的符号来区分左/右(或上/下)边缘(请记住,sobel 是导数的平滑数值近似,因此这很自然)
you can follow the code.
import cv2
img = cv2.imread('type you images path here / image.jpg')
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
x = cv2.Sobel(gray, ddept, 1,0, ksize=3, scale=1)
y = cv2.Sobel(gray, ddept, 0,1, ksize=3, scale=1)
absx= cv2.convertScaleAbs(x)
absy = cv2.convertScaleAbs(y)
edge = cv2.addWeighted(absx, 0.5, absy, 0.5,0)
cv2.imshow('edge', edge)
cv2.waitKey(0)
cv2.destroyAllWindows()
or you could download the file from Github.
Github link : https://github.com/Angileca/Sobel-edge-detection
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.