Solved

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

Posted on 2014-10-26
1
405 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
Comment Utility
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

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…
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…
The goal of this is to tech the user how to export photos out of Lightroom and the different options they have. Select which photos you want to export : Select where you want to export and with which options : Export :
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.

771 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

11 Experts available now in Live!

Get 1:1 Help Now