1、VC6.0开发环境WindowsXP,安装OpenCV1.0.
2、新建单文档或多文档工程。
加OnOpenDocument函数。
1、在DOC类的头文件(…DOC.h)中加入图像变量声明.
// Implementation
public:
BITMAPINFO * lpbmi;
unsigned char * m_pBits;
BITMAPFILEHEADER bmpFH;
int imageWidth;
int imageHeight;
int m_nColorBits;//8 24
int m_nOpenMode;//=0 RAW =1 BMP =2 OpenCV
virtual ~CCCJImageDoc();
2、在DOC类构造函数中加入变量初始化
CCCJImageDoc::CCCJImageDoc()
{
// TODO: add one-time construction code here
m_pBits=NULL;
lpbmi=NULL;
imageWidth=768;
imageHeight=576;
m_nOpenMode=0;
m_nColorBits=8;
}
3、在DOC类的头文件(…DOC.h)中加入读取图像函数声明
void ReadRAW(LPCTSTR lpszPathName, int nWidth, int nHeight);
BOOL ReadBMP(LPCTSTR lpszPathName);
4、在DOC类CPP文件中加入函数体
BOOL CCCJImageDoc::ReadBMP(LPCTSTR lpszPathName)
{
long lTotal=0;
CFile file;
file.Open(lpszPathName,CFile::modeRead);
file.Read (&bmpFH,sizeof(BITMAPFILEHEADER));
lpbmi=(LPBITMAPINFO)new char[sizeof(BITMAPINFO)+4*(1<<8)];
file.Read (lpbmi,sizeof(BITMAPINFOHEADER));
m_nColorBits=lpbmi->bmiHeader.biBitCount;
imageHeight=lpbmi->bmiHeader.biHeight;
imageWidth=lpbmi->bmiHeader.biWidth;
if(m_nColorBits==8)
{
lTotal=imageWidth*imageHeight;
file.Read (&(lpbmi->bmiColors[0]),256*4);
}else if(m_nColorBits==24)
{
lTotal=imageWidth*imageHeight*3;
}else
{
file.Close();
return FALSE;
}
m_pBits=new unsigned char[lTotal];
file.Read(m_pBits,lTotal);
file.Close();
return TRUE;
}
5、ReadBMP在OnOpenDocument中调用,读取图象数据。
BOOL CCCJImageDoc::OnOpenDocument(LPCTSTR lpszPathName)
{
if (!CDocument::OnOpenDocument(lpszPathName))
return FALSE;
// TODO: Add your specialized creation code here
CString str=lpszPathName;
str.MakeLower();
if(str.Find(".bmp")!=-1)
{
m_nOpenMode=1;
if(!ReadBMP(lpszPathName)) return FALSE;
}
return TRUE;
}
7、在View的OnDraw函数中调用StretchDIBits函数显示图像。
void CCCJImageView::OnDraw(CDC* pDC)
{
CCCJImageDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
// TODO: add draw code for native data here
if(pDoc->m_nOpenMode==1)//BMP
{
StretchDIBits(pDC->m_hDC,0,0,
pDoc->imageWidth,pDoc->imageHeight,0,0,pDoc->imageWidth,pDoc->imageHeight,
pDoc->m_pBits,pDoc->lpbmi,DIB_RGB_COLORS,SRCCOPY);
}
}
8、编译执行就可以读取和显示图像。
加入OPenCV图像读取及显示功能
1、在DOC类的头文件(…DOC.h)中加入OpenCV头文件.
#include "cxcore.h" // OpenCV 头文件
#include "highgui.h" // OpenCV 头文件
class CCCJImageDoc : public CDocument
{
protected: // create from serialization only
CCCJImageDoc();
DECLARE_DYNCREATE(CCCJImageDoc)
2、在DOC.h头文件中加入OpenCV图像指针
class CCCJImageDoc : public CDocument
{
protected: // create from serialization only
CCCJImageDoc();
DECLARE_DYNCREATE(CCCJImageDoc)
// Attributes
public:
IplImage* image;//OpenCV图像指针
// Implementation
public:
BITMAPINFO * lpbmi;
unsigned char * m_pBits;
BITMAPFILEHEADER bmpFH;
int imageWidth;
int imageHeight;
int m_nColorBits;//8 24
int m_nOpenMode;//=0 RAW =1 BMP =2 OpenCV
virtual ~CCCJImageDoc();
3、在DOC类构造函数中加入变量初始化
CCCJImageDoc::CCCJImageDoc()
{
// TODO: add one-time construction code here
m_pBits=NULL;
lpbmi=NULL;
imageWidth=768;
imageHeight=576;
m_nOpenMode=0;
m_nColorBits=8;
image=NULL;
}
4、OpenCV的cvLoadImage在OnOpenDocument中调用,读取图象数据。
BOOL CCCJImageDoc::OnOpenDocument(LPCTSTR lpszPathName)
{
if (!CDocument::OnOpenDocument(lpszPathName))
return FALSE;
// TODO: Add your specialized creation code here
CString str=lpszPathName;
str.MakeLower();
if(str.Find(".bmp")!=-1)
{
m_nOpenMode=1;
if(!ReadBMP(lpszPathName)) return FALSE;
}
if(str.Find(".jpg")!=-1)
{
m_nOpenMode=2;
image=cvLoadImage(lpszPathName);
lpbmi=(LPBITMAPINFO)new char[sizeof(BITMAPINFO)+4*(1<<8)];
lpbmi->bmiHeader.biBitCount=image->depth*image->nChannels;
lpbmi->bmiHeader.biClrUsed=0;
lpbmi->bmiHeader.biHeight=image->height;
lpbmi->bmiHeader.biWidth=image->width;
lpbmi->bmiHeader.biSize=sizeof(BITMAPINFOHEADER);
lpbmi->bmiHeader.biSizeImage=image->width*image->height*image->nChannels;
lpbmi->bmiHeader.biClrImportant=0;
lpbmi->bmiHeader.biCompression=0;
lpbmi->bmiHeader.biPlanes=1;//image->nChannels;
for(int i=0;i<(1<<8);i++)
{
RGBQUAD &o=lpbmi->bmiColors[i];
o.rgbBlue=i;
o.rgbGreen=i;
o.rgbRed=i;
o.rgbReserved=0;
}
}
return TRUE;
}
5、在View的OnDraw函数中调用StretchDIBits函数显示OpenCV图像。
void CCCJImageView::OnDraw(CDC* pDC)
{
CCCJImageDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
// TODO: add draw code for native data here
if(pDoc->m_nOpenMode==1)//BMP
{
StretchDIBits(pDC->m_hDC,0,0,
pDoc->imageWidth,pDoc->imageHeight,0,0,pDoc->imageWidth,pDoc->imageHeight,
pDoc->m_pBits,pDoc->lpbmi,DIB_RGB_COLORS,SRCCOPY);
}
if(pDoc->m_nOpenMode==2)//JPG
{
StretchDIBits(pDC->m_hDC,0,0,pDoc->image->width,pDoc->image->height,
0,pDoc->image->height,pDoc->image->width,-pDoc->image->height,
pDoc->image->imageData,pDoc->lpbmi,
DIB_RGB_COLORS,SRCCOPY);
}
}
6、设置OpenCV的Include和Lib路径。
Include 路径
Lib路径
设置链接连接库函数cxcored.lib cvd.lib highguid.lib
7、编译执行就可以读取和显示图像。
执行时,需要把 cvcam100.dll
cxcore100.dll
cv100.dll
cvaux100.dll
highgui100.dll
copy到程序的Debug目录中