对一张处理过后的图片进行显示,发现在OpenCV中显示的效果会好些,在MFC中显示的图片好像蒙上了一层灰色。请问怎么讲在MFC中显示的图片跟在OpenCV中显示的效果一样??

解决方案 »

  1.   

    // test2Dlg.cpp : implementation file
    //#include "stdafx.h"
    #include "test2.h"
    #include "test2Dlg.h"
    #include "cv.h"
    #include "highgui.h"
    #include "cxcore.h"
    #include "Myclass.h"#ifdef _DEBUG
    #define new DEBUG_NEW
    #undef THIS_FILE
    static char THIS_FILE[] = __FILE__;
    #endif/////////////////////////////////////////////////////////////////////////////
    // CAboutDlg dialog used for App Aboutclass CAboutDlg : public CDialog
    {
    public:
    CAboutDlg();// Dialog Data
    //{{AFX_DATA(CAboutDlg)
    enum { IDD = IDD_ABOUTBOX };
    //}}AFX_DATA // ClassWizard generated virtual function overrides
    //{{AFX_VIRTUAL(CAboutDlg)
    protected:
    virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
    //}}AFX_VIRTUAL// Implementation
    protected:
    //{{AFX_MSG(CAboutDlg)
    //}}AFX_MSG
    DECLARE_MESSAGE_MAP()
    };CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
    {
    //{{AFX_DATA_INIT(CAboutDlg)
    //}}AFX_DATA_INIT
    }void CAboutDlg::DoDataExchange(CDataExchange* pDX)
    {
    CDialog::DoDataExchange(pDX);
    //{{AFX_DATA_MAP(CAboutDlg)
    //}}AFX_DATA_MAP
    }BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
    //{{AFX_MSG_MAP(CAboutDlg)
    // No message handlers
    //}}AFX_MSG_MAP
    END_MESSAGE_MAP()/////////////////////////////////////////////////////////////////////////////
    // CTest2Dlg dialogCTest2Dlg::CTest2Dlg(CWnd* pParent /*=NULL*/)
    : CDialog(CTest2Dlg::IDD, pParent)
    {
    //{{AFX_DATA_INIT(CTest2Dlg)
    m_edit = _T("");
    //}}AFX_DATA_INIT
    // Note that LoadIcon does not require a subsequent DestroyIcon in Win32
    m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
    }void CTest2Dlg::DoDataExchange(CDataExchange* pDX)
    {
    CDialog::DoDataExchange(pDX);
    //{{AFX_DATA_MAP(CTest2Dlg)
    DDX_Text(pDX, IDC_edit, m_edit);
    //}}AFX_DATA_MAP
    }BEGIN_MESSAGE_MAP(CTest2Dlg, CDialog)
    //{{AFX_MSG_MAP(CTest2Dlg)
    ON_WM_SYSCOMMAND()
    ON_WM_PAINT()
    ON_WM_QUERYDRAGICON()
    ON_BN_CLICKED(IDC_BUTTON1, OnShowImage)
    ON_BN_CLICKED(IDC_BUTTON2, OnCluster)
    ON_BN_CLICKED(IDC_BUTTON3, Onok)
    ON_BN_CLICKED(IDC_BUTTON4, Oncancel)
    //}}AFX_MSG_MAP
    END_MESSAGE_MAP()/////////////////////////////////////////////////////////////////////////////
    // CTest2Dlg message handlersBOOL CTest2Dlg::OnInitDialog()
    {
    CDialog::OnInitDialog(); // Add "About..." menu item to system menu. // IDM_ABOUTBOX must be in the system command range.
    ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
    ASSERT(IDM_ABOUTBOX < 0xF000); CMenu* pSysMenu = GetSystemMenu(FALSE);
    if (pSysMenu != NULL)
    {
    CString strAboutMenu;
    strAboutMenu.LoadString(IDS_ABOUTBOX);
    if (!strAboutMenu.IsEmpty())
    {
    pSysMenu->AppendMenu(MF_SEPARATOR);
    pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
    }
    } // Set the icon for this dialog.  The framework does this automatically
    //  when the application's main window is not a dialog
    SetIcon(m_hIcon, TRUE); // Set big icon
    SetIcon(m_hIcon, FALSE); // Set small icon

    // TODO: Add extra initialization here
    CvSize Imagesize;
    Imagesize.height=IMAGE_HEIGHT;
    Imagesize.width=IMAGE_WIDTH;
    TheImage=cvCreateImage(Imagesize,IPL_DEPTH_8U,IMAGE_CHANNELS);
    cvNamedWindow("1");
    return TRUE;  // return TRUE  unless you set the focus to a control
    }void CTest2Dlg::OnSysCommand(UINT nID, LPARAM lParam)
    {
    if ((nID & 0xFFF0) == IDM_ABOUTBOX)
    {
    CAboutDlg dlgAbout;
    dlgAbout.DoModal();
    }
    else
    {
    CDialog::OnSysCommand(nID, lParam);
    }
    }// If you add a minimize button to your dialog, you will need the code below
    //  to draw the icon.  For MFC applications using the document/view model,
    //  this is automatically done for you by the framework.void CTest2Dlg::OnPaint() 
    {
    if (IsIconic())
    {
    CPaintDC dc(this); // device context for painting SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0); // Center icon in client rectangle
    int cxIcon = GetSystemMetrics(SM_CXICON);
    int cyIcon = GetSystemMetrics(SM_CYICON);
    CRect rect;
    GetClientRect(&rect);
    int x = (rect.Width() - cxIcon + 1) / 2;
    int y = (rect.Height() - cyIcon + 1) / 2; // Draw the icon
    dc.DrawIcon(x, y, m_hIcon);
    }
    else
    {
    CDialog::OnPaint();
    CDialog::UpdateWindow();
    ShowImage(TheImage,IDC_show);
    }
    }// The system calls this to obtain the cursor to display while the user drags
    //  the minimized window.
    HCURSOR CTest2Dlg::OnQueryDragIcon()
    {
    return (HCURSOR) m_hIcon;
    }void CTest2Dlg::OnShowImage() 
    {
    // TODO: Add your control notification handler code here
    CFileDialog dlg(TRUE,_T("*.bmp"), NULL,
    OFN_FILEMUSTEXIST|OFN_PATHMUSTEXIST|OFN_HIDEREADONLY,
    _T("image files (*.bmp; *.jpg) |*.bmp;*.jpg|All Files (*.*)|*.*||"),NULL);
    dlg.m_ofn.lpstrTitle= _T("Open Image");

    if (dlg.DoModal() != IDOK) 
    return;
    CString path= dlg.GetPathName();  // contain the selected filename
    IplImage *image= cvLoadImage(path);             // load the image
    //cvShowImage("Original Image", image); // display it
    if(!image)
    return;
    if( TheImage)
    cvZero( TheImage );
    // 对读入的图片进行缩放,使其宽或高最大值者刚好等于 256,再复制到 TheImage 中
    //假如文件不是256*256的
    if(image->height!=256||image->width!=256)
    {
    cvResize(image, TheImage, CV_INTER_LINEAR );
    //cvResize(image,srcImage,CV_INTER_LINEAR);
    cvReleaseImage(&image);
    }
    else
    {
    TheImage=cvLoadImage(path,CV_LOAD_IMAGE_ANYCOLOR );
    //srcImage=cvLoadImage(path,CV_LOAD_IMAGE_ANYCOLOR);
    cvReleaseImage(&image);
    }
    //srcImage=cvCloneImage(TheImage);
    //ResizeImage( image );
    // 调用显示图片函数
    ShowImage( TheImage, IDC_show );
    // 释放 ipl 占用的内存
    //UpdateWindow();
       cvReleaseImage(&image);//释放图像

    }
    void CTest2Dlg::ShowImage(IplImage* img,UINT ID)
    {
    // 获得显示控件的 DC
    CDC* pDC = GetDlgItem( ID ) ->GetDC();
    // 获取 HDC(设备句柄) 来进行绘图操作
    HDC hDC = pDC ->GetSafeHdc();

    CRect rect;
    GetDlgItem(ID) ->GetClientRect( &rect );
    // 求出图片控件的宽和高
    int rw = rect.right - rect.left;
    int rh = rect.bottom - rect.top;
    // 读取图片的宽和高
    int iw = img->width;
    int ih = img->height;
    // 使图片的显示位置正好在控件的正中
    int tx = (int)(rw - iw)/2;
    int ty = (int)(rh - ih)/2;
    SetRect( rect, tx, ty, tx+iw, ty+ih );
    // 复制图片
    CvvImage cimg;
    cimg.CopyOf(img,img->nChannels); //显示单通道
    //cimg.CopyOf( img );//原来显示的是3通道
    // 将图片绘制到显示控件的指定区域内
    cimg.DrawToHDC( hDC, &rect );

    ReleaseDC( pDC );
    }void CTest2Dlg::OnCluster() 
    {
    // TODO: Add your control notification handler code here
    /* Myclass cluster;
    cluster.two_meansCluster(TheImage);
    ShowImage(TheImage,IDC_Showdstimage1);*/
    IplImage* imgc1=cvCreateImage(cvGetSize(TheImage),IPL_DEPTH_8U,0);
    cvCvtColor(TheImage,imgc1,CV_BGR2GRAY);
    CvMat* mat=cvCreateMat(TheImage->height,TheImage->width,CV_32FC1);//用于将原图像转换成矩阵,便于处理
    cvZero(mat);
    CvMat* mat_2=cvCreateMat(TheImage->height,TheImage->width,CV_32FC1);
    cvConvert(imgc1,mat);//将图像转换为矩阵
    int m=mat->height*mat->width;
    CvMat pointshdr,*points=cvReshape(mat,&pointshdr,1,m);//创建一个一维矩阵或数组
    CvMat* clusters=cvCreateMat(m,1,CV_32SC1);
    cvZero(clusters);
    cvKMeans2(points,2,clusters,cvTermCriteria(CV_TERMCRIT_EPS+CV_TERMCRIT_ITER,10,1.0));
    CvMat dsthdr,*dst=cvReshape(clusters,&dsthdr,1,TheImage->height); 
    cvConvert(dst,mat_2);
    for(int i=0;i<mat_2->height;i++)
    for(int j=0;j<mat_2->width;j++)
    {
    if(cvGetReal2D(mat_2,i,j)==0) cvSetReal2D(mat_2,i,j,1);
    else cvSetReal2D(mat_2,i,j,0);
    }
    TheImage1=cvCreateImage(cvGetSize(mat_2),IPL_DEPTH_8U,0);
    cvGetImage(mat_2,imgc1);
    // cvCvtColor(imgc1,TheImage1,CV_GRAY2BGR);
    cvShowImage("1",imgc1);
    cvConvertScale(imgc1,TheImage1,100,0);
    cvCvtColor(TheImage1,TheImage,CV_GRAY2BGR);
    //cvShowImage("1",TheImage);
    ShowImage(TheImage,IDC_Showdstimage1);
    cvReleaseImage(&imgc1);
    cvReleaseMat(&mat);
    cvReleaseMat(&mat_2);
          cvReleaseMat(&clusters);

    }void CTest2Dlg::Onok() 
    {
    // TODO: Add your control notification handler code here
    cvDestroyAllWindows();
    Onok();

    }void CTest2Dlg::Oncancel() 
    {
    // TODO: Add your control notification handler code here
    cvDestroyAllWindows();
    Oncancel();

    }
    不好意思,忘记了