From the course: Fundamentals of Dynamic Programming

Unlock this course with a free trial

Join today to access over 22,700 courses taught by industry experts.

Solution: Calculating the energy of an image

Solution: Calculating the energy of an image - Python Tutorial

From the course: Fundamentals of Dynamic Programming

Solution: Calculating the energy of an image

- [Instructor] Let's take a look at how the energy calculation is implemented in Python. We're focusing on the file energy.py. First up is the energy_at function. Since the image is represented as a list of lists in Python, we can get the height of the image using the length of the outer list and the width using the length of any of the inner lists. Now, we have to figure out what are the x-coordinates for the pixels to the left and right of the requested pixel. Notice that if the current pixel has an x-coordinate of zero, we can't go to the left and we simply use the current pixel. The same applies if the x-coordinate of the current pixel is all the way to the right of the image. With the x-coordinates of the two surrounding pixels we can take the difference of the red, green, and blue components of these two pixels. The order in which we do the subtraction doesn't matter because we will be squaring the differences anyway.…

Contents