3

I am trying to use sobel filter on an image of a wall but it doesn't work.

My code is :

im=scipy.misc.imread('IMG_1479bis.JPG')
im = im.astype('int32')
dx=ndimage.sobel(im,1)
dy=ndimage.sobel(im,0)
mag=np.hypot(dx,dy)
mag*=255.0/np.max(mag)
cv2.imshow('sobel.jpg', mag)

enter image description here

I really don't understand where is my mistake. Any help would be appreciated ! Thanks in advance !

Pauline1006
  • 127
  • 1
  • 9
  • 1
    Are you getting any errors or does nothing happen? – Scott May 19 '15 at 22:55
  • 1
    *"...it doesn't work."* Please explain what that means. If you get an error, show the complete traceback. If the output is not what you expected, show what you got, and explain what you expected. – Warren Weckesser May 19 '15 at 23:41

1 Answers1

4

This is a difficult image to apply simple edge detection due to the stone and concrete textures. The texture makes it almost as though you have a very noisy image to which you are applying first derivative. You'll end up with many small undesired edges.

Here is your code working (not resulting in error):

import scipy.ndimage as nd
import numpy as np
import matplotlib.pyplot as plt

im = scipy.ndimage.imread('ygqCd.jpg', True)
im = im.astype('int32')
dx = nd.sobel(im,1)
dy = nd.sobel(im,0)
mag = np.hypot(dx,dy)
mag *= 255.0/np.max(mag) 

fig, ax = plt.subplots()
ax.imshow(mag, cmap = 'gray')
plt.xticks([]), plt.yticks([])
plt.show()

The image read line scipy.ndimage.imread('ygqCd.jpg', True) is converting to gray-scale. See ndimage.imread

And output image (very noisy as expected): enter image description here

I'm more familiar with cv2. I played with the image a bit with cv2.sobel and by median filtering before applying sobel. Here are the results, not terrific but a good start:

enter image description here

and the code to generate the images:

import cv2
import numpy as np
from matplotlib import pyplot as plt

def interval_mapping(image, from_min, from_max, to_min, to_max):
    from_range = from_max - from_min
    to_range = to_max - to_min
    scaled = np.array((image - from_min) / float(from_range), dtype=float)
    return to_min + (scaled * to_range)

img = cv2.imread('ygqCd.jpg', 0)
blurred_img = cv2.medianBlur(img, 11)

s_mask = 17

sobelx = np.abs(cv2.Sobel(img, cv2.CV_64F, 1, 0, ksize=s_mask))
b_sobelx = np.abs(cv2.Sobel(blurred_img, cv2.CV_64F, 1, 0, ksize=s_mask))
sobelx = interval_mapping(sobelx, np.min(sobelx), np.max(sobelx), 0, 255)
b_sobelx = interval_mapping(b_sobelx, np.min(sobelx), np.max(sobelx), 0, 255)

sobely = np.abs(cv2.Sobel(img,cv2.CV_64F,0,1,ksize=s_mask))
sobely = interval_mapping(sobely, np.min(sobely), np.max(sobely), 0, 255)
b_sobely = np.abs(cv2.Sobel(blurred_img, cv2.CV_64F, 0, 1, ksize=s_mask))
b_sobely = interval_mapping(b_sobely, np.min(sobely), np.max(sobely), 0, 255)

sobel_xy = 0.5 * sobelx + 0.5 * sobely
b_sobel_xy = 0.5 * b_sobelx + 0.5 * b_sobely

fig = plt.figure(figsize=(10, 14))
plt.subplot(3,2,1),plt.imshow(sobelx,cmap = 'gray')
plt.title('Sobel X'), plt.xticks([]), plt.yticks([])
plt.subplot(3,2,2),plt.imshow(b_sobelx,cmap = 'gray')
plt.title('Blurred Sobel X'), plt.xticks([]), plt.yticks([])
plt.subplot(3,2,3),plt.imshow(sobely,cmap = 'gray')
plt.title('Sobel Y'), plt.xticks([]), plt.yticks([])
plt.subplot(3,2,4),plt.imshow(b_sobely,cmap = 'gray')
plt.title('Blurred Sobel Y'), plt.xticks([]), plt.yticks([])
plt.subplot(3,2,5),plt.imshow(sobel_xy,cmap = 'gray')
plt.title('Sobel XY'), plt.xticks([]), plt.yticks([])
plt.subplot(3,2,6),plt.imshow(b_sobel_xy,cmap = 'gray')
plt.title('Blurred Sobel XY'), plt.xticks([]), plt.yticks([])
plt.tight_layout()

plt.show()
Scott
  • 6,089
  • 4
  • 34
  • 51