Cython: ошибка C2106: '=': левый операнд должен иметь значение l - PullRequest
0 голосов
/ 11 июня 2018

Я знаю, что на проблемы с этим кодом ошибки отвечали несколько раз, но я не смог найти ответа, который решает эту конкретную проблему.

Я строю алгоритм связанного компонента 3D с использованием Cython.Когда я компилирую файл .pyx, компилятор выдает следующую ошибку:

connectedComponent3D.c(3026): error C2106: '=': left operand must be l-value

В строке 3026 файла connectedComponent3D.c я вижу следующий код:

            /* "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
...