Seam Carving. Stage 3/6

Look at energy

Report a typo

Description

Now you are able to manipulate pictures and ready to start.

The first step is to calculate the energy for each pixel of the image. Energy is the pixel's importance. The higher the pixel energy, the less likely this pixel is to be removed from the picture while reducing.

There are several different energy functions invented for seam carving. In this project, we will use dual-gradient energy function.

The energy of the pixel (x,y)(x, y) is E(x,y)=Δx2(x,y)+Δy2(x,y)E(x, y) = \sqrt{\Delta_x^2(x, y)+\Delta_y^2(x, y)}

Where the square of x-gradient Δx2(x,y)=Rx(x,y)2+Gx(x,y)2+Bx(x,y)2\Delta_x^2(x, y) = R_x(x, y)^2+G_x(x, y)^2+B_x(x, y)^2

y-gradient Δy2(x,y)=Ry(x,y)2+Gy(x,y)2+By(x,y)2\Delta_y^2(x, y) = R_y(x, y)^2+G_y(x, y)^2+B_y(x, y)^2

Don't be confused! You will calculate values Δx2(x,y)\Delta_x^2(x, y) and Δy2(x,y)\Delta_y^2(x, y) that are already squared, so you don't need to square them again to calculate E(x,y)E(x, y).

Where Rx(x,y)R_x(x, y), Gx(x,y)G_x(x, y), Bx(x,y)B_x(x, y) are differences of red, green and blue components between pixels (x+1,y)(x+1, y) and (x1,y)(x -1 , y).

Let's consider a grey pixel (2,1)(2, 1) on the example image:

dual gradient energy calculation

X-differences are:
Rx(2,1)=255150=105,  Gx(2,1)=250150=100,  Bx(2,1)=155100=55R_x(2, 1) = 255-150=105,\ \ G_x(2, 1) = 250-150 = 100, \ \ B_x(2, 1) = 155 - 100 = 55
So, x-gradient is Δx2(2,1)=1052+1002+552=24050\Delta_x^2(2, 1) = 105^2+100^2+55^2=24050

Y-differences are:
Ry(2,1)=5010=40,  Gy(2,1)=255250=5,  By(2,1)=25540=215R_y(2, 1) = 50 - 10 = 40, \ \ G_y(2, 1)=255-250 = 5, \ \ B_y(2, 1) = 255 - 40 = 215
Same for y-gradient Δy2=402+52+2152=47850\Delta_y^2 = 40^2 + 5^2+215^2 = 47850

Finally, E(2,1)=24050+47850=268.14E(2, 1) = \sqrt{24050 + 47850} = 268.14

Energy for border pixels is calculated with a 1-pixel shift from the border. For example:
E(0,2)=Δx2(1,2)+Δy2(0,2)E(0, 2)=\sqrt{\Delta_x^2(1, 2) + \Delta_y^2(0, 2)}
E(1,0)=Δx2(1,0)+Δy2(1,1)E(1, 0)=\sqrt{\Delta_x^2(1, 0) + \Delta_y^2(1, 1)}
E(0,0)=Δx2(1,0)+Δy2(0,1)E(0, 0)=\sqrt{\Delta_x^2(1, 0) + \Delta_y^2(0, 1)}

Objective

Calculate energies for all pixels of the image. Normalize calculated energies using the following formula:
intensity = (255.0 * energy / maxEnergyValue).toInt()

To display energy as a grey-scale image, set color components of the output image pixels to calculated intensity. For example, (red = intensity, green = intensity, blue = intensity).

You should use double precision in this and in the following stages.

Example

The greater-than symbol followed by a space (> ) represents the user input. Note that it's not part of the input.

> java Main -in sky.png -out sky-energy.png

For the following sky.png:

river side building construction

sky-energy.png should look like this:

river side building energy map

Write a program
IDE integration
Checking the IDE status
___

Create a free account to access the full topic