安装opencv,vc6.0配置工程目录
Include目录:
E:\PROGRAM FILES\OPENCV\OPENCV1.0\CV\INCLUDE
E:\PROGRAM FILES\OPENCV\OPENCV1.0\CVAUX\INCLUDE
E:\PROGRAM FILES\OPENCV\OPENCV1.0\CXCORE\INCLUDE
E:\PROGRAM FILES\OPENCV\OPENCV1.0\ML\INCLUDE
E:\PROGRAM FILES\OPENCV\OPENCV1.0\OTHERLIBS\HIGHGUI
E:\PROGRAM FILES\OPENCV\OPENCV1.0\OTHERLIBS\CVCAM\INCLUDE
Lbirary目录:
E:\PROGRAM FILES\OPENCV\OPENCV1.0\LIB
Source目录:
E:\PROGRAM FILES\OPENCV\OPENCV1.0\CV\SRC
E:\PROGRAM FILES\OPENCV\OPENCV1.0\CVAUX\SRC
E:\PROGRAM FILES\OPENCV\OPENCV1.0\CXCORE\SRC
E:\PROGRAM FILES\OPENCV\OPENCV1.0\ML\SRC
E:\PROGRAM FILES\OPENCV\OPENCV1.0\OTHERLIBS\HIGHGUI
E:\PROGRAM FILES\OPENCV\OPENCV1.0\OTHERLIBS\CVCAM\SRC
创建MFC工程
添加链接库 cv.lib cvaux.lib cxcore.lib ml.lib highgui.lib cvcam.lib
StdAfx.h中添加
#include
#include
#include "Processing.h"
注意: 安装opencv过程中,勾选添加路径到系统变量。工程建立好之后,若提示缺少dll文件,就把opencv安装目录下的bin文件夹里面的所有dll复制到系统C盘的windows/ system32 (64位系统为sysWOW64)中去。
添加成员
IplImage* pImg; // 读入图像文件的原始位图
int m_Display; // 图像显示标记
CString m_filepath; // 图像路径
重载打开文档虚函数
virtual BOOL OnOpenDocument(LPCTSTR lpszPathName);
pImg = NULL;
m_Display = -1; // 初始为-1
m_filepath="";
if (pImg) cvReleaseImage(&pImg);
在类视图中,右键CxxxDoc名称,点击“Add VirtuAl Function …”,双击左边列表中“OnOpenDocument”,确定。
BOOL CxxxDoc::OnOpenDocument(LPCTSTR lpszPathName)
{
if (!CDocument::OnOpenDocument(lpszPathName))
return FALSE;
// TODO: Add your specialized creation code here
m_filepath = lpszPathName;
/* 选择一副临时图像 */
IplImage* pTempImg=NULL;
pTempImg = cvLoadImage(lpszPathName,-1);
if (!pTempImg)
{
AfxMessageBox("Load Image Error!");
return FALSE;
}
/* 图像保存在Doc类成员 */
if (pImg)
cvReleaseImage(&pImg);
pImg=cvCreateImage(cvGetSize(pTempImg),pTempImg->depth,pTempImg->nChannels);
cvCopy(pTempImg,pImg); // 复制图像
cvReleaseImage(&pTempImg); // 释放临时图像
//pImg=cvCloneImage(pTempImg);//可代替上面三行 (但易造成内存泄漏,推荐用cvCopy)
m_Display = 0; // 读取到图像后为0
return TRUE;
}
添加成员变量
IplImage* saveImg; // 备份位图
IplImage* workImg; // 工作位图
// workImg的信息;
int m_nHeight, m_nWidth, m_nWidthStep, m_nChannels, m_nDepth;
void CModificationView::OnDraw(CDC* pDC)
{
CModificationDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
// TODO: add draw code for native data here
if (pDoc->pImg != NULL) // 有磁盘输入图像
{
if (pDoc->m_Display == 0) // 磁盘中有新图像, 未显示
{
// 释放原图像
if (workImg) cvReleaseImage(&workImg);
if (saveImg) cvReleaseImage(&saveImg);
// 重新分配空间
CvSize size= cvGetSize(pDoc->pImg);
int dep = pDoc->pImg->depth;
int chan = pDoc->pImg->nChannels;
workImg= cvCreateImage(size,dep,chan);
saveImg= cvCreateImage(size,dep,chan);
// 拷贝图像
cvCopy(pDoc->pImg,workImg);
cvCopy(pDoc->pImg,saveImg);
m_ImageType = imageType(workImg); //图像类型
pDoc->m_Display = 1; // 显示标记,置1
}
}
if (workImg) // 刷新窗口画面
{
// 图像基本信息
m_nWidth = workImg->width;
m_nHeight = workImg->height;
m_nWidthStep= workImg->widthStep;
m_nChannels = workImg->nChannels;
m_nDepth = workImg->depth;
// 设置滚动条 -- 需要设置视图积累为ScrollView,否者删除下面3行
CSize sizeTotal;
sizeTotal = CSize(workImg->width,workImg->height);
SetScrollSizes(MM_TEXT,sizeTotal);
// 显示
RECT rect; // 使用CvvImage类绘图函数的必要参数
rect.top = 0;
rect.left=0;
rect.bottom=m_nHeight;
rect.right=m_nWidth;
HDC hDC = pDC->GetSafeHdc() ; //pDC->m_hDC;this->m_hDc // 获取输出对象句柄, ::GetDC(m_hWnd)为全局函数
CvvImage image;
image.CopyOf(workImg,workImg->nChannels);
image.DrawToHDC(hDC, &rect); // image.Show(hDC,0,0,m_nWidth,m_nHeight); // 采用cv绘图方式,原点在左下角。
image.Destroy();
}
}
对按钮“另存为“(ID_FILE_SAVE_AS)添加消息响应函数
void CModificationView::OnFileSaveAs()
{
// TODO: Add your command handler code here
if (!workImg)
{
MessageBox("Current View has no image!");
return;
}
CString csBMP="BMP Files(*.BMP)|*.BMP|";
CString csJPG="JPG Files(*.JPG)|*.JPG|";
CString csTIF="TIF Files(*.TIF)|*.TIF|";
CString csPNG="PNG Files(*.PNG)|*.PNG|";
CString csDIB="DIB Files(*.DIB)|*.DIB|";
CString csPBM="PBM Files(*.PBM)|*.PBM|";
CString csPGM="PGM Files(*.PGM)|*.PGM|";
CString csPPM="PPM Files(*.PPM)|*.PPM|";
CString csSR ="SR Files(*.SR) |*.SR|";
CString csRAS="RAS Files(*.RAS)|*.RAS||";
CString csFilter=csBMP+csJPG+csTIF+csPNG+csDIB
+csPBM+csPGM+csPPM+csSR+csRAS;
CString name[]={"","bmp","jpg","tif","png","dib",
"pbm","pgm","ppm","sr", "ras",""};
CString strFileName;
CString strExtension;
CFileDialog FileDlg(false,NULL,NULL,OFN_HIDEREADONLY,csFilter);
// 文件存盘对话框
if (FileDlg.DoModal()==IDOK ) // 输入了文件名
{
strFileName = FileDlg.m_ofn.lpstrFile;
if (FileDlg.m_ofn.nFileExtension == 0) // 无文件后缀
{
strExtension = name[FileDlg.m_ofn.nFilterIndex];
strFileName = strFileName + '.' + strExtension;
// 加文件后缀
}
cvSaveImage(strFileName,workImg); // 当前画面存盘
}
}
代码下载地址:http://download.csdn.net/detail/wanggao_1990/9500062