From a7d2c74a8998252d79c76e0cbc61986b9ea91165 Mon Sep 17 00:00:00 2001 From: augustin64 Date: Thu, 3 Apr 2025 08:29:35 +0200 Subject: [PATCH] mask: use valid (>0) unsigned char --- src/seam-carving.cpp | 35 +++++++++++++++++++++++------------ 1 file changed, 23 insertions(+), 12 deletions(-) diff --git a/src/seam-carving.cpp b/src/seam-carving.cpp index e138076..d840206 100644 --- a/src/seam-carving.cpp +++ b/src/seam-carving.cpp @@ -28,7 +28,9 @@ bool nearly_equal(float a, float b) { std::pair operator+(std::pair& p1, std::pair& p2) { return { - (p1.first==1) || (p2.first==1) ? 1 : std::max(p1.first, p2.first), + // If one of the two pixels is "protected" (ie 2), we want to prevent this line removing + // Else, we want to keep the information of "is there a pixel to remove in this seam" (ie 0) + (p1.first==2) || (p2.first==2) ? 2 : std::min(p1.first, p2.first), p1.second+p2.second }; } @@ -43,7 +45,7 @@ namespace limits { return std::numeric_limits::max(); } operator std::pair() { - return {2, std::numeric_limits::max()}; + return {3, std::numeric_limits::max()}; } }; } @@ -236,7 +238,7 @@ void recompute_energy_along_seam( compute_energy_for_pixel( carved_img, newWidth, newHeight, x, y, nbChannels, - nbColorChannels, output_energy[width*j+i] + nbColorChannels, output_energy[width*y+x] ); } } @@ -261,7 +263,7 @@ void recompute_energy_along_seam( compute_energy_for_pixel( carved_img, newWidth, newHeight, x, y, nbChannels, - nbColorChannels, output_energy[width*j+i].first + nbColorChannels, output_energy[width*y+x].first ); } } @@ -371,6 +373,14 @@ void seam_carving(unsigned char *source, int width, int height, int nbChannels, } if (nbChannels == 4) test_energy_output[nbChannels * k + 3] = source_img[nbChannels * k + 3]; + + if (mask) { + if (mask[k] == 2) // Green + test_energy_output[nbChannels*k + 1] = 125; + else if (mask[k] == 0) {// Red + test_energy_output[nbChannels * k] = 125; + } + } } } @@ -482,17 +492,18 @@ int main(int argc, char **argv) { unsigned char r, g, b; for (auto i=0; i < width*height; i++) { r = mask[maskChannels*i]; - g = mask[maskChannels*i]; - b = mask[maskChannels*i]; - bool positive = (g > r && g > b); - bool negative = (r > g && r > b); - mask[i] = positive ? 1 : (negative ? -1 : 0); + g = mask[maskChannels*i+1]; + b = mask[maskChannels*i+2]; + bool positive = (g > r && g > b && g > 100); // Mask images are not always the cleanest + bool negative = (r > g && r > b && r > 100); + + mask[i] = positive ? 2 : (negative ? 0 : 1); } //* 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 + //* . (2) we want to keep the pixel + //* . (1) nothing in particular + //* . (0) we want to remove the pixel } nbSeams = std::min(nbSeams, vertical ? width-1 : height-1); // We want to keep at least one row or column