5

Long story short, I'm just simply trying to get a canny edged image of image.jpg.

The documentation is very spotty so I'm getting very confused. If anyone can help that'd be greatly appreciated.

from scipy import misc
import numpy as np
from skimage import data
from skimage import feature
from skimage import io


im=misc.imread('image1.jpg')
edges1 = feature.canny(im)
...

And I'm getting this error

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

Can anyone explain how to create a 2D array from an image file? Thanks!

user1985351
  • 4,589
  • 6
  • 22
  • 25

2 Answers2

9

I suspect image1.jpg is a color image, so im is 3D, with shape (num_rows, num_cols, num_color_channels). One option is to tell imread to flatten the image into a 2D array by giving it the argument flatten=True:

im = misc.imread('image1.jpg', flatten=True)

Or you could apply canny to just one of the color channels, e.g.

im = misc.imread('image1.jpg')
red_edges = feature.canny(im[:, :, 0])
Warren Weckesser
  • 110,654
  • 19
  • 194
  • 214
0

The canny edge detection needs a grayscale image input in order to work. You can convert 3D (color) images to 2D (grayscale) using the rgb2gray module in scikit-image.

from skimage import io, features
from skimage.color import rgb2gray

image = rgb2gray(io.imread("image.png"))
edges = feature.canny(image)
nem
  • 1
  • 1