int main() try { vector<int> v; cout << "sizeof vector: " << sizeof(v) << "\n"; Mini_vec<int> mv; cout << "sizeof Mini_vec: " << sizeof(mv) << "\n"; Mini_vec<int> mv10 = Mini_vec<int>(10); cout << mv10[0] << "\n"; for (int i = 0; i<mv10.size(); ++i) mv10[i] = i; for (int i = 0; i<mv10.size(); ++i) cout << mv10[i] << ' '; cout << "\n"; Mini_vec<int> mv_cpctr = mv10; for (int i = 0; i<mv_cpctr.size(); ++i) cout << mv_cpctr[i] << ' '; cout << "\n"; Mini_vec<int> mv_cpasgn; mv_cpasgn = mv10; for (int i = 0; i<mv_cpasgn.size(); ++i) cout << mv_cpasgn[i] << ' '; cout << "\n"; mv_cpasgn.resize(5); for (int i = 0; i<mv_cpasgn.size(); ++i) cout << mv_cpasgn[i] << ' '; cout << "\n"; vector<vector<vector<int> > > v3(100); Mini_vec<Mini_vec<Mini_vec<int> > > mv3(100); cout << "sizeof vector<vector<vector<int> > >: " << sizeof(v3) << "\n"; cout << "sizeof Mini_vec<Mini_vec<Mini_vec<int> > >: " << sizeof(mv3) << "\n"; Mini_vec<int> mv4; for (int i = 0; i<10; ++i) mv4.push_back(i); for (int i = 0; i<mv4.size(); ++i) cout << mv4[i] << ' '; cout << "\n"; } catch (exception& e) { cerr << "exception: " << e.what() << endl; keep_window_open(); } catch (...) { cerr << "exception\n"; keep_window_open(); }
const Mat& findBlobs::findCentroidGrid() { m_mCentroidGrid.create(m_uBlobY, m_uBlobX, CV_64FC2); m_mCentroidGrid = 0; Point origin = m_vCartCoord[0]; Point x1 = m_vCartCoord[1]; Point y1 = m_vCartCoord[2]; Mat mc = Mat(m_vCentroids).reshape(1).t(); //find the left top three points in the centroids. int mind = findNearestPointInMatrix(origin, mc); Mat cco(2, 1, mc.type()); mc.col(mind).copyTo(cco); //orgin point DeleteOneColOfMat(mc, mind); m_mCentroidGrid.at<Vec2d>(0, 0) = cco; mind = findNearestPointInMatrix(x1, mc); Mat ccx(2, 1, mc.type()); mc.col(mind).copyTo(ccx); DeleteOneColOfMat(mc, mind); m_mCentroidGrid.at<Vec2d>(0, 1) = ccx; mind = findNearestPointInMatrix(y1, mc); Mat ccy(2, 1, mc.type()); mc.col(mind).copyTo(ccy); DeleteOneColOfMat(mc, mind); m_mCentroidGrid.at<Vec2d>(1, 0) = ccy; std::vector<Point> pairs; pairs.clear(); pairs.push_back(Point(1, 2)); pairs.push_back(Point(0, 2)); pairs.push_back(Point(0, 1)); Mat co = ccx; Mat v = co - cco; int j = 0; //find the first line of the centroids for (int i = 1; i < m_uBlobX-1; i++) { std::vector<int> idx; sortTheDistFromThePoint(co, mc, idx); const int len = 6; int tmpidx[len] = {2, 3, 4, 5, 6, 7}; int tmpCount = 0; int tmpCountmax = len - 1; bool ok = false; Mat cxy; Mat vxy; std::vector<int> indxy; std::vector<double> cosa; std::vector<double> cosb; Point pair; int mincosind; while (!ok) { cxy.create(2, 3, mc.type()); cxy = 0; for(size_t i = 0; i < 2; i++) { mc.col(idx[i]).copyTo(cxy.col(i)); } mc.col(idx[tmpidx[tmpCount]]).copyTo(cxy.col(2)); vxy = cxy - co * Mat::ones(1, cxy.cols, co.type()); computeCos(co, v, cco, cxy, vxy, pairs, cosa, cosb); std::vector<double>::iterator minIter = std::min_element(cosa.begin(), cosa.end()); mincosind = std::distance(cosa.begin(), minIter); indxy.clear(); if (2 == mincosind) { pair = pairs[mincosind]; indxy.push_back(idx[pair.x]); indxy.push_back(idx[pair.y]); } else { pair = pairs[mincosind]; indxy.push_back(idx[pair.x]); indxy.push_back(idx[tmpidx[tmpCount]]); } ok = true; std::vector<double>::iterator maxIter = std::max_element(cosb.begin(), cosb.end()); if ( acos(*maxIter)*180/CV_PI > 25 ) { if (tmpCount < len) { tmpCount += 1; ok = false; } else std::cout << "In blobsgrid: Can not find (0," << i+1 << ")" << std::endl; } } Mat cxyo = cxy.clone(); cxyo.col(pair.x).copyTo(cxy.col(0)); cxyo.col(pair.y).copyTo(cxy.col(1)); cxy = cxy.t(); cxy.pop_back(); cxy = cxy.t(); Mat vxyo = vxy.clone(); vxyo.col(pair.x).copyTo(vxy.col(0)); vxyo.col(pair.y).copyTo(vxy.col(1)); vxy = vxy.t(); vxy.pop_back(); vxy = vxy.t(); Mat ll = Mat::zeros(1, 2, cxy.type()); vxy.push_back(ll); Mat vtmp = vxy.col(0).cross(vxy.col(1)); int trueminind = pair.x; if (vtmp.at<double>(2,0) < 0) { flip(vxy, vxy, 1); flip(cxy, cxy, 1); flip(indxy, indxy, 1); trueminind = pair.y; } std::vector<double>::iterator maxIter = std::max_element(cosa.begin(), cosa.end()); int maxind = std::distance(cosa.begin(), maxIter); if ( (*maxIter > cos(5.0/180.0*CV_PI)) && (maxind!=trueminind )) { Mat m1 = cxy.col(0) - cxyo.col(mincosind); Mat m2 = m1.t() * v; if (m2.at<double>(0,0) > 0) { cxyo.col(mincosind).copyTo(cxy.col(0)); if (2 == mincosind) { indxy[0] = idx[tmpidx[tmpCount]]; } else { indxy[0] = idx[mincosind]; } } } m_mCentroidGrid.at<Vec2d>(j, i+1) = cxy.col(0); v = cxy.col(0) - co; co = cxy.col(0); DeleteOneColOfMat(mc, indxy[0]); } //find the first col of the centroids int i = 0; co = ccy; v = co - cco; for (int j = 1; j < m_uBlobY-1; j++) { std::vector<int> idx; sortTheDistFromThePoint(co, mc, idx); const int len = 6; int tmpidx[len] = {2, 3, 4, 5, 6, 7}; int tmpCount = 0; int tmpCountmax = len - 1; bool ok = false; Mat cxy; Mat vxy; std::vector<int> indxy; std::vector<double> cosa; std::vector<double> cosb; Point pair; int mincosind; while (!ok) { cxy.create(2, 3, mc.type()); cxy = 0; for(size_t i = 0; i < 2; i++) { mc.col(idx[i]).copyTo(cxy.col(i)); } mc.col(idx[tmpidx[tmpCount]]).copyTo(cxy.col(2)); vxy = cxy - co * Mat::ones(1, cxy.cols, co.type()); computeCos(co, v, cco, cxy, vxy, pairs, cosa, cosb); std::vector<double>::iterator minIter = std::min_element(cosa.begin(), cosa.end()); mincosind = std::distance(cosa.begin(), minIter); indxy.clear(); if (2 == mincosind) { pair = pairs[mincosind]; indxy.push_back(idx[pair.x]); indxy.push_back(idx[pair.y]); } else { pair = pairs[mincosind]; indxy.push_back(idx[pair.x]); indxy.push_back(idx[tmpidx[tmpCount]]); } ok = true; std::vector<double>::iterator maxIter = std::max_element(cosb.begin(), cosb.end()); if ( *maxIter < cos(30.0/180.0*CV_PI) ) { if (tmpCount < len) { tmpCount += 1; ok = false; } else std::cout << "In blobsgrid: Can not find (" << j+1 << ",0)" << std::endl; } } Mat cxyo = cxy.clone(); cxyo.col(pair.x).copyTo(cxy.col(0)); cxyo.col(pair.y).copyTo(cxy.col(1)); cxy = cxy.t(); cxy.pop_back(); cxy = cxy.t(); Mat vxyo = vxy.clone(); vxyo.col(pair.x).copyTo(vxy.col(0)); vxyo.col(pair.y).copyTo(vxy.col(1)); vxy = vxy.t(); vxy.pop_back(); vxy = vxy.t(); Mat ll = Mat::zeros(1, 2, cxy.type()); vxy.push_back(ll); Mat vtmp = vxy.col(0).cross(vxy.col(1)); int trueminind = pair.x; if (vtmp.at<double>(2,0) > 0) { flip(vxy, vxy, 1); flip(cxy, cxy, 1); flip(indxy, indxy, 1); trueminind = pair.y; } std::vector<double>::iterator maxIter = std::max_element(cosa.begin(), cosa.end()); int maxind = std::distance(cosa.begin(), maxIter); if ( (*maxIter > cos(5.0/180.0*CV_PI)) && (maxind!=trueminind )) { Mat m1 = cxy.col(0) - cxyo.col(mincosind); Mat m2 = m1.t() * v; if (m2.at<double>(0,0) > 0) { cxyo.col(mincosind).copyTo(cxy.col(0)); if (2 == mincosind) { indxy[0] = idx[tmpidx[tmpCount]]; } else { indxy[0] = idx[mincosind]; } } } m_mCentroidGrid.at<Vec2d>(j+1, i) = cxy.col(0); v = cxy.col(0) - co; co = cxy.col(0); DeleteOneColOfMat(mc, indxy[0]); } double costol = cos(30.0/180.0*CV_PI); for (int j = 1; j < m_uBlobY; j++) { for (int i = 1; i < m_uBlobX; i++) { if ((i==m_uBlobX-1) && (j==m_uBlobX-1)) { m_mCentroidGrid.at<Vec2d>(j, i) = mc.col(0); } else { Mat leftTopMat; std::vector<int> sind; int size[2] = {3, 2}; Vec2d v1 = m_mCentroidGrid.at<Vec2d>(j-1, i-1); Vec2d v2 = m_mCentroidGrid.at<Vec2d>(j-1, i); Vec2d v3 = m_mCentroidGrid.at<Vec2d>(j, i-1); leftTopMat.push_back(v1); leftTopMat.push_back(v2); leftTopMat.push_back(v3); leftTopMat = leftTopMat.reshape(1).t(); sind.clear(); sortTheDistFromThreePoint(leftTopMat, mc, sind); Mat cclose; if (sind.size() > 2) { cclose.create(2, 3, mc.type()); cclose = 0; for(size_t i = 0; i < 3; i++) { mc.col(sind[i]).copyTo(cclose.col(i)); } } else { cclose.create(2, 2, mc.type()); cclose = 0; for(size_t i = 0; i < 2; i++) { mc.col(sind[i]).copyTo(cclose.col(i)); } } Vec2d v = v2 - v1; Mat mv3(v3); Mat vs = mv3 * Mat::ones(1, cclose.cols, mc.type()); subtract(cclose, vs, vs); std::vector<double> cosa; cosa.clear(); for (int k = 0; k < cclose.cols; k++) { Mat tmp = Mat(v).t() * vs.col(k); double tcos = tmp.at<double>(0,0) / (norm(v)*norm(vs.col(k))); cosa.push_back(tcos); } std::vector<double>::iterator maxIter = std::max_element(cosa.begin(), cosa.end()); int maxind = std::distance(cosa.begin(), maxIter); int minind = 0; if (cosa[0] > costol) { minind = sind[0]; } else if (cosa[0] < costol && cosa[1] > costol) { minind = sind[1]; } else { minind = sind[maxind]; } m_mCentroidGrid.at<Vec2d>(j, i) = mc.col(minind); DeleteOneColOfMat(mc, minind); } } } std::string centXMLFileName = m_sSubDirectory + "\\" + m_sImageFileName + "_centroidsGrid.xml"; cv::FileStorage fs(centXMLFileName, cv::FileStorage::WRITE); fs << "centroidsGridMat" << m_mCentroidGrid; fs.release(); return m_mCentroidGrid; }