does not recompute energy for horizontal seams too

This commit is contained in:
François Colin de Verdière 2025-04-01 17:01:53 +02:00
parent 93e14375dd
commit 4de6090ab9

View File

@ -181,7 +181,8 @@ std::vector<int> carving_step(const std::vector<unsigned char> source_img,
std::vector<float> source_energy, std::vector<float> source_energy,
std::vector<unsigned char> &output_img, std::vector<unsigned char> &output_img,
std::vector<float> &output_energy, int width, std::vector<float> &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. /** Carves an image and its energy by one seam, and recomputes the energy.
Returns the optimal seam used */ Returns the optimal seam used */
std::vector<int> opt_seam = std::vector<int> opt_seam =
@ -199,20 +200,29 @@ std::vector<int> carving_step(const std::vector<unsigned char> source_img,
vertical ? height : height - 1, nbChannels); vertical ? height : height - 1, nbChannels);
std::copy(energy.begin(), energy.end(), output_energy.begin()); std::copy(energy.begin(), energy.end(), output_energy.begin());
} else { } else {
int nbColorChannels = 3;
std::cout << "starting to recompute the energy along the seam" << std::endl;
// ASSUME WE ARE DOING A VERTICAL SEAM // ASSUME WE ARE DOING A VERTICAL SEAM
for (auto j = 0; j < height; j++) { if (vertical) {
for (auto i = -1; i < 2; i++) { for (auto j = 0; j < height; j++) {
if ((0 < (opt_seam[j] + i)) && ((opt_seam[j] + i) < width)) { for (auto i = -1; i < 2; i++) {
compute_energy_for_pixel(output_img, (width - 1), height, if ((0 < (opt_seam[j] + i)) && ((opt_seam[j] + i) < width - 1)) {
(opt_seam[j] + i), j, nbChannels, compute_energy_for_pixel(output_img, (width - 1), height,
nbColorChannels, output_energy); (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; return opt_seam;
} }
@ -257,13 +267,13 @@ void seam_carving(unsigned char *source, int width, int height, int nbChannels,
//* Prepare final output //* Prepare final output
float max_energy=__FLT_MAX__; float max_energy = __FLT_MAX__;
for (auto k=0;k<width*height;k++){ for (auto k = 0; k < width * height; k++) {
max_energy=fmax(max_energy, ini_energy[k]); max_energy = fmax(max_energy, ini_energy[k]);
} }
if (max_energy!=0){ if (max_energy != 0) {
for (auto k=0;k<width*height;k++){ for (auto k = 0; k < width * height; k++) {
ini_energy[k]/=max_energy; ini_energy[k] /= max_energy;
} }
} }
for (auto k = 0; k < width * height; k++) { for (auto k = 0; k < width * height; k++) {
@ -281,19 +291,15 @@ void seam_carving(unsigned char *source, int width, int height, int nbChannels,
SimpleProgressBar::ProgressBar bar(nbSeams); SimpleProgressBar::ProgressBar bar(nbSeams);
for (auto seam_index = 0; seam_index < nbSeams; seam_index++) { for (auto seam_index = 0; seam_index < nbSeams; seam_index++) {
std::vector<int> opt_seam = std::vector<int> opt_seam = carving_step(
carving_step(source_img, source_energy, output_img, output_energy, source_img, source_energy, output_img, output_energy, curWidth,
curWidth, curHeight, nbChannels, vertical); curHeight, nbChannels, nbColorChannels, vertical);
std::copy(output_img.begin(), output_img.end(), source_img.begin()); std::copy(output_img.begin(), output_img.end(), source_img.begin());
std::copy(output_energy.begin(), output_energy.end(), std::copy(output_energy.begin(), output_energy.end(),
source_energy.begin()); source_energy.begin());
if (vertical) // We just reduced the dimension vertical ? curWidth-- : curHeight--; // We just reduced the dimension
curWidth--; if (test_energy) { // Update blacklist
else
curHeight--;
if (test_energy) { // Update blacklist
for (auto i = 0; i < dim_long; i++) { for (auto i = 0; i < dim_long; i++) {
int j, cur_j = 0; // cur_j is the index relative to the current carved 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 assert(cur_j == opt_seam[i]); // Else, j == width and cur_j is not in
// the source image.. // the source image..
complete_blacklist[im_index(i, j)] = true; complete_blacklist[im_index(i, j)] = true;
test_energy_output[nbChannels * im_index(i, j)] = test_energy_output[nbChannels * im_index(i, j)] =
255; // Set carved pixel to red 255; // Set carved pixel to red
} }
} }
bar.increment(); bar.increment();
bar.print(); bar.print();
} }
// std::cout << std::endl;
if (test_energy) { if (test_energy) {
export_image(out_filename, test_energy_output.data(), width, height, export_image(out_filename, test_energy_output.data(), width, height,