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);