Sobel Operator || Pixel Constraint I am trying out the Sobel vertical operator to identify vertical edges in a picture. In each image there is one rectangle but the difference is that in 1 image the 2 vertical lines in the rectangle is 1 pixel apart whereas in the other it is more than 1 pixel apart as shown below.

 

Now when i run the sobel vertical filter my expectation is that in both cases i should get 2 edges. But to my surprise it returns 2 edges for the bigger rectangle but only one edge for the smaller rectangle as shown below.

   



My Python code is as follows.

from PIL import Image
import numpy as np
im = Image.open('Test.png').convert('L')
im.save('greyscale.png')
pix_val = np.array(list(im.getdata())).reshape(81,91)
for i in range(12,14):
    for j in range(12,78):
        pix_val[i,j]=255      
for i in range(63,65):
    for j in range(12,78):
        pix_val[i,j]=255
for i in range(14,63):
    for j in range(12,14):
        pix_val[i,j]=255
for i in range(14,63):
    for j in range(76,78):
        pix_val[i,j]=255
Oim1=Image.fromarray(pix_val.astype(np.uint8))
Oim1.save('out1.bmp')
pix_val2=np.delete(pix_val,range(16,75), 1)
Oim2=Image.fromarray(pix_val2.astype(np.uint8))
Oim2.save('out2.bmp')
from scipy import ndimage
dy1 = ndimage.sobel(Oim1, 1)
Oim3=Image.fromarray(dy1.astype(np.uint8))
Oim3.save('Sobel1.bmp')
dy2 = ndimage.sobel(Oim2, 1)
Oim4=Image.fromarray(dy2.astype(np.uint8))
Oim4.save('Sobel2.bmp')


Is this a limitation of sobel operator that it cant detect edges that are very near to each other?
This is not a limitation of the Sobel operator, if the boundaries are clear, even a single pixel wide rectangle should be detectable.

I ran your code, but just used an empty image as the "im" image and it worked fine for me.

Here is my code (similar to yours, just without 'Test.png' and without saving the images):

from PIL import Image
import numpy as np
from scipy import ndimage

im = Image.fromarray(np.zeros((81,91)).astype(np.uint8))
pix_val = np.array(list(im.getdata())).reshape(81,91)
for i in range(12,14):
    for j in range(12,78):
        pix_val[i,j]=255      
for i in range(63,65):
    for j in range(12,78):
        pix_val[i,j]=255
for i in range(14,63):
    for j in range(12,14):
        pix_val[i,j]=255
for i in range(14,63):
    for j in range(76,78):
        pix_val[i,j]=255

Oim1 = Image.fromarray(pix_val.astype(np.uint8))
pix_val2 = np.delete(pix_val,range(16,75), 1)
Oim1.show()

Oim2 = Image.fromarray(pix_val2.astype(np.uint8))
Oim2.show()

dy1 = ndimage.sobel(Oim1, 1)
Oim3 = Image.fromarray(dy1.astype(np.uint8))
Oim3.show()

dy2 = ndimage.sobel(Oim2, 1)
Oim4 = Image.fromarray(dy2.astype(np.uint8))
Oim4.show()


This is what I get: