MCPcopy Create free account
hub / github.com/apple/ml-4m / crop_sample

Method crop_sample

fourm/data/modality_transforms.py:448–480  ·  view source on GitHub ↗

Crop the sample using crop coordinates.

(self, sample, crop_coords)

Source from the content-addressed store, hash-verified

446 return fn
447
448 def crop_sample(self, sample, crop_coords):
449 """ Crop the sample using crop coordinates.
450 """
451 top, left, h, w = crop_coords
452 crop_region = (left, top, left + w, top + h)
453 crop_points = np.array([
454 [crop_region[0], crop_region[1]],
455 [crop_region[2], crop_region[1]],
456 [crop_region[2], crop_region[3]],
457 [crop_region[0], crop_region[3]],
458 ])[:,None]
459 border_functions = self.get_border_functions(crop_points)
460 cropped_sample = []
461 for instance in sample:
462 instance = self.extend_instance_points(instance, border_functions)
463 filter_condition = (
464 (instance[:, :, 0] > crop_region[0]) &
465 (instance[:, :, 0] < crop_region[2]) &
466 (instance[:, :, 1] > crop_region[1]) &
467 (instance[:, :, 1] < crop_region[3])
468 )
469 if not np.any(filter_condition):
470 continue
471
472 instance_copy = instance.copy()
473 instance_copy[:, :, 0] = np.clip(instance[:, :, 0], a_min=crop_region[0], a_max=crop_region[2])
474 instance_copy[:, :, 1] = np.clip(instance[:, :, 1], a_min=crop_region[1], a_max=crop_region[3])
475 instance_copy = self.remove_redundant_lines(instance, instance_copy)
476 instance_copy[:, :, 0] -= crop_region[0]
477 instance_copy[:, :, 1] -= crop_region[1]
478
479 cropped_sample.append(instance_copy)
480 return cropped_sample
481
482 def resize_sample(self, sample, original_size, target_size):
483 """ Resize the sample

Callers 1

image_augmentMethod · 0.95

Calls 3

get_border_functionsMethod · 0.95

Tested by

no test coverage detected