diff --git a/src/seam-carving.cpp b/src/seam-carving.cpp index 2a6a250..a8d7586 100644 --- a/src/seam-carving.cpp +++ b/src/seam-carving.cpp @@ -27,6 +27,13 @@ bool nearly_equal(float a, float b) { std::nextafter(a, std::numeric_limits::max()) >= b; } +std::pair operator+(std::pair& p1, std::pair& p2) { + return { + (p1.first==1) || (p2.first==1) ? 1 : std::max(p1.first, p2.first), + p1.second+p2.second + }; +} + void export_image(const char *filename, const void *data, int width, int height, int nbChannels) { if (!silent) @@ -40,7 +47,7 @@ void export_image(const char *filename, const void *data, int width, int height, } #define compute_energy_for_pixel(source, width, height, i, j, nbChannels, \ - nbColorChannels, energy) \ + nbColorChannels, dest) \ auto indexPixel = (nbChannels) * (width * (j) + (i)); \ auto indexPixel_up = \ ((j) - 1 > 0) ? (nbChannels) * (width * ((j) - 1) + (i)) : indexPixel; \ @@ -52,9 +59,9 @@ void export_image(const char *filename, const void *data, int width, int height, auto indexPixel_right = ((i) + 1 < width) \ ? (nbChannels) * (width * (j) + ((i) + 1)) \ : indexPixel; \ - energy[width * j + i] = 0; \ + dest = 0; \ for (auto ch = 0; ch < (nbColorChannels); ch++) { \ - energy[(width) * (j) + (i)] += \ + 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]) + \ @@ -74,19 +81,21 @@ std::vector energy_e1(std::vector source, int width, float max_energy = 0; for (auto i = 0; i < width; i++) { for (auto j = 0; j < height; j++) { - compute_energy_for_pixel(source, width, height, i, j, nbChannels, - nbColorChannels, energy); + compute_energy_for_pixel( + source, width, height, + i, j, nbChannels, nbColorChannels, energy[width*j+i] + ); } } return energy; } -std::vector optimal_seam(std::vector energy, int width, int height, +/** Given the energy value, returns the optimal seam */ +template std::vector optimal_seam(std::vector energy, int width, int height, bool vertical) { - /** Given the energy value, returns the optimal seam */ // dyn_energy is indexed by [dim_large*(i : dim_long) + (j : dim_large)] - std::vector dyn_energy(width * height); + std::vector dyn_energy(width * height); int dim_large = vertical ? width : height; int dim_long = vertical ? height : width; // Number of elements in the seam @@ -98,23 +107,24 @@ std::vector optimal_seam(std::vector energy, int width, int height, for (auto i = 1; i < dim_long; i++) { // Propagate dyn_energy for (auto j = 0; j < dim_large; j++) { - dyn_energy[dim_large * i + j] = __FLT_MAX__; + dyn_energy[dim_large * i + j] = std::numeric_limits::max(); int lower_bound = std::max(j - max_step, 0); int upper_bound = std::min(j + max_step, dim_large - 1); for (auto k = lower_bound; k <= upper_bound; k++) { // Compute energy based on predecessors dyn_energy[dim_large * i + j] = std::min( - dyn_energy[dim_large * i + j], dyn_energy[dim_large * (i - 1) + k]); + dyn_energy[dim_large * i + j], dyn_energy[dim_large * (i - 1) + k] + ); } - dyn_energy[dim_large * i + j] += energy[im_index(i, j)]; + dyn_energy[dim_large * i + j] = dyn_energy[dim_large * i + j]+energy[im_index(i, j)]; } } std::vector result(dim_long); // Find the seam end int min_idx = -1; - float min_val = __FLT_MAX__; + T min_val = std::numeric_limits::max(); for (auto j = 0; j < dim_large; j++) { if (min_val > dyn_energy[dim_large * (dim_long - 1) + j]) { min_idx = j; @@ -166,8 +176,8 @@ std::vector optimal_seam(std::vector energy, int width, int height, return result; } -/** Carves an image by one seam. Returns the optimal seam used */ +/** Carves an image by one seam. Returns the optimal seam used */ template void remove_seam(const std::vector source, std::vector &output, int width, int height, int nbChannels, bool vertical, @@ -191,14 +201,68 @@ void remove_seam(const std::vector source, std::vector &output, int width, } } +// It would be preferable to use templates only for the value assignation but this is in fact far less efficient +void recompute_energy_along_seam( + std::vector carved_img, 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*j+i] + ); + } + } + } +} + +void recompute_energy_along_seam( + std::vector carved_img, 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*j+i].first + ); + } + } + } +} + + +/** Carves an image and its energy by one seam, and recomputes the energy. + Returns the optimal seam used */ +//template std::vector carving_step(const std::vector source_img, - std::vector source_energy, + std::vector> source_energy, std::vector &output_img, - std::vector &output_energy, int width, + std::vector> &output_energy, int width, int height, int nbChannels, int nbColorChannels, bool vertical) { - /** Carves an image and its energy by one seam, and recomputes the energy. - Returns the optimal seam used */ + std::vector opt_seam = optimal_seam(source_energy, width, height, vertical); remove_seam(source_img, output_img, width, height, nbChannels, vertical, @@ -207,40 +271,27 @@ std::vector carving_step(const std::vector source_img, remove_seam(source_energy, output_energy, width, height, 1, vertical, opt_seam); - // Recompute the energy along the seam - if (energy_recompute_all) { - std::vector energy = - energy_e1(output_img, vertical ? width - 1 : width, - vertical ? height : height - 1, nbChannels); - std::copy(energy.begin(), energy.end(), output_energy.begin()); - } else { + // Recompute the energy along the seam, we need a separate function for templating + recompute_energy_along_seam( + source_img, output_energy, opt_seam, + width, height, nbChannels, nbColorChannels, + vertical + ); - // ASSUME WE ARE DOING A VERTICAL SEAM - if (vertical) { - for (auto j = 0; j < height; j++) { - for (auto i = -1; i < 2; i++) { - if ((0 < (opt_seam[j] + i)) && ((opt_seam[j] + i) < width - 1)) { - compute_energy_for_pixel(output_img, (width - 1), height, - (opt_seam[j] + i), j, nbChannels, - nbColorChannels, output_energy); - } - } - } - } else { - for (auto i = 0; i < width; i++) { - for (auto j = -1; j < 2; j++) { - if ((0 < (opt_seam[i] + j)) && ((opt_seam[i] + j) < height - 1)) { - compute_energy_for_pixel(output_img, width, height - 1, i, - (opt_seam[i] + j), nbChannels, - nbColorChannels, output_energy); - } - } - } - } - } return opt_seam; } +std::vector> mask_energy(std::vector energy, + int width, int height, unsigned char* mask) { + std::vector> output(width*height); + + for (auto i=0; i < width*height; i++) { + output[i] = {mask[i], energy[i]}; + } + + return output; +} + void seam_carving(unsigned char *source, int width, int height, int nbChannels, const char *out_filename, int nbSeams, bool vertical, unsigned char* mask=nullptr) { int nbColorChannels = nbChannels > 3 ? 3 : nbChannels; @@ -255,6 +306,9 @@ void seam_carving(unsigned char *source, int width, int height, int nbChannels, // Contains at each step the carved image std::vector source_energy(width * height); // Contains at each step the carved energy + std::vector> masked_energy; + std::vector> output_masked_energy(width*height); + // Source energy with (-1, 0, 1) on first element according to mask value std::vector output_img(width * height * nbChannels); // Receives at each step the newly carved image std::vector output_energy(width * height); @@ -271,6 +325,8 @@ void seam_carving(unsigned char *source, int width, int height, int nbChannels, } source_energy = energy_e1(source_img, width, height, nbChannels); + if (mask) + masked_energy = mask_energy(source_energy, width, height, mask); if (test_energy) { ini_energy = energy_e1(source_img, width, height, nbChannels); @@ -303,14 +359,27 @@ void seam_carving(unsigned char *source, int width, int height, int nbChannels, } SimpleProgressBar::ProgressBar bar(nbSeams); + bar.print(); for (auto seam_index = 0; seam_index < nbSeams; seam_index++) { - std::vector opt_seam = carving_step( - source_img, source_energy, output_img, output_energy, curWidth, - curHeight, nbChannels, nbColorChannels, vertical); + std::vector opt_seam; + if (mask) { + opt_seam = carving_step( + source_img, masked_energy, output_img, output_masked_energy, + curWidth, curHeight, nbChannels, nbColorChannels, vertical + ); + //} else { + // opt_seam = carving_step( + // source_img, source_energy, output_img, output_energy, + // curWidth, curHeight, nbChannels, nbColorChannels, vertical + // ); + } std::copy(output_img.begin(), output_img.end(), source_img.begin()); std::copy(output_energy.begin(), output_energy.end(), source_energy.begin()); + if (mask) + std::copy(output_masked_energy.begin(), output_masked_energy.end(), + masked_energy.begin()); vertical ? curWidth-- : curHeight--; // We just reduced the dimension @@ -335,6 +404,7 @@ void seam_carving(unsigned char *source, int width, int height, int nbChannels, bar.increment(); bar.print(); } + std::cout << std::endl; // Add newline after ProgressBar if (test_energy) { export_image(out_filename, test_energy_output.data(), width, height, @@ -352,7 +422,7 @@ int main(int argc, char **argv) { std::string outputImage = "output.png"; int nbSeams = 1; bool vertical = false; - + app.add_option("-s,--source", sourceImage, "Source image") ->required() ->check(CLI::ExistingFile); @@ -384,7 +454,7 @@ int main(int argc, char **argv) { int maskWidth, maskHeight, maskChannels; mask = stbi_load(maskImage.c_str(), &maskWidth, &maskHeight, &maskChannels, 0); - + if (maskWidth != width || maskHeight != height) { std::cerr << maskImage << " and " << sourceImage << " differ in dimension. Please provide a valid mask." @@ -400,11 +470,15 @@ int main(int argc, char **argv) { r = mask[maskChannels*i]; g = mask[maskChannels*i]; b = mask[maskChannels*i]; - mask[2*i] = (r == 0) && (g == 255) && (b == 0); - mask[2*i+1] = (r == 255) && (g == 0) && (b == 0); + bool positive = (g > r && g > b); + bool negative = (r > g && r > b); + mask[i] = positive ? 1 : (negative ? -1 : 0); } - //* From now on, mask has the same dimensions as source and exactly 2 channels - //* The first channel is positive, the second one negative. + //* From now on, mask has the same dimensions as source and one single channel + //* The values are: + //* . ( 1) we want to keep the pixel + //* . (-1) we want to remove the pixel + //* . ( 0) nothing in particular } nbSeams = std::min(nbSeams, vertical ? width-1 : height-1); // We want to keep at least one row or column