1. 新建MFC项目

点击完成。

2. 添加按钮

在“工具箱”中找到“Button”控件,添加至界面: 

2. 配置opencv, 添加colordetector.h

#include<iostream>
#include "opencv2/opencv.hpp" using namespace std; class ColorDetector{
private:
//最小可接受距离
int minDist;
//目标色
cv::Vec3b target;
//结果图像
cv::Mat result;
public:
//构造函数
ColorDetector() : minDist()
{
//初始化默认参数
target[] = target[] = target[] = ;
}
//设置彩色距离阈值,阈值须为非负数
void setColorDistabceThreshold(int distance)
{
if (distance < )
distance = ;
minDist = distance;
}
//获取彩色距离阈值
int getColorDistanceThreshold() const
{
return minDist;
}
//设置需检测的颜色
void setTargetColor(unsigned char red, unsigned char green, unsigned char blue)
{
target[] = red;
target[] = green;
target[] = blue;
}
//设置需检测的颜色
void setTargetColor(cv::Vec3b color)
{
target = color;
}
//获取需检测的颜色
cv::Vec3b getTargetColor() const
{
return target;
}
//二值化处理函数
cv::Mat_<uchar> process(cv::Mat &image)
{
result.create(image.rows, image.cols, CV_8U);
//得到迭代器
cv::Mat_<cv::Vec3b>::const_iterator it = image.begin<cv::Vec3b>();
cv::Mat_<cv::Vec3b>::const_iterator itend = image.end<cv::Vec3b>();
cv::Mat_<uchar>::iterator itout = result.begin<uchar>(); while (it != itend)
{
if (getDistance(*it) < minDist)
{
*itout = ;
}
else
{
*itout = ;
}
it++;//更新输入迭代器
itout++;;//更新输出迭代器
}
return result;
}
//计算颜色距离
int getDistance(const cv::Vec3b &color) const
{
cv::Vec3b dist;
cv::absdiff(color, target, dist);
return cv::sum(dist)[];
}
}; class ColorDetectController
{
private:
ColorDetector* cdetect;//算法类
cv::Mat image;//待处理的图像
cv::Mat result;//结果
public:
ColorDetectController()
{
cdetect = new ColorDetector();
}
//设置色彩距离阈值
void setColorDistanceThreshold(int distance)
{
cdetect->setColorDistabceThreshold(distance);
}
//获取色彩距离阈值
int getColorDistancethreshold() const
{
return cdetect->getColorDistanceThreshold();
}
//设置要检测的颜色
void setTargetColor(unsigned char red,
unsigned char green, unsigned char blue)
{
cdetect->setTargetColor(red, green, blue);
}
//获取要检测的颜色
void getTargetColor(unsigned char& red, unsigned char& green, unsigned char& blue) const
{
cv::Vec3b color = cdetect->getTargetColor();
red = color[];
green = color[];
blue = color[];
}
//设置输入图像,通过文件读取
bool setInputImage(string filename)
{
image = cv::imread(filename);
if (!image.data)
return false;
else
return true;
}
//返回当前的输入图像
const cv::Mat getInputImage() const
{
return image;
}
//开始处理图像
void process()
{
result = cdetect->process(image);
}
//获取最近一次处理的结果
const cv::Mat getLastResult() const
{
return result;
}
//删除当前控制器创建的处理对象
~ColorDetectController()
{
delete cdetect;
}
};

在3_2模块间通信.h 添加头文件 #include "colordetector.h"

在3_2模块间通信Dlg.h 添加公有成员 ColorDetectController controller;

3. 右击“Button1”,选择属性,将”Caption“属性改为”打开图像“,ID 属性改为 OpenImg。双击按钮添加代码段:

   CFileDialog dlg(TRUE, _T("*.bmp"), NULL,
OFN_FILEMUSTEXIST | OFN_PATHMUSTEXIST | OFN_HIDEREADONLY,
_T("image file(*.bmp;*,jpg)|*.bmp;*.jpg|ALL Files(*.*)|*.*||"), NULL); dlg.m_ofn.lpstrTitle = _T("Open Image");
// if a filename has been selected
if (dlg.DoModal() == IDOK) {
// get the path of the selected filename
CString strMfc = dlg.GetPathName();
std::string filename = CT2CA(strMfc.GetBuffer());
// set and display the input image
controller.setInputImage(filename);
cv::imshow("Input Image", controller.getInputImage());
}

右击“Button1”,选择属性,将”Caption“属性改为”处理图像“,ID 属性改为 ProcessImg。双击按钮添加代码段:

 // target color is hard-coded here
controller.setTargetColor(, , );
// process the input image and display result
controller.process();
cv::imshow("Output Result", controller.getLastResult());

OpenCV MFC 模块间通信的更多相关文章

  1. Prism for WPF再探(基于Prism事件的模块间通信)

    上篇博文链接 Prism for WPF初探(构建简单的模块化开发框架) 一.简单介绍: 在上一篇博文中初步搭建了Prism框架的各个模块,但那只是搭建了一个空壳,里面的内容基本是空的,在这一篇我将实 ...

  2. Prism之使用EventAggregation进行模块间通信

    在开发Silverlight程序的时候,经常需要在不同的组件间进行通信.比如点击一个button,可能就需要改变另一个控件的内容.比较直接的办法是使用事件,当然使用MVVM的时候也可以使用comman ...

  3. 如何使用Prism框架的EventAggregator在模块间进行通信

    目的 本文主要介绍如何使用Prism类库提供的事件机制在松耦合组件之间相互通信,Prism类库的事件机制建立在事件聚合服务之上,允许发布者和订阅者通过事件进行通信,不需要彼此之间引用. 事件聚合 Ev ...

  4. 在Prism 框架中,实现主程序与模块间 UI 的通信

    背景: 在模块的UI中包含 TreeView 控件,在该树形控件的每一节点前面定义了一个复选框,如图 需求: 在两个不同的应用程序中使用该控件,而它在不同应用程序中的外观则并不一致,按照本例,即一个显 ...

  5. 系统间通信(10)——RPC的基本概念

    1.概述 经过了详细的信息格式.网络IO模型的讲解,并且通过JAVA RMI的讲解进行了预热.从这篇文章开始我们将进入这个系列博文的另一个重点知识体系的讲解:RPC.在后续的几篇文章中,我们首先讲解R ...

  6. Linux下多任务间通信和同步-信号

    Linux下多任务间通信和同步-信号 嵌入式开发交流群280352802,欢迎加入! 1.概述 信号是在软件层次上对中断机制的一种模拟,是一种异步通信方式.信号可以直接进行用户空间进程和内核进程之间的 ...

  7. React 组件间通信介绍

    React 组件间通信方式简介 React 组件间通信主要分为以下四种情况: 父组件向子组件通信 子组件向父组件通信 跨级组件之间通信 非嵌套组件间通信 下面对这四种情况分别进行介绍:   父组件向子 ...

  8. Vue 根组件,局部,全局组件 | 组件间通信,案例组件化

    一 组件 <div id="app"> <h1>{{ msg }}</h1> </div> <script src=" ...

  9. python 全栈开发,Day91(Vue实例的生命周期,组件间通信之中央事件总线bus,Vue Router,vue-cli 工具)

    昨日内容回顾 0. 组件注意事项!!! data属性必须是一个函数! 1. 注册全局组件 Vue.component('组件名',{ template: `` }) var app = new Vue ...

随机推荐

  1. DBA 经典面试题(1)

    1:列举几种表连接方式 hash join.  merge join. nest loop join(cluster join). index join    2:不借助第三方工具,怎样查看sql的执 ...

  2. base64转码

    Base64是一种编码方法,可以将任意字符转成可打印字符.使用这种编码方法,主要不是为了加密,而是为了不出现特殊字符,简化程序的处理. JavaScript原生提供两个Base64相关方法. btoa ...

  3. mycat(4)

    2016二月 24 置原 配置MyCat-eye 接下来在开始使用MyCat之前,我们先把监控平台部署好. 下载MyCat-eye项目,mvn打包. 之后得到类似于Mycat-web-1.0-SNAP ...

  4. blockUI

    组件主页 主要使用到 blockUI 组件实现 将jquery和组件的JS下载到本地 然后直接就可以实现遮罩层功能 显示遮罩层:$.blockUI(); 隐藏遮罩层:$.unblockUI(); 该网 ...

  5. 开源 免费 java CMS - FreeCMS2.0 会员我的评论

    项目地址:http://www.freeteam.cn/ 我的评论 从左側管理菜单点击我的评论进入. 在这里能够查看当前登录会员的全部评论记录. 删除评论 选择评论然后点击删除button能够完毕删除 ...

  6. 《TCP/IP具体解释卷2:实现》笔记--4种不同类型的mbuf

    mbuf的主要用途是保存子进程和网络接口间互相传递的用户数据.但mbuf也用于保存其它各种数据:源于目的地址.插口 选项等等. 以下介绍我们要遇到的四种类型的mbuf,它们根据在成员m_flag中填写 ...

  7. Python模块学习笔记— —time与datatime

    Python提供了多个内置模块用于操作日期时间.像calendar,time,datetime.首先对time模块中最经常使用的几个函数作一个介绍,它提供的接口与C标准库time.h基本一致.然后再介 ...

  8. 推荐几本不错的ASP.NET MVC书

    以前主要是做PHP应用的,由于工作需要,捡起来.NET, 特别是新技术层出不穷,找了几本书看,个人感觉还不错,网上也有电子版的下载 一. ASP.NET MVC4 Web 编程 O'Reilly出版社 ...

  9. Webform Lable

  10. easyui的combobox将得到的数据设定为下拉框默认值和复选框设定默认值

    通过easyui做了一个表,表里是从数据库拿到的数据. 现在双击某一行,通过点击行的id取到这一行的所有数据,现在需要修改这些得到的数据, 其中部分数据是<select>这个选择的, 问题 ...