Optimizing Seam Carving

After my post about the various content-aware image resizing projects I couldn’t resist to take a closer look at Joa Ebert’s ImageResizing class and take him on his word “If you can improve something do not be shy and post it”. I just love code optimizing and saw several points to attack.

First I replaced the loop that moved the pixels via getPixel()/setPixel() by a displacementMap filter. The additional benefit of this is that I can use the same displacement map on the grayscale map which is used for the energy calculation so it just needs to get calculated once. It is even possible to apply it to the energy map, too but that map should only be used for a few carves and then get recalculated. Otherwise artifacts will show up quickly.

Another part I changed is the energy function itself. I’m not sure if that improves the speed, but I think it improves the seam selection quality. Instead of a convolution filter I blur the image first and then draw() it onto itself offset by a few pixels with difference mode. The idea behind this is that this will keep strong edges but does not emphasize noise and small patterns as much.

Here is the optimized ImageResizing AS source. And here’s a demo of the optimized version: