Пример #1
0
def runProgram(image: np.ndarray,
               PS: programState) -> Union[programState, BaseException]:
    """
    Executes all steps from the image
    :param image: input image
    :param PS: current program state with which to make the next step
    :return: Either the last program state, or a runtime exception
    """
    newState = copy.deepcopy(PS)

    if colors.isBlack(imageWrapper.getPixel(image, newState.position)):
        return errors.inBlackPixelError(
            "Programstate starts in black pixel at {}".format(
                newState.position))

    currentCodel = imageWrapper.getCodel(image, newState.position)
    newGraph = newState.graph.graph
    graphNode = newGraph[currentCodel]
    newToken = graphNode.graphNode[newState.direction][0]

    if isinstance(newToken, tokens.terminateToken):
        return newState

    newState = takeStep(image, newState)
    if isinstance(newState, BaseException):
        return newState

    return runProgram(image, newState)
Пример #2
0
    def updateEdgesInfo(self, image, inputGraph, programState):
        edgesInfo = self.builder.get_object('codelEdgesMessage',
                                            self.generalInfo)

        if colors.isBlack(imageWrapper.getPixel(image, programState.position)):
            edgesInfo.configure(
                text="Black pixels are no codel, and have no edges")
            return None

        codel = imageWrapper.getCodel(image, programState.position)
        baseString = "Next step will be:\n"

        graphNode = inputGraph.graph[codel]

        edge = graphNode.graphNode[programState.direction]
        baseString += self.getEdgeDescription(edge, programState.direction)

        baseString += "\nCodel edges are as follows:\n"
        #Generate pointers
        edgePointers = list(
            map(lambda i: direction((i % 4, int(i / 4))), iter(range(8))))
        for edgePointer in edgePointers:
            edge = graphNode.graphNode[edgePointer]
            baseString += self.getEdgeDescription(edge, edgePointer)
        edgesInfo.configure(text=baseString)
Пример #3
0
def graphImage(image: np.ndarray) -> Tuple[graph, List[BaseException]]:
    """
    Returns a dict with hashes of each codel as keys, and a codelDict as value. That codelDict contains hashed pointers (Tuple[int, int]) as keys to tokens as values.
    :param image:
    :return:
    """
    coords = np.ndindex(image.shape[1], image.shape[0])
    # Converts tuples of coordinates into position objects
    positions = map(position, coords)
    # Makes a list of non-black pixel positions
    nonBlackPositions = list(
        filter(
            lambda pos: not colors.isBlack(imageWrapper.getPixel(image, pos)),
            positions))
    # Gets all codels from all non-black pixel positions
    allCodels = getCodels(image, nonBlackPositions)
    # Makes a graph with the codel as key, and the node as value
    return codelsToGraph(image, allCodels)
Пример #4
0
def edgeToToken(
        image: np.ndarray,
        inputEdge: edge) -> Union[tokens.baseLexerToken, BaseException]:
    """
    This function creates a token based on the given edge
    :param image: input image
    :param inputEdge: an edge containing (coords, direction)
    :return: Either a newly created token, or an exception
    """
    if not imageWrapper.boundsChecker(image, inputEdge.edge[0]):
        return IndexError("Edge position {} is not in image".format(
            inputEdge.edge[0]))

    nextPosition = movement.getNextPosition(inputEdge.edge[0],
                                            inputEdge.edge[1].pointers[0])
    if not imageWrapper.boundsChecker(image, nextPosition):
        return tokens.toBlackToken("edge")

    pixel = imageWrapper.getPixel(image, nextPosition)

    if colors.isBlack(pixel):
        return tokens.toBlackToken("toBlack")

    if colors.isWhite(pixel):
        return tokens.toWhiteToken()

    if not colors.isColor(pixel):
        return tokens.toBlackToken("Unknown color")

    colorChange = colors.getPixelChange(
        imageWrapper.getPixel(image, inputEdge.edge[0]),
        imageWrapper.getPixel(image, nextPosition))
    if isinstance(colorChange, BaseException):
        # Modify existing error message with location
        newText = "{} at position {}".format(colorChange.args[0], nextPosition)
        return errors.UnknownColorError(newText)

    tokenType = tokens.getTokenType(colorChange['hueChange'],
                                    colorChange['lightChange'])
    return tokens.toColorToken(
        tokenType, len(imageWrapper.getCodel(image, inputEdge.edge[0]).codel))
Пример #5
0
def getCodels(image: np.ndarray, positionList: List[position]) -> List[codel]:
    """
    Makes a list of codels from an image and a lits of positions to check
    :param image: an np.ndarray representing the image
    :param positionList: A list of positions, for which to find adjacent pixels of the same color
    :return: A list of codels found in the given image
    """
    if len(positionList) == 0:
        return []

    copiedList = positionList.copy()
    newPosition = copiedList.pop(0)

    if colors.isBlack(imageWrapper.getPixel(image, newPosition)):
        return getCodels(image, copiedList)

    newCodel = imageWrapper.getCodel(image, newPosition)

    # Remove found positions from coords list
    copiedList = list(set(copiedList) - newCodel.codel)
    codelList = getCodels(image, copiedList)

    codelList.append(newCodel)
    return codelList