Content-aware image resizing with seam carving

The following code demonstrates how the scikit-image library's transform module's seam_curve() function can be used for content-aware image resizing. Let's first import the required packages, load the original input airplane image, and display the image using the following code block:

# for jupyter notebook uncomment the next line of code
# % matplotlib inline
from skimage import data, draw
from skimage import transform, util
import numpy as np
from skimage import filters, color
from matplotlib import pyplot as pylab
image = imread('../images/aero.jpg')
print(image.shape)
# (821, 616, 3)
image = util.img_as_float(image)
energy_image = filters.sobel(color.rgb2gray(image))
pylab.figure(figsize=(20,16)), pylab.title('Original Image'), pylab.imshow(image), pylab.show()

The following shows the output of the preceding code block:

Let's make this image smaller with the resize() function, shrinking the width of the image using the usual downsampling, as in the following code snippet:

resized = transform.resize(image, (image.shape[0], image.shape[1] - 200), mode='reflect')
print(resized.shape)
# (821, 416, 3)
pylab.figure(figsize=(20,11)), pylab.title('Resized Image'), pylab.imshow(resized), pylab.show()

The following shows the output of the preceding code block. As can be seen in the following output, there is a considerable reduction of size of the airplanes, but they have been distorted too, as simply resizing to a new aspect ratio distorts image contents:

Now let's resize the image with the seam_carve() function. Here, a the sobel filter is used as the energy function, to signify the importance of each pixel:

image = util.img_as_float(image)
energy_image = filters.sobel(color.rgb2gray(image))
out = transform.seam_carve(image, energy_image, 'vertical', 200)
pylab.figure(figsize=(20,11)), pylab.title('Resized using Seam Carving'), pylab.imshow(out)

The following shows the output of the preceding code block. As can be seen, seam carving attempts to resize without distortion by removing regions of an image that it deems less important (that is, with low energy). As a result, the airplanes do not have any visible distortion:

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.22.242.141