posts - 48, comments - 16, trackbacks - 4

My Links

Archives

Post Categories

Web Dev

Seam Carving for Resizing using SEAMonster

I first saw seam carving, http://www.seamcarving.com/, about a year ago when it was so hot that just downloading the white paper was a challenge (see post). I immediately dug for code with no luck. A few months later I noted some python code, thought of translating that but the time just wasn't available. I did another check for seam carving code lately and found SEAMonster. The code runs well as is, though the algorithm was a bit slower than I had hoped. Resizing a 400x300 jpg image on my core2 laptop took a few seconds. While this is just great for applications, not quite where I want it for web use, say in a resize library for a web app.

 

One issue I did find with SEAMonster, a small 'bug' in the Carve method when it carves to a size. The algorithm will stop on the first constraint minimizing, and will not alternate carving directions automatically. I made a small change to it so that it will stop when both dimensions have been met and so that it will carve down both sizes. The redefined Carve() method is included below.

 

The SEAMonster library, once compiled can be referenced from a web app. I originally wanted to include the source code within the web app for convenience, but the unsafe code steered my away from that approach.

 

Some Thoughts:

  • I noticed that the source for the carving depends on unsafe code; I would like to look into this to see if it can be made 'safe' for web apps.
  • Hopefully profiling/tuning/cleaning the code can get carving images in the 600x600 range to less than a second.
  • Add seam insertion ability?
  • Wish I saw this earlier… and dang, I even missed this at Mix in Vegas.

 

Special Thanks to Mike Swanson

 

public void Carve(Direction direction, ComparisonMethod comparisonMethod, Size minimumSize)

{

if (modifiedWidth <= 0 || modifiedHeight <= 0)

{

return;

}

 

// Time this carve

HiPerfTimer timer = new HiPerfTimer();

timer.Start();

 

bmd = bitmap.LockBits(new Rectangle(0, 0, bitmap.Width, bitmap.Height),

ImageLockMode.ReadWrite, bitmap.PixelFormat);

 

// Modified By CCook

// Changed it so that it continues until both constraints are met,

// not either one. (while x and y are greater stops on first...)

// Checks are also done on whether the Direction is capable of continuing.

while ((modifiedWidth > 0 && modifiedHeight > 0) && ((

((modifiedWidth > minimumSize.Width ||

modifiedHeight > minimumSize.Height)) && direction == Direction.Optimal) ||

(((modifiedWidth > minimumSize.Width &&

modifiedHeight > minimumSize.Height)) && direction != Direction.Optimal)))

{

// Find the lowest energy seam

Seam lowestEnergySeam;

if (direction == Direction.Vertical || direction == Direction.Horizontal)

{

lowestEnergySeam = seamFunction.FindLowestEnergy(direction, comparisonMethod, Size);

}

else

{

// Modified By CCook

// If a constraint is matched the direction is known so pick the appropriate direction.

// If however, either direction is possible, choose the optimal one.

if (minimumSize.Width == modifiedWidth)

lowestEnergySeam = seamFunction.FindLowestEnergy(Direction.Horizontal, comparisonMethod, Size);

else if (minimumSize.Height == modifiedHeight)

lowestEnergySeam = seamFunction.FindLowestEnergy(Direction.Vertical, comparisonMethod, Size);

else

{

 

// Find lowest energy across both horizontal and vertical directions

Seam isLowest1 = seamFunction.FindLowestEnergy(Direction.Vertical, comparisonMethod, Size);

Seam isLowest2 = seamFunction.FindLowestEnergy(Direction.Horizontal, comparisonMethod, Size);

 

// Use compareValue, since it's been run through the seam comparison method

if (isLowest1.compareValue < isLowest2.compareValue)

{

lowestEnergySeam = isLowest1;

}

else

{

lowestEnergySeam = isLowest2;

}

}

}

 

// Resume unchanged code ...

 

 

Print | posted on Thursday, June 05, 2008 9:06 PM | Filed Under [ Web Programming ]

kick it on DotNetKicks.com

Feedback

Gravatar

# re: Seam Carving for Resizing using SEAMonster

As I 'distribute any portion of the software in source code form', here is the license:

Microsoft Public License (Ms-PL)

This license governs use of the accompanying software. If you use the software, you accept this license. If you do not accept the license, do not use the software.

1. Definitions

The terms "reproduce," "reproduction," "derivative works," and "distribution" have the same meaning here as under U.S. copyright law.

A "contribution" is the original software, or any additions or changes to the software.

A "contributor" is any person that distributes its contribution under this license.

"Licensed patents" are a contributor's patent claims that read directly on its contribution.

2. Grant of Rights

(A) Copyright Grant- Subject to the terms of this license, including the license conditions and limitations in section 3, each contributor grants you a non-exclusive, worldwide, royalty-free copyright license to reproduce its contribution, prepare derivative works of its contribution, and distribute its contribution or any derivative works that you create.

(B) Patent Grant- Subject to the terms of this license, including the license conditions and limitations in section 3, each contributor grants you a non-exclusive, worldwide, royalty-free license under its licensed patents to make, have made, use, sell, offer for sale, import, and/or otherwise dispose of its contribution in the software or derivative works of the contribution in the software.

3. Conditions and Limitations

(A) No Trademark License- This license does not grant you rights to use any contributors' name, logo, or trademarks.

(B) If you bring a patent claim against any contributor over patents that you claim are infringed by the software, your patent license from such contributor to the software ends automatically.

(C) If you distribute any portion of the software, you must retain all copyright, patent, trademark, and attribution notices that are present in the software.

(D) If you distribute any portion of the software in source code form, you may do so only under this license by including a complete copy of this license with your distribution. If you distribute any portion of the software in compiled or object code form, you may only do so under a license that complies with this license.

(E) The software is licensed "as-is." You bear the risk of using it. The contributors give no express warranties, guarantees or conditions. You may have additional consumer rights under your local laws which this license cannot change. To the extent permitted under your local laws, the contributors exclude the implied warranties of merchantability, fitness for a particular purpose and non-infringement.
6/5/2008 9:11 PM | License
Gravatar

# Seam Carving for Resizing using SEAMonster

You've been kicked (a good thing) - Trackback from DotNetKicks.com
7/12/2008 8:11 PM | DotNetKicks.com

Post Comment

Title  
Name  
Email
Url
Comment   
Please add 8 and 4 and type the answer here:

Powered by: