Ask Your Question
0

Chain Code Histogram

asked 2016-03-26 00:45:00 -0600

bob409 gravatar image

I have already extracted chain code of the contour, calculated the number of occurence of each code. Then calculated the frequency of each code. Now I have to plot it in a histogram, I do not know how to create a histogram for chain code the no. of bins should be 8 thats all I know. But how to create it in opencv 3.0 using c++?

int main()
{
int count0 = 0;
int count1 = 0;
int count2 = 0;
int count3 = 0;
int count4 = 0;
int count5 = 0;
int count6 = 0;
int count7 = 0;

double totalCount = 0;

Mat image = imread("rectangle.jpg", 0);

Canny(image, image, 100, 100 * 2, 3, false);

CvChain* chain;
CvMemStorage* storage = 0;
storage = cvCreateMemStorage();

cvFindContours(&IplImage(image), storage, (CvSeq**)(&chain), sizeof(*chain), CV_RETR_EXTERNAL, CV_CHAIN_CODE);

int total = chain->total;


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

    int numChain = 0;

    CvSeqReader reader;
    int i, total = chain->total;

    cvStartReadSeq((CvSeq*)chain, &reader, 0);


    for (i = 0; i < total; i++)
    {
        char code;
        CV_READ_SEQ_ELEM(code, reader);
        int Fchain = (int)code;

        if (Fchain == 0)
        {
            count0++;
        }

        else if (Fchain == 1)
        {
            count1++;
        }

        else if (Fchain == 2)
        {
            count2++;
        }

        else if (Fchain == 3)
        {
            count3++;
        }

        else if (Fchain == 4)
        {
            count4++;
        }

        else if (Fchain == 5)
        {
            count5++;
        }

        else if (Fchain == 6)
        {
            count6++;
        }

        else if (Fchain == 7)
        {
            count7++;
        }

        totalCount++;
    }
}

cout << "0: " << count0 << endl;
cout << "1: " << count1 << endl;
cout << "2: " << count2 << endl;
cout << "3: " << count3 << endl;
cout << "4: " << count4 << endl;
cout << "5: " << count5 << endl;
cout << "6: " << count6 << endl;
cout << "7: " << count7 << endl;

cout << "Total: " << totalCount << endl;

double prob0 = (count0 / totalCount);
double prob1 = (count1 / totalCount);
double prob2 = (count2 / totalCount);
double prob3 = (count3 / totalCount);
double prob4 = (count4 / totalCount);
double prob5 = (count5 / totalCount);
double prob6 = (count6 / totalCount);
double prob7 = (count7 / totalCount);


cout << "Proababilty 0: " << prob0 << endl;
cout << "Proababilty 1: " << prob1 << endl;
cout << "Proababilty 2: " << prob2 << endl;
cout << "Proababilty 3: " << prob3 << endl;
cout << "Proababilty 4: " << prob4 << endl;
cout << "Proababilty 5: " << prob5 << endl;
cout << "Proababilty 6: " << prob6 << endl;
cout << "Proababilty 7: " << prob7 << endl;


waitKey(0);
return 0;

}

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
2

answered 2016-03-26 02:11:10 -0600

berak gravatar image

updated 2016-03-26 02:14:52 -0600

a histogram is basically just an array (we'll use a cv::Mat), so your loop boils down to:

// 1 row, 8 cols, filled with zeros, (float type, because we want to normalize later):
cv::Mat hist(1, 8, CV_32F, Scalar(0)); 

for (; chain != NULL; chain = (CvChain*)chain->h_next)
{   
    CvSeqReader reader;
    int i, total = chain->total;

    cvStartReadSeq((CvSeq*)chain, &reader, 0);

    for (i = 0; i < total; i++)
    {
        char code;
        CV_READ_SEQ_ELEM(code, reader);
        int Fchain = (int)code;

        // increase the counter for the respective bin:
        hist.at<float>(0,FChain) ++;

        totalCount++;
    }
}

// print the raw histogram:
cout << "Histo: " << hist << endl;
cout << "Total: " << totalCount << endl;

// normalize it:
Mat prob = hist / totalCount;
cout << "Proba: " << prob << endl;
edit flag offensive delete link more

Comments

Thanks. Which classification method is more appropriate for chain code histogram feature?

bob409 gravatar imagebob409 ( 2016-03-26 02:13:27 -0600 )edit

how many classes (different chains) are there ?

some classifiers(like Boost) are restricted to binary (2 class, yes/no or such) classification.

apart from that, you can use any classification you like. start with an SVM, MLP, or KNearest, you'll probably have to try out some

berak gravatar imageberak ( 2016-03-26 02:19:31 -0600 )edit

also (for simplicity): compareHist

berak gravatar imageberak ( 2016-03-26 02:28:35 -0600 )edit

12 different classes and each class has 10 instances.

bob409 gravatar imagebob409 ( 2016-03-26 02:54:35 -0600 )edit

"and each class has 10 instances" -- using more instances will probably improve it

berak gravatar imageberak ( 2016-03-26 03:03:31 -0600 )edit

I did not understand by what you want to say by improving it.

bob409 gravatar imagebob409 ( 2016-03-26 03:36:16 -0600 )edit

moretraining data usually improves the classification

berak gravatar imageberak ( 2016-03-26 03:42:32 -0600 )edit

Question Tools

2 followers

Stats

Asked: 2016-03-26 00:45:00 -0600

Seen: 447 times

Last updated: Mar 26 '16