From 4de6090ab9e0442f6f51c45e52ccab875eeb0ce9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fran=C3=A7ois=20Colin=20de=20Verdi=C3=A8re?= Date: Tue, 1 Apr 2025 17:01:53 +0200 Subject: [PATCH] does not recompute energy for horizontal seams too --- src/seam-carving.cpp | 59 +++++++++++++++++++++++--------------------- 1 file changed, 31 insertions(+), 28 deletions(-) diff --git a/src/seam-carving.cpp b/src/seam-carving.cpp index 1c90c7a..04fa366 100644 --- a/src/seam-carving.cpp +++ b/src/seam-carving.cpp @@ -181,7 +181,8 @@ std::vector carving_step(const std::vector source_img, std::vector source_energy, std::vector &output_img, std::vector &output_energy, int width, - int height, int nbChannels, bool vertical) { + 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 = @@ -199,20 +200,29 @@ std::vector carving_step(const std::vector source_img, vertical ? height : height - 1, nbChannels); std::copy(energy.begin(), energy.end(), output_energy.begin()); } else { - int nbColorChannels = 3; - std::cout << "starting to recompute the energy along the seam" << std::endl; // ASSUME WE ARE DOING A VERTICAL SEAM - 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)) { - compute_energy_for_pixel(output_img, (width - 1), height, - (opt_seam[j] + i), j, nbChannels, - nbColorChannels, output_energy); + 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); + } } } } - std::cout << "DONE" << std::endl; } return opt_seam; } @@ -257,13 +267,13 @@ void seam_carving(unsigned char *source, int width, int height, int nbChannels, //* Prepare final output - float max_energy=__FLT_MAX__; - for (auto k=0;k opt_seam = - carving_step(source_img, source_energy, output_img, output_energy, - curWidth, curHeight, nbChannels, vertical); + std::vector 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 (vertical) // We just reduced the dimension - curWidth--; - else - curHeight--; - - if (test_energy) { // Update blacklist + vertical ? curWidth-- : curHeight--; // We just reduced the dimension + if (test_energy) { // Update blacklist for (auto i = 0; i < dim_long; i++) { int j, cur_j = 0; // cur_j is the index relative to the current carved @@ -305,17 +311,14 @@ void seam_carving(unsigned char *source, int width, int height, int nbChannels, } assert(cur_j == opt_seam[i]); // Else, j == width and cur_j is not in // the source image.. - complete_blacklist[im_index(i, j)] = true; test_energy_output[nbChannels * im_index(i, j)] = 255; // Set carved pixel to red } } - bar.increment(); bar.print(); } - // std::cout << std::endl; if (test_energy) { export_image(out_filename, test_energy_output.data(), width, height,