OpenCV 3.0 - How can I create a cv::Mat from CSV s

2019-02-24 03:47发布

问题:

I'm trying to create a cv::Mat from a CSV string that I pipe from a python-script. I'm currently using python 2.7, C++ and OpenCV 3.0, and all I can find is how to do it in OpenCV 2.4, but the problem is that it differs a lot from 3.0 where the ml::TrainData somehow is used instead. I don't understand how it works, and theres no example code! :-(

What my python-script does is that it pipes the np.array as CSV, it looks like this:

import sys
import numpy as np
import csv


csvToSend = csv.writer(sys.stdout, delimiter=',')

data = np.array([ [3, 7, 9],
               [1, 7, 0],
               [4, 12, 8]])

csvToSend.writerows(data)

The python-script is currently simulating a camera sending picture in bgr RAW format.

The piped data is received by my C++ code, this is how the code looks for the moment:

#include <iostream>
#include <string>
#include <opencv2/core/core.hpp>

using namespace std;
using namespace cv;

string values = "", csvPart;
Mat img;
int i;

int main()
{
    for (i = 0; i < 3; i++)
    {

        cin >> csvPart; //here I get my CSV lines from python-script

        if (values == "") values = values + csvPart;

        else values = values + "\n" + csvPart;  //makes the CVS lines look like a 3x3 matrix again
    }

    cout << values << endl; //prints the CVS to see that it looks like it did before piping it over to the C++ code

    /* how the printed string looks like
    3,7,9 
    1,7,0
    4,12,8    */

/*      Here I want to load the "values" into the Mat img
        so I can do my mojo with openCV in C++          */
}

It would be much appreciated if someone could help me out with this and give me a simple code example. If there is a better way of piping the np.array over to the C++ code and load it into a cv::Mat my arms are open for you! ;-)

回答1:

Each row of the csv file represent a line. If your image is 1 channel each value is the grayscale value for the pixel:

1,2,3 
4,5,6 
7,8,9 

The resulting 3x3 1 channel image is:

If your image is 3 channel, 3 consecutive values in each line represent a bgr triplet. For example this:

1,2,3,200,100,200
4,5,6,47,205,103
7,8,9,95,23,209

results in a 3x2 3 channel image:

The loadFromCSV function accepts the string of the csv content, and the OpenCV image type, and returns the image:

Mat loadFromCSV(const string& values, int opencv_type);

Take a look at the full code:

#include <opencv2\opencv.hpp>
#include <string>
#include <sstream>
#include <iostream>
using namespace std;
using namespace cv;

Mat loadFromCSV(const string& values, int opencv_type)
{
    Mat m;

    stringstream ss(values);
    string line;
    while (getline(ss, line))
    {
        vector<double> dvals;

        stringstream ssline(line);
        string val;
        while (getline(ssline, val, ','))
        {
            dvals.push_back(stod(val));
        }

        Mat mline(dvals, true);
        transpose(mline, mline);

        m.push_back(mline);
    }

    int ch = CV_MAT_CN(opencv_type);

    m = m.reshape(ch);
    m.convertTo(m, opencv_type);
    return m;
}

int main()
{
    string values = "1,2,3\n4,5,6\n7,8,9";
    Mat img = loadFromCSV(values, CV_8UC1);
    //Mat img = loadFromCSV(values, CV_8UC3);

    //string values = "1,2,3,200,100,200\n4,5,6,47,205,103\n7,8,9,95,23,209";
    //Mat img = loadFromCSV(values, CV_8UC3);

    cout << "Img:" << endl;
    cout << img << endl;

    imshow("Img", img);
    waitKey();

    return 0;
}