CSV file read and write under OpenCV

Source: Internet
Author: User
Tags strtok

1. Introduction to CSV file format

comma-separated values (comma-separatedvalues,csv, sometimes referred to as character-delimited values, because delimited characters can also be not commas), whose files store tabular data (numbers and text) in plain text. Plain text means that the file is a sequence of characters and does not contain data that must be interpreted like a binary number. A CSV file consists of any number of records separated by a newline character, each record consists of a field, and the delimiter between the fields is another character or string, most commonly a comma or tab. Typically, all records have exactly the same sequence of fields.

The Common Criteria for the CSV file format do not exist, but there is a basic description in RFC 4180. The character encoding used is also not specified, but 7-BITASCII is the most basic generic encoding.

in general, the CSV file format rules are as follows:

1. The opening is not left blank to the behavior unit.

2. May contain or does not contain the column name, contains the column name to reside in the document first line.

3. Row of data does not span rows, no blank lines.

4. A comma (that is,) is used as a delimiter, and the column is empty to express its existence.

5. The contents of the column, such as the presence of a comma (that is,) enclose the field value in a half-width double quotation mark ("").

6. The column contents, such as the presence of a half-width quotation mark (""), should be replaced with a half-width double quotation mark ("") escaped, and the field value is enclosed with a half-width quotation mark ("").

7. The file reads and writes quotes, the comma operation rule is mutually inverse.

8. The internal code format is unlimited, can be ASCII, Unicode or other.

9. Special characters are not supported

2. MAT matrix data is stored in a CSV file

Blog "C + + read and write CSV file" proposed a CSV file read traversal algorithm and write algorithm,http://www.cnblogs.com/snake-hand/p/3170483.html, and the use of C + + It realizes the read and write operation to the CSV file.

In this paper, the "store mat matrix data to CSV file" function is more cleverly implemented with OPENCV CSV-style formatted output and stream buffer redirection .

The code is as follows:

Save cout Stream buffer pointer streambuf *coutbuf = COUT.RDBUF (); FStream matdata ("E:\\test\\data\\fire_2.csv", Ios::out|ios::trunc); if (!matdata) {cerr<< "File Open or create error!" <<endl;exit (1);} Gets the stream buffer pointer to the file fire.csv streambuf *filebuf = Matdata.rdbuf ();//sets the cout stream buffer pointer to the stream buffer pointer of the file Cout.rdbuf (filebuf);cout< <format (Svmmat, "CSV"); Matdata.flush (); Matdata.close ();//restore cout original stream buffer pointer cout.rdbuf (COUTBUF);

Test results:


3. Reading data from a CSV file to the mat matrix

The following code, for my own read_csv function, implements the ability to read data from a CSV file into the mat matrix.

/*** function function: Extract CSV file data into Mat type Matrix * Input: filepath file path array pointer; img_size mat type Data size;img_type mat type data type (32FC1) * return value: Mat matrix */mat Read_csv (const char *filepath, Size img_size, int img_type) {Mat image;image.create (img_size,img_type); string pixel;  Ifstream file (filepath, ifstream::in), if (!file) cout << "CSV read fail" << endl;int nl= image.rows; Number of lines int nc= image.cols;     Number of columns int eolelem = image.cols-1;//subscript of the last element of each line int elemcount = 0;if (image.iscontinuous ()) {nc= nc*nl;  Then no padded pixels nl= 1;  It is now a 1D array} for (int i = 0; i<nl; i++) {float* data = (float*) image.ptr<ushort> (i); for (int j = 0; J < NC; J + +) {if (Elemcount = = Eolelem) {getline (file,pixel, ' \ n ');//arbitrarily read in, until the Delim character ' \ n ' is read, the Delim character is not put into B Uffer Data[j] = (float) atof (Pixel.c_str ());//Convert the string str to a double value and return the result Elemcount = 0;//counter 0}else{getline (File,pixel, ' , ');//arbitrarily read into the Delim character ', ' the ' Delim character will not be put into buffer data[j] = (float) atof (Pixel.c_str ());//Convert the string str to a double value and return the result Elemcount++;}} }return image;}
Test Results:


Precautions:

Note that each record ends with ' \ n ' and not ', ' so special handling is required.

OpenCV implementation of:

when I called the read_csv function, I found that OpenCV already had functions that implemented similar functions: intcvmldata::read_csv (const char* filename). using CMake to compile OpenCV, click the right mouse button to go to the definition can easily view OpenCV source code. (see the link below for details)http://blog.csdn.net/solomon1558/article/details/43780533

Implement one:

Routines fisherfaces in OpenCV read_csv functions

static void Read_csv (const string& filename, vector<mat>&images, vector<int>& labels, char separator = '; ') {    Std::ifstream file (Filename.c_str (), ifstream::in);    if (!file) {        string error_message = "No valid input file is given, please check thegiven filename.";        Cv_error (Cv_stsbadarg, error_message);    }    String line, path, Classlabel;    while (getline (file, line)) {        StringStream-liness (line);        Getline (liness, path, separator);        Getline (liness, Classlabel);        if (!path.empty () &&!classlabel.empty ()) {            images.push_back (imread (path, 0));           Labels.push_back (Atoi (Classlabel.c_str ()));}}}    

Implementation two: int cvmldata::read_csv (const char* filename)

int cvmldata::read_csv (constchar* filename) {const int M = 1000000;    const char Str_delimiter[3] = {', ' delimiter '};    file* file = 0;    cvmemstorage* storage;    cvseq* seq;    Char *ptr;    Float*el_ptr;    Cvseqreader reader;    Intcols_count = 0;    Uchar *var_types_ptr = 0;    Clear ();    FILE = fopen (filename, "RT");    if (!file) return-1;    Read the Firstline and determine the number of variables Std::vector<char>_buf (M);    char* buf =&_buf[0];        if (!fgets_chomp (buf, M, file)) {fclose (file);    return-1;    } ptr = buf;    while (*ptr== ') ptr++;    for (; *ptr!= ');)            {if (*ptr== delimiter | | *ptr = = ") {cols_count++;            ptr++;        while (*ptr = = ") ptr++;    } else ptr++;    } cols_count++;        if (Cols_count = = 0) {fclose (file);    return-1; }//Createtemporary memory storage to store the whole database El_ptr = Newfloat[cols_count];    Storage = Cvcreatememstorage ();    Seq = cvcreateseq (0, sizeof (*SEQ), cols_count*sizeof (float), storage);    Var_types = Cvcreatemat (1, cols_count,cv_8u);    Cvzero (var_types);    Var_types_ptr = var_types->data.ptr;    for (;;)        {Char*token = NULL;        Inttype;        token = Strtok (buf, Str_delimiter);        if (!token) break;            for (int i = 0; i < cols_count-1; i++) {Str_to_flt_elem (token, el_ptr[i],type);            Var_types_ptr[i] |= type;            token = strtok (null,str_delimiter);                if (!token) {fclose (file);                Delete[] el_ptr;            return-1;        }} str_to_flt_elem (Token,el_ptr[cols_count-1], type);        VAR_TYPES_PTR[COLS_COUNT-1] |= type;        Cvseqpush (seq, el_ptr);    if (!fgets_chomp (buf, M, file)) break;    } fclose (file); Values = Cvcreatemat (Seq->total,cols_count,CV_32FC1);    Missing = Cvcreatemat (Seq->total,cols_count, cv_8u);    Var_idx_mask = Cvcreatemat (1,values->cols, CV_8UC1);    Cvset (Var_idx_mask, Cvrealscalar (1));     Train_sample_count = seq->total;    Cvstartreadseq (seq, &reader);        for (int i = 0; i < seq->total; i++) {const float* sdata = (float*) reader.ptr;        Float*ddata = Values->data.fl + cols_count*i;        uchar* dm = Missing->data.ptr +cols_count*i;            for (int j = 0; J < Cols_count; J + +) {Ddata[j] = sdata[j];        DM[J] = (Fabs (miss_val-sdata[j]) <= Flt_epsilon);    } cv_next_seq_elem (Seq->elem_size,reader);    } if (Cvnorm (missing, 0, CV_L1) <= Flt_epsilon) Cvreleasemat (&missing);    Cvreleasememstorage (&storage);    Delete[]el_ptr; return 0;}


CSV file read and write under OpenCV

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.