OpenCV MFC 模块间通信
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 模块间通信的更多相关文章
- Prism for WPF再探(基于Prism事件的模块间通信)
上篇博文链接 Prism for WPF初探(构建简单的模块化开发框架) 一.简单介绍: 在上一篇博文中初步搭建了Prism框架的各个模块,但那只是搭建了一个空壳,里面的内容基本是空的,在这一篇我将实 ...
- Prism之使用EventAggregation进行模块间通信
在开发Silverlight程序的时候,经常需要在不同的组件间进行通信.比如点击一个button,可能就需要改变另一个控件的内容.比较直接的办法是使用事件,当然使用MVVM的时候也可以使用comman ...
- 如何使用Prism框架的EventAggregator在模块间进行通信
目的 本文主要介绍如何使用Prism类库提供的事件机制在松耦合组件之间相互通信,Prism类库的事件机制建立在事件聚合服务之上,允许发布者和订阅者通过事件进行通信,不需要彼此之间引用. 事件聚合 Ev ...
- 在Prism 框架中,实现主程序与模块间 UI 的通信
背景: 在模块的UI中包含 TreeView 控件,在该树形控件的每一节点前面定义了一个复选框,如图 需求: 在两个不同的应用程序中使用该控件,而它在不同应用程序中的外观则并不一致,按照本例,即一个显 ...
- 系统间通信(10)——RPC的基本概念
1.概述 经过了详细的信息格式.网络IO模型的讲解,并且通过JAVA RMI的讲解进行了预热.从这篇文章开始我们将进入这个系列博文的另一个重点知识体系的讲解:RPC.在后续的几篇文章中,我们首先讲解R ...
- Linux下多任务间通信和同步-信号
Linux下多任务间通信和同步-信号 嵌入式开发交流群280352802,欢迎加入! 1.概述 信号是在软件层次上对中断机制的一种模拟,是一种异步通信方式.信号可以直接进行用户空间进程和内核进程之间的 ...
- React 组件间通信介绍
React 组件间通信方式简介 React 组件间通信主要分为以下四种情况: 父组件向子组件通信 子组件向父组件通信 跨级组件之间通信 非嵌套组件间通信 下面对这四种情况分别进行介绍: 父组件向子 ...
- Vue 根组件,局部,全局组件 | 组件间通信,案例组件化
一 组件 <div id="app"> <h1>{{ msg }}</h1> </div> <script src=" ...
- python 全栈开发,Day91(Vue实例的生命周期,组件间通信之中央事件总线bus,Vue Router,vue-cli 工具)
昨日内容回顾 0. 组件注意事项!!! data属性必须是一个函数! 1. 注册全局组件 Vue.component('组件名',{ template: `` }) var app = new Vue ...
随机推荐
- js jquery 验证写法
<?php header("Content-type: text/html; charset=utf-8"); ?> <script src="jque ...
- VS快捷编码方式
概念: 代码段是将预先定义好的可重用代码块快速插入到代码文件中,代码段提高了开发效率,增强了代码的可重用性:既节约了时间,又实现了不同开发人员间代码的共享.同时也可保证同一项目中代码风格的统一. ...
- python学习之路-5 基础进阶篇
本篇涉及内容 双层装饰器字符串格式化 双层装饰器 装饰器基础请点我 有时候一个功能需要有2次认证的时候就需要用到双层装饰器了,下面我们来通过一个案例详细介绍一下双层装饰器: 执行顺序:自上而下 解释顺 ...
- asp.net 通过js调用webService注意
通过JavaSrcipt调用WebService格式: //通过SricptManager 的,services标签添加web服务引用 <asp:ScriptManager runat=&quo ...
- iOS_ @property参数分析
@propert的相关参数 因为现在Xcode都是默认使用ARC所以现在分析主要是以ARC为主. 1.@property有哪些参数? 第一组: 内存管理特性 retain assign copy ...
- UILable文本常见属性说明
1.text:设置标签显示文本. 2.attributedText:设置标签属性文本. NSString *text = @"first"; NSMutableAttributed ...
- CAN总线
1.CAN(controller area network) LAN(local area network)局域网 2.CAN等通信协议的开发,使多种LAN通过网关进行数据交换得以实现.如应用在汽车电 ...
- for循环执行顺序
for循环的执行顺序用如下表达式: for(expression1;expression2;expression3) { expression4; } 执行的顺序应该是: 1)第一次循环,即初始化循环 ...
- SeekBar和RatingBar
今天在看一个音乐播放器的源代码时候用到了SeekBar,就翻出来mars老师的视频复习了一下,然后综合使用了一下. 首先先看下运行效果: 下来我们看看布局文件的设计: main.xml: < ...
- 【HTML5】在head 设置 meta 能更方便开发
<meta name="viewport" content="width=device-width, target-densitydpi=160dpi, initi ...