Sobel Edge detection in Python and Opencv

13,743

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)

Share:
13,743
omkar pathak
Author by

omkar pathak

Updated on July 12, 2022

Comments

  • omkar pathak
    omkar pathak almost 2 years

    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?

  • Jeru Luke
    Jeru Luke about 7 years
    Yes, but does it use the same kernel as that of Sobel operator for convolution?
  • alexisrozhkov
    alexisrozhkov about 7 years
    I can't say for sure right now, but I don't see a reason to think otherwise
  • alexisrozhkov
    alexisrozhkov about 7 years
    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
  • m3h0w
    m3h0w about 7 years
    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.