Ask Your Question

Revision history [back]

Unable to generate Freeman chain code using OpenCv

I have some 77 vertices data.I am trying to generate freeman chain code for this contour using OpenCv in C++.

I have written the following function.This function basically takes the x and y values as inputs and generates a freeman chain code.

void GenerateFreemanChainCode(std::vector<double> X, std::vector<double> Y, std::vector<char> &freemanChainCode)
{
    cv::Mat img = cv::Mat::zeros(2, 2, CV_8UC1);
    for (int Idx = 0; Idx < X.size()-1; Idx++)
    {
        cv::line(img, Point(X.at(Idx), Y.at(Idx)), Point(X.at(Idx+1), Y.at(Idx+1)), cv::Scalar(255), 1);
    }

    imshow("Test", img);

    vector<vector<Point> > contours;

    findContours(img, contours, RETR_EXTERNAL, CV_CHAIN_CODE);
    //cout << Mat(contours[0]) << endl;

    findContours(img, contours, RETR_EXTERNAL, CHAIN_APPROX_SIMPLE);
    //cout << "CHAIN_APPROX_SIMPLE" << endl;
    //cout << Mat(contours[0]) << endl;

    CvChain* chain = 0;
    CvMemStorage* storage = 0;
    storage = cvCreateMemStorage(0);
    cvFindContours(&IplImage(img), storage, (CvSeq**)(&chain), sizeof(*chain), CV_RETR_EXTERNAL, CV_CHAIN_CODE);

    for (; chain != NULL; chain = (CvChain*)chain->h_next)
    {
        //chain=(CvChain*)chain ->h_next; 
        //if(chain==NULL){break;}
        CvSeqReader reader;
        int i, total = chain->total;
        cvStartReadSeq((CvSeq*)chain, &reader, 0);
        //printf("--------------------chain\n");

        for (i = 0; i<total; i++)
        {
            char code;
            CV_READ_SEQ_ELEM(code, reader);
            //printf("%d", code);
            freemanChainCode.push_back(code);
        }
    }
}

My vertices data looks as follows(X,Y Data seperate by space):

0 0
0 0.00144928
0.00144928 0.00144928
0.00289855 0.00144928
0.00724638 0.00434783
0.015942 0.00434783
0.0275362 0.00434783
0.0434783 0.00434783
0.0637681 0.00434783
0.0782609 0.00434783
0.0869565 0.00434783
0.102899 0.0057971
0.111594 0.0057971
0.126087 0.0057971
0.136232 0.0057971
0.15942 0.0057971
0.172464 0.0057971
0.182609 0.00434783
0.198551 0.00434783
0.214493 0.00434783
0.23913 0.00434783
0.250725 0.00434783
0.269565 0.00434783
0.284058 0.00434783
0.305797 0.00434783
0.331884 0.00434783
0.344928 0.00434783
0.371014 0.0057971
0.386957 0.0057971
0.402899 0.00724638
0.423188 0.00724638
0.436232 0.00724638
0.45942 0.00724638
0.47971 0.00724638
0.5 0.00724638
0.513043 0.00724638
0.524638 0.00724638
0.553623 0.00724638
0.578261 0.00724638
0.592754 0.00724638
0.602899 0.00724638
0.62029 0.00724638
0.634783 0.00724638
0.646377 0.00724638
0.672464 0.00724638
0.686957 0.00724638
0.701449 0.00724638
0.718841 0.00724638
0.737681 0.00724638
0.763768 0.00724638
0.773913 0.00724638
0.797101 0.00724638
0.817391 0.00724638
0.824638 0.00724638
0.826087 0.00724638
0.831884 0.00724638
0.846377 0.00724638
0.862319 0.00724638
0.886957 0.00724638
0.895652 0.00724638
0.902899 0.00724638
0.917391 0.00724638
0.928986 0.00724638
0.949275 0.00724638
0.965217 0.00724638
0.997101 0.00869565
1 0.00869565

I would be really glad,if someone can help me ,ideintify the mistake. Thanks in advance.