gpt4 book ai didi

c++ - 如何修复腐 eclipse 的矩形?

转载 作者:行者123 更新时间:2023-11-28 01:08:36 25 4
gpt4 key购买 nike

基本上,我有这样一张图片 enter image description here

或在同一图像中具有多个矩形的图像。矩形完全是黑色和白色,有“脏”的边缘和凹痕,但很容易看出它们是矩形。更准确地说,它们是图像蒙版。白色区域是要“单独保留”的图像部分,但黑色部分要变成双色调。

我的问题是,如何从这个退化的矩形中制作出漂亮而清晰的矩形?我是 Python 专家,但我必须使用 Qt 和 C++ 来完成这项任务。如果不使用其他库会更可取。

谢谢!

最佳答案

如果包含所有非黑色像素的边界框可以执行您想要的操作,那么这应该可以解决问题:

int boundLeft = INT_MAX;
int boundRight = -1;
int boundTop = INT_MAX;
int boundBottom = -1;
for(int y=0;y<imageHeight;++y) {
bool hasNonMask = false;
for(int x=0;x<imageWidth;++x) {
if(isNotMask(x, y)) {
hasNonMask = true;
if(x < boundLeft) boundLeft = x;
if(x > boundRight) boundRight = x;
}
}
if(hasNonMask) {
if(y < boundTop) boundTop = y;
if(y > boundBottom) boundBottom = y
}
}

如果结果的大小为负数,则图像中没有非 mask 像素。代码可以更优化,但我还没有喝够咖啡。 :)

关于c++ - 如何修复腐 eclipse 的矩形?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4841086/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com