{"id":858,"date":"2018-09-12T01:30:23","date_gmt":"2018-09-12T01:30:23","guid":{"rendered":"http:\/\/muthu.co\/?p=858"},"modified":"2021-05-24T03:03:26","modified_gmt":"2021-05-24T03:03:26","slug":"sobel-feldman-operator-or-sobel-filter","status":"publish","type":"post","link":"http:\/\/write.muthu.co\/sobel-feldman-operator-or-sobel-filter\/","title":{"rendered":"Sobel Feldman operator or Sobel filter"},"content":{"rendered":"\n

Sobel operator is used in computer vision particularly in edge detection algorithms. The operator uses two 3\u00d73 kernels which are convolved with the original image to calculate the image derivatives \u2013 one for horizontal changes, and one for vertical. If we define A<\/em> as the original image, and Gx<\/em> and Gy<\/em> are two images which at each point contain the horizontal and vertical derivative approximations respectively, the computations are as follows:<\/p>\n\n\n\n

#Let A denote a grayscale image\nA = np.array([[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],\n                   [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],\n                   [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],\n                   [0, 0, 0, 1, 1, 1, 1, 1, 1, 0, 0, 0],\n                   [0, 0, 0, 1, 1, 1, 1, 1, 1, 0, 0, 0],\n                   [0, 0, 0, 1, 1, 1, 1, 1, 1, 0, 0, 0],\n                   [0, 0, 0, 1, 1, 1, 1, 1, 1, 0, 0, 0],\n                   [0, 0, 0, 1, 1, 1, 1, 1, 1, 0, 0, 0],\n                   [0, 0, 0, 1, 1, 1, 1, 1, 1, 0, 0, 0],\n                   [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],\n                   [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],\n                   [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]])<\/code><\/pre>\n\n\n\n

The image output is:<\/p>\n\n\n\n

\"\"<\/a><\/figure><\/div>\n\n\n\n

Now, we have to find the horizontal and vertical derivative of the input image using the kernels as shown below.<\/p>\n\n\n\n

\"\"<\/figure><\/div>\n\n\n\n

Convolution process:<\/strong><\/p>\n\n\n\n

from scipy import signal\n\nkernel_horizontal = np.array([[-1, 0, 1], [-2, 0, 2], [-1, 0, 1]])\nkernel_vertical   = np.array([[1, 2, 1], [0, 0, 0], [-1, -2, -1]]) \n\nGx = signal.convolve2d(A, kernel_horizontal, boundary='symm', mode='same')\nGy = signal.convolve2d(A, kernel_vertical, boundary='symm', mode='same')\n\n<\/code><\/pre>\n\n\n\n

output images after convolution:<\/p>\n\n\n\n

\"\"<\/a><\/figure><\/div>\n\n\n\n

Now we find the sum of squares of the gradients to get the gradient magnitude.<\/p>\n\n\n\n

Gradient magnitude:<\/strong><\/p>\n\n\n\n

\"\"<\/a><\/td>\n
gradient_magnitude = np.sqrt(Gx * Gx + Gy * Gy)<\/pre>\n

 <\/p>\n<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n

\"\"<\/a><\/figure><\/div>\n\n\n\n

The above output may not seem like getting us anywhere but when the same is applied to a full real image, this is the output I get.<\/p>\n\n\n\n

\"\"<\/a>
Original image<\/figcaption><\/figure><\/div>\n\n\n\n
\"\"<\/a>
After Sobel Filter<\/figcaption><\/figure><\/div>\n\n\n\n

You can find the full source of the above examples here:<\/p>\n\n\n\n

Sobel filter a simple image array.ipynb<\/a><\/p>\n\n\n\n

Sobel Feldman filter colored image.ipynb<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"

Sobel operator is used in computer vision particularly in edge detection algorithms. The operator uses two 3\u00d73 kernels which are convolved with the original image to calculate the image derivatives \u2013 one for horizontal changes, and one for vertical. If we define A as the original image, and Gx and Gy are two images which […]<\/p>\n","protected":false},"author":1,"featured_media":869,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[38],"tags":[47],"_links":{"self":[{"href":"http:\/\/write.muthu.co\/wp-json\/wp\/v2\/posts\/858"}],"collection":[{"href":"http:\/\/write.muthu.co\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/write.muthu.co\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/write.muthu.co\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/write.muthu.co\/wp-json\/wp\/v2\/comments?post=858"}],"version-history":[{"count":2,"href":"http:\/\/write.muthu.co\/wp-json\/wp\/v2\/posts\/858\/revisions"}],"predecessor-version":[{"id":1885,"href":"http:\/\/write.muthu.co\/wp-json\/wp\/v2\/posts\/858\/revisions\/1885"}],"wp:featuredmedia":[{"embeddable":true,"href":"http:\/\/write.muthu.co\/wp-json\/wp\/v2\/media\/869"}],"wp:attachment":[{"href":"http:\/\/write.muthu.co\/wp-json\/wp\/v2\/media?parent=858"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/write.muthu.co\/wp-json\/wp\/v2\/categories?post=858"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/write.muthu.co\/wp-json\/wp\/v2\/tags?post=858"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}