|
| 1 | +import numpy as np |
| 2 | +import matplotlib.pyplot as plt |
| 3 | +import os |
| 4 | +import math |
| 5 | +path = '../../datasets/unlabeled2017/000000002272.jpg' |
| 6 | + |
| 7 | +class ImageSlicer(object): |
| 8 | + def __init__(self, source, size, strides=[None, None], BATCH = False, PADDING=False): |
| 9 | + self.source = source |
| 10 | + self.size = size |
| 11 | + self.strides = strides |
| 12 | + self.BATCH = BATCH |
| 13 | + self.PADDING = PADDING |
| 14 | + |
| 15 | + def __read_images(self): |
| 16 | + Images = [] |
| 17 | + image_names = sorted(os.listdir(self.source)) |
| 18 | + for im in image_names: |
| 19 | + image = plt.imread(os.path.join(dir_path,im)) |
| 20 | + Images.append(image) |
| 21 | + return Images |
| 22 | + |
| 23 | + def __offset_op(self, input_length, output_length, stride): |
| 24 | + offset = (input_length) - (stride*((input_length - output_length)//stride)+output_length) |
| 25 | + return offset |
| 26 | + |
| 27 | + def __padding_op(self, Image): |
| 28 | + if self.offset_x > 0: |
| 29 | + padding_x = self.strides[0] - self.offset_x |
| 30 | + else: |
| 31 | + padding_x = 0 |
| 32 | + if self.offset_y > 0: |
| 33 | + padding_y = self.strides[1] - self.offset_y |
| 34 | + else: |
| 35 | + padding_y = 0 |
| 36 | + Padded_Image = np.zeros(shape=(Image.shape[0]+padding_x, Image.shape[1]+padding_y, Image.shape[2]),dtype=Image.dtype) |
| 37 | + Padded_Image[padding_x//2:(padding_x//2)+(Image.shape[0]),padding_y//2:(padding_y//2)+Image.shape[1],:] = Image |
| 38 | + return Padded_Image |
| 39 | + |
| 40 | + def __convolution_op(self, Image): |
| 41 | + start_x = 0 |
| 42 | + start_y = 0 |
| 43 | + self.n_rows = Image.shape[0]//self.strides[0] + 1 |
| 44 | + self.n_columns = Image.shape[1]//self.strides[1] + 1 |
| 45 | + # print(str(self.n_rows)+" rows") |
| 46 | + # print(str(self.n_columns)+" columns") |
| 47 | + small_images = [] |
| 48 | + for i in range(self.n_rows-1): |
| 49 | + for j in range(self.n_columns-1): |
| 50 | + new_start_x = start_x+i*self.strides[0] |
| 51 | + new_start_y= start_y+j*self.strides[1] |
| 52 | + small_images.append(Image[new_start_x:new_start_x+self.size[0],new_start_y:new_start_y+self.size[1],:]) |
| 53 | + return small_images |
| 54 | + |
| 55 | + def transform(self): |
| 56 | + |
| 57 | + if not(os.path.exists(self.source)): |
| 58 | + raise Exception("Path does not exist!") |
| 59 | + |
| 60 | + else: |
| 61 | + if self.source and not(self.BATCH): |
| 62 | + Image = plt.imread(self.source) |
| 63 | + Images = [Image] |
| 64 | + else: |
| 65 | + Images = self.__read_images() |
| 66 | + |
| 67 | + im_size = Images[0].shape |
| 68 | + num_images = len(Images) |
| 69 | + transformed_images = dict() |
| 70 | + Images = np.array(Images) |
| 71 | + |
| 72 | + if self.PADDING: |
| 73 | + |
| 74 | + padded_images = [] |
| 75 | + |
| 76 | + if self.strides[0]==None and self.strides[1]==None: |
| 77 | + self.strides[0] = self.size[0] |
| 78 | + self.strides[1] = self.size[1] |
| 79 | + self.offset_x = Images.shape[1]%self.size[0] |
| 80 | + self.offset_y = Images.shape[2]%self.size[1] |
| 81 | + padded_images = list(map(self.__padding_op, Images)) |
| 82 | + |
| 83 | + elif self.strides[0]==None and self.strides[1]!=None: |
| 84 | + self.strides[0] = self.size[0] |
| 85 | + self.offset_x = Images.shape[1]%self.size[0] |
| 86 | + if self.strides[1] <= Images.shape[2]: |
| 87 | + self.offset_y = self.__offset_op(Images.shape[2], self.size[1], self.strides[1]) |
| 88 | + else: |
| 89 | + raise Exception("stride_y must be between {0} and {1}".format(1,Images.shape[2])) |
| 90 | + padded_images = list(map(self.__padding_op, Images)) |
| 91 | + |
| 92 | + elif self.strides[0]!=None and self.strides[1]==None: |
| 93 | + self.strides[1] = self.size[1] |
| 94 | + self.offset_y = Images.shape[2]%self.size[1] |
| 95 | + if self.strides[0] <=Images.shape[1]: |
| 96 | + self.offset_x = self.__offset_op(Images.shape[1], self.size[0], self.strides[0]) |
| 97 | + else: |
| 98 | + raise Exception("stride_x must be between {0} and {1}".format(1,Images.shape[1])) |
| 99 | + padded_images = list(map(self.__padding_op, Images)) |
| 100 | + |
| 101 | + else: |
| 102 | + if self.strides[0] > Images.shape[1]: |
| 103 | + raise Exception("stride_x must be between {0} and {1}".format(1,Images.shape[1])) |
| 104 | + |
| 105 | + elif self.strides[1] > Images.shape[2]: |
| 106 | + raise Exception("stride_y must be between {0} and {1}".format(1,Images.shape[2])) |
| 107 | + |
| 108 | + else: |
| 109 | + self.offset_x = self.__offset_op(Images.shape[1], self.size[0], self.strides[0]) |
| 110 | + self.offset_y = self.__offset_op(Images.shape[2], self.size[1], self.strides[1]) |
| 111 | + padded_images = list(map(self.__padding_op, Images)) |
| 112 | + |
| 113 | + for i, Image in enumerate(padded_images): |
| 114 | + transformed_images[str(i)] = self.__convolution_op(Image) |
| 115 | + |
| 116 | + else: |
| 117 | + if self.strides[0]==None and self.strides[1]==None: |
| 118 | + self.strides[0] = self.size[0] |
| 119 | + self.strides[1] = self.size[1] |
| 120 | + |
| 121 | + elif self.strides[0]==None and self.strides[1]!=None: |
| 122 | + if self.strides[1] > Images.shape[2]: |
| 123 | + raise Exception("stride_y must be between {0} and {1}".format(1,Images.shape[2])) |
| 124 | + self.strides[0] = self.size[0] |
| 125 | + |
| 126 | + elif self.strides[0]!=None and self.strides[1]==None: |
| 127 | + if self.strides[0] > Images.shape[1]: |
| 128 | + raise Exception("stride_x must be between {0} and {1}".format(1,Images.shape[1])) |
| 129 | + self.strides[1] = self.size[1] |
| 130 | + else: |
| 131 | + if self.strides[0] > Images.shape[1]: |
| 132 | + raise Exception("stride_x must be between {0} and {1}".format(1,Images.shape[1])) |
| 133 | + elif self.strides[1] > Images.shape[2]: |
| 134 | + raise Exception("stride_y must be between {0} and {1}".format(1,Images.shape[2])) |
| 135 | + |
| 136 | + for i, Image in enumerate(Images): |
| 137 | + transformed_images[str(i)] = self.__convolution_op(Image) |
| 138 | + |
| 139 | + return transformed_images |
| 140 | + |
| 141 | + def save_images(self,transformed): |
| 142 | + self.r,self.c = self.n_rows-1, self.n_columns-1 |
| 143 | + for key, val in transformed.items(): |
| 144 | + val = np.array(val, dtype=np.float64) /127.5 -1. |
| 145 | + val = .5 * val + 0.5 |
| 146 | + return val |
0 commit comments