Seam carving is an image processing technique for content aware image resizing.
In this stage, a rectangle image of provided width and height is created, with a red rectangle drawn diagonally on both sides.
Stage implementation: RectangleBuilder.kt
Example
Enter rectangle width:
> 20
Enter rectangle height:
> 10
Enter output image name:
> out.png
out.png looks like this:
In this stage, our Kotlin program converts an image into a negative one, by inverting its rgb values.
Inverted color for (r, g, b)
is (255 - r, 255 - g, 255 - b)
Stage implementation: ColorInverter.kt
Args: -in inputPath\imageName.png -out outputPath\outputImageName.png
Example:
args:
-in sky.png -out sky_negative.png
For the following sky.png:
Outputs the following sky-negative.png:
In this stage, the energy for each pixel is calculated using the dual-gradient energy function.
Then, the energies are normalised using the following formula:
intensity = (255.0 * energy / maxEnergyValue).toInt()
And each pixel's rgb values are set to the intensity
value, so the energy is represented as a grey-scale image.
Stage implementation: EnergyCalculator.kt
Example:
args: -in sky.png -out sky-energy.png
For the following sky.png:
Outputs the following sky-energy.png:
Vertical seam is a sequence of adjacent pixels crossing the image top to bottom. The seam can have only one pixel in each row of the image.
For example, subsequent pixels for pixel (x,y)
are (x - 1, y + 1)
, (x. y + 1)
, and (x + 1, y + 1)
.
The best seam to remove is the seam with the lowest sum of pixel energies from all possible seams. The problem of finding the best seam is very similar to finding the shortest path in a graph.
In this stage,
-
The energies for each pixel are calculated, based on the implementation of stage 3.
-
Cumulative energies(lowest sum of possible energies) is calculated as well, which, for each pixel in a row is the energy of the current pixel plus the energy of one of the three possible pixels above it.
-
The vertical seam with the minimal seam energy is found.
-
The found seam is colorized with the color red.
Stage implementation: SeamHighlighter.kt
Example:
args: -in sky.png -out sky-seam.png
For the following sky.png:
Outputs the following sky-seam.png:
In this stage, the horizontal seam for the given input image is found and is highlighted in red.
There are many ways to find a horizontal seam for an image, such as using the transposed view of an image for the vertical seam.
This implementation uses a different approach, inheriting and overriding the previous stage's implementation.
Stage implementation: HorizontalSeamHighlighter.kt
Example:
args: -in sky.png -out sky-horizontal-seam.png
For the following sky.png:
Outputs the following sky-horizontal-seam.png:
In this stage:
- Two more command line parameters are added,
-width
for the number of vertical seams to remove and,-height
for the number of horizontal seams. - Find a vertical seam and remove all the pixels that this seam contains. Then updates the energy values, and find another vertical seam on the resulted image and delete all the pixels that the second seam contains, repeats the process until the specified number of vertical seams are removed.
- Removes the horizontal seams the in same way, from the resulted image.
- Saves the output file to the specified path
Stage implementation: ImageResizer.kt
Example 1:
args: -in sky.png -out sky-reduced.png -width 125 -height 50
For the following sky.png:
Outputs the following sky-reduced.png:
Example 2:
args: -in trees.png -out trees-reduced.png -width 100 -height 30
Outputs the following trees-reduced.png: