Mat CmSaliencyRC::GetBorderReg(CMat &idx1i, int regNum, double ratio, double thr) { // Variance of x and y vecD vX(regNum), vY(regNum); int w = idx1i.cols, h = idx1i.rows;{ vecD mX(regNum), mY(regNum), n(regNum); // Mean value of x and y, pixel number of region for (int y = 0; y < idx1i.rows; y++){ const int *idx = idx1i.ptr<int>(y); for (int x = 0; x < idx1i.cols; x++, idx++) mX[*idx] += x, mY[*idx] += y, n[*idx]++; } for (int i = 0; i < regNum; i++) mX[i] /= n[i], mY[i] /= n[i]; for (int y = 0; y < idx1i.rows; y++){ const int *idx = idx1i.ptr<int>(y); for (int x = 0; x < idx1i.cols; x++, idx++) vX[*idx] += abs(x - mX[*idx]), vY[*idx] += abs(y - mY[*idx]); } for (int i = 0; i < regNum; i++) vX[i] = vX[i]/n[i] + EPS, vY[i] = vY[i]/n[i] + EPS; } // Number of border pixels in x and y border region vecI xbNum(regNum), ybNum(regNum); int wGap = cvRound(w * ratio), hGap = cvRound(h * ratio); vector<Point> bPnts; { ForPoints2(pnt, 0, 0, w, hGap) // Top region ybNum[idx1i.at<int>(pnt)]++, bPnts.push_back(pnt); ForPoints2(pnt, 0, h - hGap, w, h) // Bottom region ybNum[idx1i.at<int>(pnt)]++, bPnts.push_back(pnt); ForPoints2(pnt, 0, 0, wGap, h) // Left region xbNum[idx1i.at<int>(pnt)]++, bPnts.push_back(pnt); ForPoints2(pnt, w - wGap, 0, w, h) xbNum[idx1i.at<int>(pnt)]++, bPnts.push_back(pnt); } Mat bReg1u(idx1i.size(), CV_8U);{ // likelihood map of border region double xR = 1.0/(4*hGap), yR = 1.0/(4*wGap); vector<byte> regL(regNum); // likelihood of each region belongs to border background for (int i = 0; i < regNum; i++) { double lk = xbNum[i] * xR / vY[i] + ybNum[i] * yR / vX[i]; regL[i] = lk/thr > 1 ? 255 : 0; //saturate_cast<byte>(255 * lk / thr); } for (int r = 0; r < h; r++) { const int *idx = idx1i.ptr<int>(r); byte* maskData = bReg1u.ptr<byte>(r); for (int c = 0; c < w; c++, idx++) maskData[c] = regL[*idx]; } } for (size_t i = 0; i < bPnts.size(); i++) bReg1u.at<byte>(bPnts[i]) = 255; return bReg1u; }
void Model::loadFile(QString filePath) { QFile file(filePath); if (!file.open(QIODevice::ReadOnly)) return; //如下两个变量存放模型的边界 Point3d boundsMin( 1e9, 1e9, 1e9); Point3d boundsMax(-1e9,-1e9,-1e9); //读取obj格式的文件 QTextStream in(&file); //now begin to read the verters Point3d pi; float currentZ = 0.0; float currentX = 0.0; float currentY = 0.0; QRegExp regZ("[\w\d\s]*Z([0-9\.]*[0-9]*)"); QRegExp regL(";LAYER: *([0-9]*)"); QRegExp regXY("G1 [\w\d\s]* *X([0-9\.]*[0-9]*) *Y([0-9\.]*[0-9]*)"); bool isZ = false; bool isIn = false; while(!in.atEnd()) { QString current_str = in.readLine(); if(current_str.contains(regXY)) { currentX = regXY.cap(1).toFloat(); currentY = regXY.cap(2).toFloat(); boundsMin.x = qMin(boundsMin.x,currentX); boundsMax.x = qMax(boundsMax.x, currentX); boundsMin.y = qMin(boundsMin.y,currentY); boundsMax.y = qMax(boundsMax.y, currentY); boundsMin.z = qMin(boundsMin.z,currentZ); boundsMax.z = qMax(boundsMax.z, currentZ); qDebug()<< QObject::tr("Xs=%1").arg(currentX); pi = Point3d(currentX,currentY,currentZ); m_points.push_back(pi); } if(current_str.contains(regL)) { if((regL.cap(1))=="0") isIn = false; else isIn = true; isZ = true; } if(current_str.contains(regZ)&&(isZ)) { isZ = false; QString str_Z = regZ.cap(1); currentZ = str_Z.toFloat(); } } const Point3d bounds = boundsMax - boundsMin; const qreal scale = 100 / qMax(bounds.x, qMax(bounds.y, bounds.z)); for(int j=0;j<m_points.size();j++) { m_points[j] = (m_points[j] - (boundsMin + bounds * 0.5)) * scale; qDebug()<< QObject::tr("X=%1").arg(m_points[j].x); } }