4

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?

2
  • 1
    In the heading you're talking about horizontal and vertical, yet your question is about "left to right and vice versa", meaning left to right and right to left. Thus I'm not sure if you're question is about why vertical and horizontal directions separated or if there would be a reason to use vertical Sobel with normal and opposite sign. Commented Mar 15, 2017 at 8:32
  • My question is how to make detection from left to right or vice versa if the derivative is vertical Commented Mar 15, 2017 at 9:13

2 Answers 2

2

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)

Sign up to request clarification or add additional context in comments.

4 Comments

Yes, but does it use the same kernel as that of Sobel operator for convolution?
I can't say for sure right now, but I don't see a reason to think otherwise
According to the doc, for kernel sizes > 1 a separable kernel is used (faster than naive approach, yields same result), also it is possible to use Scharr kernel(more accurate derivative approximation), but in your snippet it's not used
I don't see why this answer is downvoted. It answers the question. Sobel result from right to left is the result from left to right taken with the opposite sign.
1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.