我知道有关此错误代码的问题已经多次回答,但我无法找到解决此特定问题的任何答案。
我正在使用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