【问题标题】:Convert image magick command to magick++ c++ code将图像magick命令转换为magick++ c++代码
【发布时间】:2017-09-23 10:16:21
【问题描述】:

我正在我的大学从事图像预处理项目,并使用图像魔法脚本清理图像背景。现在我想通过 Magick++(用于 imageMagick 的 c++ api)获得相同的输出。

ImageMagick 命令:"convert -respect-parenthesis (INPUT_IMAGE.jpg -colorspace gray -contrast-stretch 0) (-clone 0 -colorspace gray -negate -lat 25x25+30% -contrast-stretch 0) -撰写 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");

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

【问题讨论】:

    标签: c++ imagemagick magick++ image-preprocessing


    【解决方案1】:

    -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");
    

    希望有帮助!

    【讨论】:

    • 非常感谢。有了这个帮助,我可以进一步进行转换。但是我从 image magick 命令得到的输出与 magick++ 代码不同。据我所知,这应该是由于我传递给 AdaptiveThreshold() 函数的值造成的。我尝试更改这些值,但输出图像仍然删除了图像中的大部分特征并使其更白。顺便说一句,上述解释解决了我的大部分疑问。谢谢。
    • 请发布预期的图像、结果以及使用的 ImageMagick 版本。或许我能找出问题所在
    • 感谢您和您的指导,我找到了问题所在。我评论了对图像执行“对比度拉伸”并获得类似于命令行执行的确切输出的两行。我的问题解决了,留下一个问题“那么对比拉伸和 QuantumRange 的目的是什么”,我应该深入研究。
    • 实际上,尝试将QuantumRange 替换为backgroundImage.columns() * backgroundImage.rows() / 100.0 用于contrastStretch 方法中的白色像素值。看看有没有用,我自己可能理解错了方法。
    • 我已尝试按照您的建议更改 QuantumRange 值,但 ImageMagick 命令行输出和 magick++ 输出图像彼此不同。我正在使用“ImageMagick-7.0.4-Q16”。
    猜你喜欢
    • 1970-01-01
    • 2015-01-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-01-03
    • 1970-01-01
    • 2014-02-23
    • 2018-02-16
    相关资源
    最近更新 更多