0

I'm using Python + Scipy + Scikit-image + numpy for the first time.

I'm using this page for help, and I've only changed the given code a bit to pick up an image of my liking:

tree = misc.imread('C:\\Users\\app\\Pictures\\treephoto1.jpg')
type(tree)
<type 'numpy.ndarray' >
tree.shape, tree.dtype((512, 512), dtype('uint8'))

But I'm getting the following error:

 type(tree) <type 'numpy.ndarray'>
                                   ^
SyntaxError: invalid syntax

What's wrong with the syntax? I'm using python 2.7 on Windows, and all the related toolkits are also according to Python 2.7.

I need to convert the image to a 2-D numpy array so that I can use the canny edge detector with it.

user961627
  • 12,379
  • 42
  • 136
  • 210

1 Answers1

3

Writing without thinking may be dangerous but in your case it is just wrong. The page you've mentioned shows this:

>>> lena = misc.imread('lena.png')
>>> type(lena)
<type 'numpy.ndarray'>
>>> lena.shape, lena.dtype
((512, 512), dtype('uint8'))

>>> is Python's interactive console prompt string. It is there were commands go.

<type 'numpy.ndarray'> and ((512, 512), dtype('uint8')) are results of commands. So your corresponding code should be only

tree = misc.imread('C:\\Users\\app\\Pictures\\treephoto1.jpg')
type(tree)
tree.shape, tree.dtype

Notice that

type(tree)
tree.shape, tree.dtype

do nothing and just show you information about your image data.

Update

Basically (not always) your image is layered. RGB is three separate layers. So if your filtering isn't aware of it you'll have to separate layers yourself.

Hope that helps.

twil
  • 6,032
  • 1
  • 30
  • 28
  • I see! Thanks. Okay I think I missed out an important word in the question, I'm editing it now. I want the numpy array to be 2D. I learned that images are by default 2-D arrays, but when I run "edges = filter.canny(tree)", it gives me an error saying that the input image must be a 2-D array. – user961627 Apr 03 '14 at 18:48
  • Alright I just found out! I can convert it to a grayscale image to turn it into a 2D array. I did this: graytree = color.rgb2gray(tree). Just a side question: are most image processing algorithms carried out on grayed images? – user961627 Apr 03 '14 at 19:41