【问题标题】:Strange behaviour with GetOpenFileName dialog and opencvGetOpenFileName 对话框和 opencv 的奇怪行为
【发布时间】:2015-03-11 03:39:09
【问题描述】:

我正在尝试使用打开文件对话框让我的程序的用户选择一个图像,然后使用 opencv 处理该图像。

我有一个功能,我可以在其中打开对话框并尝试加载图像:

Mat load_image()
{

OPENFILENAME ofn;       // common dialog box structure
TCHAR szFile[260];       // buffer for file name
HWND hwnd;              // owner window
HANDLE hf;              // file handle

// Initialize OPENFILENAME
ZeroMemory(&ofn, sizeof(ofn));
ofn.lStructSize = sizeof(ofn);
ofn.hwndOwner = hwnd;
ofn.lpstrFile = szFile;
// Set lpstrFile[0] to '\0' so that GetOpenFileName does not 
// use the contents of szFile to initialize itself.
ofn.lpstrFile[0] = '\0';
ofn.nMaxFile = sizeof(szFile);
ofn.lpstrFilter = L"All\0*.*\0Text\0*.TXT\0";
ofn.nFilterIndex = 1;
ofn.lpstrFileTitle = NULL;
ofn.nMaxFileTitle = 0;
ofn.lpstrInitialDir = NULL;
ofn.Flags = OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST;

// Display the Open dialog box. 
if (GetOpenFileName(&ofn)==TRUE) 
    hf = CreateFile(ofn.lpstrFile, 
                    GENERIC_READ,
                    0,
                    (LPSECURITY_ATTRIBUTES) NULL,
                    OPEN_EXISTING,
                    FILE_ATTRIBUTE_NORMAL,
                    (HANDLE) NULL);
char filepath[260];
wcstombs(filepath, ofn.lpstrFile, 260);
cout << filepath << endl;
Mat src = imread(filepath);
//imshow("src", src);
return src;
}

您在此处看到的代码有效,可打开对话框并允许用户选择文件。然后将文件路径转换为字符串(传递给 imread)。当我尝试与图像 src 交互时会出现问题。

例如,如果我取消注释 'imshow' 行,GetOpenFileName 将返回 0 并且对话框甚至不会打开。这种情况多半发生,我实际上无法访问图像,因为我尝试的一切都会导致这种情况发生。

为了使函数工作,它被这样调用:

load_image();

但如果我尝试分配图像,即:

img = load_image(); 

同样的问题发生了。帮助!这可能是什么原因造成的?

【问题讨论】:

    标签: c++ windows winforms opencv dialog


    【解决方案1】:

    正如前面评论中提到的,CreateFile 方法会锁定图像文件,因此cv::imread() 无法访问/读取其内容。

    尽量避免使用CreateFile,我认为没有理由在您的代码中使用它,以下代码运行良好:

    cv::Mat load_image()
    {
        cv::Mat outMat;
        OPENFILENAME ofn;       // common dialog box structure
        TCHAR szFile[260];      // buffer for file name
    
        // Initialize OPENFILENAME
        ZeroMemory(&ofn, sizeof(ofn));
        ofn.lStructSize = sizeof(ofn);
        ofn.hwndOwner = NULL;
        ofn.lpstrFile = szFile;
        ofn.lpstrFile[0] = '\0';
        ofn.nMaxFile = sizeof(szFile);
        ofn.lpstrFilter = L"All\0*.*\0Text\0*.TXT\0";
        ofn.nFilterIndex = 1;
        ofn.lpstrFileTitle = NULL;
        ofn.nMaxFileTitle = 0;
        ofn.lpstrInitialDir = NULL;
        ofn.Flags = OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST;
    
        // Display the Open dialog box. 
        if (GetOpenFileName(&ofn) == TRUE)
        {
            char filepath[260];
    
            wcstombs(filepath, ofn.lpstrFile, 260);
            cout << filepath << endl;
    
            outMat = cv::imread(filepath);
        }
    
        return outMat;
    }
    
    int main()
    {
        cv::Mat image = load_image();
    
        cv::imshow("image", image);
        cv::waitKey(-1);
    
        return 0;
    }
    

    【讨论】:

      【解决方案2】:

      这些只是我在您的代码中注意到的...所以我不希望这会起作用,但也许会有所帮助。

      imread 可能失败了。您使用CreateFile 打开文件并由于打开它而将其锁定(无共享属性。有关更多信息,请参阅CreateFile)。 imread 无论如何都会尝试打开文件,因此您无需创建自己的句柄!只需使用收到的文件路径并调用imread 而不是使用CreateFile

      Mat src;
      if (GetOpenFileName(&ofn)==TRUE)
      {
          // Succeeded in choosing a file
          char filepath[260];
          wcstombs(filepath, ofn.lpstrFile, 260);
          cout << filepath << endl;
          src = imread(filepath);
      }
      

      另外,如果您没有hwnd,那么您应该只设置ofn.hwndOwner=NULL,而不是随意设置。

      如果您选择使用 CreateFile(也许您希望在 OpenCV 之外出于您自己的目的手动读取访问权限),请确保在再次打开文件之前调用文件句柄上的CloseHandle( HANDLE h )

      【讨论】:

      • 感谢您的回复,但是当我尝试执行此操作时,我遇到了同样的问题(对话框未打开,代码继续并失败,因为没有图像)
      猜你喜欢
      • 2014-10-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-10-03
      • 1970-01-01
      • 1970-01-01
      • 2015-10-15
      相关资源
      最近更新 更多