| 105 | |
| 106 | |
| 107 | class Canvas(object): |
| 108 | def __init__(self, ph, pw, |
| 109 | nr_row, nr_col, |
| 110 | channel, border, bgcolor): |
| 111 | self.ph = ph |
| 112 | self.pw = pw |
| 113 | self.nr_row = nr_row |
| 114 | self.nr_col = nr_col |
| 115 | |
| 116 | if border is None: |
| 117 | border = int(0.05 * min(ph, pw)) |
| 118 | self.border = border |
| 119 | |
| 120 | if isinstance(bgcolor, int): |
| 121 | bgchannel = 1 |
| 122 | else: |
| 123 | bgchannel = 3 |
| 124 | self.bgcolor = bgcolor |
| 125 | self.channel = max(channel, bgchannel) |
| 126 | |
| 127 | self.canvas = np.zeros((nr_row * (ph + border) - border, |
| 128 | nr_col * (pw + border) - border, |
| 129 | self.channel), dtype='uint8') |
| 130 | |
| 131 | def draw_patches(self, plist): |
| 132 | assert self.nr_row * self.nr_col >= len(plist), \ |
| 133 | "{}*{} < {}".format(self.nr_row, self.nr_col, len(plist)) |
| 134 | if self.channel == 3 and plist.shape[3] == 1: |
| 135 | plist = np.repeat(plist, 3, axis=3) |
| 136 | cur_row, cur_col = 0, 0 |
| 137 | if self.channel == 1: |
| 138 | self.canvas.fill(self.bgcolor) |
| 139 | else: |
| 140 | self.canvas[:, :, :] = self.bgcolor |
| 141 | for patch in plist: |
| 142 | r0 = cur_row * (self.ph + self.border) |
| 143 | c0 = cur_col * (self.pw + self.border) |
| 144 | self.canvas[r0:r0 + self.ph, c0:c0 + self.pw] = patch |
| 145 | cur_col += 1 |
| 146 | if cur_col == self.nr_col: |
| 147 | cur_col = 0 |
| 148 | cur_row += 1 |
| 149 | |
| 150 | def get_patchid_from_coord(self, x, y): |
| 151 | x = x // (self.pw + self.border) |
| 152 | y = y // (self.pw + self.border) |
| 153 | idx = y * self.nr_col + x |
| 154 | return idx |
| 155 | |
| 156 | |
| 157 | def stack_patches( |
no outgoing calls
no test coverage detected