Chain code and xml

asked 2017-05-21 06:48:25 -0600

Nuz gravatar image

updated 2017-05-21 09:30:51 -0600

Hello, i am having a problem when saving the xml file.. Only a single row is being showed in the xml..but, i want to retrieve several rows with valid values to represent the images found in the folder. Can someone please help me with this.

#include "opencv2/imgproc.hpp"
#include "opencv2/highgui.hpp"
#include "opencv2/imgproc/imgproc_c.h"
#include <iostream>
#include <opencv2/core.hpp>
#include <opencv2/opencv.hpp>
#include "opencv2/imgcodecs.hpp"
#include <opencv2/highgui.hpp>
#include <opencv2/ml.hpp>
  #include <fstream>
#include<string.h>

using namespace std;
using namespace cv;

vector<String> files;

int main() {


Mat image;
double totalCount = 0;

//Mat image = imread("C:/Users/Desktop/Outline.jpg");

cv::glob("C:/Users/Desktop/outline/*.jpg", files);
for (size_t i = 0; i < files.size(); i++) {
    image = imread(files[i]);


    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;


    // 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;

        FileStorage fs("freeman.xml", FileStorage::WRITE);
        fs << "chain" << prob;

}


    waitKey(0);

    return 0;

}

This is the xml file that i got: image description

This is a similar xml that i want as result: image description

edit retag flag offensive close merge delete

Comments

opencv version? may be you should update to 2.4.13 or 3.2

LBerger gravatar imageLBerger ( 2017-05-21 11:48:44 -0600 )edit

@LBerger version 3.0

Nuz gravatar imageNuz ( 2017-05-21 12:51:20 -0600 )edit

try something like this :

  cv::Mat hist(1, 8, CV_32F, Scalar(0));
  cv::Mat propglob;
   .......
   // print the raw histogram:
    cout << "Histo: " << hist << endl;
    cout << "Total: " << totalCount << endl;

    // normalize it:
    Mat prob = hist / totalCount;
    cout << "Proba: " << prob << endl;
    propglob.push_back(prob);

    }
    FileStorage fs("freeman.xml", FileStorage::WRITE);
    fs << "chain" << probglog;
LBerger gravatar imageLBerger ( 2017-05-21 13:16:02 -0600 )edit

@LBerger still the same output xml...is there something wrong in the code i provided? Don't know where is the problem

Nuz gravatar imageNuz ( 2017-05-21 13:31:59 -0600 )edit

this code is ok. Check it and insert in your code :

Mat prob;
for (int i=0;i<10;i++)
{
    cv::Mat hist(1, 8, CV_32F, Scalar(i));
    prob.push_back(hist);
}
FileStorage fff("test.yml",FileStorage::WRITE);
fff<<"e"<<prob;
fff.release();
LBerger gravatar imageLBerger ( 2017-05-21 13:39:10 -0600 )edit