diff --git a/src/seam-carving.cpp b/src/seam-carving.cpp index 4fe1348..a8605fa 100644 --- a/src/seam-carving.cpp +++ b/src/seam-carving.cpp @@ -53,7 +53,20 @@ std::string function = "grad"; ? (nbChannels) * (width * (j) + ((i) + 1)) \ : indexPixel; \ dest = 0; \ - if (function == "gradnorm") { \ + if (function == "grad") { \ + for (auto ch = 0; ch < (nbColorChannels); ch++) { \ + dest += ((fabs((float)source[indexPixel_up + ch] - \ + source[indexPixel + ch])) + \ + \ + (fabs((float)source[indexPixel_down + ch] - \ + source[indexPixel + ch])) + \ + \ + (fabs((float)source[indexPixel_left + ch] - \ + source[indexPixel + ch])) + \ + (fabs((float)source[indexPixel_right + ch] - \ + source[indexPixel + ch]))); \ + } \ + } else if (function == "gradnorm") { \ for (auto ch = 0; ch < (nbColorChannels); ch++) { \ dest += (std::pow(fabs((float)source[indexPixel_up + ch] - \ source[indexPixel + ch]), \ @@ -70,17 +83,20 @@ std::string function = "grad"; source[indexPixel + ch]), \ 2)); \ } \ - } else if (function == "grad") { \ + } else if (function == "gradhoriz") { \ + /* take the gradient along the horizontal only*/ \ + for (auto ch = 0; ch < (nbColorChannels); ch++) { \ + dest += ((fabs((float)source[indexPixel_left + ch] - \ + source[indexPixel + ch])) + \ + (fabs((float)source[indexPixel_right + ch] - \ + source[indexPixel + ch]))); \ + } \ + } else if (function == "gradvertic") { \ + /* take the gradient along the vertical only*/ \ for (auto ch = 0; ch < (nbColorChannels); ch++) { \ dest += ((fabs((float)source[indexPixel_up + ch] - \ source[indexPixel + ch])) + \ - \ (fabs((float)source[indexPixel_down + ch] - \ - source[indexPixel + ch])) + \ - \ - (fabs((float)source[indexPixel_left + ch] - \ - source[indexPixel + ch])) + \ - (fabs((float)source[indexPixel_right + ch] - \ source[indexPixel + ch]))); \ } \ } else if (function == "gradnorminf") { \ @@ -249,14 +265,19 @@ void recompute_energy_along_seam(std::vector carved_img, int newWidth = vertical ? width - 1 : width; int newHeight = vertical ? height : height - 1; - for (auto j = 0; j < dim_long; j++) { - for (auto i = -1; i < 2; i++) { - int x = vertical ? (opt_seam[j] + i) : j; - int y = vertical ? j : (opt_seam[j] + i); - if ((0 < (opt_seam[j] + i)) && ((opt_seam[j] + i) < dim_large - 1)) { - compute_energy_for_pixel(carved_img, newWidth, newHeight, x, y, - nbChannels, nbColorChannels, - output_energy[width * y + x]); + for (auto j0 = 0; j0 < dim_long; j0++) { + auto i0 = opt_seam[j0]; + for (auto i_offset = -max_step - 1; i_offset <= max_step + 1; i_offset++) { + for (auto j_offset = -max_step - 1; j_offset <= max_step + 1; + j_offset++) { + int x = vertical ? (i0 + i_offset) : j0 + j_offset; + int y = vertical ? j0 + j_offset : (i0 + i_offset); + if (((0 < (i0 + i_offset)) && ((i0 + i_offset) < dim_large - 1)) && + (((0 < j0 + j_offset) && (j0 + j_offset < dim_long)))) { + compute_energy_for_pixel(carved_img, newWidth, newHeight, x, y, + nbChannels, nbColorChannels, + output_energy[width * y + x]); + } } } } @@ -267,20 +288,26 @@ void recompute_energy_along_seam( std::vector> &output_energy, std::vector opt_seam, int width, int height, int nbChannels, int nbColorChannels, bool vertical) { + int dim_large = vertical ? width : height; int dim_long = vertical ? height : width; int newWidth = vertical ? width - 1 : width; int newHeight = vertical ? height : height - 1; - for (auto j = 0; j < dim_long; j++) { - for (auto i = -1; i < 2; i++) { - int x = vertical ? (opt_seam[j] + i) : j; - int y = vertical ? j : (opt_seam[j] + i); - if ((0 < (opt_seam[j] + i)) && ((opt_seam[j] + i) < dim_large - 1)) { - compute_energy_for_pixel(carved_img, newWidth, newHeight, x, y, - nbChannels, nbColorChannels, - output_energy[width * y + x].first); + for (auto j0 = 0; j0 < dim_long; j0++) { + auto i0 = opt_seam[j0]; + for (auto i_offset = -max_step - 1; i_offset <= max_step + 1; i_offset++) { + for (auto j_offset = -max_step - 1; j_offset <= max_step + 1; + j_offset++) { + int x = vertical ? (i0 + i_offset) : j0 + j_offset; + int y = vertical ? j0 + j_offset : (i0 + i_offset); + if (((0 < (i0 + i_offset)) && ((i0 + i_offset) < dim_large - 1)) && + (((0 < j0 + j_offset) && (j0 + j_offset < dim_long)))) { + compute_energy_for_pixel(carved_img, newWidth, newHeight, x, y, + nbChannels, nbColorChannels, + output_energy[width * y + x].first); + } } } } @@ -538,7 +565,7 @@ int main(int argc, char **argv) { mask[i] = positive ? 2 : (negative ? 0 : 1); } //* From now on, mask has the same dimensions as source and one single - //channel + // channel //* The values are: //* . (2) we want to keep the pixel //* . (1) nothing in particular