1. 程式人生 > >#OpenCV##MFC#利用MFC和OpenCV通過系統對話框打開和保存圖片

#OpenCV##MFC#利用MFC和OpenCV通過系統對話框打開和保存圖片

代碼 pat pda show 選擇 gif buffer length anti

打開圖片:

void CImageProDlg::OnImageopen()
{
    // TODO:  在此添加命令處理程序代碼
    Invalidate();
    CFileDialog dlg(TRUE, NULL, NULL, OFN_FILEMUSTEXIST | OFN_PATHMUSTEXIST | OFN_READONLY,
        TEXT("Supported Types (*.jpg;*.png;*.gif;*.bmp;...)|*.jpg;*.png;*.gif;*.bmp|Tiff(*.tiff;*.tif)|*.tiff;*.tif|All Files(*.*)|*.*||
"), NULL); dlg.m_ofn.nFilterIndex = 1; dlg.m_ofn.hwndOwner = m_hWnd; dlg.m_ofn.lStructSize = sizeof(OPENFILENAME); dlg.m_ofn.lpstrTitle = TEXT("Opening Image...\0"); dlg.m_ofn.nMaxFile = MAX_PATH; if (dlg.DoModal() == IDOK) { m_path = dlg.GetPathName(); m_capacity
= TRUE; UpdateData(FALSE); } else return; //左邊圖片控件顯示圖片 //string s_path(m_path.GetBuffer()); //這樣就要#include <string.h> char *s_path; s_path = m_path.GetBuffer(m_path.GetLength()); //將CString轉化為Char * 格式 //m_StaticPath.SetWindowTextA(m_path); //將圖片路徑顯示到靜態文本控件
Mat Mat_image = imread(s_path, 1); SavedImage = Mat_image; //全局變量 IplImage image = Mat_image; DrawPicToHDC(&image, ID_SHOWLEFT); }

保存圖片:

(需輸入完整路徑,包括拓展名)

void CImageProDlg::OnSaveas()
{
    // TODO:  在此添加命令處理程序代碼
    if (m_capacity)
    {
        CFileDialog dlg(FALSE, NULL, NULL, OFN_FILEMUSTEXIST | OFN_PATHMUSTEXIST | OFN_READONLY,
            TEXT("Supported Types (*.jpg;*.png;*.gif;*.bmp;...)|*.jpg;*.png;*.gif;*.bmp|Tiff(*.tiff;*.tif)|*.tiff;*.tif|All Files(*.*)|*.*||"), NULL);
        dlg.m_ofn.nFilterIndex = 1;
        dlg.m_ofn.hwndOwner = m_hWnd;
        dlg.m_ofn.lStructSize = sizeof(OPENFILENAME);
        dlg.m_ofn.lpstrTitle = TEXT("Saving Image...[Meantime input extension name! ]\0");
        dlg.m_ofn.nMaxFile = MAX_PATH;
        CString strPath(""), strExt("");
        char write[10000];
        if (dlg.DoModal() == IDOK)
        {
            strPath = dlg.GetPathName(); //文件名
            char *s_path;
            s_path = strPath.GetBuffer(strPath.GetLength());
            imwrite(s_path, SavedImage);
            strPath.ReleaseBuffer();
        }
    }
    else
        MessageBox("還未選擇原始圖片,無法保存圖片!");
}

#OpenCV##MFC#利用MFC和OpenCV通過系統對話框打開和保存圖片