zoukankan      html  css  js  c++  java
  • IplImage 封装释放

    IplImage是openCV库中非常重要的一个结构体,库中的图像都是保存为这个结构体后再进行操作的,详细结构例如以下:

    </pre><pre>

    typedef struct _IplImage
    {
    int nSize;                /* IplImage大小 */
    int ID;                  /* 版本号 (=0)*/
    int nChannels;           /* 大多数OPENCV函数支持1,2,3 或 4 个通道 */
    int alphaChannel;        /* 被OpenCV忽略 */
    int depth;               /* 像素的位深度: IPL_DEPTH_8U, IPL_DEPTH_8S, IPL_DEPTH_16U,IPL_DEPTH_16S, IPL_DEPTH_32S, IPL_DEPTH_32F and IPL_DEPTH_64F 可支持 */
    char colorModel[4];      /* 被OpenCV忽略 */
    char channelSeq[4];      /* 同上 */
    int dataOrder;           /* 0 - 交叉存取颜色通道, 1 - 分开的颜色通道. cvCreateImage仅仅能创建交叉存取图像 */
    int origin;              /* 0 - 顶—左结构,1 - 底—左结构 (Windows bitmaps 风格) */
    int align;               /* 图像行排列 (4 or 8). OpenCV 忽略它,使用 widthStep 取代 */
    int width;               /* 图像宽像素数 */
    int height;              /* 图像高像素数*/
    struct _IplROI *roi;     /* 图像感兴趣区域. 当该值非空仅仅对该区域进行处理 */
    struct _IplImage *maskROI; /* 在 OpenCV中必须置NULL */
    void *imageId;             /* 同上*/
    struct _IplTileInfo *tileInfo; /*同上*/
    int imageSize;           /* 图像数据大小(在交叉存取格式下imageSize=image->height*image->widthStep),单位字节*/
    char *imageData;         /* 指向排列的图像数据 */
    int widthStep;           /* 排列的图像行大小,以字节为单位 */
    int BorderMode[4];       /* 边际结束模式, 被OpenCV忽略 */
    int BorderConst[4];      /* 同上 */
    char *imageDataOrigin;   /* 指针指向一个不同的图像数据结构(不是必须排列的),是为了纠正图像内存分配准备的 */
    }IplImage;

    cvCreateImage()来创建包括头与数据内存,而cvCreateImageHeader则是仅仅创建结构头,当能够预先分配好内存然后使用cvSetData影射到IplImage上。正是这个结构非常重要,可是非常人在使用IplImage对象时申请了内存会常常忘记释放内存。我在下提供一个封装好的IplImage类:

    class  CVImage
    {
    public :
    	CVImage();
    	CVImage(unsigned int width, unsigned int height, unsigned short depth, unsigned short nChannels = 3);
    	CVImage(CVImage& img);
    	~CVImage();
    
    	void ReleaseImage();
    	int Resize(unsigned int width, unsigned int height, unsigned short depth, unsigned short nChannels = 3);
    	
    	operator IplImage*() { return m_image; };
    	inline IplImage* GetImage() { return m_image; };
    
    private:
    	IplImage* m_image;
    };
    CVImage.cpp
    CVImage::CVImage() : m_image(NULL)
    {
    }
    
    CVImage::CVImage( unsigned int width, unsigned int height, unsigned short depth, unsigned short nChannels ) : m_image(NULL)
    {
    	Resize(width, height, nChannels);
    }
    
    CVImage::CVImage( CVImage& img)
    {
    	IplImage* tmpImg = img.GetImage();
    
    	if(m_image)
    	{
    		if(m_image->width != tmpImg->width || m_image->height != tmpImg->height)
    		{
    			cvReleaseImage(&m_image);
    		}
    	}
    	m_image = cvCloneImage(tmpImg);
    }
    
    CVImage::~CVImage()
    {
    	ReleaseImage();
    }
    
    void CVImage::ReleaseImage()
    {
    	if(m_image)
    	{
    		cvReleaseImage(&m_image);
    	}
    }
    
    int CVImage::Resize( unsigned int width, unsigned int height, unsigned short depth, unsigned short nChannels)
    {
    	if(0 == width || 0 == height || 0 == depth || 0 == nChannels)
    		return -1;
    
    	if(m_image)
    	{
    		if(width != m_image->width || height != m_image->height)
    			ReleaseImage();
    	}
    	m_image = cvCreateImage(cvSize(width, height), depth, nChannels);
    	
    	return NULL == m_image ? -1 : 0;
    }


  • 相关阅读:
    梦想就是梦想,不要让它成为杯具
    程序员,离开了库你还能干什么?
    采用WPF框架编写的3D软渲染Demo
    what the hell with Gimbal Lock?
    FX Composer VS RenderMonkey 使用对比之 FX Composer篇
    为什么你应该使用OpenGL而不是DirectX?
    游戏中的夜视效果实现
    {转}深入浅出之正则表达式(一)
    正则表达式30分钟入门教程版本:v2.31 (2009411) 作者:deerchao 转载请注明来源
    2013年
  • 原文地址:https://www.cnblogs.com/mfrbuaa/p/4325914.html
Copyright © 2011-2022 走看看