def reduceHeight (im, energyImage): cum_energy = cumulative_minimum_energy_map(energyImage, "HORIZONTAL") hSeam = find_optimal_horizontal_seam(cum_energy) new_im = np.empty_like(im) crop_im = new_im[:(im.shape[0]-1), :] # print "crop_im size is" # print crop_im.shape new_energy_map = np.empty_like(energyImage) # print "new_energy_map size is" # print new_energy_map.shape crop_energy_map = new_energy_map[:(energyImage.shape[0]-1), :] # print "crop_energy_map size is" # print crop_energy_map.shape for col in range(im.shape[1]): for row in range(im.shape[0]): if( row < hSeam[col]): #print "Col is %d and hSeam[col] is %d" % (col, hSeam[col]) crop_im[row][col] = im[row][col] crop_energy_map[row][col] = energyImage[row][col] elif(row == hSeam[col]): pass else: crop_im[row-1][col] = im[row][col] crop_energy_map[row-1][col] = energyImage[row][col] # imsave("crop_output.jpg", crop_im) return crop_im, crop_energy_map
def reduceWidth (im, energyImage): cum_energy = cumulative_minimum_energy_map(energyImage, "VERTICAL") vSeam = find_optimal_vertical_seam(cum_energy) #new_im = np.zeros(im.shape[0], im.shape[1]-1) #new_energy_map = np.empty_like(energyImage) new_im = np.empty_like(im) crop_im = new_im[:, :(im.shape[1]-1)] # print "crop_im size is" # print crop_im.shape new_energy_map = np.empty_like(energyImage) # print "new_energy_map size is" # print new_energy_map.shape crop_energy_map = new_energy_map[:, :(new_energy_map.shape[1]-1)] # print "crop_energy_map size is" # print crop_energy_map.shape for row in range(im.shape[0]): for col in range(im.shape[1]): if( col < vSeam[row]): #print "Col is %d and vSeam[row] is %d" % (col, vSeam[row]) crop_im[row][col] = im[row][col] crop_energy_map[row][col] = energyImage[row][col] elif(col == vSeam[row]): pass else: crop_im[row][col-1] = im[row][col] crop_energy_map[row][col-1] = energyImage[row][col] return crop_im, crop_energy_map
def reduceWidth(im, energyImage): mapV = cumulative_minimum_energy_map(energyImage, "VERTICAL") seam = find_optimal_vertical_seam(mapV) newIm = np.zeros((im.shape[0], im.shape[1] - 1, im.shape[2])).astype(int) for i in xrange(len(seam)): newIm[i] = np.delete(im[i], seam[i], 0) return [newIm, energy_image(newIm)]
def reduceHeight(im, energyImage): mapH = cumulative_minimum_energy_map(energyImage, "HORIZONTAL") seam = find_optimal_horizontal_seam(mapH) newIm = np.zeros((im.shape[0] - 1, im.shape[1], im.shape[2])).astype(int) for i in xrange(len(seam)): newIm[:, i] = np.delete(im[:, i], seam[i], 0) return [newIm, energy_image(newIm)]
def reduceHeight(im, energyImage): m, n = im.shape[: 2] cumulativeMinimumEnergyMap = cumulative_minimum_energy_map(energyImage,'HORIZONTAL') horizontal_seam = find_optimal_horizontal_seam(cumulativeMinimumEnergyMap) reducedColorImage = np.zeros((m-1,n,3),dtype = np.uint8) reducedEnergyImage = np.zeros((m-1,n),dtype = np.float64) for col in range(n): imageCopy = im[:,col,:] mask = np.ones(m,dtype = bool) mask[horizontal_seam[col]] = False reducedColorImage[:,col,:] = imageCopy[mask] reducedEnergyImage = energy_image(reducedColorImage) return [reducedColorImage,reducedEnergyImage]
def reduceWidth(im, energyImage): m, n = im.shape[:2] cumulativeMinimumEnergyMap = cumulative_minimum_energy_map( energyImage, 'VERTICAL') verticalSeam = find_optimal_vertical_seam(cumulativeMinimumEnergyMap) reducedColorImage = np.zeros((m, n - 1, 3), dtype=np.uint8) reducedEnergyImage = np.zeros((m, n - 1), dtype=np.float64) for rows in range(m): imageCopy = im[rows, :, :] mask = np.ones(n, dtype=bool) mask[verticalSeam[rows]] = False reducedColorImage[rows, :, :] = imageCopy[mask] reducedEnergyImage = energy_image(reducedColorImage) return [reducedColorImage, reducedEnergyImage]
def reduceWidth(im, energyImage): map = cumulative_minimum_energy_map(energyImage, 'VERTICAL') x = find_optimal_vertical_seam(map) y = np.arange(im.shape[0]) height, width, channels = im.shape e_height, e_width = energyImage.shape final_im = np.zeros((height, width - 1, channels), dtype=np.uint8) energy_im = np.zeros((e_height, e_width - 1), dtype=float) for y_index in range(0, height): left = im[y_index][:x[y_index]][:] right = im[y_index][x[y_index] + 1:][:] total = np.concatenate((left, right), axis=0) final_im[y_index] = total left_e = energyImage[y_index][:x[y_index]] right_e = energyImage[y_index][x[y_index] + 1:] total_e = np.concatenate((left_e, right_e), axis=0) energy_im[y_index] = total_e return (final_im, energy_im)
def reduceHeight(im, energyImage): """ Args: energyImage: 2D matrix of datatype double im: MxNx3 matrix of datatype uint8 Returns: reducedColorImage: 3D matrix same as the input image but with its height reduced by one pixel reducedEnergyImage: 2D matrix same as the inputenergyImage, but with its height reduced by one pixel """ row = np.size(energyImage, 0) col = np.size(energyImage, 1) cumap = cumulative_minimum_energy_map(energyImage, 'HORIZONTAL') seam = find_optimal_horizontal_seam(cumap) reducedColorImage = np.zeros((row - 1, col, 3), dtype=np.uint8) reducedEnergyImage = np.zeros((row - 1, col), dtype=np.double) for i in range(col): reducedColorImage[0:seam[i], i, :] = im[0:seam[i], i, :] reducedEnergyImage[0:seam[i], i] = energyImage[0:seam[i], i] reducedColorImage[seam[i]:row - 1, i, :] = im[seam[i] + 1:row, i, :] reducedEnergyImage[seam[i]:row - 1, i] = energyImage[seam[i] + 1:row, i] return reducedColorImage, reducedEnergyImage
def reduceWidth(im, energyImage): """ Args: energyImage: 2D matrix of datatype double im: MxNx3 matrix of datatype uint8 Returns: reducedColorImage: 3D matrix same as the input image but with its width reduced by one pixel reducedEnergyImage: 2D matrix same as the inputenergyImage, but with its width reduced by one pixel """ row=np.size(energyImage,0) col=np.size(energyImage,1) cumap=cumulative_minimum_energy_map(energyImage, 'VERTICAL') seam=find_optimal_vertical_seam(cumap) reducedColorImage=np.zeros((row,col-1,3),dtype=np.uint8) reducedEnergyImage=np.zeros((row,col-1),dtype=np.double) for i in range(row): reducedColorImage[i,0:seam[i],:]=im[i,0:seam[i],:] reducedEnergyImage[i,0:seam[i]]=energyImage[i,0:seam[i]] reducedColorImage[i,seam[i]:col-1,:]=im[i,seam[i]+1:col,:] reducedEnergyImage[i,seam[i]:col-1]=energyImage[i,seam[i]+1:col] return reducedColorImage, reducedEnergyImage
def reduceHeight(im, energyImage): input_color_image = im input_image_nth_row, input_image_nth_col, input_image_nth_channel = input_color_image.shape reducedColorImage = np.zeros((input_image_nth_row - 1, input_image_nth_col, input_image_nth_channel), dtype=np.uint8) horizontalSeam = find_optimal_horizontal_seam( cumulative_minimum_energy_map(energyImage, 'HORIZONTAL')) for ith_col in xrange(0, input_image_nth_col): reducedColorImage[:, ith_col, 0] = np.delete(input_color_image[:, ith_col, 0], horizontalSeam[ith_col]) reducedColorImage[:, ith_col, 1] = np.delete(input_color_image[:, ith_col, 1], horizontalSeam[ith_col]) reducedColorImage[:, ith_col, 2] = np.delete(input_color_image[:, ith_col, 2], horizontalSeam[ith_col]) reducedEnergyImage = energy_image(reducedColorImage) return reducedColorImage, reducedEnergyImage
x1 = np.array([0, -(0.5), 0.5, 0]) x2 = np.array([0, 0.5, -0.5, 0]) xconvolution = ndimage.convolve(x1, x2, mode='constant', cval=0.0) img = imread('inputSeamCarvingPrague.jpg') #print img.dtype, img.shape #plt.imshow(img) #plt.imshow(img[: : ], cmap = plt.get_cmap('gray')) #plt.imshow(energy_image(img), cmap = plt.get_cmap('gray')) en_image = energy_image(img) plt.figure() plt.title('Gradient Energy Intensity') plt.imshow(en_image) plt.show() v_cum_min_energy = cumulative_minimum_energy_map(en_image, 'VERTICAL') plt.figure() plt.title('Cumulative Vertical Intensity') plt.imshow(v_cum_min_energy) plt.show() h_cum_min_energy = cumulative_minimum_energy_map(en_image, 'HORIZONTAL') plt.figure() plt.title('Cumulative Horizontal Intensity') plt.imshow(h_cum_min_energy) plt.show() plt.figure() plt.title('Original Image') plt.imshow(img) plt.show()
import matplotlib.image as mpimg ### prague img = mpimg.imread('./inputSeamCarvingPrague.jpg') plt.imshow(img) # plt.savefig('./energyImagePrague.png') plt.show() ### energyImage energyImage = energy_image(img) plt.imshow(energyImage, cmap=plt.get_cmap('gray')) # plt.savefig('./energyImagePrague.png') plt.show() ### cumulative energy map mapV = cumulative_minimum_energy_map(energyImage, "VERTICAL") mapH = cumulative_minimum_energy_map(energyImage, "HORIZONTAL") plt.imshow(mapV, cmap=plt.get_cmap('gray')) # plt.savefig('./verticalCumulativeEnergyMapPrague.png') plt.show() plt.imshow(mapH, cmap=plt.get_cmap('gray')) # plt.savefig('./horizontalCumulativeEnergyMapPrague.png') plt.show() ### vertical seam vSeam = find_optimal_vertical_seam(mapV) ### horizontal seam hSeam = find_optimal_horizontal_seam(mapH) ### display seam