平常使用Open CV時總是跳出一個個窗口,很難將項目進行系統集成,特別是在MFC等Windows環境中加載顯示Open CV中的IplImage圖像;
使用Open CVhighgui.h 中定義的CvvImage類,可以很好的實現Open CV和Windows MFC顯示接口;先介紹一下CvvImage類: 由於CvvImage是在 highgui.h 頭文件中聲明的,因此如果您的程序中需要使用,則必須在開頭包含此頭文件 #include <highgui.h> CvvImage對應CImage宏: #define CImage CvvImage 注意事項:
CvvImage::Createbool CvvImage::Create(int w, int h, int bpp, int origin); 創建一個圖像。 成功返回true, 失敗返回false。 w 圖像寬 h 圖像高 bpp 每個像素的bit數, 值等於像素深度乘以通道數 origin 0 - 頂—左結構, 1 - 底—左結構 (Windows bitmaps 風格) 例:// 創建一個400行600列的, IPL_DEPTH_8U類型的3通道圖像, 頂—左結構 CvvImage img; bool flag = img.Create(600, 400, IPL_DEPTH_8U*3, 0); if(!flag) printf("創建圖像失敗!");
CvvImage::CopyOf void CvvImage::CopyOf(CvvImage& img, int desired_color); 從img復制圖像到當前的對象中。 img 要復制的圖像。 desired_color 為復制后圖像的通道數, 復制后圖像的像素深度為8bit。 例:// 讀一個圖像,然后復制為1個3通道的彩色圖像 img1.Load("example.tiff"); img2.CopyOf(img1, 3); CvvImage::Loadbool CvvImage::Load(const char* filename, int desired_color); 裝載一個圖像。
CvvImage::LoadRectbool CvvImage::LoadRect(const char* filename, int desired_color, CvRect rect); 從圖像讀出一個區域。
CvvImage::Savebool CvvImage::Save(const char* filename);保存圖像。 和cvSaveImage相似。 CvvImage::Showvoid CvvImage::Show(const char* window);顯示一個圖像。 和cvShowImage相似。 CvvImage::Showvoid CvvImage::Show(HDC dc, int x, int y, int w, int h, int from_x, int from_y); 繪制圖像的部分到DC。 圖像沒有縮放。此函數僅在Windows下有效。
CvvImage::DrawToHDCvoid CImage::DrawToHDC(HDC hDCDst, RECT* pDstRect); 繪制圖像的ROI區域到DC的pDstRect,如果圖像大小和pDstRect不一致,圖像會拉伸/壓縮。此函數僅在Windows下有效。
例: CvvImage img; img.Load("example.tiff"); CRect rect; rect.left = 100; rect.top = 200; rect.right = rect.left + 600; rect.bottom = rect.top + 400; img.DrawToHDC(hDC, &rect);
CvvImage::Fillvoid CvvImage::Fill(int color); 以color顏色填充圖像。 CvvImage類定義 class CV_EXPORTS CvvImage
{public:
CvvImage();
virtual ~CvvImage();
virtual bool Create( int width,int height,int bits_per_pixel,int image_origin=0 ); virtual bool Load( const char* filename, int desired_color = 1 ); virtual bool LoadRect( const char* filename,int desired_color, CvRect r );
#ifdef WIN32
virtual bool LoadRect( const char* filename,int desired_color, RECT r ) { return LoadRect( filename, desired_color,cvRect( r.left, r.top, r.right - r.left, r.bottom - r.top )); } #endif
virtual bool Save( const char* filename );
virtual void CopyOf( CvvImage& image, int desired_color = -1 );
virtual void CopyOf( IplImage* img, int desired_color = -1 );
IplImage* GetImage() { return m_img; };
virtual void Destroy(void);
int Width() { return !m_img ? 0 : !m_img->roi ? m_img->width : m_img->roi->width; };
int Height() { return !m_img ? 0 : !m_img->roi ? m_img->height : m_img->roi->height;};
int Bpp() { return m_img ? (m_img->depth & 255)*m_img->nChannels : 0; };
virtual void Fill( int color );
virtual void Show( const char* window ); #ifdef WIN32
virtual void Show(HDC dc,int x,int y,int width,int height,int from_x = 0,int from_y = 0 ); virtual void DrawToHDC( HDC hDCDst, RECT* pDstRect ); #endif
protected:
IplImage* m_img; };
有了上面對CvvImage類的介紹,可以很容易的在Windows窗口中顯示圖像了,下面是在MFC的CView窗口中
中顯示IplImage圖像的函數代碼
void CMyView::ShowIplImage(IplImage* img) { CDC* pDC = GetDC(); }
void Cdialog_iplImageDlg::DrawPicToHDC(IplImage* iplimg , UINT ID)
cimg.CopyOf(iplimg);
|