def test_crop(): test1 = cle.push_zyx( np.asarray([[0, 0, 0, 1], [0, 0, 3, 1], [0, 0, 3, 1], [1, 1, 1, 1]])) reference = cle.push_zyx(np.asarray([[0, 0, 0], [0, 0, 3], [0, 0, 3]])) result = cle.create(reference) cle.crop(test1, result, 0, 0) a = cle.pull(result) b = cle.pull(reference) print(a) assert (np.array_equal(a, b))
def test_generate_angle_matrix(): input = cle.push( np.asarray([[5, 0, 4, 0, 3], [0, 6, 0, 0, 0], [7, 0, 1, 0, 2], [0, 0, 0, 0, 0], [8, 0, 9, 0, 0]])) reference = cle.push( np.asarray([[0., -45., 90., 45., 45., 0., -45., -90.]])) pointlist = cle.labelled_spots_to_pointlist(input) angle_matrix = cle.generate_angle_matrix(pointlist, pointlist) angle_matrix_degrees = cle.radians_to_degrees(angle_matrix) angle_from_1 = cle.crop(angle_matrix_degrees, start_x=2, start_y=1, width=8, height=1) a = cle.pull(angle_from_1) b = cle.pull(reference) print(a) print(b) assert (np.allclose(a, b, 0.001))
def test_crop_2d(): input_image = cle.create([100, 100]) print(input_image.shape) output_image = cle.crop(input_image, width=10, height=10) print(output_image.shape) assert(len(output_image.shape) == 2) assert(output_image.shape[0] == 10) assert(output_image.shape[1] == 10)
def game_step(self): """Forwards the game by one step and computes the new playground Returns ------- an image with the current state of the game """ # read camera image _, picture = self._video_source.read() # push to GPU and bring in right format current_image = self._push_and_format(picture) # determine difference = cle.sum_z_projection( cle.squared_difference(current_image, self._image_at_beginning)) crop_left = cle.crop(difference, start_x=0, start_y=0, width=self.sensitive_area_width, height=self.height) crop_right = cle.crop(difference, start_x=self.width - self.sensitive_area_width, start_y=0, width=self.sensitive_area_width, height=self.height) # check player positions self.player1_position = self._determine_player_position( self.player1_position, crop_left) self.player2_position = self._determine_player_position( self.player2_position, crop_right) squared_difference_picture = cle.pull(difference) # move puck self.puck_x = self.puck_x + self.puck_delta_x self.puck_y = self.puck_y + self.puck_delta_y # check puck_position if self.puck_y < 0 or self.puck_y > self.height: self.puck_delta_y = -self.puck_delta_y self.puck_y = self.puck_y + self.puck_delta_y # puck at player 1 if self.puck_x <= self.player1_x: self.puck_delta_x = -self.puck_delta_x self.puck_x = self.puck_x + self.puck_delta_x if abs(self.puck_y - self.player1_position) > self.bar_radius: # player 2 scores self.player2_score = self.player2_score + 1 self._level_up() else: self.puck_delta_y = ( self.puck_y - self.player1_position) / self.bar_radius * 5 # puck at player 2 if self.puck_x >= self.player2_x: self.puck_delta_x = -self.puck_delta_x self.puck_x = self.puck_x + self.puck_delta_x if abs(self.puck_y - self.player2_position) > self.bar_radius: # player 1 scores self.player1_score = self.player1_score + 1 self._level_up() else: self.puck_delta_y = ( self.puck_y - self.player2_position) / self.bar_radius * 5 # show game status self.result_label.setText( str(self.player1_score) + ":" + str(self.player2_score)) # draw playground cle.set(self.playground, 0.1) # draw player 1 cle.draw_box(self.playground, self.player1_x, self.player1_position - self.bar_radius, 0, 3, self.bar_radius * 2, 0) # draw player 2 cle.draw_box(self.playground, self.player2_x, self.player2_position - self.bar_radius, 0, 3, self.bar_radius * 2, 0) # draw puck cle.draw_sphere(self.playground, self.puck_x - self.puck_width / 2, self.puck_y - self.puck_height / 2, 0, self.puck_width, self.puck_height, 1) # binary playground image game_state = cle.pull(self.playground) # turn camera view in single channel grey-value image grey = cle.mean_z_projection(current_image) # warp the image gradient_radius = 25 position_delta = 25000 blurred_playground = cle.gaussian_blur(self.playground, sigma_x=gradient_radius, sigma_y=gradient_radius) gradient = cle.laplace_box(blurred_playground) vector_x = cle.multiply_image_and_scalar(gradient, scalar=position_delta) vector_y = cle.multiply_image_and_scalar(gradient, scalar=position_delta) warped_playground = cle.apply_vector_field(grey, vector_x, vector_y) return [ # images to add in napari [ cle.pull(grey), squared_difference_picture, game_state, cle.pull(vector_x), cle.pull(vector_x), cle.pull(warped_playground) ], # names [ 'camera input', 'squared difference', 'game state', 'vector x', 'vector y', 'warped view' ], # visibility [False, False, False, False, False, True] ]
def crop_fov(self, image, output=None): return cle.crop(image, output, start_x=self.fov_x, width=self.size[1], height=self.size[0])