OpenCV + VS2012 打开图像/图像文件

来源:互联网 发布:dwg文件打印软件 编辑:程序博客网 时间:2024/05/23 11:04
  • 这些小知识每次生成应用时都会用到,但是总是忘记记录,要用时就得翻看以前的程序,挺耗费时间的,所以在此记下。

  • 打开单张图像


  1. 使用工具箱生成如下界面
  2. 将textControl关联CString的变量:m_path
  3. 使用CFileDialog完成获取文件路径的的功能,双击button"打开图像",输入如下代码:
    // TODO: 在此添加控件通知处理程序代码
        //打开对话框
        CFileDialog hFileDlg(TRUE,NULL,NULL,OFN_FILEMUSTEXIST|OFN_PATHMUSTEXIST|OFN_READONLY,
           TEXT("支持的图像文件 (*.jpg;*.gif;*.bmp;...)|*.jpg;*.gif;*.bmp|专用Tiff图像文件(*.tiff;*.tif)|*.tiff;*.tif|所有文件(*.*)|*.*||"),NULL);
        hFileDlg.m_ofn.nFilterIndex=1;
        hFileDlg.m_ofn.hwndOwner=m_hWnd;
        hFileDlg.m_ofn.lStructSize=sizeof(OPENFILENAME);
        hFileDlg.m_ofn.lpstrTitle=TEXT("打开图像文件...\0");
        hFileDlg.m_ofn.nMaxFile=MAX_PATH;
        if(hFileDlg.DoModal() == IDOK)  //判断路径是否为空
        {
           m_path = hFileDlg.GetPathName();  //获取路径名称
           UpdateData(FALSE);       
        }  
  4. 使用OpenCV读取并显示图像,在if里添加如下代码
    cv::Mat image=cv::imread(LPCSTR(m_path),-1);
    cv::imshow("image",image);
  5. 示例如下:
          
  • 打开多张图像


  1. 代码如下
    void CBagOfWordsDlg::OnBnClickedOpenimglist()  //打开多张图像
    {
        // TODO: 在此添加控件通知处理程序代码
        CArray<CString,CString>ary_filename;  //存放路径
        CArray<CString,CString>ary_fileTitle;  //存放文件标题
        CString fileTemp,fileTitle,fileName;
        CString sFilter = _T("image Files (*.jpg;*.gif;*.bmp;*.tif;...)|*.jpg;*.gif;*.bmp*.tif;|All Files (*.*)|*.*||");
        CFileDialog my_file_dialog(TRUE_T("xxx"),NULL,
                               OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT | OFN_FILEMUSTEXIST | OFN_ALLOWMULTISELECT,
                               sFilter, this);
        if ( my_file_dialog.DoModal()!=IDOK )
           return;
        POSITION pos ( my_file_dialog.GetStartPosition() );
        int imageSum=0; //统计图像数
        while( pos )
        {
            imageSum++; //统计0
           //do something with the filename variable
            fileTemp=my_file_dialog.GetNextPathName(pos);
            ary_filename.Add(fileTemp);
            //获取文件名 
                //从字符串的后面往前遍历,如果遇到'\'则结束遍历,'\'右边的字符串则为文件名
                int length = fileTemp.GetLength();      
                for(int i = length -1; i>0;i--)
                {
                    if(fileTemp.GetAt(i)=='\\')//'\'==fileTemp.GetAt(i)
                    {//判断当前字符是否是'\'
                        fileName = fileTemp.Right(length - i -1);
                        break;//跳出循环
                    }
                }//endfor
                //获取文件名(不包含后缀)
                //采用CString的Left(int count)截取CString中从左往右数的count个字符
                //fileName.GetLength()-4中的4表示".dat"四个字符
                fileTitle = fileName.Left(fileName.GetLength()-4);
                //AfxMessageBox(fileTitle);
                ary_fileTitle.Add(fileTitle);//将文件名(不包含后缀)添加到数组中
           
        }
        m_imageList.Format("%d",imageSum);
        UpdateData(FALSE);
    }
  • 打开文件夹并获取文件夹内信息


    1. 代码如下
          // TODO: 在此添加控件通知处理程序代码
          CArray<CString,CString>ary_filename;  //存放路径
          BROWSEINFO bi = { 0 };
          TCHAR path[MAX_PATH];
          bi.lpszTitle = _T("Pick a Directory");
          bi.pszDisplayName = path;
          LPITEMIDLIST pidl = SHBrowseForFolder ( &bi );
          if ( pidl != 0 )
          {
              SHGetPathFromIDList(pidl , path);//把项目标识列表转化成字符串,获取文件夹名称
              m_FilePath = path;
          }
          CFileFind ff;
          CString FilePath;
          if (m_FilePath.Right(1)!="/")
          {
             m_FilePath+="/";
          }
          m_FilePath+="*.*";
          BOOL res=ff.FindFile(m_FilePath);
          while (res)
          {
             res=ff.FindNextFile();
             FilePath=ff.GetFilePath();
          /*   if (ff.IsDirectory() && !ff.IsDots())// 找到的是文件夹
             {
              FileSearch(FilePath);// 递归
             }*/
             /*else*/ if (!ff.IsDirectory() && !ff.IsDots())// 找到的是文件
             {
                 ary_filename.Add(FilePath);
             }
          }
          m_FilePath=ary_filename.GetAt(1);
          UpdateData(FALSE);


0 0
原创粉丝点击