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檢測");
}