0

I have been trying to read some images in a loop using imread into a vector of Mat (images) with opencv by getting the filenames from a vector of QString but it will not compile. I know it's the format of my QString that's the problem but cannot seem to work it out. I have also tried ...image = imread( image_vector[i] );... and several other ways. Any help is greatly appreciated.

for ( unsigned int i = 0; i < image_names.size(); i++ )
{ 
    image = imread( image_names.data()->at(i) );

    if( !image.data )
    {
        qDebug()<< "Error";
    }
    else
    {
         image_vector.push_back( image );
    }
}
omegaFlame
  • 245
  • 3
  • 9
  • 21
  • What is in `image_vector`? You seem to be both reading the filenames from it as well as adding cv matrices to it? – pezcode Jun 17 '14 at 15:26
  • Sorry, I put the wrong names, I have edited. – omegaFlame Jun 17 '14 at 15:32
  • 1
    try something like `image_names[i].toStdString().c_str()`. – Micka Jun 17 '14 at 15:37
  • if that's really a compile error, why don't you post the compiler error message? – Micka Jun 17 '14 at 15:38
  • 3
    The declaration of imread is: `CV_EXPORTS_W Mat imread( const string& filename, int flags=1 );` so, if `image_names` is an array or vector of QString, you should use: `image = imread( image_names[i].toStdString() );` – asclepix Jun 17 '14 at 15:49
  • Sure, the compile error is: `invalid initialization of reference of type 'const string& {aka const std::basic_string&}' from expression of type 'const QChar' image = imread( image_names.data()->at(i) );` – omegaFlame Jun 17 '14 at 15:50
  • Which is the definition of `image_names`? Is it a QVector or a QString (in this case `image_names.data()` return a QChar * pointer and `image_names.data()->at(i)` return a QChar)? – asclepix Jun 17 '14 at 16:01
  • It's declared as `vector image_names`. – omegaFlame Jun 17 '14 at 16:05
  • 1
    Right: `image_names.data()` returns a QString * pointer and `image_names.data()->at(i)` returns the ith QChar of the first QString of the vector. – asclepix Jun 18 '14 at 07:38

1 Answers1

2

For using imread you have to pass a std::string to it .

Mat imread( const std::string& filename, int flags=1 );

In your case image_names is vector<QString> so the correct code is :

image = imread( image_names[i].toStdString());
uchar
  • 2,552
  • 4
  • 29
  • 50