reecriture remove_seam

This commit is contained in:
François Colin de Verdière 2025-04-01 15:00:08 +02:00
parent f15b4c471b
commit e7ab4f0bd6

View File

@ -154,29 +154,17 @@ std::vector<int> optimal_seam(std::vector<float> energy, int width, int height,
return result;
}
void remove_seam(const std::vector<unsigned char> source,
std::vector<unsigned char> &output, int width, int height,
int nbChannels, bool vertical, const std::vector<int> seam)
{
/* remove the given seam from the image, the result is in output*/
std::vector<bool> blacklist(width * height);
int nbChannels, bool vertical, const std::vector<int> seam) {
// remove the given seam from the image, the result is in output
int dim_large = vertical ? width : height;
int dim_long = vertical ? height : width;
for (auto k = 0; k < width * height; k++) {
blacklist[k] = false;
}
for (auto i = 0; i < dim_long; i++) {
int index = vertical ? seam[i] + i * width : i + width * seam[i];
blacklist[index] = true;
}
for (auto i = 0; i < dim_long; i++) {
int cur_j = 0;
for (auto j = 0; cur_j < dim_large - 1 && j < dim_large; j++) {
if (!blacklist[im_index(i, j)]) {
if (seam[i] != j) {
int out_pixelIndex = nbChannels * (vertical ? ((width - 1) * i + cur_j)
: (width * cur_j + i));
int src_pixelIndex = nbChannels * im_index(i, j);
@ -187,25 +175,18 @@ void remove_seam(const std::vector<unsigned char> source,
}
}
}
}
std::vector<int> carving_step(const std::vector<unsigned char> source,
std::vector<float> energy,
std::vector<unsigned char> &output, int width,
int height, int nbChannels, bool vertical) {
/** Carves an image by one seam. Returns the optimal seam used */
std::vector<float> energy = energy_e1(source, width, height, nbChannels);
std::vector<int> opt_seam = optimal_seam(energy, width, height, vertical);
remove_seam(source, output, width, height, nbChannels, vertical, opt_seam);
return opt_seam;
}
void seam_carving(unsigned char *source, int width, int height, int nbChannels,
const char *out_filename, int nbSeams, bool vertical,
bool test_energy = false) {
@ -215,18 +196,18 @@ void seam_carving(unsigned char *source, int width, int height, int nbChannels,
int dim_large = vertical ? width : height;
int dim_long = vertical ? height : width;
// dim_long=longueur des seam
std::vector<unsigned char> carve_output(
width * height *
nbChannels); // Receives at each step the newly carved image
std::vector<unsigned char> source_img(
width * height * nbChannels); // Contains at each step the carved image
std::vector<bool> complete_blacklist(
width * height); // Contains all removed pixels, for "test_energy"
std::vector<float>
ini_energy; // Contains the initial energy, only for "test_energy"
std::vector<unsigned char> test_energy_output(
width * height * nbChannels); // Final output for "test_energy"
std::vector<unsigned char> carve_output(width * height * nbChannels);
// Receives at each step the newly carved image
std::vector<unsigned char> source_img(width * height * nbChannels);
// Contains at each step the carved image
std::vector<bool> complete_blacklist(width * height);
// Contains all removed pixels, for "test_energy"
std::vector<float> ini_energy;
// Contains the initial energy, only for "test_energy"
std::vector<unsigned char> test_energy_output(width * height * nbChannels);
// Final output for "test_energy"
for (auto i = 0; i < width * height * nbChannels; i++) {
source_img[i] = source[i];
@ -253,8 +234,11 @@ void seam_carving(unsigned char *source, int width, int height, int nbChannels,
SimpleProgressBar::ProgressBar bar(nbSeams);
for (auto seam = 0; seam < nbSeams; seam++) {
std::vector<int> opt_seam = carving_step(source_img, carve_output, curWidth,
curHeight, nbChannels, vertical);
std::vector<float> energy =
energy_e1(source_img, width, height, nbChannels);
std::vector<int> opt_seam =
carving_step(source_img, energy, carve_output, curWidth, curHeight,
nbChannels, vertical);
std::copy(carve_output.begin(), carve_output.end(), source_img.begin());
if (vertical) // We just reduced the dimension
@ -282,7 +266,7 @@ void seam_carving(unsigned char *source, int width, int height, int nbChannels,
bar.increment();
bar.print();
}
std::cout << std::endl;
// std::cout << std::endl;
if (test_energy) {
export_image(out_filename, test_energy_output.data(), width, height,