Diagnosing imread failure

imread() is failing silently on my system. The returned image is always empty().

I am an absolute beginner with OpenCV.

My system is:
Windows 10
Visual Studio 2017
OpenCV 4.5.1

I am building against: ...OpenCV_4_5_1\opencv\build\x64\vc15\lib\opencv_world451.lib and
...OpenCV_4_5_1\opencv\build\x64\vc15\bin\opencv_world451.dll .

I have tried the obvious:

  • The images are ordinary 1054 x 1004 pixel .bmp, .png or .jpg files. They open normally in other Windows programs.

  • My program can open the files with <fstream>, so it is not a path or access issue.

#include <opencv2/core.hpp>
#include <opencv2/imgcodecs.hpp>
#include <opencv2/highgui.hpp>
#include <iostream>
#include <fstream>

int main(int argc, char** argv)
{
	cv::String sFilename = cv::String(argv[1]);

	std::cout << "Image to display is: " << sFilename.c_str() << std::endl;

	// check that the file can be opened.
	std::fstream fsIn(sFilename.c_str(), std::fstream::in);
	if( fsIn.is_open() )
	{
		std::cout << " Opened: '" << argv[1] << "'" << std::endl;
		fsIn.close();
	}
	else {
		std::cout << " Couldn't open: '" << argv[1] << "'" << std::endl;
	}
		
	
	// read the image in OpenCV
	cv::Mat image;
	image = cv::imread( sFilename ); // Read the file
	if (image.empty()) // Check for invalid input
	{
		std::cout << "imread() failed to open the image." << std::endl;
		return -1;
	}
	cv::namedWindow("Display window", cv::WINDOW_AUTOSIZE); // Create a window for display.
	cv::imshow("Display window", image); // Show our image inside it.
	cv::waitKey(0); // Wait for a keystroke in the window
	return 0;
} 

I think you need to load and link imgcodecs library. Look at class definition for opencv imreadā€¦

Thanks, visualbill. I will look into that.

can you add in your program

cout << cv::getBuildInformation()<< endl;

and show us result
and what does it mean ā€œimread() is failing silently on my systemā€?

Do you run your program in windows command or you double click on icon?

no you donā€™t have to do that. if you link against world, that contains everything. if you didnā€™t, you would have to make sure to link imgcodecs

1 Like

Hello Laurent and other commentators,

Thank you very much your help

I am running the program in Visual Studioā€™s debugger.

To my utter astonishment, simply adding `cout << cv::getBuildInformation()<< endl;ā€™ made the program work.

Ah! The other thing I did (and thought Iā€™d tested) was to ensure the Debug build had
just `opencv_world451d.libā€™ in the Additional Dependencies and the Release build had just the non-debug include library. Letā€™s see if I can break it againā€¦ Yup, Can do.

Got it!
I must have had both opencv_world451.lib and opencv_world451**d**.lib in the Additional Dependencies. Silly me. How curious that the effect was that ā€˜imread()ā€™ failed although the program built and linked OK.

For reference, the console output is now:

Image to display is: C:\Users\[my_username]\Pictures\DSC_6455_d4.bmp
 Opened: 'C:\Users\[my_username]\Pictures\DSC_6455_d4.bmp'

General configuration for OpenCV 4.5.1 =====================================
  Version control:               4.5.1

  Platform:
    Timestamp:                   2020-12-22T00:43:18Z
    Host:                        Windows 10.0.19041 AMD64
    CMake:                       3.16.4
    CMake generator:             Visual Studio 15 2017
    CMake build tool:            C:/Program Files (x86)/Microsoft Visual Studio/2017/Professional/MSBuild/15.0/Bin/MSBuild.exe
    MSVC:                        1916

  CPU/HW features:
    Baseline:                    SSE SSE2 SSE3
      requested:                 SSE3
    Dispatched code generation:  SSE4_1 SSE4_2 FP16 AVX AVX2 AVX512_SKX
      requested:                 SSE4_1 SSE4_2 AVX FP16 AVX2 AVX512_SKX
      SSE4_1 (15 files):         + SSSE3 SSE4_1
      SSE4_2 (1 files):          + SSSE3 SSE4_1 POPCNT SSE4_2
      FP16 (0 files):            + SSSE3 SSE4_1 POPCNT SSE4_2 FP16 AVX
      AVX (4 files):             + SSSE3 SSE4_1 POPCNT SSE4_2 AVX
      AVX2 (29 files):           + SSSE3 SSE4_1 POPCNT SSE4_2 FP16 FMA3 AVX AVX2
      AVX512_SKX (4 files):      + SSSE3 SSE4_1 POPCNT SSE4_2 FP16 FMA3 AVX AVX2 AVX_512F AVX512_COMMON AVX512_SKX

  C/C++:
    Built as dynamic libs?:      YES
    C++ standard:                11
    C++ Compiler:                C:/Program Files (x86)/Microsoft Visual Studio/2017/Professional/VC/Tools/MSVC/14.16.27023/bin/Hostx86/x64/cl.exe  (ver 19.16.27042.0)
    C++ flags (Release):         /DWIN32 /D_WINDOWS /W4 /GR  /D _CRT_SECURE_NO_DEPRECATE /D _CRT_NONSTDC_NO_DEPRECATE /D _SCL_SECURE_NO_WARNINGS /Gy /bigobj /Oi  /fp:precise     /EHa /wd4127 /wd4251 /wd4324 /wd4275 /wd4512 /wd4589 /MP2  /MD /O2 /Ob2 /DNDEBUG
    C++ flags (Debug):           /DWIN32 /D_WINDOWS /W4 /GR  /D _CRT_SECURE_NO_DEPRECATE /D _CRT_NONSTDC_NO_DEPRECATE /D _SCL_SECURE_NO_WARNINGS /Gy /bigobj /Oi  /fp:precise     /EHa /wd4127 /wd4251 /wd4324 /wd4275 /wd4512 /wd4589 /MP2  /MDd /Zi /Ob0 /Od /RTC1
    C Compiler:                  C:/Program Files (x86)/Microsoft Visual Studio/2017/Professional/VC/Tools/MSVC/14.16.27023/bin/Hostx86/x64/cl.exe
    C flags (Release):           /DWIN32 /D_WINDOWS /W3  /D _CRT_SECURE_NO_DEPRECATE /D _CRT_NONSTDC_NO_DEPRECATE /D _SCL_SECURE_NO_WARNINGS /Gy /bigobj /Oi  /fp:precise     /MP2   /MD /O2 /Ob2 /DNDEBUG
    C flags (Debug):             /DWIN32 /D_WINDOWS /W3  /D _CRT_SECURE_NO_DEPRECATE /D _CRT_NONSTDC_NO_DEPRECATE /D _SCL_SECURE_NO_WARNINGS /Gy /bigobj /Oi  /fp:precise     /MP2 /MDd /Zi /Ob0 /Od /RTC1
    Linker flags (Release):      /machine:x64  /INCREMENTAL:NO
    Linker flags (Debug):        /machine:x64  /debug /INCREMENTAL
    ccache:                      NO
    Precompiled headers:         NO
    Extra dependencies:
    3rdparty dependencies:

  OpenCV modules:
    To be built:                 calib3d core dnn features2d flann gapi highgui imgcodecs imgproc ml objdetect photo stitching video videoio world
    Disabled:                    python2 python3
    Disabled by dependency:      -
    Unavailable:                 java ts
    Applications:                apps
    Documentation:               NO
    Non-free algorithms:         NO

  Windows RT support:            NO

  GUI:
    Win32 UI:                    YES
    VTK support:                 NO

  Media I/O:
    ZLib:                        build (ver 1.2.11)
    JPEG:                        build-libjpeg-turbo (ver 2.0.6-62)
    WEBP:                        build (ver encoder: 0x020f)
    PNG:                         build (ver 1.6.37)
    TIFF:                        build (ver 42 - 4.0.10)
    JPEG 2000:                   build (ver 2.3.1)
    OpenEXR:                     build (ver 2.3.0)
    HDR:                         YES
    SUNRASTER:                   YES
    PXM:                         YES
    PFM:                         YES

  Video I/O:
    DC1394:                      NO
    FFMPEG:                      YES (prebuilt binaries)
      avcodec:                   YES (58.91.100)
      avformat:                  YES (58.45.100)
      avutil:                    YES (56.51.100)
      swscale:                   YES (5.7.100)
      avresample:                YES (4.0.0)
    GStreamer:                   NO
    DirectShow:                  YES
    Media Foundation:            YES
      DXVA:                      YES

  Parallel framework:            Concurrency

  Trace:                         YES (with Intel ITT)

  Other third-party libraries:
    Intel IPP:                   2020.0.0 Gold [2020.0.0]
           at:                   C:/build/master_winpack-build-win64-vc15/build/3rdparty/ippicv/ippicv_win/icv
    Intel IPP IW:                sources (2020.0.0)
              at:                C:/build/master_winpack-build-win64-vc15/build/3rdparty/ippicv/ippicv_win/iw
    Eigen:                       NO
    Custom HAL:                  NO
    Protobuf:                    build (3.5.1)

  OpenCL:                        YES (NVD3D11)
    Include path:                C:/build/master_winpack-build-win64-vc15/opencv/3rdparty/include/opencl/1.2
    Link libraries:              Dynamic load

  Python (for build):            C:/utils/soft/python27-x64/python.exe

  Java:
    ant:                         C:/utils/soft/apache-ant-1.9.7/bin/ant.bat (ver 1.9.7)
    JNI:                         C:/Program Files/Java/jdk1.8.0_112/include C:/Program Files/Java/jdk1.8.0_112/include/win32 C:/Program Files/Java/jdk1.8.0_112/include
    Java wrappers:               NO
    Java tests:                  NO

  Install to:                    C:/build/master_winpack-build-win64-vc15/install
-----------------------------------------------------------------

Again, many thanks to all. Letā€™s hope this helps someone else.
Peace and blessing.

Richard

1 Like

Donā€™t mix debug and release lib thatā€™s it!