图片去黑边算法

算法

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

相关推荐
GuWenyue2 小时前
传统Agent工具两大痛点!300行代码落地MCP跨语言工具,彻底解耦LLM与工具
前端·人工智能·算法
我叫洋洋4 小时前
C ++ [ hello world ]
c语言·c++·算法
奋发向前wcx5 小时前
y1,y2总复习笔记5 2026.7.19
数据结构·笔记·算法
战族狼魂8 小时前
高频面试题精选:分治与AI Agent架构
人工智能·算法·大模型·大语言模型
李剑一8 小时前
你用过网易的将军令嘛?它底层实现账号保护的原理相当简单粗暴
算法
Scott9999HH8 小时前
告别流量波动玄学!从法拉第电磁定律到底层 C/C++ 流量累积算法,深度解密工业级电磁流量计选型与开发
c语言·c++·算法
中达瑞和-高光谱·多光谱9 小时前
1nm到8nm光谱分辨率,你的应用该选哪款光谱相机?
算法·高光谱·高光谱相机
香辣牛肉饭10 小时前
【算法】动态规划 最长公共子序列(LCS)
经验分享·笔记·算法·动态规划
旖-旎10 小时前
LeetCode 279:完全平方数(完全背包)—— 题解
c++·算法·leetcode·动态规划·背包问题