将图像 magick 命令转换为 magick++ c++ 代码

Convert image magick command to magick++ c++ code

我在我的大学从事图像预处理项目,并使用图像魔术脚本来清理图像background.Now我想通过 Magick++(c++ api for imageMagick)获得相同的输出.

ImageMagick 命令:"convert -respect-parenthesis ( INPUT_IMAGE.jpg -colorspace gray -contrast-stretch 0 ) ( -clone 0 -colorspace gray -negate -lat 25x25+30% -contrast-stretch 0 ) -compose copy_opacity -composite -fill white -opaque none -alpha off -background white OUTPUT_IMAGE.jpg"

我试图将此代码转换为 Magick++ 代码,但在“-lat”、“-contrast-stretch”和“-compose”位置失败了。

到目前为止,这是我的 C++ 代码:

Image backgroungImage;
backgroungImage.read("INPUT_IMAGE.jpg");
backgroungImage.colorSpace(GRAYColorspace);
backgroungImage.type(GrayscaleType);
backgroungImage.contrastStretch(0, QuantumRange);
backgroungImage.write("Partial_output.jpg");

如果有人有想法或更好的解决方案,请告诉我。 提前谢谢。

-contrast-stretch 的方向是正确的。对于 -lat,请记住这是 "Local Adaptive Threshold" 的缩写。所以 C++ 代码看起来像...

Image backgroundImage;
// INPUT_IMAGE.jpg
backgroundImage.read("INPUT_IMAGE.jpg");
// -colorspace gray 
backgroundImage.colorSpace(GRAYColorspace);
// -contrast-stretch 0
backgroundImage.contrastStretch(0, QuantumRange);
// -clone 0
Image foregroundImage(backgroundImage);
// -negate
foregroundImage.negate();
// -lat 25x25+30%
foregroundImage.adaptiveThreshold(25, 25, QuantumRange * 0.30);
// -contrast-stretch 0
backgroundImage.contrastStretch(0, QuantumRange);
// -compose copy_opacity -composite
backgroundImage.composite(foregroundImage, 0, 0, CopyAlphaCompositeOp);
// -fill white -opaque none
backgroundImage.opaque(Color("NONE"), Color("WHITE"));
// -alpha off
backgroundImage.alpha(false);
// -background white
backgroundImage.backgroundColor(Color("WHITE"));
// OUTPUT_IMAGE.jpg
backgroundImage.write("OUTPUT_IMAGE.jpg");

希望对您有所帮助!