highgui模块中常用函数源码
其头文件:highgui.hpp
其中源文件
1、imread()函数
原型:CV_EXPORTS_W Mat imread( const string& filename, intflags=1 );
源代码:
Mat imread( const string& filename, int flags )
{
Mat img;
imread_( filename, flags, LOAD_MAT, &img );
return img;
}
其中imread_定义如下:
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;
}
2、imwrite()函数
原型:CV_EXPORTS_W boolimwrite( const string&filename, InputArrayimg,const vector<int>¶ms=vector<int>());
源代码:
bool imwrite( const string& filename, InputArray _img,const vector<int>& params )
{
Mat img = _img.getMat();
return imwrite_(filename, img, params, false);
}
static bool imwrite_( const string& filename, const Mat& image,
const vector<int>& params, bool flipv )
{
Mat temp;
const Mat* pimage = ℑ
CV_Assert( image.channels() == 1 || image.channels() == 3 || image.channels() == 4 );
ImageEncoder encoder = findEncoder( filename );
if( encoder.empty() )
CV_Error( CV_StsError, "could not find a writer for the specified extension" );
if( !encoder->isFormatSupported(image.depth()) )
{
CV_Assert( encoder->isFormatSupported(CV_8U) );
image.convertTo( temp, CV_8U );
pimage = &temp;
}
if( flipv )
{
flip(*pimage, temp, 0);
pimage = &temp;
}
encoder->setDestination( filename );
bool code = encoder->write( *pimage, params );
//CV_Assert( code );
return code;
}
3、imshow()函数
原型:CV_EXPORTS_W void imshow(const string&winname, InputArraymat);
源代码:
void cv::imshow( const string& winname, InputArray _img )
{
const Size size = _img.size();
#ifndef HAVE_OPENGL
CV_Assert(size.width>0 && size.height>0);
{
Mat img = _img.getMat();
CvMat c_img = img;
cvShowImage(winname.c_str(), &c_img);
}
#else
const double useGl = getWindowProperty(winname, WND_PROP_OPENGL);
CV_Assert(size.width>0 && size.height>0);
if (useGl <= 0)
{
Mat img = _img.getMat();
CvMat c_img = img;
cvShowImage(winname.c_str(), &c_img);
}
else
{
const double autoSize = getWindowProperty(winname, WND_PROP_AUTOSIZE);
if (autoSize > 0)
{
resizeWindow(winname, size.width, size.height);
}
setOpenGlContext(winname);
if (_img.kind() == _InputArray::OPENGL_TEXTURE)
{
cv::ogl::Texture2D& tex = wndTexs[winname];
tex = _img.getOGlTexture2D();
tex.setAutoRelease(false);
setOpenGlDrawCallback(winname, glDrawTextureCallback, &tex);
}
else
{
cv::ogl::Texture2D& tex = ownWndTexs[winname];
if (_img.kind() == _InputArray::GPU_MAT)
{
cv::ogl::Buffer& buf = ownWndBufs[winname];
buf.copyFrom(_img);
buf.setAutoRelease(false);
tex.copyFrom(buf);
tex.setAutoRelease(false);
}
else
{
tex.copyFrom(_img);
}
tex.setAutoRelease(false);
setOpenGlDrawCallback(winname, glDrawTextureCallback, &tex);
}
updateWindow(winname);
}
#endif
}
4、waitKey()函数
原型:CV_EXPORTS_W int waitKey(int delay = 0);
源代码:
int cv::waitKey(int delay)
{
return cvWaitKey(delay);
}
CV_IMPL int cvWaitKey( int )
{
CV_NO_GUI_ERROR( "cvWaitKey" );
return -1;
}
#define CV_NO_GUI_ERROR(funcname) \
cvError( CV_StsError, funcname, \
"The function is not implemented. " \
"Rebuild the library with Windows, GTK+ 2.x or Carbon support. "\
"If you are on Ubuntu or Debian, install libgtk2.0-dev and pkg-config, then re-run cmake or configure script", \
__FILE__, __LINE__ )
5、namedWindow()函数
原型:CV_EXPORTS_W void namedWindow(const string&winname, intflags = WINDOW_AUTOSIZE);
源代码:
void cv::namedWindow( const string& winname, int flags )
{
cvNamedWindow( winname.c_str(), flags );
}
CV_IMPL int cvNamedWindow( const char*, int )
{
CV_NO_GUI_ERROR("cvNamedWindow");
return -1;
}
6、创建滚动条
原型:
(1)typedef void (*MouseCallback)(int event, int x, int y, int flags, void* userdata);
//! assigns callback for mouse events
(2)typedef void (CV_CDECL *TrackbarCallback)(int pos, void* userdata);
(3)CV_EXPORTS int createTrackbar(const string& trackbarname, const string& winname,
int* value, int count,
TrackbarCallback onChange = 0,
void* userdata = 0);
(4)CV_EXPORTS_W int getTrackbarPos(const string& trackbarname, const string& winname);
(5)CV_EXPORTS_W void setTrackbarPos(const string& trackbarname, const string& winname, int pos);
源代码:
int cv::createTrackbar(const string& trackbarName, const string& winName,
int* value, int count, TrackbarCallback callback,
void* userdata)
{
return cvCreateTrackbar2(trackbarName.c_str(), winName.c_str(),
value, count, callback, userdata);
}
void cv::setTrackbarPos( const string& trackbarName, const string& winName, int value )
{
cvSetTrackbarPos(trackbarName.c_str(), winName.c_str(), value );
}
int cv::getTrackbarPos( const string& trackbarName, const string& winName )
{
return cvGetTrackbarPos(trackbarName.c_str(), winName.c_str());
}
7、鼠标操作
CV_EXPORTS void setMouseCallback(const string& winname, MouseCallback onMouse, void* userdata = 0);
void cv::setMouseCallback( const string& windowName, MouseCallback onMouse, void* param) { cvSetMouseCallback(windowName.c_str(), onMouse, param); }