opencv+mfc: 点击按钮, 显示处理前后的图片

1.建立一个基于mfc的对话框程序

2. 对话框的控件设置:

    1) 工具箱中拖曳3个picture control+2个button+3个edit control, 然后picture control修改ID,button修改caption, edit control设置read only为true

    2) 按钮右键添加事件处理程序,edit control设置value变量

3. 修改代码

这里显示图片采用的是将cv窗口嵌入picture control控件方法,每次点击按钮读取新的图片一定要先destroyAllWindows()

// weed mfcDlg.h : 头文件

#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <string>
#include "Resource.h"
#include "afxwin.h"
using namespace std;
using namespace cv;

#pragma once

// weed mfcDlg.cpp : 实现文件
#include "stdafx.h"
#include "weed mfc.h"
#include "weed mfcDlg.h"
#include "afxdialogex.h"
#include "imgprocess.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#endif

CImgProcess m_Img;

void CweedmfcDlg::OnBnClickedOpenImage()
{
    // TODO: 在此添加控件通知处理程序代码
    destroyAllWindows();
    CString picPath;   //定义图片路径变量  
    //选择文件对话框: TRUE表示打开对话框,默认的文件扩展名,默认的文件名
    CFileDialog dlg(TRUE, NULL, "D:\debug", OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT|OFN_ALLOWMULTISELECT, NULL, this); 
    if(dlg.DoModal() == IDOK)  
    {  
        picPath= dlg.GetPathName();  //获取图片路径  
    }  
    //CString to string  使用这个方法记得字符集选用“使用多字节字符”,不然会报错  
    string picpath=picPath.GetBuffer(0);    
//显示图片      
    src=imread(picpath);   
    actwidth=src.cols;
    actheight=src.rows;
    
    showImage(src,IDC_src,"src");
    m_Img.Imageprocess();
    showImage(Gray,IDC_gray,"Gray");
    showImage(bineary,IDC_dst,"bineary");
    m_threshold=m_Img.threshold;
    m_time=m_Img.time;
    UpdateData(false);//将变量值传到界面

}

void CweedmfcDlg::showImage(Mat img,int ID,string view)
{

    CRect rect;
    GetDlgItem(ID)->GetClientRect(&rect);

    namedWindow(view, WINDOW_NORMAL);//WINDOW_NORMAL调整窗口大小
    resizeWindow(view,rect.Width(),rect.Height());

    HWND hWnd = (HWND)cvGetWindowHandle(view.c_str());//获取cv窗口的句柄hWnd
    HWND hParent = ::GetParent(hWnd);获取cv父窗口的句柄hParent
    ::SetParent(hWnd,GetDlgItem(ID)->m_hWnd);//hWnd的父窗口设置为MFC中图片控件
    ::ShowWindow(hParent,SW_HIDE);//hParen隐藏起来
    imshow(view,img);
}

 

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值