qt - Qt 有办法找到图像的边界框吗?

标签 qt

给定一个 .png具有透明背景的图像,我想找到非透明数据的边界框。使用嵌套 forQImage.pixel() 循环痛苦地缓慢。 Qt 中是否有这样做的内置方法?

最佳答案

如果 pixel() 对您来说太慢,请考虑更有效的按行数据寻址,给定 QImage p:

int l =p.width(), r = 0, t = p.height(), b = 0;
for (int y = 0; y < p.height(); ++y) {
    QRgb *row = (QRgb*)p.scanLine(y);
    bool rowFilled = false;
    for (int x = 0; x < p.width(); ++x) {
        if (qAlpha(row[x])) {
            rowFilled = true;
            r = std::max(r, x);
            if (l > x) {
                l = x;
                x = r; // shortcut to only search for new right bound from here
            }
        }
    }
    if (rowFilled) {
        t = std::min(t, y);
        b = y;
    }
}

我怀疑它会比这更快。

关于qt - Qt 有办法找到图像的边界框吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3720947/

相关文章:

qt - QGridLayout 不返回其所有子部件

android - Qt5 Android 最大化应用

c++ - 连接qt和matlab时出错

c - GTK 中的信号和槽

c++ - 可以运行 Qt GUI 应用程序的最小 linux

c++ - 精确的信号发生器

c++ - 指向 vector 中元素的指针

c++ - 使用 C++ 从系统注册表中检索 ActiveX classID

c++ - 使用Qt模拟gps数据以与traccar一起使用

c++ - QAbstractItemModel + ModelTest::rowsInserted ASSERTion问题