图片去黑边算法

算法

统计一条线上黑色点数量,少于线长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。

相关推荐
程与留2 小时前
15_国际化和本地化:tr()、ts 文件、QM 文件、多语言切换
c++·qt
地平线开发者3 小时前
【模型轻量化专题】衡量模型轻量性的指标
算法
程与留3 小时前
14_Qt 样式表(QSS)入门(语法、选择器、美化实战)
c++·qt
学习星球6 小时前
OFDM技术精讲:正交性推导、循环前缀原理与80行Python链路仿真(附实测数据)
算法·面试·前端框架
Quz10 小时前
QML 拖拽篇:接收拖拽与数据交换
qt
alphaTao12 小时前
LeetCode 每日一题 2026/8/24-2026/8/30
python·算法·leetcode
Interview Aid11212 小时前
TikTok OA 四题分享|半小时内 AC,题目基本都是实现题
java·开发语言·算法·面试·职场和发展
顶点多余21 小时前
那些在算法中适合巩固的知识点---1
java·前端·算法
罗西的思考1 天前
【Agentic RL / 强化学习框架】Molt 设计解读
人工智能·算法·机器学习
hahaha60161 天前
HLS高层次综合设计技巧--C++类和模板
图像处理·人工智能·算法·计算机视觉