There are several ways to make an image smaller. The most commonly-used by far is to simply reduce the dimensions while maintaining the aspect ratio. Sometimes though, there are situations where it's only necessary to adjust the width of an image. However, this typically limits resizing options to either reducing the dimension uniformly which produces a "squashed" effect, or by cropping the image, which may cause relevant information at the edge of the image to be removed.
However, LEADTOOLS has implementations for the seam-carving algorithm in the form of the IntelligentDownScaleCommand class. This algorithm works by removing pixels in the image which are deemed to have "low energy" and subsequently make less of a contribution to the image. This is called content-aware resizing.
Here's our documentation page for the IntelligentDownScaleCommand class, which implements this behavior.
https://www.leadtools.com/help/leadtools/v20/dh/ps/intelligentdownscalecommand.htmlThere's a code sample and some additional notes. Here's the code sample.
Code:
RasterCodecs codecs = new RasterCodecs();
codecs.ThrowExceptionsOnInvalidImages = true;
RasterImage image = codecs.Load(Path.Combine(LEAD_VARS.ImagesDir, "Sample3.cmp"));
IntelligentDownScaleCommand command = new IntelligentDownScaleCommand();
command.DownScalingOrder = 1;
command.NewWidth = image.Width - 100;
command.NewHeight = image.Height;
command.UsePreserveObjectColor = false;
command.UseRemoveObjectColor = false;
// Apply the intelligent downscale on the image.
command.Run(image);
codecs.Save(image, Path.Combine(LEAD_VARS.ImagesDir, "IntelligentDownScaleResult.jpg"), RasterImageFormat.Jpeg, 24);
Essentially, this reduces the size of the input image by eliminating pixels deemed unnecessary while maintaining an illusion of the original aspect ratio. Here's an input image:
Here's the side-by-side of two outputs, the former made smaller using the IntelligentDownScaleCommand, and the latter using the typical SizeCommand. Note how the former maintains the overall width of the island and the length of the tree branch, albeit at the expense of the water at the right side of the island, which the algorithm deemed unnecessary. By comparison, the resized image is squashed with the trees being clumped too close together on the island.
There's more information on the documentation page, but the algorithm favors darker pixels to be removed. However, it's possible to designate specific regions of the image that the algorithm should weigh more heavily toward being removed. For this, a mask can be used. Here's the code snippet.
Code:
RasterImage mask = codecs.Load(maskPath);
IntelligentDownScaleCommand command = new IntelligentDownScaleCommand();
command.DownScalingOrder = 1;
command.MaskImage = mask;
command.UsePreserveObjectColor = false;
command.UseRemoveObjectColor = true;
command.RemoveObjectColor = RasterColor.Black;
Note the distinct difference here is that the MaskImage property has a parameter assigned to it, UseRemoveObjectColor is set to true, the RemoveObjectColor has a value assigned to it (RasterColor.Black) and the NewWidth and NewHeight properties have been removed. In this case, the mask has the tree on the left has been partially blacked out, so the algorithm will favor removing it over other portions of the image. Here's both the mask and the output image.
The differences between the standard rescale and the one with the mask applied can be seen here as well. The IntelligentDownScaleCommand has other features associated with it that can be useful as well which is touched more on in our documentation. Do note this command is useful when wanting to resize an image while maintaining an aspect ratio, but with all size change operations, this does modify and remove some of the underlying pixel data, and should be used with care.
Edited by user Monday, August 6, 2018 12:11:08 PM(UTC)
| Reason: Updating links to v20
Nick Crook
Developer Support Engineer
LEAD Technologies, Inc.