vector<LPPACKAGE> cvPic::getPrintPack() { string srcImg = ImgName; // 源图片的位置 int num = 0; // 连通图个数 cv::Mat grayImg = cv::imread( srcImg ); // 将图片加载 cv::Mat labelImg; vector<int> stRun,enRun,rowRun,runLabels; vector<pair<int,int>> equivalences; vector<int> imgX,imgY,imgColor; int imgWidth,imgHeight; cv::cvtColor( grayImg, labelImg, CV_BGR2GRAY ); // 灰度化图像 cv::threshold( labelImg, labelImg, 150, 255, CV_THRESH_BINARY ); // 二值化图像 packagePictureInfo( labelImg, imgX, imgY, imgColor ); imgWidth = labelImg.cols; imgHeight = labelImg.rows; fillRunVectors( labelImg, num, stRun, enRun, rowRun ); // 完成团的标记与等价对列表的生成 firstPass( stRun, enRun, rowRun, num, runLabels, equivalences, 1 ); // 八邻接 replaceSameLabel( runLabels, equivalences ); printf( "连通图数目为:%d\n", num ); return getPackages(imgWidth,imgHeight,imgX,imgY,imgColor); }
//找最大的团并返回外轮廓 std::vector<MYPOINT> FindBiggestContour(cimg_library::CImg<unsigned char> origin, std::vector<MYPOINT> Points) { std::vector<MYPOINT> contour; cimg_library::CImg<unsigned char> mask; //compute the binary img int threshold = otsu(origin, Points, mask, 1); unsigned char *bi_img = new unsigned char[origin.width()*origin.height()]; for (int i = 0; i < origin.height(); i++){ for (int j = 0; j < origin.width(); j++){ if (origin.atXY(j, i) >= threshold && mask.atXY(j, i) == 255) bi_img[i*origin.width() + j] = 255; else bi_img[i*origin.width() + j] = 0; } } //返回二值图像 cimg_library::CImg<unsigned char> bi(bi_img, origin.width(), origin.height()); //done //找团 std::vector<int> stRun, enRun, rowRun, masRun; int NumberofRuns = 0, offset = 1, maxRun = 1; fillRunVector(bi, NumberofRuns, stRun, enRun, rowRun, masRun); std::vector<int> runLabels; std::vector<std::pair<int, int>> equivalences; firstPass(stRun, enRun, rowRun, NumberofRuns, runLabels, equivalences, offset); if (!NumberofRuns){ std::cout << "NOTHING FOUND!!!" << std::endl <<"size of contour is: "<< contour.size() << std::endl; return contour; } replaceSameLabel(runLabels, equivalences); int maxLabel = *max_element(runLabels.begin(), runLabels.end()); int *MassofRuns = new int[maxLabel + 1]; memset(MassofRuns, 0, (maxLabel + 1)*sizeof(int)); memset(bi_img, 0, (bi.height()*bi.width())*sizeof(unsigned char)); for (int c = 0; c < NumberofRuns; c++){ MassofRuns[runLabels[c]] += masRun[c]; if (MassofRuns[runLabels[c]]>maxRun){ maxRun = MassofRuns[runLabels[c]]; maxLabel = runLabels[c]; } int i = rowRun[c]; for (int j = stRun[c]; j <= enRun[c]; j++){ bi_img[i*bi.width() + j] = runLabels[c]; } } for (int i = 0; i < bi.height(); i++) for (int j = 0; j < bi.width(); j++) { if (bi_img[i*bi.width()+j] == maxLabel) bi_img[i*bi.width() + j] = 255; else bi_img[i*bi.width() + j] = 0; } return FindContour(bi_img, bi.width(), bi.height()); }
void ObstacleDetection::ObstacleDetecting(const int obstacleMap[120][120], RobPose cRobotPos) { int cObstacleMap[120][120]; memcpy(cObstacleMap, obstacleMap, 120*120*sizeof(int)); stRun.clear(); enRun.clear(); rowRun.clear(); runLabels.clear(); equivalences.clear(); numberOfRuns = 0; offset = 1; obsNum = 0; obsPoses.clear(); std::stringstream out; out<<frames_num; std::string filename = "ObstacleMap" + out.str() + ".txt"; std::ofstream Obstaclemapfile(filename); if (Obstaclemapfile.is_open()) { for(int i = 0; i < 120; i++) { for(int j = 0; j < 120; j++) { Obstaclemapfile<<cObstacleMap[i][j]<<" "; } Obstaclemapfile<<std::endl; } } frames_num++; fillRunVectors(cObstacleMap, numberOfRuns, stRun, enRun, rowRun); if(numberOfRuns > 0) { firstPass(stRun, enRun, rowRun, numberOfRuns, runLabels, equivalences, offset); replaceSameLabel(runLabels, equivalences); FindObstacle(stRun, enRun, rowRun, numberOfRuns, runLabels, obsNum, obsPoses, cRobotPos); cout<<"Num of Obstalces: "<<obsNum<<endl; } }