From the course: Fundamentals of Dynamic Programming

What is content-aware image resizing? - Python Tutorial

From the course: Fundamentals of Dynamic Programming

What is content-aware image resizing?

- [Narrator] Content-aware image resizing, is when you change the size of an image, taking into account what's in that image. That means different images are resized differently. Seam carving is one implementation of content aware resizing, in which an uninteresting sequence of pixels is removed from the image. Let's motivate content-area resizing with an image. Take this photo of a surfer. Say we want to squash this image horizontally, while keeping its height fixed. How would we do that? We could just chop off rectangles from the left and right, this is known as cropping. The problem is we have to sacrifice content at the edges of the image, either the waves behind the surfer, or the turbulent water in front of the surfer, or both. Really, the parts we don't care about are in the middle of image. So maybe we can cut out a rectangle from the middle. Unfortunately, that leaves a visible artifact right down the middle of the image. Going back to the original photo, we can classify some parts of the image as interesting, and others as uninteresting. For example, the waves on the right, and the surfer are both interesting. The turbulent water in front of the surfer is also interesting, but less so. Now that we know which part of the photo we can sacrifice, we want to find a string of pixels in the uninteresting region, that goes down the height of the image. The important part is that the pixels are connected. But not necessarily all in a straight line. Disconnected string of pixels, is known as a seam. In our photo of the surfer, such a seam might go down the image right next to the waves. For visibility, I've shown the seam as wider than one pixel. But in reality, it's just one pixel wide. Since this string of pixels is in an uninteresting region, we can simply remove the pixels in the seam, the image will be one pixel smaller in width, but it will look like we did anything. Finally, we can repeat this process of finding uninteresting seams and removing them. Here, I've reduced the width by 1024 pixels, and the image still looks fairly natural, there's still a bit of visual artifacting near the wave. But the result is way better than the other methods we've seen so far. In this chapter, we'll use dynamic programming to implement seam carving on real images. Unlike all the small problems we saw before, real world dynamic programming applications require a few extra steps. First, some pre-processing. This is needed to get the image into a form where we can apply dynamic programming. For us that means reading the image and finding the uninteresting parts. Next, we applied the actual dynamic programming algorithm. In this problem, that means finding the least interesting seam. Finally, we need to do some post processing, to take the results of the dynamic programming algorithm and use them. In our case, that means removing the relevant pixels from the image and maybe saving that image to a file.

Contents