OPENCV处理图片矩形区域

Mat image = imread(m_strImageFullPath.toStdString(), 1);
    Mat gray;
//    Mat bw;
    cvtColor(image, gray, COLOR_BGR2GRAY);

//    threshold(gray, bw, 120, 255, 1); //通过0,1调节二值图像背景颜色

//    // 高斯滤波
//    Mat gauss;
//    GaussianBlur(gray, gauss, Size(11, 11), 0, 0);

//    Mat edge;
//    Canny(gauss, edge, 30, 150);
//    imwrite("D:\\yeqiang\\opencvDemo\\results\\edge.jpg", edge);

//    Mat contour;
//    image.copyTo(contour);

    emit signalLog(m_u8Row, QObject::tr("Processing..."));

    vector<vector<Point>> contours;
    vector<Vec4i> hierarchy;

    // 构造一个方形结构元素,生成形态学运算的内核,表示的是使用参考点位于中心5x5的核
    Mat kernel = getStructuringElement(MORPH_RECT, Size(5, 5));
    // 返回图片为膨胀图与腐蚀图之差,可以保留物体的边缘轮廓
    Mat gradient;
    morphologyEx(gray, gradient, MORPH_GRADIENT, kernel);
    // 二值图像的轮廓提取
    findContours(gradient, contours, hierarchy, RETR_EXTERNAL, CHAIN_APPROX_SIMPLE);

    std::vector<Cluster> v;
    for (uint32_t i = 0; i < contours.size(); i++) {
        Rect rect = boundingRect(contours.at(i));
        A a(i, rect.x, rect);
        v.push_back(Cluster(a));
    }

    hcm(m_u8ClusterPoint, v);
    std::sort(v.begin(), v.end(), [](const Cluster a, const Cluster b) {
        return a.gravity < b.gravity;
    });

    int cnt = 0;
    for (uint32_t i = 0; i < v.size(); i++) {
        std::sort(v[i].clu.begin(), v[i].clu.end(), [](const A a1, const A a2) {
            return a1.rect.y < a2.rect.y;
        });

        QString strLang = m_mapLanguage.value(m_mapLanguage.keys().at(i));
        int index = 0;
        if (m_i32Index == -1) {
            index = m_mapImageIdx.value(m_mapImageIdx.keys().at(i));
        } else  {
            index = m_i32Index;
        }

        for (uint32_t j = 0; j < v[i].clu.size(); j++) {
            rectangle(image, v[i].clu[j].rect, Scalar(0, 0, 255));

            emit signalProgress(m_u8Row, ((cnt + 1) * 100) / contours.size());

            Mat ROI = image(v[i].clu[j].rect);
            QString path = QApplication::applicationDirPath() + QString("\\results\\%1\\image_%2.jpg").arg(strLang).arg(index, 4, 10, QChar('0'));
            QDir dir(QApplication::applicationDirPath() + QString("\\results\\%1").arg(strLang));
            if (!dir.exists()) {
                dir.mkpath(QApplication::applicationDirPath() + QString("\\results\\%1").arg(strLang));
            }

            imwrite(path.toStdString(), ROI);

            index++;
            cnt++;
        }

        m_mapImageIdx[m_mapImageIdx.keys().at(i)] = index;
    }

    emit signalLog(m_u8Row, QObject::tr("Process Complete"));
    imwrite((QApplication::applicationDirPath() + QString("\\results\\image_%1.jpg")).arg(m_u8Row).toStdString(), image);

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值