Ask Your Question
1

reading csv file in opencv

asked 2015-02-14 03:57:16 -0600

Mahavir gravatar image

updated 2015-02-14 03:58:09 -0600

Hello everyone I want to write code in opencv for reading csv file in opencv. I have csv file(which i directly got from MS excel) and I want to read it and store it in a array. My csv files has 2 rows and 108 colums. How to do that? Thanks in advance!!!

            #include <opencv2\highgui\highgui.hpp>
            #include <opencv2\core\core.hpp>
            #include <opencv2\core\mat.hpp>
            #include <opencv2\imgproc\imgproc.hpp>
            #include<opencv2\features2d\features2d.hpp>
            #include<iostream>
            #include<math.h>
            #include<conio.h>
            #include<fstream>
            #include<sstream>
            using namespace std;
            using namespace cv;
            char filename[80];

             int main()
            {
                ifstream reader;
                Mat vect=Mat::zeros(2,108,CV_8U);
                reader.open("periods of arnold transform.csv");
                //What to do from this onwards???
                return 0;
            }
edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
2

answered 2015-02-14 04:37:01 -0600

updated 2015-02-14 06:36:20 -0600

OpenCV does not contain a builtin file reading system. Just use the C++ interface. Starting from the idea that you are using the ifstream interface and that each row is a new line and the colums are stured by a seperator which is a comma. this code can work for you.

ifstream inputfle("periods of arnold transform.csv");
string current_line;
// vector allows you to add data without knowing the exact size beforehand
vector< vector<int> > all_data;
// Start reading lines as long as there are lines in the file
while(getline(inputfile, current_line)){
   // Now inside each line we need to seperate the cols
   vector<int> values;
   stringstream temp(current_line);
   string single_value;
   while(getline(temp,single_value,',')){
        // convert the string element to a integer value
        values.push_back(atoi(single_value.c_str()));
   }
   // add the row to the complete data vector
   all_data.push_back(values);
}

// Now add all the data into a Mat element
Mat vect = Mat::zeros((int)all_data.size(), (int)all_data[0].size(), CV_8UC1);
// Loop over vectors and add the data
for(int rows = 0; row < (int)all_data.size(); rows++){
   for(int cols= 0; cols< (int)all_data[0].size(); cols++){
      vect.at<uchar>(rows,cols) = all_data[rows][cols];
   }
}
edit flag offensive delete link more

Comments

@ steven when i ran above code in vc2010 it is showing error for while(getline(current_line,single_value,',')) and error is no instance of overloaded function getline matches argument list.

Mahavir gravatar imageMahavir ( 2015-02-14 06:22:20 -0600 )edit

That is because you will need to add the proper includes for this to work, because the function does exist.

StevenPuttemans gravatar imageStevenPuttemans ( 2015-02-14 06:25:54 -0600 )edit

O I made an error! it should be temp instead of current_line. Adapted the code!

StevenPuttemans gravatar imageStevenPuttemans ( 2015-02-14 06:36:08 -0600 )edit
1

@ steven Thank you...

Mahavir gravatar imageMahavir ( 2015-02-14 12:56:18 -0600 )edit

if the data in a cell is more than 255 how can i read and store it?

damiya14 gravatar imagedamiya14 ( 2016-06-15 01:37:41 -0600 )edit

you will have to increase the size of the matrix

StevenPuttemans gravatar imageStevenPuttemans ( 2016-06-19 15:30:06 -0600 )edit

i have float/double values in my csv. How can i read them??

damiya14 gravatar imagedamiya14 ( 2016-09-30 02:13:59 -0600 )edit

Same way, you simply need to define a seperator from your file, then read in the values and change the type of your matrix in which you will store them.

StevenPuttemans gravatar imageStevenPuttemans ( 2016-09-30 02:44:18 -0600 )edit

thanks alot!!

damiya14 gravatar imagedamiya14 ( 2016-09-30 04:09:42 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2015-02-14 03:57:16 -0600

Seen: 11,229 times

Last updated: Feb 14 '15