Commit 2cc58ed0 authored by Sasank Chilamkurthy's avatar Sasank Chilamkurthy
Browse files

change x,y,w,h -> i,j,h,w

parent 47800d49
...@@ -209,15 +209,15 @@ def pad(img, padding, fill=0): ...@@ -209,15 +209,15 @@ def pad(img, padding, fill=0):
return ImageOps.expand(img, border=padding, fill=fill) return ImageOps.expand(img, border=padding, fill=fill)
def crop(img, x, y, w, h): def crop(img, i, j, h, w):
"""Crop the given PIL.Image. """Crop the given PIL.Image.
Args: Args:
img (PIL.Image): Image to be cropped. img (PIL.Image): Image to be cropped.
x: Left pixel coordinate. i: Upper pixel coordinate.
y: Upper pixel coordinate. j: Left pixel coordinate.
w: Width of the cropped image.
h: Height of the cropped image. h: Height of the cropped image.
w: Width of the cropped image.
Returns: Returns:
PIL.Image: Cropped image. PIL.Image: Cropped image.
...@@ -225,20 +225,20 @@ def crop(img, x, y, w, h): ...@@ -225,20 +225,20 @@ def crop(img, x, y, w, h):
if not _is_pil_image(img): if not _is_pil_image(img):
raise TypeError('img should be PIL Image. Got {}'.format(type(img))) raise TypeError('img should be PIL Image. Got {}'.format(type(img)))
return img.crop((x, y, x + w, y + h)) return img.crop((j, i, j + w, i + h))
def scaled_crop(img, x, y, w, h, size, interpolation=Image.BILINEAR): def scaled_crop(img, i, j, h, w, size, interpolation=Image.BILINEAR):
"""Crop the given PIL.Image and scale it to desired size. """Crop the given PIL.Image and scale it to desired size.
Notably used in RandomSizedCrop. Notably used in RandomSizedCrop.
Args: Args:
img (PIL.Image): Image to be cropped. img (PIL.Image): Image to be cropped.
x: Left pixel coordinate. i: Upper pixel coordinate.
y: Upper pixel coordinate. j: Left pixel coordinate.
w: Width of the cropped image.
h: Height of the cropped image. h: Height of the cropped image.
w: Width of the cropped image.
size (sequence or int): Desired output size. Same semantics as ``scale``. size (sequence or int): Desired output size. Same semantics as ``scale``.
interpolation (int, optional): Desired interpolation. Default is interpolation (int, optional): Desired interpolation. Default is
``PIL.Image.BILINEAR``. ``PIL.Image.BILINEAR``.
...@@ -246,7 +246,7 @@ def scaled_crop(img, x, y, w, h, size, interpolation=Image.BILINEAR): ...@@ -246,7 +246,7 @@ def scaled_crop(img, x, y, w, h, size, interpolation=Image.BILINEAR):
PIL.Image: Cropped image. PIL.Image: Cropped image.
""" """
assert _is_pil_image(img), 'img should be PIL Image' assert _is_pil_image(img), 'img should be PIL Image'
img = crop(img, x, y, w, h) img = crop(img, i, j, h, w)
img = scale(img, size, interpolation) img = scale(img, size, interpolation)
return img return img
...@@ -406,13 +406,13 @@ class CenterCrop(object): ...@@ -406,13 +406,13 @@ class CenterCrop(object):
output_size (tuple): Expected output size of the crop. output_size (tuple): Expected output size of the crop.
Returns: Returns:
tuple: params (x, y, w, h) to be passed to ``crop`` for center crop. tuple: params (i, j, h, w) to be passed to ``crop`` for center crop.
""" """
w, h = img.size w, h = img.size
th, tw = output_size th, tw = output_size
x1 = int(round((w - tw) / 2.)) i = int(round((h - th) / 2.))
y1 = int(round((h - th) / 2.)) j = int(round((w - tw) / 2.))
return x1, y1, tw, th return i, j, th, tw
def __call__(self, img): def __call__(self, img):
""" """
...@@ -422,8 +422,8 @@ class CenterCrop(object): ...@@ -422,8 +422,8 @@ class CenterCrop(object):
Returns: Returns:
PIL.Image: Cropped image. PIL.Image: Cropped image.
""" """
x1, y1, tw, th = self.get_params(img, self.size) i, j, h, w = self.get_params(img, self.size)
return crop(img, x1, y1, tw, th) return crop(img, i, j, h, w)
class Pad(object): class Pad(object):
...@@ -504,16 +504,16 @@ class RandomCrop(object): ...@@ -504,16 +504,16 @@ class RandomCrop(object):
output_size (tuple): Expected output size of the crop. output_size (tuple): Expected output size of the crop.
Returns: Returns:
tuple: params (x, y, w, h) to be passed to ``crop`` for random crop. tuple: params (i, j, h, w) to be passed to ``crop`` for random crop.
""" """
w, h = img.size w, h = img.size
th, tw = output_size th, tw = output_size
if w == tw and h == th: if w == tw and h == th:
return img return img
x1 = random.randint(0, w - tw) i = random.randint(0, h - th)
y1 = random.randint(0, h - th) j = random.randint(0, w - tw)
return x1, y1, tw, th return i, j, th, tw
def __call__(self, img): def __call__(self, img):
""" """
...@@ -526,9 +526,9 @@ class RandomCrop(object): ...@@ -526,9 +526,9 @@ class RandomCrop(object):
if self.padding > 0: if self.padding > 0:
img = pad(img, self.padding) img = pad(img, self.padding)
x1, y1, tw, th = self.get_params(img, self.size) i, j, h, w = self.get_params(img, self.size)
return crop(img, x1, y1, tw, th) return crop(img, i, j, h, w)
class RandomHorizontalFlip(object): class RandomHorizontalFlip(object):
...@@ -572,7 +572,7 @@ class RandomSizedCrop(object): ...@@ -572,7 +572,7 @@ class RandomSizedCrop(object):
img (PIL.Image): Image to be cropped. img (PIL.Image): Image to be cropped.
Returns: Returns:
tuple: params (x, y, w, h) to be passed to ``crop`` for a random tuple: params (i, j, h, w) to be passed to ``crop`` for a random
sized crop. sized crop.
""" """
for attempt in range(10): for attempt in range(10):
...@@ -587,15 +587,15 @@ class RandomSizedCrop(object): ...@@ -587,15 +587,15 @@ class RandomSizedCrop(object):
w, h = h, w w, h = h, w
if w <= img.size[0] and h <= img.size[1]: if w <= img.size[0] and h <= img.size[1]:
x = random.randint(0, img.size[0] - w) i = random.randint(0, img.size[1] - h)
y = random.randint(0, img.size[1] - h) j = random.randint(0, img.size[0] - w)
return x, y, w, h return i, j, h, w
# Fallback # Fallback
w = min(img.size[0], img.shape[1]) w = min(img.size[0], img.shape[1])
x = (img.shape[0] - w) // 2 i = (img.shape[1] - w) // 2
y = (img.shape[1] - w) // 2 j = (img.shape[0] - w) // 2
return x, y, w, w return i, j, w, w
def __call__(self, img): def __call__(self, img):
""" """
...@@ -605,5 +605,5 @@ class RandomSizedCrop(object): ...@@ -605,5 +605,5 @@ class RandomSizedCrop(object):
Returns: Returns:
PIL.Image: Randomly cropped and scaled image. PIL.Image: Randomly cropped and scaled image.
""" """
x, y, w, h = self.get_params(img) i, j, h, w = self.get_params(img)
return scaled_crop(img, x, y, w, h, self.size, self.interpolation) return scaled_crop(img, i, j, h, w, self.size, self.interpolation)
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment