Ask Your Question
0

Vector of int to Mat

asked 2016-04-04 08:05:26 -0500

HugoM gravatar image

updated 2016-04-04 08:38:10 -0500

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.

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> 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(48,48,CV_8UC1);

Anyone has an idea?

edit retag flag offensive close merge delete

Comments

  • can you give us a bit more context (code) ?

  • why do you have a vector<int> in the 1st place ?

berak gravatar imageberak ( 2016-04-04 08:14:48 -0500 )edit

As you asked I gave more code.

HugoM gravatar imageHugoM ( 2016-04-04 08:39:11 -0500 )edit
1

please try with a vector<uchar>, not a vector<int>

berak gravatar imageberak ( 2016-04-04 08:44:07 -0500 )edit

I tried to use vector<uchar> but doesn't work

HugoM gravatar imageHugoM ( 2016-04-05 03:23:19 -0500 )edit

"but doesn't work" is never helpful. don't be that lame. if there are errors, we need to see them.

berak gravatar imageberak ( 2016-04-05 03:41:42 -0500 )edit

When I try to run it with debugger, I using the Visual Studio 2013, I get this error "The application was unable to start correctly (0xc000007b)". Apparently I have the include and lib correctly linked. btw thanks for spend time with me

HugoM gravatar imageHugoM ( 2016-04-05 04:00:35 -0500 )edit
1

"Apparently I have the include and lib correctly linked" -- please doubl check again. also, make sure, you got the path to the resp. set of dlls in your system-wide PATH env var.

berak gravatar imageberak ( 2016-04-05 04:09:34 -0500 )edit

is it this ?

berak gravatar imageberak ( 2016-04-05 04:29:13 -0500 )edit

No, it is this.

HugoM gravatar imageHugoM ( 2016-04-05 04:40:09 -0500 )edit

1 answer

Sort by ยป oldest newest most voted
2

answered 2016-04-05 04:31:32 -0500

berak gravatar image

updated 2016-04-05 04:42:47 -0500

if your data is from https://www.kaggle.com/c/facial-keypo..., then rather try like this:

(avoid all string conversions in the 1st place)

#include <iostream>
#include <fstream>
using namespace std;

#include <opencv2/opencv.hpp>
using namespace cv;

int main(int argc, char **argv)
{
    const int imgsize = 96; // not 48, see: https://www.kaggle.com/c/facial-keypoints-detection/data?test.zip

    vector<int> ids;
    vector<Mat> imgs;

    ifstream in("test.csv");
    // skip 1st row:
    string dummy;
    getline(in, dummy);

    for(int i=0; i<1783; i++)
    {
        // read id:
        int id;
        in >> id;
        ids.push_back(id);

        // skip the bloody comma ;(
        char comma;
        in >> comma;

        // read pixels into a flat column:
        Mat img;
        for (int i=0; i<imgsize*imgsize; i++)
        {
            int pixel;
            in >> pixel;
            img.push_back(uchar(pixel));
        }
        // reshape to quad again:
        img = img.reshape(1,imgsize);
        imgs.push_back(img);

        cerr << id << endl;
        imshow("face", img);
        waitKey(2);
    }
    return 0;
}
edit flag offensive delete link more

Comments

This code is in OpenCv 3.X, right?

HugoM gravatar imageHugoM ( 2016-04-05 04:50:53 -0500 )edit
1

yes, but (apart from the changed headers) , it should run fine on 2.4, too.

berak gravatar imageberak ( 2016-04-05 04:52:23 -0500 )edit

Thank you berak, this works very well. Today I got some time to test .

HugoM gravatar imageHugoM ( 2016-04-14 15:03:02 -0500 )edit
Login/Signup to Answer

Question Tools

1 follower

Stats

Asked: 2016-04-04 08:05:26 -0500

Seen: 286 times

Last updated: Apr 05 '16