简体   繁体   English

如何从PyArrayObject获取值?

[英]How to get values from PyArrayObject?

I try to get values from list. 我尝试从列表中获取值。 It's defined in Python as: 在Python中定义为:

Line1 = [[[146.0, 560.0], [337.0, 478.0]], 
         [[204.0, 612.0], [397.0, 536.0]],
         [[166.0, 437.0], [350.0, 434.0]], 
         [[166.0, 552.0], [350.0, 546.0]],
         [[146.0, 560.0], [337.0, 478.0]], 
         [[204.0, 612.0], [397.0, 536.0]],
         [[166.0, 437.0], [350.0, 434.0]], 
         [[166.0, 552.0], [350.0, 546.0]]] 

parsed into PyArrayObject by: 通过以下方式解析为PyArrayObject:

PyArrayObject *lineA = (PyArrayObject*) PyArray_FROMANY (objLineA, PyArray_DOUBLE, 3, 3, NPY_IN_ARRAY);
if (!lineA) {
    PyErr_SetString(PyExc_ValueError, "expected [Layer][point][x] ");
       return NULL;
}

Till now everything seems to be OK, dimensions are OK. 到目前为止,一切似乎都还可以,尺寸还可以。 But when im trying: 但是当我尝试时:

struct pPoint {
    float x;
    float y;
};
...

struct pPoint p1a, p1b;

...
    p1a.x = *((float * )PyArray_GETPTR3(lineA, 0, 0, 0));
    p1a.y = *((float * )PyArray_GETPTR3(lineA, 0, 0, 1));
    p1b.x = *((float * )PyArray_GETPTR3(lineA, 0, 1, 0));
    p1b.y = *((float * )PyArray_GETPTR3(lineA, 0, 1, 1));
...

all values are ZERO. 所有值均为零。

What is a best and easiest way to get values from PyObjArray? 从PyObjArray获取值的最佳和最简单的方法是什么? Why 0.0? 为什么是0.0?

I met the same problem. 我遇到了同样的问题。 When I print out the value, it is always 0.0. 当我打印出该值时,它始终为0.0。 Try to change all 'float' to 'double' in C. The float type in Python is the double type in C. You also claimed its type as PyArray_DOUBLE. 尝试将C中的所有'float'更改为'double'。Python中的float类型是C中的double类型。您还声明了其类型为PyArray_DOUBLE。

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

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