mask: use valid (>0) unsigned char

This commit is contained in:
augustin64 2025-04-03 08:29:35 +02:00
parent 5a9eb05b6a
commit a7d2c74a89

View File

@ -28,7 +28,9 @@ bool nearly_equal(float a, float b) {
std::pair<int, float> operator+(std::pair<int, float>& p1, std::pair<int, float>& p2) { std::pair<int, float> operator+(std::pair<int, float>& p1, std::pair<int, float>& p2) {
return { 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 p1.second+p2.second
}; };
} }
@ -43,7 +45,7 @@ namespace limits {
return std::numeric_limits<T>::max(); return std::numeric_limits<T>::max();
} }
operator std::pair<int, float>() { operator std::pair<int, float>() {
return {2, std::numeric_limits<float>::max()}; return {3, std::numeric_limits<float>::max()};
} }
}; };
} }
@ -236,7 +238,7 @@ void recompute_energy_along_seam(
compute_energy_for_pixel( compute_energy_for_pixel(
carved_img, newWidth, newHeight, carved_img, newWidth, newHeight,
x, y, nbChannels, 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( compute_energy_for_pixel(
carved_img, newWidth, newHeight, carved_img, newWidth, newHeight,
x, y, nbChannels, 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) if (nbChannels == 4)
test_energy_output[nbChannels * k + 3] = source_img[nbChannels * k + 3]; 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; unsigned char r, g, b;
for (auto i=0; i < width*height; i++) { for (auto i=0; i < width*height; i++) {
r = mask[maskChannels*i]; r = mask[maskChannels*i];
g = mask[maskChannels*i]; g = mask[maskChannels*i+1];
b = mask[maskChannels*i]; b = mask[maskChannels*i+2];
bool positive = (g > r && g > b); bool positive = (g > r && g > b && g > 100); // Mask images are not always the cleanest
bool negative = (r > g && r > b); bool negative = (r > g && r > b && r > 100);
mask[i] = positive ? 1 : (negative ? -1 : 0);
mask[i] = positive ? 2 : (negative ? 0 : 1);
} }
//* From now on, mask has the same dimensions as source and one single channel //* From now on, mask has the same dimensions as source and one single channel
//* The values are: //* The values are:
//* . ( 1) we want to keep the pixel //* . (2) we want to keep the pixel
//* . (-1) we want to remove the pixel //* . (1) nothing in particular
//* . ( 0) 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 nbSeams = std::min(nbSeams, vertical ? width-1 : height-1); // We want to keep at least one row or column