def carv(I, nr, nc): [n, m, d] = np.shape(I) T = np.zeros(np.array([nr + 1, nc + 1])) Ic = np.zeros(np.array([n - nr, m - nc, 3])) table = np.empty((nr + 1, nc + 1), dtype=np.ndarray) table[0, 0] = I track = T for i in range(0, nr + 1): for j in range(0, nc + 1): if (i == 0 and j == 0): T[i, j] = 0 elif i == 0: e = genEngMap(table[i, j - 1]) [Mx, Tbx] = cumMinEngVer(e) [Ix, Ex] = rmVerSeam(table[i, j - 1], Mx, Tbx) table[i, j] = Ix T[i, j] = T[i, j - 1] + Ex track[i, j] = -1 elif j == 0: e = genEngMap(table[i - 1, j]) [My, Tby] = cumMinEngHor(e) [Iy, Ey] = rmHorSeam(table[i - 1, j], My, Tby) table[i, j] = Iy T[i, j] = T[i - 1, j] + Ey track[i, j] = 1 else: e1 = genEngMap(table[i, j - 1]) e2 = genEngMap(table[i - 1, j]) [Mx, Tbx] = cumMinEngVer(e1) [My, Tby] = cumMinEngHor(e2) [Ix, Ex] = rmVerSeam(table[i, j - 1], Mx, Tbx) [Iy, Ey] = rmHorSeam(table[i - 1, j], My, Tby) if (T[i - 1, j] + Ey <= T[i, j - 1] + Ex): table[i, j] = Iy T[i, j] = T[i - 1, j] + Ey track[i, j] = 1 else: table[i, j] = Ix T[i, j] = T[i, j - 1] + Ex track[i, j] = -1 vid = [] [r, c] = [nr, nc] while track[r, c] != 0: if track[r, c] == 1: vid.append(table[r, c]) r = r - 1 elif track[r, c] == -1: vid.append(table[r, c]) c = c - 1 vid.append(table[0, 0]) vid.reverse() Ic = table[nr, nc] return Ic, T, vid
def carv(I, nr, nc, I_label): # record=np.zeros(np.array([nr,nc])) # T=mincost(nr, nc, I, record) T = np.zeros(np.array([nr + 1, nc + 1])) # x_mesh, y_mesh=np.meshgrid(np.arange(nr), np.arange(nc)) dict = {(0, 0): I} #res_list = [] for i in range(0, nr + 1): print(i) for j in range(0, nc + 1): print(j) if (i == 0 and j == 0): T[i, j] = 0 elif i == 0: emap = genEngMap(dict[(i, j - 1)], I_label) [Mx, Tbx] = cumMinEngVer(emap) [Ix, Ex, I_label] = rmVerSeam(dict[(i, j - 1)], Mx, Tbx, I_label) dict[(i, j)] = Ix T[i, j] = T[i, j - 1] + Ex elif j == 0: emap = genEngMap(dict[(i - 1, j)], I_label) [My, Tby] = cumMinEngHor(emap) [Iy, Ey, I_label] = rmHorSeam(dict[(i - 1, j)], My, Tby, I_label) dict[(i, j)] = Iy T[i, j] = T[i - 1, j] + Ey else: emap = genEngMap(dict[(i, j - 1)], I_label) emap2 = genEngMap(dict[(i - 1, j)], I_label) [Mx, Tbx] = cumMinEngVer(emap) [My, Tby] = cumMinEngHor(emap2) [Ix, Ex, I_label] = rmVerSeam(dict[(i, j - 1)], Mx, Tbx, I_label) [Iy, Ey, I_label] = rmHorSeam(dict[(i - 1, j)], My, Tby, I_label) if (T[i - 1, j] + Ey <= T[i, j - 1] + Ex): dict[(i, j)] = Iy T[i, j] = T[i - 1, j] + Ey else: dict[(i, j)] = Ix T[i, j] = T[i, j - 1] + Ex #res_list.append(dict[(i, j)]) return dict[(i, j)], T #, res_list
def getTupleLeft(clone): dataEn = genEngMap(clone) Mx, Tbx = cumMinEngVer(dataEn) clone2, e = rmVerSeam(clone, Mx, Tbx) return clone2, e
def getTupleUp(clone): dataEn = genEngMap(clone) My, Tby = cumMinEngHor(dataEn) clone2, e = rmHorSeam(clone, My, Tby) return clone2, e
def do_the_actual_seam_carving(bounding_box): e = genEngMap(bounding_box) Mx, Tbx = cumMinEngVer(e) seam_to_be_removed = rmVerSeam(bounding_box, Mx, Tbx) return seam_to_be_removed
def carv(I, nr, nc): res_list = [] height, width, color = I.shape T = np.zeros((nr + 1, nc + 1)) e = genEngMap(I) Ic = I Mx, Tbx = cumMinEngVer(e) My, Tby = cumMinEngHor(e) removed_c = 0 removed_r = 0 T[0][1] = np.amin(Mx[height - 1]) My_transpose = np.transpose(My) T[1][0] = np.amin(My_transpose[height - 1]) for i in range(nr + nc): if removed_c == nc: print("all columns removed") My, Tby = cumMinEngHor(e) Ic, E = rmHorSeam(Ic, My, Tby) res_list.append(Ic) e = genEngMap(Ic) removed_r += 1 elif removed_r == nr: print("all rows removed") Mx, Tbx = cumMinEngVer(e) Ic, E = rmVerSeam(Ic, My, Tby) res_list.append(Ic) e = genEngMap(Ic) removed_c += 1 elif T[removed_r][removed_c + 1] > T[removed_r + 1][removed_c]: # here is the problem print("removing row") My, Tby = cumMinEngHor(e) Ic, E = rmHorSeam(Ic, My, Tby) res_list.append(Ic) e = genEngMap(Ic) removed_r += 1 Mx, Tbx = cumMinEngVer(e) My, Tby = cumMinEngHor(e) T[removed_r][removed_c + 1] = np.amin(Mx[height - 1 - removed_r]) My_transpose = np.transpose(My) if (removed_r < nr): T[removed_r + 1][removed_c] = np.amin(My_transpose[width - 1 - removed_c]) else: print("removing column") Mx, Tbx = cumMinEngVer(e) Ic, E = rmVerSeam(Ic, Mx, Tbx) res_list.append(Ic) e = genEngMap(Ic) removed_c += 1 Mx, Tbx = cumMinEngVer(e) My, Tby = cumMinEngHor(e) if (removed_c < nc): T[removed_r][removed_c + 1] = np.amin(Mx[height - 1 - removed_r]) My_transpose = np.transpose(My) T[removed_r + 1][removed_c] = np.amin(My_transpose[width - 1 - removed_c]) return np.uint8(Ic), T
def carv(I, nr, nc): res_list = [] print I.shape height, width, color = I.shape T = np.zeros((nr + 1, nc + 1)) e = genEngMap(I) Ic = I Mx, Tbx = cumMinEngVer(e) My, Tby = cumMinEngHor(e) removed_c = 0 removed_r = 0 T[0][1] = np.amin(Mx[height - 1]) My_transpose = np.transpose(My) T[1][0] = np.amin(My_transpose[height - 1]) for i in range(nr + nc): # print i if removed_c == nc: print "all columns removed" My, Tby = cumMinEngHor(e) Ic, E = rmHorSeam(Ic, My, Tby) res_list.append(Ic) e = genEngMap(Ic) removed_r += 1 elif removed_r == nr: print "all rows removed" Mx, Tbx = cumMinEngVer(e) Ic, E = rmVerSeam(Ic, My, Tby) res_list.append(Ic) e = genEngMap(Ic) removed_c += 1 elif T[removed_r][removed_c + 1] > T[removed_r + 1][removed_c]: #here is the problem print "romoving row" My, Tby = cumMinEngHor(e) Ic, E = rmHorSeam(Ic, My, Tby) res_list.append(Ic) e = genEngMap(Ic) removed_r += 1 Mx, Tbx = cumMinEngVer(e) My, Tby = cumMinEngHor(e) T[removed_r][removed_c + 1] = np.amin(Mx[height - 1 - removed_r]) My_transpose = np.transpose(My) if (removed_r < nr): T[removed_r + 1][removed_c] = np.amin(My_transpose[width - 1 - removed_c]) else: print "removing column" Mx, Tbx = cumMinEngVer(e) Ic, E = rmVerSeam(Ic, Mx, Tbx) res_list.append(Ic) e = genEngMap(Ic) removed_c += 1 Mx, Tbx = cumMinEngVer(e) My, Tby = cumMinEngHor(e) if (removed_c < nc): T[removed_r][removed_c + 1] = np.amin(Mx[height - 1 - removed_r]) My_transpose = np.transpose(My) T[removed_r + 1][removed_c] = np.amin(My_transpose[width - 1 - removed_c]) # print Ic.shape imageio.mimsave('./Shanghai2Output.gif', res_list) return Ic, T # test = np.array([[5.5, 8, 4.5, 6, 3], [13, 9, 30, 27, 19], [6.5, 7, 6, 12.5, 8], [16, 24, 27, 11, 13], [3, 6, 4.5, 8, 5.5]]) # data1 = np.array([[[1, 2, 3], [1, 2, 3], [1, 6, 3], [1, 2, 3], [1, 2, 3]], [[4, 5, 6], [5, 5, 6], [4, 5, 6], [4, 5, 6], [4, 5, 6]], [[1, 2, 3], [1, 2, 3], [1, 2, 4], [1, 2, 3], [1, 2, 3]], [[4, 5, 6], [4, 5, 6], [4, 5, 6], [4, 7, 6], [4, 5, 6]], [[7, 8, 9], [7, 8, 9], [10, 11, 12], [3, 5, 0], [2, 1, 6]]]) # carv(data1, 2, 2) # img1 = Image.open("shanghai2.jpg") # data1 = np.asarray(img1, dtype="int32") # Ic, T = carv(data1, 50, 30) # Ic = np.asarray(Ic, dtype="uint8") # plt.imshow(Ic) # plt.show()
def carv(I, nr, nc): # Your Code Here N, M, _ = I.shape # construct the empty matrix T T = np.zeros((nr + 1, nc + 1)) # initialize the mapping dictionary image_map = {} image_map[(0, 0)] = I # initial the track table, 1 is left and 2 is up Tbt = np.zeros((nr + 1, nc + 1)) Tbt[0, :] = 1 Tbt[:, 0] = 2 Tbt[0, 0] = 0 # fill the first row and col of the T matrix for i in range(1, nr + 1): # new coor coor = (i, 0) up_coor = (i - 1, 0) I_up = image_map[up_coor] # new image with one row deleted e = genEngMap(I_up) My, Tby = cumMinEngHor(e) Iy, E = rmHorSeam(I_up, My, Tby) # add the new image to the dict and fill the (i,0) of E matrix image_map[coor] = Iy T[coor] = T[up_coor] + E for j in range(1, nc + 1): # new coor coor = (0, j) left_coor = (0, j - 1) I_left = image_map[left_coor] # new image with one column deleted e = genEngMap(I_left) Mx, Tbx = cumMinEngVer(e) Ix, E = rmVerSeam(I_left, Mx, Tbx) # add the new image to the dict and fill the (0,j) of E matrix image_map[coor] = Ix T[coor] = T[left_coor] + E # DP to complete T matrix for ii in range(1, nr + 1): for jj in range(1, nc + 1): coor = (ii, jj) up_coor = (ii - 1, jj) left_coor = (ii, jj - 1) candidate_img_list = [] cost_list = [] # grow from left, delete col I_left = image_map[left_coor] e = genEngMap(I_left) Mx, Tbx = cumMinEngVer(e) Ix, Ex = rmVerSeam(I_left, Mx, Tbx) # Ix = np.pad(Ix, ((0, ii), (0, jj)), 'constant') cost_from_left = Ex + T[left_coor] # grow from up, delete row I_up = image_map[up_coor] e = genEngMap(I_up) My, Tby = cumMinEngHor(e) Iy, Ey = rmHorSeam(I_up, My, Tby) # Iy = np.pad(Iy, ((0,ii),(0,jj)), 'constant') cost_from_up = Ey + T[up_coor] # construct cost list (2 elements) cost_list.append(cost_from_left) cost_list.append(cost_from_up) # add candidate imgs to the list candidate_img_list.append(Ix) candidate_img_list.append(Iy) # construct Tbt and T table # 1 is left and 2 is up indentify_direction = np.argmin(cost_list) Tbt[ii, jj] = indentify_direction + 1 T[ii, jj] = cost_list[indentify_direction] image_map[coor] = candidate_img_list[indentify_direction] # from (nr,nc) trace back along the shortest path path, path_imags = shortest_path_delete_imags(image_map, Tbt) # for the return Ic image Ic = path_imags[-1] # make the gif make_gif(N, M, path_imags) return Ic, T
def delVer(I): e = genEngMap(I) Mv, Tbv = cumMinEngVer(e) Iv, Ev = rmVerSeam(I, Mv, Tbv) return Iv, Ev
def delHor(I): e = genEngMap(I) Mh, Tbh = cumMinEngHor(e) Ih, Eh = rmHorSeam(I, Mh, Tbh) return Ih, Eh
def cutV(I): Mx, Tbx = cumMinEngVer(genEngMap(I)) Ix, Ex = rmVerSeam(I, Mx, Tbx) return Ix, Ex
def cutH(I): My, Tby = cumMinEngHor(genEngMap(I)) Iy, Ey = rmHorSeam(I, My, Tby) return Iy, Ey
def carv(I, nr, nc): # Your Code Here r = nr + 1 c = nc + 1 image_list = [[[] for j in range(c)] for i in range(r)] T = np.zeros((r, c)) Tr = np.zeros((r, c)) image_list[0][0] = np.copy(I) for i in list(range(1, c)): img = np.copy(image_list[0][i - 1]) e = genEngMap.genEngMap(img) M_left, Tb_left = cumMinEngVer.cumMinEngVer(e) image, cost = rmVerSeam.rmVerSeam(img, M_left, Tb_left) image_list[0][i] = np.copy(image) Tr[0][i] = Tr[0][i - 1] + cost for j in list(range(1, r)): img = np.copy(image_list[j - 1][0]) e = genEngMap.genEngMap(img) M_up, Tb_up = cumMinEngHor.cumMinEngHor(e) image_b, cost = rmHorSeam.rmHorSeam(img, M_up, Tb_up) image_list[j][0] = np.copy(image_b) Tr[j][0] = Tr[j - 1][0] + cost T[j][0] = 1 for i in list(range(1, r)): for j in list(range(1, c)): img = np.copy(image_list[i][j - 1]) e_left = genEngMap.genEngMap(img) M_left, Tb_left = cumMinEngVer.cumMinEngVer(e_left) image_left, cost_left = rmVerSeam.rmVerSeam(img, M_left, Tb_left) img = np.copy(image_list[i - 1][j]) e_up = genEngMap.genEngMap(img) M_up, Tb_up = cumMinEngHor.cumMinEngHor(e_up) image_up, cost_up = rmHorSeam.rmHorSeam(img, M_up, Tb_up) cmp_list = [Tr[i, j - 1] + cost_left, Tr[i - 1, j] + cost_up] min_val = min(cmp_list) Tr[i, j] = min_val min_index = cmp_list.index(min_val) T[i, j] = min_index if (min_index == 0): image_list[i][j] = np.copy(image_left) else: image_list[i][j] = np.copy(image_up) Ic = image_list[r - 1][c - 1] #Saving the last frame im = Image.fromarray(Ic) im.save('Scenary_final.jpg') #creating the GIF gif_list = [[] for j in range(nr + nc + 1)] i = nr + nc carved_list = backtrack(T, nr, nc, image_list, gif_list, i) imageio.mimsave('Scenary.gif', carved_list, duration=0.2) return Ic, Tr
plt.figure(1) plt.imshow(I) # auto-refined mask x1 = ind[1] y1 = ind[0] x2 = ind[3] y2 = ind[2] mask, h, w = Domask(I, x1, x2, y1, y2) I_label = label(I, mask, x1, x2, y1, y2) plt.figure(2) plt.imshow(I_label) # object removal with weighted seam carving nr = 0 nc = ind[3] - ind[1] [Ic, T] = carv(I, nr, nc, I_label) fig2 = plt.figure(3) plt.imshow(Ic) # seam insertion Ic_n = Ic summ = int(nc / 2) I_label_insert = np.ones(np.shape(Ic_n[:, :, 0])) a = genEngMap(Ic_n, I_label_insert) [Mx, Tbx] = cumMinEngVer(a) [Ix, Ex] = insertVerSeam(Ic_n, Mx, Tbx, summ) plt.figure(4) plt.imshow(Ix) plt.show()