cv::imread() won't work with .jp2 files after GetOpenFileName() dialog

655 Views Asked by At

I've encountered a weird issue that I seem to be unable to solve, involving OpenCV, Win32 API and jpeg2000 file format.

I've made a C++ program that asks for an image and then, later on, uses it as a texture, manipulates it etc. But here's the problem:

I ask for the path to the image file with OPENFILENAME and GetOpenFileName(). Here's an example code for this very basic format:

#include "pch.h"
#include <iostream>
#include <opencv2/opencv.hpp>
#include <Windows.h>


void SelectAndOpenImage()
{
    OPENFILENAME openFile;
    char filePath[MAX_PATH] = { 0 };

    ZeroMemory(&openFile, sizeof(OPENFILENAME));

    openFile.lStructSize = sizeof(OPENFILENAME);
    openFile.hwndOwner = NULL;
    openFile.lpstrFile = filePath;
    openFile.lpstrFile[0] = '\0';
    openFile.nMaxFile = MAX_PATH;
    openFile.lpstrFilter = "Jpeg2000 (*.jp2)\0*.jp2\0Bitmap (*.bmp)\0*.bmp\0Jpeg (*.jpg)\0*.jpg\0Png (*.png)\0*.png\0";
    openFile.nFilterIndex = 1;
    openFile.lpstrInitialDir = NULL;
    openFile.Flags = OFN_FILEMUSTEXIST | OFN_HIDEREADONLY | OFN_NOCHANGEDIR;

    if (GetOpenFileName(&openFile))
    {
        cv::Mat image = cv::imread(openFile.lpstrFile, CV_LOAD_IMAGE_COLOR);
        if (image.empty()) std::cout << "Empty" << std::endl;
        else
        {
            cv::imshow("testing", image);
            cv::waitKey(5000);
        }
    }
    else
    {
        std::cout << "Failure" << std::endl;
    }
}


int main()
{
    SelectAndOpenImage();
    return 0;
}

The problem is that cv::imread() works correctly for every image file format except Jpeg2000, cv::imshow() shows just a black image (data is full of zeros). Jpeg2000 works well along with the others if I do not use this GetOpenFileName() dialog at all (instead I just insert the file path directly to the source code). However, if I use the dialog but ignore the file selection and use my own hard coded file path instead, Jpeg2000 file format won't work then either, again just a blank black image. All other image file formats work, no matter how I do it.

I welcome all ideas. I'm missing something.

I'm currently using Windows10 and Visual Studio 2017 version 15.8.2 (MSVC++ 14.15). OpenCV is 3.4.2 (Release).

0

There are 0 best solutions below