Cython:错误C2106:“=”:左操作数必须是lvalu

2024-04-29 23:16:29 发布

您现在位置:Python中文网/ 问答频道 /正文

我知道关于这个错误代码的问题被回答了好几次,但是我找不到任何解决这个特定问题的答案。你知道吗

我正在使用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

Tags: inpylabelsnpneighborscythonintpyx