图片去黑边算法

算法

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

相关推荐
折哥的程序人生 · 物流技术专研20 小时前
Java面试85题图解版 · 特别篇:2026后端高频面试题复盘(算法底层逻辑+高并发架构设计全解析,附Java实战代码)
java·网络·数据库·算法·面试
想吃火锅10051 天前
【leetcode】14.最长公共前缀js
算法·leetcode·职场和发展
云絮.1 天前
数据库操作
数据库·mysql·算法·oracle
小林ixn1 天前
LeetCode 206. 反转链表(迭代 + 递归详解)
算法·leetcode·链表
凡人叶枫1 天前
Effective C++ 条款17:以独立语句将 newed 对象置入智能指针
java·linux·开发语言·c++·算法
菜鸟‍1 天前
LeetCode 1 27 和 704 || 两数之和 移除元素 二分查找
算法·leetcode·职场和发展
数据法师1 天前
QuickSay :基于 Qt 的轻量级快捷短语管理工具
开发语言·qt
小短腿的代码世界1 天前
行情快照与增量更新引擎:Qt在高频交易数据分发中的核心架构——你的行情推送为什么延迟了500ms?
开发语言·qt·架构
退休倒计时1 天前
【每日一题】LeetCode 142. 环形链表 II TypeScript
算法·leetcode·链表·typescript
popcorn_min1 天前
Digits 手写数字识别:随机森林多分类 + 像素级特征热力图
算法·随机森林·分类