OpenCV源代码阅读(一) cvLoadImage()

CV_IMPL IplImage*
cvLoadImage( const char* filename, int iscolor )
{
    return (IplImage*)cv::imread_(filename, iscolor, cv::LOAD_IMAGE );
}

调用成员函数imread_(), 该函数原型如下:

enum { LOAD_CVMAT=0, LOAD_IMAGE=1, LOAD_MAT=2 };

static void*
imread_( const string& filename, int flags, int hdrtype, Mat* mat=0 )
{
	IplImage* image = 0;
	CvMat *matrix = 0;
	Mat temp, *data = &temp;

	ImageDecoder decoder = findDecoder(filename);
	if( decoder.empty() )
		return 0;
	decoder->setSource(filename);
	if( !decoder->readHeader() )
		return 0;

	CvSize size;
	size.width = decoder->width();
	size.height = decoder->height();

	int type = decoder->type();
	if( flags != -1 )
	{
		if( (flags & CV_LOAD_IMAGE_ANYDEPTH) == 0 )
			type = CV_MAKETYPE(CV_8U, CV_MAT_CN(type));

		if( (flags & CV_LOAD_IMAGE_COLOR) != 0 ||
			((flags & CV_LOAD_IMAGE_ANYCOLOR) != 0 && CV_MAT_CN(type) > 1) )
			type = CV_MAKETYPE(CV_MAT_DEPTH(type), 3);
		else
			type = CV_MAKETYPE(CV_MAT_DEPTH(type), 1);
	}

	if( hdrtype == LOAD_CVMAT || hdrtype == LOAD_MAT )
	{
		if( hdrtype == LOAD_CVMAT )
		{
			matrix = cvCreateMat( size.height, size.width, type );
			temp = cvarrToMat(matrix);
		}
		else
		{
			mat->create( size.height, size.width, type );
			data = mat;
		}
	}
	else
	{
		image = cvCreateImage( size, cvIplDepth(type), CV_MAT_CN(type) );
		temp = cvarrToMat(image);
	}

	if( !decoder->readData( *data ))
	{
		cvReleaseImage( &image );
		cvReleaseMat( &matrix );
		if( mat )
			mat->release();
		return 0;
	}

	return hdrtype == LOAD_CVMAT ? (void*)matrix :
		hdrtype == LOAD_IMAGE ? (void*)image : (void*)mat;
}


此外,还有另外一个版本的cvLoadImageM():

CV_IMPL CvMat*
cvLoadImageM( const char* filename, int iscolor )
{
    return (CvMat*)cv::imread_( filename, iscolor, cv::LOAD_CVMAT );
}

而imread_()还有另外一个版本的函数:

Mat imread( const string& filename, int flags )
{
    Mat img;
    imread_( filename, flags, LOAD_MAT, &img );
    return img;
}


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值