LIFUGUAN
LIFUGUAN

Reputation: 73

OpenCV3.4.2 : Can not read file both imread and cvLoadImage

I wrote a little demo to test whether my OpenCV work or not. My computer contains version 3.4.2 and 3.4.0.

#include <iostream>
#include <opencv2/opencv.hpp>
using namespace std;
int main()
{
    
    cv::Mat image = cv::imread("map.jpg");
    cv::imshow("test", image);
    cv::waitKey(0);
    return EXIT_SUCCESS;
}

CMake file :

cmake_minimum_required(VERSION 3.0.0)
project(opencv_test VERSION 0.1.0)

include(CTest)
enable_testing()

find_package( OpenCV 3.4.2 REQUIRED )
add_executable(opencv_test main.cpp)
target_link_libraries(opencv_test ${OpenCV_LIBS} )

set(CPACK_PROJECT_NAME ${PROJECT_NAME})
set(CPACK_PROJECT_VERSION ${PROJECT_VERSION})
include(CPack)

Then it throw the exception like this:

 OpenCV(3.4.2) /home/robomaster/opencv/modules/highgui/src/window.cpp:356: error: (-215:Assertion failed) size.width>0 && size.height>0 in function 'imshow'

I have tried and checked the following methods:

  1. check the absolute and relative path and they are correct.
  2. check imread() and cvLoadImage() and the ouptut is the same.

And I have no idea how to fix it. Any help will be grateful!!

Upvotes: 0

Views: 259

Answers (1)

Sdra
Sdra

Reputation: 2347

As per OpenCV docs,

If the image cannot be read (because of missing file, improper permissions, unsupported or invalid format), the function returns an empty matrix ( Mat::data==NULL )

which means that after cv::imread you should check:

cv::Mat image = cv::imread("map.jpg");
if( image.data == nullptr )
  return 1; // EXIT_ERROR

Again, as per the docs, possible reasons for failure are:

  • missing file
  • improper permissions
  • unsupported or invalid format

Possible solutions:

  • Use full path, check file existence
  • chmod +w map.jpg
  • Change image format

Upvotes: 1

Related Questions