Solved

How do you find and remove a seam when doing seam carving in Python for image processing?

Posted on 2014-10-26
1
414 Views
Last Modified: 2014-10-28
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:

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()

Open in new window

seamcarver.py
someimage.png
0
Comment
Question by:AttilaB
1 Comment
 
LVL 45

Accepted Solution

by:
aikimark earned 500 total points
ID: 40406953
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In a previously published article (http://www.experts-exchange.com/articles/10331/Automatic-Duplex-Scanning-in-PaperPort-Versions-11-12-14.html) here at Experts Exchange, I explained how to achieve duplex (double-sided) scanning in Nuance's PaperPor…
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 what exposure is and how to use the exposure slider. Analyze the photo that you want to edit, then adjust the exposure slider to your liking.
The goal of the tutorial is to teach the user how to use import presets downloaded from the internet into Adobe Lightroom. Once you downloaded the presets go into the preset folder and press import then import your preset and your set it to go.

919 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now