0

I have raster with one band and I would like to do edge detection with Canny edge filter.

This is my raster:

enter image description here

I have opened it with rasterio and covert it into array :

img=srs.open("raster.tif")
array=img.read()

Then i'm trying to apply the Canny filter as following (based on this: https://scikit-image.org/docs/dev/auto_examples/edges/plot_canny.html):

# Compute the Canny filter for two values of sigma
edges1 = feature.canny(array)
edges2 = feature.canny(array,sigma=3)

but it fails with this error:

ValueError: The parameter image must be a 2-dimensional array

I read that this can happpen if my image is not in grey scale, but I have shown it as grey scale:

show((img,1),cmap='Greys_r')

so I tried to checl what is my array type:

type(array)
>>>numpy.ndarray

so I have no idea why I get this error message saying the image is not 2 dimensoinal as it is 1 band with X and Y axis.

My end goal - to be able to apply Canny edge on my image and to understand why I get this error

2
  • @BERA (1302, 2115) – Reut Jun 25 '20 at 14:28
  • well I just used reshape in it worked, but I was afraid to loost the coordinates data – Reut Jun 25 '20 at 14:30
2

Sometimes an image "looks" gray, but actually has 3 color channels with the same red, green, and blue values. To check that your image is grayscale, check array.shape rather than type(array). If it is (X, Y, 3), then you have an RGB image. To convert, you can use skimage.color.rgb2gray, and then pass the result to canny.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.