简体   繁体   English

在python中将图像转换为numpy数组

[英]Converting image to numpy array in python

I'm using Python + Scipy + Scikit-image + numpy for the first time. 我是第一次使用Python + Scipy + Scikit-image + numpy。

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. 我在Windows上使用的是python 2.7,所有相关的工具包也都遵循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. 我需要将图像转换为二维numpy数组,以便可以将Canny边缘检测器与其一起使用。

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. >>>是Python的交互式控制台提示字符串。 It is there were commands go. 那里有命令去了。

<type 'numpy.ndarray'> and ((512, 512), dtype('uint8')) are results of commands. <type 'numpy.ndarray'>((512, 512), dtype('uint8'))是命令的结果 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. RGB是三个独立的层。 So if your filtering isn't aware of it you'll have to separate layers yourself. 因此,如果您不知道过滤条件,则必须自己分离图层。

Hope that helps. 希望能有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM