Ask Your Question
0

Is there something missing in the chain code?

asked 2017-05-16 12:07:14 -0500

Nuz gravatar image

I am not able to read the image and how can i retrieve the xml file and save it? Can someone help..

#include "opencv2/imgproc.hpp"
#include "opencv2/highgui.hpp"
#include "opencv2/imgproc/imgproc_c.h"
#include <iostream>

using namespace std;
using namespace cv;

int main() {

Mat img = imread("Outline.jpg"); //outline of foot


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_TREE, 
CV_CHAIN_CODE);



for (; chain != NULL; chain = (CvChain*)chain->h_next)
{

    CvSeqReader reader;
    int i, total = chain->total;
    cvStartReadSeq((CvSeq*)chain, &reader, 0);
    cout<<"--------------------chain\n";

    for (i = 0; i<total; i++)
    {
        char code;
        CV_READ_SEQ_ELEM(code, reader);
        cout<<"%d"<<code;
    }
}



waitKey(0);

return 0;
    }
edit retag flag offensive close merge delete

Comments

opencv version? 1.0 or 2.4.9 3.2.0-dev?

LBerger gravatar imageLBerger ( 2017-05-16 12:53:09 -0500 )edit

@LBerger 2.4.9

Nuz gravatar imageNuz ( 2017-05-16 13:25:11 -0500 )edit

Sorry it's too old for me. You have an example here and filestorage doc is here.

LBerger gravatar imageLBerger ( 2017-05-16 13:48:13 -0500 )edit

1 answer

Sort by ยป oldest newest most voted
0

answered 2017-05-18 00:22:57 -0500

berak gravatar image

you MUST avoid using opencv's dead C api, support for that ended in 2010 already.

maybe it's easier to derivive the freeman chain from the contours instead:

uchar encode(const Point &a, const Point &b) {
    uchar up    = (a.y > b.y);
    uchar left  = (a.x > b.x);
    uchar down  = (a.y < b.y);
    uchar right = (a.x < b.x);
    uchar equx  = (a.y == b.y);
    uchar equy  = (a.x == b.x);

    return (up    && equy)  ? 0 : // N
           (up    && right) ? 1 : // NE
           (right && equx)  ? 2 : // E
           (down  && right) ? 3 : // SE
           (down  && equy)  ? 4 : // S
           (left  && down)  ? 5 : // SW
           (left  && equx)  ? 6 : // W
                              7 ; // NW
}

// forward pass
void chain(const vector<Point> &contours, vector<uchar> &_chain) {
    int i=0;
    for (; i<contours.size()-1; i++) {
        _chain.push_back(encode(contours[i],contours[i+1]));
    }
    _chain.push_back(encode(contours[i],contours[0]));
}

findContours(img, contours, RETR_EXTERNAL, CHAIN_APPROX_NONE); // "dense" contour
vector<uchar> chaincode;
chain(contours, chaincode);

FileStorage fs("freeman.yml", 1);
fs << "chain" << chaincode; // vector<uchar> is nicely supported !
edit flag offensive delete link more
Login/Signup to Answer

Question Tools

2 followers

Stats

Asked: 2017-05-16 12:07:14 -0500

Seen: 59 times

Last updated: May 18