图片去黑边算法

算法

统计一条线上黑色点数量,少于线长10%(阈值,也可设置成变量调整)判定为边界,执行裁剪。

cpp 复制代码
void ImageWidget::deleteBlackBorder(BorderType border_type)
{
    switch (border_type) {
    case BORDER_LEFT:{
        int x1 = 0;
        for (int x=0; x<image.width()/4; x++) {
            int c = 0;
            for (int y=0; y<image.height(); y++) {
                QRgb rgb = image.pixel(x,y);
                int r = qRed(rgb);
                int g = qGreen(rgb);
                int b = qBlue(rgb);
                if (r<10 && g<10 && b<10) {
                    c++;
                }
            }
            float p = c;
            //qDebug() << x << ":" << c << image.height() << p / image.height();
            p = p / image.height();
            if (p < 0.1) {
                x1 = x;
                emit statusBar2Message("DeleteBlackBorder(x=" + QString::number(x) + ")");
                break;
            }
        }
        image = image.copy(x1, 0, image.width() - x1, image.height());
        imgtemp = image;
        update();
        break;}
    case BORDER_RIGHT:{
        int x1 = image.width() - 1;
        for (int x=x1; x>image.width()*3/4; x--) {
            int c = 0;
            for (int y=0; y<image.height(); y++) {
                QRgb rgb = image.pixel(x,y);
                int r = qRed(rgb);
                int g = qGreen(rgb);
                int b = qBlue(rgb);
                if (r<10 && g<10 && b<10) {
                    c++;
                }
            }
            float p = c;
            //qDebug() << x << ":" << c << image.height() << p / image.height();
            p = p / image.height();
            if (p < 0.1) {
                x1 = x;
                emit statusBar2Message("DeleteBlackBorder(x=" + QString::number(x) + ")");
                break;
            }
        }
        image = image.copy(0, 0, x1, image.height());
        imgtemp = image;
        update();}
        break;
    case BORDER_TOP:{        
        int y1 = 0;        
        for (int y=0; y<image.height()/4; y++) {
            int c = 0;
            for (int x=0; x<image.width(); x++) {
                QRgb rgb = image.pixel(x,y);
                int r = qRed(rgb);
                int g = qGreen(rgb);
                int b = qBlue(rgb);
                if (r<10 && g<10 && b<10) {
                    c++;
                }
            }
            float p = c;
            //qDebug() << y << ":" << c << image.width() << p / image.width();
            p = p / image.width();
            if (p < 0.1) {
                y1 = y;
                emit statusBar2Message("DeleteBlackBorder(y=" + QString::number(y) + ")");
                break;
            }
        }
        image = image.copy(0, y1, image.width()-1, image.height() - y1);
        imgtemp = image;
        update();
        break;}
    case BORDER_BOTTOM:{        
        int y1 = image.height() - 1;
        for (int y=y1; y>image.height()*3/4; y--) {
            int c = 0;
            for (int x=0; x<image.width(); x++) {
                QRgb rgb = image.pixel(x,y);
                int r = qRed(rgb);
                int g = qGreen(rgb);
                int b = qBlue(rgb);
                if (r<10 && g<10 && b<10) {
                    c++;
                }
            }
            float p = c;
            //qDebug() << y << ":" << c << image.height() << p / image.height();
            p = p / image.height();
            if (p < 0.1) {
                y1 = y;
                emit statusBar2Message("DeleteBlackBorder(y=" + QString::number(y) + ")");
                break;
            }
        }
        image = image.copy(0, 0, image.width() -1, y1);
        imgtemp = image;
        update();}
        break;
    }
}

https://www.bilibili.com/video/BV1GSEj6UELh/

Qt 版做好了,可以移植到 Android 和 JS。

相关推荐
.道阻且长.4 小时前
2.LeetCode算法习题讲解--双指针--复写零
算法·leetcode·职场和发展
To_OC6 小时前
LC 438 找到所有字母异位词:暴力超时后,我靠滑动窗口一招搞定
javascript·算法·leetcode
Forever Nore9 小时前
学完C语言力扣第一题做不来正常吗
数据结构·算法
hansang_IR9 小时前
【题解】LC:倍增 / 区间并查集(Range Parallel Unionfind)
c++·算法·并查集
Tisfy11 小时前
LeetCode 3731.找出缺失的元素:哈希 / 排序
算法·leetcode·哈希算法·排序·哈希表
lucas_AI11 小时前
Q-CueGraph:你的多模态大模型会 zoom,但真的知道该看哪儿吗?
人工智能·算法
≮傷£≯√11 小时前
Opencv VideoCapture
qt·opencv
kaixin_啊啊11 小时前
test_机器学习算法学习
学习·算法·机器学习
liulilittle11 小时前
MOE路由:路由(logits: top-k/8)
c++·人工智能·算法·机器学习·llm