MCPcopy Create free account
hub / github.com/apache/singa / random_crop

Method random_crop

python/singa/image_tool.py:479–502  ·  view source on GitHub ↗

Crop the image at random offset to get a patch of the given size. Args: patch(tuple): width and height of the patch inplace(Boolean): replace the internal images list with the patches if True; otherwise, return the patches.

(self, patch, inplace=True)

Source from the content-addressed store, hash-verified

477 return new_imgs
478
479 def random_crop(self, patch, inplace=True):
480 '''Crop the image at random offset to get a patch of the given size.
481
482 Args:
483 patch(tuple): width and height of the patch
484 inplace(Boolean): replace the internal images list with the patches
485 if True; otherwise, return the patches.
486 '''
487 new_imgs = []
488 for img in self.imgs:
489 assert img.size[0] >= patch[0] and img.size[1] >= patch[1],\
490 'img size (%d, %d), patch size (%d, %d)' % \
491 (img.size[0], img.size[1], patch[0], patch[1])
492 left_offset = random.randint(0, img.size[0] - patch[0])
493 top_offset = random.randint(0, img.size[1] - patch[1])
494 box = (left_offset, top_offset, left_offset + patch[0],
495 top_offset + patch[1])
496 new_imgs.append(img.crop(box))
497
498 if inplace:
499 self.imgs = new_imgs
500 return self
501 else:
502 return new_imgs
503
504 def random_crop_resize(self, patch, inplace=True):
505 ''' Crop of the image at a random size between 0.08 to 1 of input image

Callers 1

image_transformFunction · 0.80

Calls 1

appendMethod · 0.80

Tested by

no test coverage detected