The dual_gradient_energy function computes the energy of each pixel of the image.

I am trying to find the horizontal seam of the image.

The function is supposed to return an array of H (height) integers. For each row, it should return the column of the seam.

I am unable to get the find_seam function to work. Instead of returning pixels in integers, the values being returned in the array are in scientific notation. I made sure to check boundary cases so that it does not fill an array with an invalid/nonexistent element.

Plot_seam should plot the seam on the image and highlight it in red. I have it implemented already, but I have not been able to test it since there is no seam to plot. Once the find_seam function is fixed, then a seam can be plotted.

Remove_seam function modifies the image in-place and returns a W-1 (width) x H x 3 (height) slice. In essence, it removes the seam from the image. I am having trouble implementing this because the array indices seem to be out of bounds.

I have provided an image (someimage.png) to use for testing the functions.

What is wrong with my find_seam and remove_seam functions? How can I fix my code?

Note: You may need to close the graph that is generated when you run the source code in order to see the find_seam, plot_seam, and remove_seam function calls.

See my code below:

import numpyimport scipy.misc as scmfrom pylab import *from scipy import ndimagefrom skimage import img_as_float, filterdef dual_gradient_energy(img): R = img[:, :, 0] G = img[:, :, 1] B = img[:, :, 2] hColorR = filter.hsobel(R) vColorR = filter.vsobel(R) hColorG = filter.hsobel(G) vColorG = filter.vsobel(G) hColorB = filter.hsobel(B) vColorB = filter.vsobel(B) energyArr = hColorR*hColorR+vColorR*vColorR+hColorG*hColorG+vColorG*vColorG+hColorB*hColorB+vColorB*vColorB return energyArrdef find_seam(img): height,width = img.shape[:2] seamFitness = dual_gradient_energy(img) #for i in range(0, width): #seamFitness[0][i] = img[0][i] for x in range(0, width-2): for y in range (1, height-2): #seamFitness[x][y] = img[x][y] if (x>0) and (x<width) and (y==0): seamFitness[x][y] += min(seamFitness[x][y-1], seamFitness[x+1][y-1]) if (x>0) and (x == width-1): seamFitness[x][y] += min(seamFitness[x][y-1], seamFitness[x-1][y-1]) if (x!=0): seamFitness[x][y] += min(seamFitness[x-1][y-1], seamFitness[x][y-1], seamFitness[x+1][y-1]) return seamFitness[y]def remove_seam(img,seam): attempt = 0 i = 0 height,width = img.shape[:2] seamFitness = np.zeros((height, width)) for attempt in range(attempt, img.size): bestRow = 0 for i in range(i, height-img.size): if (seamFitness[width-1][bestRow] > seamFitness[width-1][i]): bestRow = i x = width-1 if (x > 0): theMin = seamFitness[x-1][bestRow] if (bestRow > 0 and seamFitness[x-1][bestRow-1] <= theMin): bestRow = bestRow-1 elif (bestRow < height-1 and seamFitness[x-1][bestRow+1] <= theMin): bestRow = bestRow+1 return imgdef plot_seam(img, seam): height,width,dim = img.shape for i in xrange(0,len(seam)): img[i][seam[i]][0] = 255 img[i][seam[i]][1] = 0 img[i][seam[i]][2] = 0 #passdef main(): img = imread('someimage.png') img = img_as_float(img) l=dual_gradient_energy(img) #works! figure() gray() imshow(l) show() r = find_seam(img) print r s = remove_seam(img, r) imshow(plot_seam(img, r)) show()if __name__ == '__main__': main()

I would expect a floating point result, based on the definition of the energy gradient formula definition. If you need integer results, cast the floating-point values into integer values.
I was curious

I would expect a floating point result, based on the definition of the energy gradient formula definition. If you need integer results, cast the floating-point values into integer values.

I was curious why your x and y for loops have different starting values and (relative) different ending values.

btw...I'm a big fan of this algorithm and, generally, context sensitive resizing.

0

Featured Post

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

I. Introduction
In a previous article (http://www.experts-exchange.com/Web_Development/Document_Imaging/A_6537-PaperPort-Upgrade-How-to-download-and-install-updated-versions-of-PaperPort-11-and-12.html) (now deprecated), I discussed how to upgrad…

Recently, an awarded photographer, Selina De Maeyer (http://www.selinademaeyer.com/), completed a photo shoot of a beautiful event (http://www.sintjacobantwerpen.be/verslag-en-fotoreportage-van-de-sacramentsprocessie-door-antwerpen#thumbnails) in An…

The goal of the tutorial is to teach the user how to use the cropping tool and to straighten there photos so it does not look tilted.
You will choose which photo you want to edit, crop it to you liking and make it straight.