Hello

I’m a bit confused about the `calculate_energy`

function.

As I understood it so far, this subroutine is used to find the seam with minimal energy of the whole picture provided as argument.

The documentation of `calculate_energy`

states:

Calculate the total energy at every pixel of the image @p `img`

, but only considering columns with index less than @p `w`

,

Furthermore it says:

To this end, first calculate the local energy and use it to calculate the total energy.

So, given an image matrix with the color values and a width `w`

. We first need to do the local energy calculations for every pixel within the first `w-1`

columns of the matrix and then store these values where…? We are given a pointer `uint32_t const energy`

as argument. Are we supposed to write the local energy values to this (like a 2D matrix) for being able to calculate total energy later? Somehing like:

**SPOILER BELOW!**

```
int idx = yx_index(j, i, w); // where j = current row, i = current column
// just an example: every local energy is = 0 (not spoiling calculations here)
*(energy+idx) = 0; // <--- using the pointer as base index pointer of an array, representing a new matrix
```

Did I get that wrong or am I on the right track?

Thanks in advance,

confused David

1 Like

Essentially you can write your functions for finding the path and carving (both in main.c) such that you only have to allocate memory for the energy pointer once and then pass it to calculate energy (potentially multiple times…) in order to get total energy. This means that when you are passed the pointer energy in calculate_energy there is nothing interesting in it and you can just overwrite it with the local energies of the pixels. After that you go through it row by row (not column by column) to calculate the total energy.

On your code snippet: you can actually access the contents of energy by writing energy[i] which is easier on the eyes. From my limited understanding this is a way of accessing the contents of arrays as well as contents of pointers, but it does not mean that a pointer is an array or vice versa. What you wrote doesn’t make sense to me (the last comment I mean). You kinda conflate array stuff and pointer stuff. This helped me understand the difference a bit better (the accepted answer):

Hope that helps!

1 Like

Hi,

In fact, it does not matter if one uses `energy[idx]`

or `*(energy+idx)`

, the first option is just syntactic sugar for the second one. It might look a bit nicer, but both do work.

Yes, you got this argument to store the local energy there. Since `energy`

is a pointer to an 1-D array, it would be good to somehow match the matrix indices to one single array index. Therefore one could e.g. use the function `yx_index()`

.

hope this helped.

Lisa

3 Likes