int imageClassifier::labelling(int x, int y, int direction) { CvScalar currentPixel; // check 'x' and 'y' range if(x < 0 || y < 0 || x >= classImg->width || y >= classImg->height) return 0; // get current pixel's value currentPixel = cvGet2D(classImg, y, x); // if current pixel is black or already labelled, break if(currentPixel.val[0] != WHITE) return 0; // set label to current pixel currentPixel.val[0] = label; cvSet2D(classImg, y, x, currentPixel); // search near pixel if(direction != BOTTOM) labelling(x, y-1, TOP); if(direction != TOP) labelling(x, y+1, BOTTOM); if(direction != LEFT) labelling(x+1, y, RIGHT); if(direction != RIGHT) labelling(x-1, y, LEFT); return 0; }
int imageClassifier::labelling(int x, int y, int direction) { // input: coordinate, int // direction, const int, LEFT, RIGHT, TOP, BOTTOM or NONE // output: set label to 'classImg' // return: 0 // // Recursive function. // Traverse white pixels and labell CvScalar currentPixel; // check 'x' and 'y' range if(x < 0 || y < 0 || x >= classImg->width || y >= classImg->height) return 0; // get current pixel's value currentPixel = cvGet2D(classImg, y, x); // if current pixel is black or already labelled, break if(currentPixel.val[0] != WHITE) return 0; // set label to current pixel currentPixel.val[0] = label; cvSet2D(classImg, y, x, currentPixel); // search near pixel if(direction != BOTTOM) labelling(x, y-1, TOP); if(direction != TOP) labelling(x, y+1, BOTTOM); if(direction != LEFT) labelling(x+1, y, RIGHT); if(direction != RIGHT) labelling(x-1, y, LEFT); return 0; }
int imageClassifier::classify(IplImage *src) { // input: binary image, IplImage, any bit depth, 1 channel // output: set classified image to 'classImg' // return: 0 // // Classify 'src' image by putting "labels" (1 to 0xff-1 (8bit image case) value, inclusive) to region in IplImage format. CvScalar currentPixel; int i, j; // clone src to classImg classImg = cvCloneImage(src); // search for(i=0; i<src->width; i++) for(j=0; j<src->height; j++) { currentPixel = cvGet2D(classImg, j, i); // if current pixel is white (not labeled pixel) if(currentPixel.val[0] == WHITE) { labelling(i, j, NONE); labelIncrement(); } } return 0; }
void Schema::GearGraphManip::labelling(Node &node) { if (node.visited) return; //tag the node has visited to avoid infinit recursion node.visited=true; //get depth fist dependent gears std::vector<Gear*> depGears; node.gear->getDependencies(depGears); //build the corresponding nodes vector std::vector<Node*> depNodes; bool found=false; for (unsigned int i=0;i<depGears.size();++i) { found=false; for (unsigned int j=0;j<_nodes.size() && !found;++j) { if (_nodes[j].gear == depGears[i]) { depNodes.push_back(&_nodes[j]); found=true; } } } //label depending nodes for (unsigned int i=0;i<depNodes.size();++i) labelling(*depNodes[i]); //assign order node.order=_depthFirstCounter; //inc global counter _depthFirstCounter++; }
/** * perform a topological sort of a cyclic graph * return vector of ordered gears * * @param orderedGears */ void Schema::GearGraphManip::getOrderedGears(std::list<Gear*>& orderedGears) { //reset _depthFirstCounter=0; for (unsigned int i=0;i<_nodes.size();++i) { _nodes[i].order=0; _nodes[i].visited=false; } for (unsigned int i=0;i<_nodes.size();++i) { if (!_nodes[i].visited) labelling(_nodes[i]); } //sort according to order std::sort(_nodes.begin(), _nodes.end(), compareNodes); //fill the ordered gears vector now orderedGears.clear(); for (unsigned int i=0;i<_nodes.size();++i) orderedGears.push_back(_nodes[i].gear); }
int imageClassifier::classify(IplImage *src) { CvScalar currentPixel; int i, j; // clone src to classImg classImg = cvCloneImage(src); // search for(i=0; i<src->width; i++) for(j=0; j<src->height; j++) { currentPixel = cvGet2D(classImg, j, i); // if current pixel is white (not labeled pixel) if(currentPixel.val[0] == WHITE) { labelling(i, j, NONE); labelIncrement(); } } return 0; }