Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

assuming, 0 goes up, and clockwise order, like here:

image description

you can reconstruct the contours from a starting point:

Point next(const Point &p, uchar code) {
    int o[8*2] = {0,-1, 1,-1, 1,0, 1,1, 0,1, -1,1, -1,0, -1,-1};
    return Point(p.x+o[code*2], p.y+o[code*2+1]);
}
void reconstruct(const vector<uchar> &_chain, vector<Point> &contours, Point offset) {
    contours.push_back(offset);
    for (int i=0; i<_chain.size(); i++) {
        Point p = next(offset, _chain[i]);
        contours.push_back(p);
        offset = p;
    }
}

and then draw them like ordinary contours in opencv:

Mat draw(624,624,CV_8UC3,Scalar(0));
vector<vector<Point>> contours(1); // for drawContours
reconstruct(chain, contours[0], Point(10,300));
drawContours(draw, contours, -1, Scalar(0, 255, 0), 1);

image description

assuming, 0 goes up, and clockwise order, like here:

image description

you can reconstruct the contours from a starting point:

Point next(const Point &p, uchar code) {
    int o[8*2] = {0,-1, 1,-1, 1,0, 1,1, 0,1, -1,1, -1,0, -1,-1};
    return Point(p.x+o[code*2], p.y+o[code*2+1]);
}
void reconstruct(const vector<uchar> &_chain, vector<Point> &contours, Point offset) {
    contours.push_back(offset);
    for (int i=0; i<_chain.size(); i++) {
        Point p = next(offset, _chain[i]);
        contours.push_back(p);
        offset = p;
    }
}

and then draw them like ordinary contours in opencv:

vector<uchar> chain {2, 2, 2, 2, 2, 2, 2, 2, 7, 7, 7, 7, .... (+ ~1700points) ..}

Mat draw(624,624,CV_8UC3,Scalar(0));
vector<vector<Point>> contours(1); // for drawContours
reconstruct(chain, contours[0], Point(10,300));
drawContours(draw, contours, -1, Scalar(0, 255, 0), 1);

image description