def flatten_image(image, piece_size, indexed=False): """Converts image into list of square pieces. Input image is divided into square pieces of specified size and than flattened into list. Each list element is PIECE_SIZE x PIECE_SIZE x 3 :params image: Input image. :params piece_size: Size of single square piece. Each piece is PIECE_SIZE x PIECE_SIZE :params indexed: If True list of Pieces with IDs will be returned, otherwise just plain list of ndarray pieces Usage:: >>> from gaps.image_helpers import flatten_image >>> flat_image = flatten_image(image, 32) """ rows, columns = image.shape[0] // piece_size, image.shape[1] // piece_size pieces = [] # Crop pieces from original image for y in range(rows): for x in range(columns): left, top, w, h = x * piece_size, y * piece_size, ( x + 1) * piece_size, (y + 1) * piece_size piece = np.empty((piece_size, piece_size, image.shape[2])) piece[:piece_size, :piece_size, :] = image[top:h, left:w, :] pieces.append(piece) if indexed: pieces = [Piece(value, index) for index, value in enumerate(pieces)] return pieces, rows, columns print(rows, columns)
def flatten_image_stripe(image, piece_size, indexed=False): rows, columns = 1, image.shape[1] // piece_size pieces = [] # Crop pieces from original image for y in range(rows): for x in range(columns): left, top, w, h = x * piece_size, 0, ( x + 1) * piece_size, image.shape[0] piece = np.empty((image.shape[0], piece_size, image.shape[2])) piece[:, :, :] = image[top:h, left:w, :] pieces.append(piece) if indexed: pieces = [Piece(value, index) for index, value in enumerate(pieces)] return pieces, rows, columns
def best_adjoin(self, piece_size): pieces = np.reshape(self.pieces, (self.rows, self.columns)) empty_image = np.zeros( (piece_size, piece_size, pieces[0][0].shape()[2])) empty_piece = Piece(empty_image, -1) for row in range(self.rows): for col in range(self.columns): if row == 0: if col == 0 and ImageAnalysis.in_range(pieces[row][col].id, "D", pieces[row + 1][col].id) \ and ImageAnalysis.in_range(pieces[row][col].id, "R", pieces[row][col + 1].id): continue if col < self.columns - 1 and ImageAnalysis.in_range(pieces[row][col].id, "D", pieces[row + 1][col].id) \ and ImageAnalysis.in_range(pieces[row][col].id, "R", pieces[row][col + 1].id): continue if col == self.columns - 1 and ImageAnalysis.in_range(pieces[row][col].id, "D", pieces[row + 1][col].id) \ and ImageAnalysis.in_range(pieces[row][col].id, "L", pieces[row][col - 1].id): continue if 0 < row < self.rows - 1: if col == 0 and ImageAnalysis.in_range(pieces[row][col].id, "D", pieces[row + 1][col].id) \ and ImageAnalysis.in_range(pieces[row][col].id, "R", pieces[row][col + 1].id): continue if col < self.columns - 1 and ImageAnalysis.in_range(pieces[row][col].id, "D", pieces[row + 1][col].id) \ and ImageAnalysis.in_range(pieces[row][col].id, "R", pieces[row][col + 1].id): continue if col == self.columns - 1 and ImageAnalysis.in_range(pieces[row][col].id, "D", pieces[row + 1][col].id) \ and ImageAnalysis.in_range(pieces[row][col].id, "L", pieces[row][col - 1].id): continue if row == self.rows - 1: if col == 0 and ImageAnalysis.in_range(pieces[row][col].id, "T", pieces[row - 1][col].id) \ and ImageAnalysis.in_range(pieces[row][col].id, "R", pieces[row][col + 1].id): continue if col < self.columns - 1 and ImageAnalysis.in_range(pieces[row][col].id, "R", pieces[row][col + 1].id) \ and ImageAnalysis.in_range(pieces[row][col].id, "T", pieces[row - 1][col].id): continue if col == self.columns - 1 and ImageAnalysis.in_range(pieces[row][col].id, "L", pieces[row][col - 1].id) \ and ImageAnalysis.in_range(pieces[row][col].id, "T", pieces[row - 1][col].id): continue # if row == 0: # if col == 0 and ImageAnalysis.best_match(pieces[row][col].id, "D") == pieces[row + 1][col].id \ # and ImageAnalysis.best_match(pieces[row][col].id, "R") == pieces[row][col + 1].id: # continue # if col < self.columns - 1 and ImageAnalysis.best_match(pieces[row][col].id, "D") == pieces[row + 1][col].id \ # and ImageAnalysis.best_match(pieces[row][col].id, "R") == pieces[row][col + 1].id \ # and ImageAnalysis.best_match(pieces[row][col].id, "L") == pieces[row][col - 1].id: # continue # if col == self.columns - 1 and ImageAnalysis.best_match(pieces[row][col].id, "D") == pieces[row + 1][col].id \ # and ImageAnalysis.best_match(pieces[row][col].id, "L") == pieces[row][col - 1].id: # continue # if 0 < row < self.rows - 1: # if col == 0 and ImageAnalysis.best_match(pieces[row][col].id, "D") == pieces[row + 1][col].id \ # and ImageAnalysis.best_match(pieces[row][col].id, "T") == pieces[row - 1][col].id \ # and ImageAnalysis.best_match(pieces[row][col].id, "R") == pieces[row][col + 1].id: # continue # if col < self.columns - 1 and ImageAnalysis.best_match(pieces[row][col].id, "T") == pieces[row - 1][col].id \ # and ImageAnalysis.best_match(pieces[row][col].id, "D") == pieces[row + 1][col].id \ # and ImageAnalysis.best_match(pieces[row][col].id, "L") == pieces[row][col - 1].id \ # and ImageAnalysis.best_match(pieces[row][col].id, "R") == pieces[row][col + 1].id: # continue # if col == self.columns - 1 and ImageAnalysis.best_match(pieces[row][col].id, "T") == pieces[row - 1][col].id \ # and ImageAnalysis.best_match(pieces[row][col].id, "D") == pieces[row + 1][col].id \ # and ImageAnalysis.best_match(pieces[row][col].id, "L") == pieces[row][col - 1].id: # continue # if row == self.rows - 1: # if col == 0 and ImageAnalysis.best_match(pieces[row][col].id, "T") == pieces[row - 1][col].id \ # and ImageAnalysis.best_match(pieces[row][col].id, "R") == pieces[row][col + 1].id: # continue # if col < self.columns - 1 and ImageAnalysis.best_match(pieces[row][col].id, "R") == pieces[row][col + 1].id \ # and ImageAnalysis.best_match(pieces[row][col].id, "L") == pieces[row][col - 1].id \ # and ImageAnalysis.best_match(pieces[row][col].id, "T") == pieces[row - 1][col].id: # continue # if col == self.columns - 1 and ImageAnalysis.best_match(pieces[row][col].id, "L") == pieces[row][col - 1].id \ # and ImageAnalysis.best_match(pieces[row][col].id, "T") == pieces[row - 1][col].id: # continue pieces[row][col] = empty_piece self.penalize_image = pieces return image_helpers.assemble_image( [each.image for each in pieces.flatten()], self.rows, self.columns)