python - Cython:错误 C2106:'=':左操作数必须是左值
问题描述
我知道有关此错误代码的问题已多次回答,但我找不到任何解决此特定问题的答案。
我正在使用 Cython 构建一个 3D 连接组件算法。当我编译 .pyx 文件时,编译器给出以下错误:
connectedComponent3D.c(3026): error C2106: '=': left operand must be l-value
在 connectedComponent3D.c 文件的第 3026 行,我看到以下代码:
/* "connectedComponent3D.pyx":49
* labels[newItem[0], newItem[1], newItem[2]] = currentComponent
* neighbors = get26Neighbors(newItem[0], newItem[1], newItem[2])
* for neighbor in neighbors: # <<<<<<<<<<<<<<
* if binary3DArray[neighbor[0], neighbor[1], neighbor[2]] == 1:
* labels[neighbor[0], neighbor[1], neighbor[2]] = currentComponent
*/
__pyx_t_31 = (__pyx_v_neighbors + 26);
for (__pyx_t_32 = __pyx_v_neighbors; __pyx_t_32 < __pyx_t_31; __pyx_t_32++) {
__pyx_t_30 = __pyx_t_32;
__pyx_v_neighbor = (__pyx_t_30[0]); //Line 3026
不幸的是,我看不出源代码有什么问题。有人可以帮我解决问题吗?
如果您需要,这里是 connectedComponent3D.pyx:
from collections import deque
import numpy as np
cimport numpy as np
cimport cython
import logging
logging.basicConfig(level=logging.debug)
def main(binary3DArray):
cdef q = deque()
cdef int[:,:,:] binary3DArray_view = binary3DArray
cdef np.ndarray[np.int_t, ndim=3] labels = connectedComponent3D(binary3DArray_view, q)
return labels
@cython.wraparound(False)
@cython.nonecheck(False)
@cython.boundscheck(False)
cdef connectedComponent3D(int[:,:,:] binary3DArray, q):
cdef np.ndarray[np.int_t, ndim=3] labels = np.zeros_like(binary3DArray)
cdef Py_ssize_t zmax = <Py_ssize_t>binary3DArray.shape[0]
cdef Py_ssize_t ymax = <Py_ssize_t>binary3DArray.shape[1]
cdef Py_ssize_t xmax = <Py_ssize_t>binary3DArray.shape[2]
cdef Py_ssize_t z, y, x
cdef int currentComponent = 1
cdef int neighbors[26][3]
cdef int neighbor[3]
cdef int newItem[3]
for z in range(1, zmax-1):
logging.debug(z)
for y in range(1, ymax-1):
for x in range(1, xmax-1):
if binary3DArray[z, y, x] == 1 and labels[z, y, x] == 0:
q.append((z,y,x))
while not q.empty():
newItem = q.popleft()
labels[newItem[0], newItem[1], newItem[2]] = currentComponent
neighbors = get26Neighbors(newItem[0], newItem[1], newItem[2])
for neighbor in neighbors:
if binary3DArray[neighbor[0], neighbor[1], neighbor[2]] == 1:
labels[neighbor[0], neighbor[1], neighbor[2]] = currentComponent
q.append(neighbor)
currentComponent += 1
return labels
@cython.wraparound(False)
@cython.nonecheck(False)
@cython.boundscheck(False)
cdef get26Neighbors(int z, int y, int x):
"""
Input arguments: index -> This is the index(z, y, x) of element whose neighbors are need to be
calculated. type: tuple
Returns: neighbors -> indices of 26-neighbors
This function calculates all 16 neighbors of an element in 3D space.
In order to see what a 26-neighbors is check the 29/38 slide in below link. Left figure is 6-n and
right one is 26-neighbors.
Link: http://slideplayer.com/slide/8645709/
"""
cdef int neighbors[26][3]
neighbors = [[z-1, y-1, x-1], [z-1, y-1, x], [z-1, y-1, x+1],
[z-1, y, x-1], [z-1, y, x], [z-1, y, x+1],
[z-1, y+1, x-1], [z-1, y+1, x], [z-1, y+1, x+1],
[z, y-1, x-1], [z, y-1, x], [z, y-1, x+1],
[z, y, x-1], [z, y, x+1], [z, y+1, x-1],
[z, y+1, x], [z, y+1, x+1], [z+1, y-1, x-1],
[z+1, y-1, x], [z+1, y-1, x+1], [z+1, y, x-1],
[z+1, y, x], [z+1, y, x+1], [z+1, y+1, x-1],
[z+1, y+1, x], [z+1, y+1, x+1]]
return neighbors
解决方案
推荐阅读
- python - 相同元素在迭代期间变得不可点击
- node.js - 如何将模型续集作为参数传递?
- c# - 如何防止在 blazor 中路由
- java - Java RegEx 在每三个空格后拆分
- mysql - 在 MySQL 中添加索引后如何重建现有表中的索引?
- node.js - Heroku H27 客户端请求因服务器发送事件 (SSE) GET 事件而中断
- c++ - std::vector 的 C++ 类成员初始化
- arduino - 尝试在 arduino 中声明常量变量时出现问题
- java - Firebase 实时数据库规则
- python - Crontab 作业未在 django 项目中执行