Browse Source

fixed some issues with cropping

Dimitri Korsch 6 years ago
parent
commit
09b7cff0dc
3 changed files with 34 additions and 31 deletions
  1. 4 7
      nabirds/dataset/image.py
  2. 10 9
      nabirds/dataset/part.py
  3. 20 15
      scripts/display_from_info.py

+ 4 - 7
nabirds/dataset/image.py

@@ -97,10 +97,9 @@ class ImageWrapper(object):
 	def crop(self, x, y, w, h):
 		result = self.copy()
 		# result.im = self.im[y:y+h, x:x+w]
-		result.im = self.im.crop(x, y, x+w, y+h)
+		result.im = self.im.crop((x, y, x+w, y+h))
 		if self.has_parts:
-			result.parts[:, 1] -= x
-			result.parts[:, 2] -= y
+			result.parts.offset(-x, -y)
 		return result
 
 	@should_have_parts
@@ -108,10 +107,8 @@ class ImageWrapper(object):
 		idxs, (xs,ys) = self.visible_part_locs()
 		f = np.logical_and
 		mask = f(f(x <= xs, xs <= x+w), f(y <= ys, ys <= y+h))
-		idxs = np.where(mask)
-		import pdb; pdb.set_trace()
 		result = self.copy()
-		result.parts.select(idxs)
+		result.parts.select(idxs[mask])
 		return result
 
 	def uniform_parts(self, ratio):
@@ -134,7 +131,7 @@ class ImageWrapper(object):
 
 	@should_have_parts
 	def visible_crops(self, ratio):
-		return self.parts.visible_crops(ratio=ratio)
+		return self.parts.visible_crops(self.im, ratio=ratio)
 
 	@should_have_parts
 	def visible_part_locs(self):

+ 10 - 9
nabirds/dataset/part.py

@@ -7,7 +7,7 @@ class Parts(object):
 		super(Parts, self).__init__()
 		annots = utils.rescale_parts(image, part_annotations, rescale_size)
 
-		self._parts = [ImagePart(image, a) for a in annots]
+		self._parts = [ImagePart(a) for a in annots]
 		self.rescale_size = rescale_size
 
 	def __getitem__(self, i):
@@ -29,12 +29,14 @@ class Parts(object):
 		for p in self._parts:
 			p.is_visible = p._id in idxs
 
+
 	def invert_selection(self):
 		self.select(np.logical_not(self.selected))
 
-	def set_visibility(self, idxs, value):
-		for p in self._parts[idxs]:
-			p.is_visible = value
+	def offset(self, dx, dy):
+		for p in self._parts:
+			p.x += dx
+			p.y += dy
 
 	def visible_locs(self):
 		vis = [(p._id, p.xy) for p in self._parts if p.is_visible]
@@ -45,9 +47,8 @@ class Parts(object):
 		return np.array([p.crop(*args, **kwargs) for p in self._parts])
 
 class ImagePart(object):
-	def __init__(self, image, annotation):
+	def __init__(self, annotation):
 		super(ImagePart, self).__init__()
-		self.image = image
 
 		if len(annotation) == 4:
 			# here x,y are the center of the part
@@ -62,13 +63,13 @@ class ImagePart(object):
 		else:
 			raise ValueError("Unknown annotation format: {}".format(annotation))
 
-	def crop(self, ratio=None, padding_mode="edge"):
+	def crop(self, image, ratio=None, padding_mode="edge"):
 		if not self.is_visible:
-			h, w, c = utils.dimensions(self.image)
+			h, w, c = utils.dimensions(image)
 			crop_h, crop_w = int(h * ratio), int(w * ratio)
 			return np.zeros((crop_h, crop_w, c), dtype=np.uint8)
 		else:
-			return utils.crop(self.image, self.xy, ratio, padding_mode)
+			return utils.crop(image, self.xy, ratio, padding_mode)
 
 	@property
 	def is_visible(self):

+ 20 - 15
scripts/display_from_info.py

@@ -12,6 +12,7 @@ import yaml
 import logging
 import numpy as np
 import matplotlib.pyplot as plt
+from matplotlib.patches import Rectangle
 
 from argparse import ArgumentParser
 
@@ -80,23 +81,27 @@ def main(args):
 		im, parts, label = data[i]
 
 		idxs, xy = parts.visible_locs()
-		part_crops = parts.visible_crops(ratio=data.ratio)
+		part_crops = parts.visible_crops(im, ratio=data.ratio)
 		if args.rnd:
 			parts.invert_selection()
-			action_crops = parts.visible_crops(ratio=data.ratio)
-
-		fig1 = plt.figure(figsize=(16,9))
-		ax = fig1.add_subplot(2,1,1)
-		ax.imshow(im)
-		ax.set_title("Visible Parts")
-		ax.scatter(*xy, marker="x", c=idxs)
-		ax.axis("off")
-
-		ax = fig1.add_subplot(2,1,2)
-		ax.set_title("{}selected parts".format("randomly " if args.rnd else ""))
-		ax.imshow(utils.reveal_parts(im, xy, ratio=data.ratio))
-		# ax.scatter(*xy, marker="x", c=idxs)
-		ax.axis("off")
+			action_crops = parts.visible_crops(im, ratio=data.ratio)
+
+		fig1, axs = plt.subplots(2, 1, figsize=(16,9))
+		axs[0].axis("off")
+		axs[0].set_title("Visible Parts")
+		axs[0].imshow(im)
+		if not args.crop_to_bb:
+			x, y, w, h = data.bounding_box(i)
+			axs[0].add_patch(Rectangle(
+				(x,y), w, h,
+				fill=False,
+				linestyle="--"
+			))
+		axs[0].scatter(*xy, marker="x", c=idxs)
+
+		axs[1].axis("off")
+		axs[1].set_title("{}selected parts".format("randomly " if args.rnd else ""))
+		axs[1].imshow(utils.reveal_parts(im, xy, ratio=data.ratio))
 		crop_names = list(data._annot.part_names.values())
 		plot_crops(part_crops, "Selected parts", names=crop_names)