Problem using cv :: Imread


I am writing a code to read all the images in a folder and then write the information of the images in a binary all together. The problem is that when I use the function cv::imread the field .data of the cv::MAT always remains null, and when I'm going to write there is an error when accessing a null pointer. I am using absolute paths obtained from a file .txt in the following code:

void main(){
  const unsigned int SizeImg = 1024 * 768 * sizeof(char);
  std::ifstream file;"files.txt", std::ios::out);
  std::string line;
  std::string files[50];
  std::string imagen;
  std::ofstream Binary("Images.bin", std::ios::out | std::ios::binary);
  int n = 0;
  while (std::getline(file,line))
    std::cout << line << '\n';
    files[n] = line;
  for (int i = 0; i < n; i++){
    imagen = files[i];
    cv::Mat m = cv::imread(imagen, CV_LOAD_IMAGE_ANYCOLOR);
    //cv::imshow("ventana", m);
    Binary.write((char*), SizeImg);

I'm starting with OpenCV so there may be other things that are not very good either.

asked by Emilio Liaño 16.05.2017 в 13:37

1 answer


According to the documentation , I quote:


The function imread loads an image from the specified file and returns it. If the image can not be read (because of missing file, improper permissions, unsupported or invalid format), the function returns an empty matrix (Mat :: data == NULL).

The problem is that the function is not able to open the file, either because the name and / or the route are incorrect, for lack of permission or for trying to open an unsupported format.

I would start by printing the file path to see if the path is valid. At this point, if you want to use relative routes ... the working directory of the application does not have to be the same as the directory where the executable is located ...

answered by 16.05.2017 в 14:24