Переглянути джерело

Create centerArrayImage.py

dgirerd 9 роки тому
батько
коміт
67945b6595
1 змінених файлів з 51 додано та 0 видалено
  1. 51 0
      centerArrayImage.py

+ 51 - 0
centerArrayImage.py

@@ -0,0 +1,51 @@
+def pad_image(img, pad_t, pad_r, pad_b, pad_l):
+    """Add padding of zeroes to an image.
+    Add padding to an array image.
+    :param img:
+    :param pad_t:
+    :param pad_r:
+    :param pad_b:
+    :param pad_l:
+    """
+    height, width = img.shape
+
+    # Adding padding to the left side.
+    pad_left = np.zeros((height, pad_l), dtype = np.int)
+    img = np.concatenate((pad_left, img), axis = 1)
+
+    # Adding padding to the top.
+    pad_up = np.zeros((pad_t, pad_l + width))
+    img = np.concatenate((pad_up, img), axis = 0)
+
+    # Adding padding to the right.
+    pad_right = np.zeros((height + pad_t, pad_r))
+    img = np.concatenate((img, pad_right), axis = 1)
+
+    # Adding padding to the bottom
+    pad_bottom = np.zeros((pad_b, pad_l + width + pad_r))
+    img = np.concatenate((img, pad_bottom), axis = 0)
+
+    return img
+
+def center_image(img):
+    """Return a centered image.
+    :param img:
+    """
+    col_sum = np.where(np.sum(img, axis=0) > 0)
+    row_sum = np.where(np.sum(img, axis=1) > 0)
+    y1, y2 = row_sum[0][0], row_sum[0][-1]
+    x1, x2 = col_sum[0][0], col_sum[0][-1]
+
+    cropped_image = img[y1:y2, x1:x2]
+
+    zero_axis_fill = (images[0].shape[0] - cropped_image.shape[0])
+    one_axis_fill = (images[0].shape[1] - cropped_image.shape[1])
+
+    top = zero_axis_fill / 2
+    bottom = zero_axis_fill - top
+    left = one_axis_fill / 2
+    right = one_axis_fill - left
+
+    padded_image = pad_image(cropped_image, top, left, bottom, right)
+
+    return padded_image