Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sobel Edge detection in Python and Opencv

Tags:

python

opencv

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?

like image 373
omkar pathak Avatar asked Sep 14 '25 01:09

omkar pathak


1 Answers

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)

like image 75
alexisrozhkov Avatar answered Sep 15 '25 16:09

alexisrozhkov