Direction of sobel operator may be wrong

I am doing sobel exercise in unit2 of course “OpenCV for Robotics”. In course book the sobel operators are set like:

#Apply the horizontal sobel operator with a kernel size of 3
sobelx = cv2.Sobel(img,cv2.CV_64F,1,0,ksize=3)
#Apply the vertical sobel operator with a kernel size of 3
sobely = cv2.Sobel(img,cv2.CV_64F,0,1,ksize=3)

in order to get this kind of result:

But I use the same statement and get the controdictory results. My code is:

#! /usr/bin/env python
import rospy
import cv2
import numpy as np
from sensor_msgs.msg import Image
from cv_bridge import CvBridge, CvBridgeError

class Sobela_Class():

    def __init__(self):

        self.img = cv2.imread(
        self.img = cv2.resize(self.img, (450, 350))
        self.img = cv2.cvtColor(self.img, cv2.COLOR_BGR2GRAY)

    def main(self):

        # sobel operation along with horizontal direction
        self.sobel_x = cv2.Sobel(self.img, cv2.CV_64F, 1, 0, ksize=3)
        # sobel operation along with vertical direction
        self.sobel_y = cv2.Sobel(self.img, cv2.CV_64F, 0, 1, ksize=3)

        # show images
        cv2.imshow('original image', self.img)
        cv2.imshow('sobel_x', self.sobel_x)
        cv2.imshow('sobel_y', self.sobel_y)


if __name__ == '__main__':

    sobela = Sobela_Class()

and results are:

I think there may be a mistake. I am new beginner of OpenCV, but I guess there is similarity in OpenCV and Numpy. For example, the representation of 2D array is defined as [y,x], y for so-called colume, x for so-called row. So maybe here sobel_x = cv2.Sobel(img, cv2.CV_64F, 1, 0, ksize=3) is actually for vertical direction and sobel_x = cv2.Sobel(img, cv2.CV_64F, 0, 1, ksize=3) for horizontal direction.
Please let me know if I am wrong.

Hi @MeineLiebeAxt ,

I am quite familiar with OpenCV because I have worked on edge-detection projects.

Yes, Sobel operation on X axis gives you vertical lines and Sobel operation on Y axis gives you horizontal lines.

The images in the tutorial’s notebook must be swapped. Those are wrong at the moment.


Thank you @girishkumar.kannan for confirmation!

Hello @MeineLiebeAxt @girishkumar.kannan ,

I’ve just fixed the order of the images in the notebook. Thanks for the feedback!

1 Like