Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Vector of int to Mat

Hello, I am new at OpenCV.

I'm trying to convert a vector of int to Mat but the program always crash. I read from a file the first line that contains a number and the image's pixels, the data is saved in dadosString that is a vector<string>. Next I use the method StringToIn to convert the all the strings to int, so far everything works, but when I try to create a Mat it crash.

vector<int> dataInt = StringToInt(dadosString[1]); // #1 line

Mat M = Mat(dataInt); // #2 line

Anyone has an idea?

Vector of int to Mat

Hello, I am new at OpenCV.

OpenCV. I'm trying to convert a vector of int to Mat but the program always crash. I read from a file the first line that contains a number and the image's pixels, the data is saved in dadosString that is a vector<string>. Next I use the method StringToIn to convert the all the strings to int, so far everything works, but when I try to create a Mat it crash.

File: 6, 60 16 53 31 25 26 25 71 84 80 89 99 106 117 124 135 144 154 159 166 178 183 189 194 196 199 ...

      6 - is the number of image

      The rest o the numbers are the image  48x48.

Split the string read from file:

vector<string> splitString(const string& line, const string& symbol) {
vector<string> sliceString;
size_t start = 0;
size_t end = 0;
while (start != string::npos && end != string::npos) {
    start = line.find_first_not_of(symbol, end);
    if (start != string::npos) {
        end = line.find_first_of(symbol, start);
        if (end != string::npos) {
            sliceString.push_back(line.substr(start, end - start));
        }
        else {
            sliceString.push_back(line.substr(start));
        }
    }
}
return sliceString;

Convert the string into vector<int>:

vector<int> dataInt = StringToInt(dadosString[1]); // #1 line
StringToInt(string& dados1){
vector<string> dados = splitString(dados1, " ");
vector<int> dadosInt(dados.size());
int i;
for (i = 0; i < dados.size(); i++){
    dadosInt[i] = atoi(dados[i].c_str());
}
return dadosInt;

The main:

ifstream infile;
string read_file_name("D:\\datasets\\kaggle_facial_expression\\treino.csv");
infile.open(read_file_name);
string sLine;
getline(infile, sLine);
infile.close();

vector<string> dadosString = splitString(sLine,",");
vector<int> dadosInt = StringToInt(dadosString[1]);


Mat M = Mat(dataInt); // #2 line
Mat(48,48,CV_8UC1);

Anyone has an idea?