I am implementing the seam carving algorithm in Python, which is used to resize and manipulate images.

See the following links for an explanation of the seam carving algorithm:

http://cs.brown.edu/courses/cs129/results/proj3/taox/

http://www.cs.princeton.edu/courses/archive/spring13/cos226/assignments/seamCarving.html

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.

I am using Python 2.7.

Required Python libraries (for Windows 32-bit):

numpy-MKL-1.9.0.win32-py2.7

scipy-0.14.0-win32-superpack-python2.7

scikit-image-0.10.1.win32-py2.7

matplotlib-1.4.0.win32-python2.7

python-dateutil-2.2.win32-py2.7

pyparsing-2.0.3.win32-py2.7

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:

someimage.png

See the following links for an explanation of the seam carving algorithm:

http://cs.brown.edu/courses/cs129/results/proj3/taox/

http://www.cs.princeton.edu/courses/archive/spring13/cos226/assignments/seamCarving.html

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.

I am using Python 2.7.

Required Python libraries (for Windows 32-bit):

numpy-MKL-1.9.0.win32-py2.

scipy-0.14.0-win32-superpa

scikit-image-0.10.1.win32-

matplotlib-1.4.0.win32-pyt

python-dateutil-2.2.win32-

pyparsing-2.0.3.win32-py2.

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 numpy
import scipy.misc as scm
from pylab import *
from scipy import ndimage
from skimage import img_as_float, filter
def 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 energyArr
def 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 img
def 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
#pass
def 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()
```

seamcarver.pysomeimage.png

Experts Exchange Solution brought to you by

Enjoy your complimentary solution view.

Get every solution instantly with Premium.
Start your 7-day free trial.

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Python

From novice to tech pro — start learning today.

Experts Exchange Solution brought to you by

Enjoy your complimentary solution view.

Get every solution instantly with Premium.
Start your 7-day free trial.

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.