Prakruti Prakruti - 3 months ago 50
C++ Question

Bad flag error in OpenCV after throwing an instance of 'cv::Exception' . Unsupported array type in cvGetMat

I am new to OpenCV , and trying to read the sequence of images in a folder. My code is as follows :

#include <stdio.h>
#include <opencv2/opencv.hpp>
#include <cv.h>
#include <highgui.h>
#include <iostream>

using namespace cv;
using namespace std;


void loadImage(Mat image,int number)
{
char filename[100];
strcpy(filename, "/home/folder1/imagefolder/");

char frameNo[10];
sprintf(frameNo, "%08i", number);
strcat(filename, frameNo);
strcat(filename, ".png");
cout<< filename;

image = imread(filename);
if (image.empty()) //check whether the image is loaded or not
{
cout << "Error : Image not loaded." << endl;
}
}

int main()
{
Mat image;
int nImages=8;
for (int i = 1; i < nImages; i++)
{
loadImage(image,i);
namedWindow( "Display Image", CV_WINDOW_AUTOSIZE );
imshow( "Display Image", image );
waitKey(100000);
return 0;

}
}


It gets compiled, But on running the executable, it gives following error:

OpenCV Error: Bad flag (parameter or structure field) (Unrecognized or unsupported array type) in cvGetMat, file /home/vuadmin/OpenCV-2.3.1/modules/core/src/array.cpp, line 2482
terminate called after throwing an instance of 'cv::Exception'
what(): /home/vuadmin/OpenCV-2.3.1/modules/core/src/array.cpp:2482: error: (-206) Unrecognized or unsupported array type in function cvGetMat
Aborted (core dumped)

Please help me find out the error.

I also tried out the other method as given in
OpenCV: Reading image series from a folder

but in my case it shows only the first image .

Answer Source

I see several errors in your code :

your prototype void loadImage(Mat image,int number) makes a copy of the cv::Mat image, so after the call of this function, image is empty. You should set the image by reference

void loadImage(Mat& image,int number)

in this case in imshow( "Display Image", image ); image argument may not be empty. that may cause your openCV error

One other thing in your entry point of your program, you make a return 0; inside your loop, so the program exit with code value 0 on the first iteration. Put the return after the loop. Btw, you should "open" your window once, and not on each iteration, but i don't think this causes any error.

 int main()
    {
     Mat image;
     int nImages=8;
     namedWindow( "Display Image", CV_WINDOW_AUTOSIZE );
     for (int i = 1; i < nImages; i++)
      {
        loadImage(image,i);
        imshow( "Display Image", image );
        waitKey(100000);

      }

     return 0;
    }

You must check the validity of your image after your call of loadImage(image, i) because inside your function, a error message is printed on the standard output, but in your loop, you don't make any check on the image

loadImage(image, i);
if ( ! image.empty ())
    imshow( "Display Image", image );
waitKey(100000);