recomputation of energy seems to work better

This commit is contained in:
François Colin de Verdière 2025-04-03 14:36:08 +02:00
parent 3e96a3df05
commit 8612f2f0f1

View File

@ -252,22 +252,35 @@ void update_energy_along_seam(std::vector<int> opt_seam,
std::copy(energy.begin(), energy.end(), output_energy.begin());
} else {
if (vertical) { // 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 - 1)) {
compute_energy_for_pixel(output_img, (width - 1), height,
(opt_seam[j] + i), j, nbChannels,
nbColorChannels, output_energy);
for (auto j0 = 0; j0 < height; j0++) {
auto i0 = opt_seam[j0];
// recompute on a square around i0,j0
for (auto i_offset = -max_step - 2; i_offset <= max_step + 2;
i_offset++) {
for (auto j_offset = -max_step - 2; j_offset <= max_step + 2;
j_offset++) {
if (((0 < (i0 + i_offset)) && ((i0 + i_offset) < width - 1)) &&
((0 < j0 + j_offset) && (j0 + j_offset < (height)))) {
compute_energy_for_pixel(
output_img, (width - 1), height, (i0 + i_offset),
(j0 + j_offset), 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);
for (auto i0 = 0; i0 < width; i0++) {
auto j0 = opt_seam[i0];
for (auto j_offset = -max_step - 2; j_offset <= max_step + 2;
j_offset++) {
for (auto i_offset = -max_step - 2; i_offset <= max_step + 2;
i_offset++) {
if (((0 < (j0 + j_offset)) && ((j0 + j_offset) < height - 1)) &&
((0 < (i0 + i_offset)) && (i0 + i_offset) < width)) {
compute_energy_for_pixel(
output_img, width, height - 1, (i0 + i_offset),
(j0 + j_offset), nbChannels, nbColorChannels, output_energy);
}
}
}
}
@ -463,8 +476,8 @@ int main(int argc, char **argv) {
}
nbSeams = std::min(
nbSeams, vertical
? width - 1
nbSeams,
vertical ? width - 1
: height - 1); // We want to keep at least one row or column
auto result = seam_carving(source, width, height, nbChannels, nbSeams,