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 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
from energy_image import energy_image from reduceHeight import reduceHeight import matplotlib.pylab as plt import numpy as np import scipy.misc image = 'inputSeamCarvingPrague.jpg' ei = energy_image(image) image_array = np.asarray(plt.imread(image)) for i in range(0, 100): image_array, ei = reduceHeight(image_array, ei) scipy.misc.imsave('outputEnergyReduceHeightPrague.png', ei) scipy.misc.imsave('outputReduceHeightPrague.png', image_array) image = 'inputSeamCarvingMall.jpg' ei = energy_image(image) image_array = np.asarray(plt.imread(image)) for i in range(0, 100): image_array, ei = reduceHeight(image_array, ei) scipy.misc.imsave('outputReduceHeightMall.png', image_array)
# -*- coding: utf-8 -*- """ Created on Mon Sep 9 01:17:38 2019 @author: songl """ import matplotlib.pyplot as plt from energy_image import energy_image from reduceHeight import reduceHeight pic1 = plt.imread('inputSeamCarvingPrague.jpg') energy1 = energy_image(pic1) for i in range(100): pic1, energy1 = reduceHeight(pic1, energy1) plt.imsave('outputReduceHeightPrague.png', pic1) pic2 = plt.imread('inputSeamCarvingMall.jpg') energy2 = energy_image(pic2) for i in range(100): pic2, energy2 = reduceHeight(pic2, energy2) plt.imsave('outputReduceHeightMall.png', pic2)
bw = rgb2gray(im) # use prewitt filters kernely = np.array([[1, 1, 1], [0, 0, 0], [-1, -1, -1]]) # Perform x convolution y = np.absolute(ndimage.convolve(bw, kernely)) # take threshold threshold = np.mean(y) y -= threshold return y ### simple derivative imgModify = mpimg.imread('./inputSeamCarvingPrague.jpg') img = imgModify.copy() modifiedEnergyImage = energy_image_simple(img) energyImage = energy_image(img) plt.imshow(energyImage, cmap=plt.get_cmap('gray')) plt.show() plt.imshow(modifiedEnergyImage, cmap=plt.get_cmap('gray')) plt.show() for i in xrange(100): # progress(i, 100, status="reducing prague image width by 100 with simple one row derivative") [imgModify, modifiedEnergyImage] = reduceWidth(imgModify, modifiedEnergyImage) [img, energyImage] = reduceWidth(img, energyImage) plt.imshow(img) plt.show() plt.imshow(imgModify) plt.show() ### take a high threshold cutoff
cum_eng_map[index_y][ index_x] = energyImage[index_y][index_x] + min( cum_eng_map[index_y - 1][i], cum_eng_map[index_y - 1][j], cum_eng_map[index_y - 1][k]) if (seamDirection == "HORIZONTAL"): cum_eng_map[:, 0] = energyImage[:, 0] for index_x in range(1, width): for index_y in range(0, height): i = index_y - 1 j = index_y k = index_y + 1 if (index_y == 0): i = index_y if (index_y + 1 == height): k = index_y cum_eng_map[index_y][ index_x] = energyImage[index_y][index_x] + min( cum_eng_map[i][index_x - 1], cum_eng_map[j][index_x - 1], cum_eng_map[k][index_x - 1]) #plt.imshow(cum_eng_map) #plt.show() return cum_eng_map ei = energy_image('inputSeamCarvingPrague.jpg') #scipy.misc.imsave('EnergyPrague.png', ei) map = cumulative_minimum_energy_map(ei, "VERTICAL") #scipy.misc.imsave('EnergyMapPragueVertical.png', map)
import numpy as np import math from scipy.misc import imread, imsave from scipy import ndimage #from skimage import img_as_float 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()
# -*- coding: utf-8 -*- """ Created on Sun Sep 13 09:41:15 2015 @author: sbhal """ from energy_image import energy_image from reduceWidth import reduceWidth import matplotlib.pyplot as plt from scipy.misc import imsave img = imread('inputSeamCarvingPrague.jpg') print img.dtype, img.shape en_im = energy_image(img) for i in range(100): img, en_im = reduceWidth(img, en_im) plt.imsave("outputReduceWidthPrague", img) img = imread('inputSeamCarvingMall.jpg') print img.dtype, img.shape en_im = energy_image(img) for i in range(100): img, en_im = reduceWidth(img, en_im) plt.imsave("outputReduceWidthMall", img)
from energy_image import energy_image from reduceHeight import reduceHeight from progressBar import progress import matplotlib.pyplot as plt import matplotlib.image as mpimg ### prague image imgPrague = mpimg.imread('./inputSeamCarvingPrague.jpg') energyImagePrague = energy_image(imgPrague) for i in xrange(100): # progress(i, 100, status="reducing prague image height by 100") [imgPrague, energyImagePrague] = reduceHeight(imgPrague, energyImagePrague) plt.imshow(imgPrague) # plt.savefig('./outputReduceHeightPrague.png') plt.show() ### mall image imgMall = mpimg.imread('./inputSeamCarvingMall.jpg') energyImageMall = energy_image(imgMall) for i in xrange(100): # progress(i, 100, status="reducing mall image width by 100") ### reduce width [imgMall, energyImageMall] = reduceHeight(imgMall, energyImageMall) plt.imshow(imgMall) # plt.savefig('./outputReduceHeightMall.png') plt.show()