http://blog.163.com/wangxh_jy/blog/static/28233883201001581640283/

关于详细的配置及程序运行截图,请下载:http://download.csdn.net/source/1127474名为《用Visual C#开发基于OpenCV的Windows应用程序》的文章。

由于百度允许的字数太少了,所以就不贴全部程序了。有需要源程序的话,请下载:http://download.csdn.net/source/1127477

下面是主要的程序:

?using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Text;

using System.Windows.Forms;

using Emgu.CV;

using Emgu.Util;

/*

*1、判断一个视频是否读到文件尾,不能用null,要用一个为空类型的IntPtr,就是IntPtr eof = new IntPtr(),看其是否与eof相等

*2、IplImage*,CvCapture*等指针在C#中都用IntPtr来代替,且其中没有cvGetMCvSize函数,故用cvGetImageROI来暂时代替

*3、由于C#中没有取地址符号&,所以在这里所有的取地址都用引用来代替,即ref

*4、OpenCV中的所有的预定义的常量,都封装在Emgu.CV.CvEnum这个枚举类型里面

*/

namespace OpenCV

{

public partial class mainForm : Form

{

IntPtr eof = new IntPtr();

public mainForm()

{

InitializeComponent();

}

private void btnOpen_Click(object sender, EventArgs e)

{

OpenFileDialog ofd = new OpenFileDialog();

ofd.DefaultExt = "*.bmp";

ofd.Filter="BMP files(*.bmp)|*.bmp|JPG files(*.jpg)|*.jpg|AVI files(*.avi)|*.avi";

if(ofd.ShowDialog()==DialogResult.OK)

{

tbxPath.Text = ofd.FileName;

}

else

{

tbxPath.Text = "";

}

}

private void btnOpenImage_Click(object sender, EventArgs e)

{

string path = tbxPath.Text;

if(path=="")

{

MessageBox.Show("Please select an image at first.","Information");

return;

}

else

{

string ext = path.Substring(path.Length - 3,3);

ext = ext.ToLower();

ext = ext.Trim();

if (ext.CompareTo("bmp") != 0 && ext.CompareTo("jpg")!=0)

{

MessageBox.Show("You must select an .bmp or .jpg file at first.", "Information");

return;

}

IntPtr img = CvInvoke.cvLoadImage(path,Emgu.CV.CvEnum.LOAD_IMAGE_TYPE.CV_LOAD_IMAGE_ANYCOLOR);

if(img==eof)

{

MessageBox.Show("Can not open the image.", "Information");

return;

}

CvInvoke.cvNamedWindow(path);

CvInvoke.cvShowImage(path, img);

CvInvoke.cvWaitKey(0);

CvInvoke.cvReleaseImage(ref img);

CvInvoke.cvDestroyWindow(path);

}

}

private void btnOpenAVI_Click(object sender, EventArgs e)

{

string path = tbxPath.Text;

if(path=="")

{

MessageBox.Show("Please select an AVI file at first.", "Information");

return;

}

string ext = path.Substring(path.Length - 3);

ext = ext.ToLower();

ext = ext.Trim();

if(ext.CompareTo("avi")!=0)

{

MessageBox.Show("You must select an AVI file at first.", "Information");

return;

}

IntPtr capture = CvInvoke.cvCreateFileCapture(path);

if(capture==eof)

{

MessageBox.Show("Can not create file capture.", "Information");

return;

}

IntPtr frame;

CvInvoke.cvNamedWindow(path);

double fps = CvInvoke.cvGetCaptureProperty(capture, Emgu.CV.CvEnum.CAP_PROP.CV_CAP_PROP_FPS);

while((frame= CvInvoke.cvQueryFrame(capture))!=eof)

{

CvInvoke.cvShowImage(path,frame);

int ch=CvInvoke.cvWaitKey(1000/(int)fps);

if(ch==13) break;

}

CvInvoke.cvReleaseCapture(ref capture);

CvInvoke.cvDestroyWindow(path);

}

private void btnOpenCamera_Click(object sender, EventArgs e)

{

IntPtr capture = CvInvoke.cvCreateCameraCapture(-1);

if(capture==eof)

{

MessageBox.Show("Can not create camera capture.", "Information");

return;

}

IntPtr frame;

CvInvoke.cvNamedWindow("Camera");

while((frame=CvInvoke.cvQueryFrame(capture))!=eof)

{

CvInvoke.cvShowImage("Camera", frame);

int ch = CvInvoke.cvWaitKey(10);

if (ch == 13) break;

}

CvInvoke.cvReleaseCapture(ref capture);

CvInvoke.cvDestroyWindow("Camera");

}

private void btnSaveImage_Click(object sender, EventArgs e)

{

string txt = tbxPath.Text;

string path;

if(txt=="")

{

MessageBox.Show("Please select an image at first.", "Information");

return;

}

IntPtr oldImg = CvInvoke.cvLoadImage(txt, Emgu.CV.CvEnum.LOAD_IMAGE_TYPE.CV_LOAD_IMAGE_ANYCOLOR);

if(oldImg==eof)

{

MessageBox.Show("can not load the image.", "Information");

return;

}

SaveFileDialog sfd = new SaveFileDialog();

sfd.Filter = "BMP files(*.bmp)|*.bmp|JPG files(*.jpg)|*.jpg";

sfd.DefaultExt = "*.bmp";

if(sfd.ShowDialog()==DialogResult.OK)

{

path = sfd.FileName;

}

else

{

CvInvoke.cvReleaseImage(ref oldImg);

return;

}

CvInvoke.cvSaveImage(path, oldImg);

CvInvoke.cvReleaseImage(ref oldImg);

MessageBox.Show("Saved As the image successfully.","Information");

}

private void btnRgbToGray_Click(object sender, EventArgs e)

{

string path = tbxPath.Text;

if (path == "")

{

MessageBox.Show("Please select an image at first.", "Information");

return;

}

string ext = path.Substring(path.Length - 3, 3);

ext = ext.ToLower();

ext = ext.Trim();

if (ext.CompareTo("bmp") != 0 && ext.CompareTo("jpg") != 0)

{

MessageBox.Show("You must select an .bmp or .jpg file at first.", "Information");

return;

}

IntPtr oldImg = CvInvoke.cvLoadImage(path, Emgu.CV.CvEnum.LOAD_IMAGE_TYPE.CV_LOAD_IMAGE_ANYCOLOR);

if(oldImg==eof)

{

MessageBox.Show("can not load the image:" + path, "Information");

return;

}

MCvRect cr = CvInvoke.cvGetImageROI(oldImg);

int width = cr.width;

int height = cr.height;

IntPtr grayImg = CvInvoke.cvCreateImage(new MCvSize(width,height), Emgu.CV.CvEnum.IPL_DEPTH.IPL_DEPTH_8U,1);

if(grayImg==eof)

{

MessageBox.Show("can not create an image in memory.", "Information");

return;

}

CvInvoke.cvCvtColor(oldImg, grayImg, Emgu.CV.CvEnum.COLOR_CONVERSION.CV_BGR2GRAY);

CvInvoke.cvNamedWindow("灰度图");

CvInvoke.cvShowImage("灰度图", grayImg);

CvInvoke.cvWaitKey(0);

CvInvoke.cvReleaseImage(ref oldImg);

CvInvoke.cvReleaseImage(ref grayImg);

CvInvoke.cvDestroyWindow("灰度图");

}

private void btnCannyImg_Click(object sender, EventArgs e)

{

string path = tbxPath.Text;

if (path == "")

{

MessageBox.Show("Please select an image at first.", "Information");

return;

}

string ext = path.Substring(path.Length - 3, 3);

ext = ext.ToLower();

ext = ext.Trim();

if (ext.CompareTo("bmp") != 0 && ext.CompareTo("jpg") != 0)

{

MessageBox.Show("You must select an .bmp or .jpg file at first.", "Information");

return;

}

IntPtr srcImg = CvInvoke.cvLoadImage(path, Emgu.CV.CvEnum.LOAD_IMAGE_TYPE.CV_LOAD_IMAGE_GRAYSCALE);

if(srcImg==eof)

{

MessageBox.Show("Can not load the image:" + path, "Information");

return;

}

MCvRect crect = CvInvoke.cvGetImageROI(srcImg);

IntPtr dstImg = CvInvoke.cvCreateImage(new MCvSize(crect.width, crect.height), Emgu.CV.CvEnum.IPL_DEPTH.IPL_DEPTH_8U, 1);

int sobel;

double thresh1, thresh2;

CannyWindow cw = new CannyWindow();

cw.ShowDialog();

sobel = cw.sobel;

thresh1 = cw.thresh1;

thresh2 = cw.thresh2;

if (thresh1 == 0 || thresh2 == 0 || sobel == 0) return;

CvInvoke.cvCanny(srcImg, dstImg, thresh1, thresh2, sobel);

CvInvoke.cvNamedWindow("Canny检测");

CvInvoke.cvShowImage("Canny检测", dstImg);

CvInvoke.cvWaitKey(0);

CvInvoke.cvReleaseImage(ref srcImg);

CvInvoke.cvReleaseImage(ref dstImg);

CvInvoke.cvDestroyWindow("Canny检测");

}

private void btnGrayVideo_Click(object sender, EventArgs e)

{

string path = tbxPath.Text;

if (path == "")

{

MessageBox.Show("Please select an AVI file at first.", "Information");

return;

}

string ext = path.Substring(path.Length - 3, 3);

ext = ext.ToLower();

ext = ext.Trim();

if (ext.CompareTo("avi") != 0)

{

MessageBox.Show("You must select an .avi file at first.", "Information");

return;

}

IntPtr capture = CvInvoke.cvCreateFileCapture(path);

if(capture==eof)

{

MessageBox.Show("can not create file capture:" + path, "Information");

return;

}

IntPtr frame=CvInvoke.cvQueryFrame(capture);

if(frame==eof)

{

MessageBox.Show("can not query frame from the capture.", "Information");

return;

}

MCvRect c = CvInvoke.cvGetImageROI(frame);

IntPtr gray = CvInvoke.cvCreateImage(new MCvSize(c.width, c.height), Emgu.CV.CvEnum.IPL_DEPTH.IPL_DEPTH_8U, 1);

CvInvoke.cvNamedWindow("灰度视频");

while((frame=CvInvoke.cvQueryFrame(capture))!=eof)

{

CvInvoke.cvCvtColor(frame, gray, Emgu.CV.CvEnum.COLOR_CONVERSION.CV_BGR2GRAY);

CvInvoke.cvFlip(gray, gray, 0);

CvInvoke.cvShowImage("灰度视频", gray);

int ch = CvInvoke.cvWaitKey(10);

if (ch == 13) break;

}

CvInvoke.cvReleaseCapture(ref capture);

CvInvoke.cvReleaseImage(ref gray);

CvInvoke.cvDestroyWindow("灰度视频");

}

private void btnGrayCamera_Click(object sender, EventArgs e)

{

IntPtr capture = CvInvoke.cvCreateCameraCapture(-1);

if(capture==eof)

{

MessageBox.Show("can not create camera capture.", "Information");

return;

}

IntPtr frame = CvInvoke.cvQueryFrame(capture);

if(frame==eof)

{

MessageBox.Show("can not query frame from capture.", "Information");

return;

}

MCvRect c = CvInvoke.cvGetImageROI(frame);

IntPtr gray = CvInvoke.cvCreateImage(new MCvSize(c.width, c.height), Emgu.CV.CvEnum.IPL_DEPTH.IPL_DEPTH_8U, 1);

CvInvoke.cvNamedWindow("灰度摄像头");

while ((frame = CvInvoke.cvQueryFrame(capture)) != eof)

{

CvInvoke.cvCvtColor(frame, gray, Emgu.CV.CvEnum.COLOR_CONVERSION.CV_BGR2GRAY);

CvInvoke.cvFlip(gray, gray, 0);

CvInvoke.cvShowImage("灰度摄像头", gray);

int ch = CvInvoke.cvWaitKey(10);

if (ch == 13) break;

}

CvInvoke.cvReleaseCapture(ref capture);

CvInvoke.cvReleaseImage(ref gray);

CvInvoke.cvDestroyWindow("灰度摄像头");

}

private void btnCannyVideo_Click(object sender, EventArgs e)

{

string path = tbxPath.Text;

if (path == "")

{

MessageBox.Show("Please select an AVI file at first.", "Information");

return;

}

string ext = path.Substring(path.Length - 3, 3);

ext = ext.ToLower();

ext = ext.Trim();

if (ext.CompareTo("avi") != 0)

{

MessageBox.Show("You must select an .avi file at first.", "Information");

return;

}

IntPtr capture = CvInvoke.cvCreateFileCapture(path);

if(capture==eof)

{

MessageBox.Show("can not create file capture:" + path, "Information");

return;

}

IntPtr frame = CvInvoke.cvQueryFrame(capture);

if(frame==eof)

{

MessageBox.Show("can not query frame from capture.", "Information");

return;

}

MCvRect c = CvInvoke.cvGetImageROI(frame);

IntPtr gray = CvInvoke.cvCreateImage(new MCvSize(c.width, c.height), Emgu.CV.CvEnum.IPL_DEPTH.IPL_DEPTH_8U, 1);

IntPtr canny = CvInvoke.cvCreateImage(new MCvSize(c.width, c.height), Emgu.CV.CvEnum.IPL_DEPTH.IPL_DEPTH_8U, 1);

int sobel;

double thresh1, thresh2;

CannyWindow cw = new CannyWindow();

cw.ShowDialog();

sobel = cw.sobel;

thresh1 = cw.thresh1;

thresh2 = cw.thresh2;

if (thresh1 == 0 || thresh2 == 0 || sobel == 0) return;

double fps = CvInvoke.cvGetCaptureProperty(capture, Emgu.CV.CvEnum.CAP_PROP.CV_CAP_PROP_FPS);

CvInvoke.cvNamedWindow("Canny检测");

while ((frame = CvInvoke.cvQueryFrame(capture)) != eof)

{

CvInvoke.cvCvtColor(frame, gray, Emgu.CV.CvEnum.COLOR_CONVERSION.CV_BGR2GRAY);

CvInvoke.cvCanny(gray, canny, thresh1,thresh2,sobel);

CvInvoke.cvFlip(canny, canny, 0);

CvInvoke.cvShowImage("Canny检测", canny);

int ch = CvInvoke.cvWaitKey(1000/(int)fps);

if (ch == 13) break;

}

CvInvoke.cvReleaseCapture(ref capture);

CvInvoke.cvReleaseImage(ref gray);

CvInvoke.cvReleaseImage(ref canny);

CvInvoke.cvDestroyWindow("Canny检测");

}

用Visual C#开发基于OpenCV的Windows应用程序的更多相关文章

  1. 在Windows下使用Dev-C++开发基于pthread.h的多线程程序【转】

    在Windows下使用Dev-C++开发基于pthread.h的多线程程序[转]     在Windows下使用Dev-C++开发基于pthread.h的多线程程序   文章分类:C++编程     ...

  2. windows下使用pycharm开发基于ansible api的python程序

    Window下python安装ansible,基于ansible api开发python程序 在windows下使用pycharm开发基于ansible api的python程序时,发现ansible ...

  3. Creating Dialogbased Windows Application (1) / 创建基于对话框的Windows应用程序(一)新建窗体 / VC++, Windows

    创建基于对话框的Windows应用程序(一) —— 新建窗体 1.新建一个Visual C++的Empty Project.  2.在Solution Explorer中右键Add New Item, ...

  4. Creating Dialogbased Windows Application (4) / 创建基于对话框的Windows应用程序(四)Edit Control、Combo Box的应用、Unicode转ANSI、Open File Dialog、文件读取、可变参数、文本框自动滚动 / VC++, Windows

    创建基于对话框的Windows应用程序(四)—— Edit Control.Combo Box的应用.Unicode转ANSI.Open File Dialog.文件读取.可变参数.自动滚动 之前的介 ...

  5. Creating Dialogbased Windows Application (3) / 创建基于对话框的Windows应用程序(三)Checkbox的应用、窗体置顶、设置图标 / VC++, Windows

    创建基于对话框的Windows应用程序(三) —— Checkbox的应用.窗体置顶.设置图标 上一节创建的窗体应用程序中,我们用到了Button和StaticText这两个控件.这一节中我们将学习使 ...

  6. Creating Dialogbased Windows Application (2) / 创建基于对话框的Windows应用程序(二)Button的应用、新建子窗体 / VC++, Windows

    创建基于对话框的Windows应用程序(二) —— Button的应用.新建子窗体 可以发现上一节创建的窗体中,点击OK和Cancel两个按钮是没有任何反应的.现在我们来为他们添加退出对话框的功能. ...

  7. 轻装上阵Flink--在IDEA上开发基于Flink的实时数据流程序

    前言 本文介绍如何在IDEA上快速开发基于Flink框架的DataStream程序.先直接上手! 环境清单 案例是在win7运行.安装VirtualBox,在VirtualBox上安装Centos操作 ...

  8. Windows下使用Dev-C++开发基于pthread.h的多线程程序

    一.下载Windows版本的pthread 目前最新版本是:pthreads-w32-2-9-1-release.zip. 二.解压pthread到指定目录      我选择的目录是:E:\DEV-C ...

  9. 使用VS2012开发基于Office 2013的AddIn程序

    默认VS2012开发的Office Add是基于2010的,如下所示: 如果你机器上安装的Office版本是2013,那么使用VS2012创建的工程是无法运行的,弹出如下的错误: 那么此时怎么办呢?将 ...

随机推荐

  1. MySQL 中的数据类型介绍

    1.MySQL 数据类型 MySQL中定义数据字段的类型对你数据库的优化是非常重要的. MySQL支持多种类型,大致可以分为三类:数值.日期/时间和字符串(字符)类型. 2.数值类型(12) 2.1. ...

  2. React Native 【学习总结】-【常用命令】

    前言 刚接触RN,相信很多人无从下手,不知道下一步要干什么,能干什么,本次学习围绕这个问题,将RN的常用命令总结一下,帮助你快速上手 架构理解 光知道命令的作用,远远不够,如果知道命令背后的意义,才能 ...

  3. pyextend库-accepts函数参数检查

    pyextend - python extend lib accepts(exception=TypeError, **types) 参数: exception: 检查失败时的抛出异常类型 **typ ...

  4. Python3【基础】-表达式与运算符

    一.什么是表达式? 1+2*3就是一个表达式,这里的加号和乘号叫做运算符,1.2.3叫做操作数.1+2*3计算的结果是7,计算结果可以存到一个变量中,即:res = 1 + 2 * 3. 所谓的表达式 ...

  5. nginx 根据get参数重定向(根据电视访问的mac地址传递的值,来重定向访问别的url地址,这样就可以进行单台的测试环境。。)

    背景是这样的: 公司要做所有客户端的迁移到别的云平台,但又担心会有问题,所以考虑分批次迁移过去,这样就需要迁移部分用户,因为客户端刷但都是统一但rom包,不能轻易发生改动,所以决定用重定向方式将部分客 ...

  6. 王者荣耀交流协会——第7次Scrum会议

    照片由刘耀泽同学拍摄 ,王露芝同学(外援)没有参加本次会议. 要求2 : 时间跨度:2017年10月19日 15:05 - 15:20 共计15分钟 要求3 : 地点:计算机楼107教室 要求4 : ...

  7. “Hello World!”团队第六周的第二次会议

    今天是我们团队“Hello World!”团队第六周召开的第二次会议.博客内容: 一.会议时间 二.会议地点 三.会议成员 四.会议内容 五.todo list 六.会议照片 七.燃尽图 八.代码 一 ...

  8. 1.12Linux下软件安装(学习过程)

    实验介绍 介绍 Ubuntu 下软件安装的几种方式,及 apt,dpkg 工具的使用. 一.Linux 上的软件安装 通常 Linux 上的软件安装主要有三种方式: 在线安装 从磁盘安装deb软件包 ...

  9. 2018-2019-20172329 《Java软件结构与数据结构》第四周学习总结

    2018-2019-20172329 <Java软件结构与数据结构>第四周学习总结 经过这样一个国庆节的假期,心中只有一个想法,这个国庆假期放的,不如不放呢!! 教材学习内容总结 < ...

  10. Controller与Switch建立连接

    连接建立 控制器和交换机认识的过程. 用于交互Openflow版本,如果不同则没有后续. 同1. 特征请求,控制器询问交换机的特征信息. 交换机回复控制器,相当于把整个交换机的所有配置都告诉控制器了. ...