第一个OpenCV程序
/* 第一OpenCV程序 */
#include "opencv/highgui.h"
int main(int argc, char** argv)
{
IplImage* img = cvLoadImage("1.jpg", 0);
/*
* C语言版本: IplImage* cvLoadImage(const char* filename,
* int flags=CV_LOAD_IMAGE_COLOR )
* Fuction: Loads an image from a file.
* Parameters:
* filename - Name of file to be loaded.
* flags - Flags specifying the color type of a loaded image:
* 1) >0 Return a 3-channel color image (CV_LOAD_IMAGE_COLOR == 1)
* 返回一个3通道的彩色图片,RGB格式,若缺省为1);
* 2) =0 Return a grayscale image (CV_LOAD_IMAGE_GRAYSCALE == 0)
* 返回一个单通道灰白图片;
* 3) <0 Return the loaded image as is.(CV_LOAD_IMAGE_ANYCOLOR == -1)
* 返回原始图片。如果原始图片是RGBA格式等其他格式需要得到原始图片就要用3).
* 用1)和2)会被转换格式。
*/
cvNamedWindow("Example1", CV_WINDOW_AUTOSIZE);
/*
* C语言版本: int cvNamedWindow(const char* name, int flags)
* Function: Creates a window.
* Parameters:
* name – Name of the window in the window caption that may be used as
* a window identifier.(窗口的标题和标识符)
* flags – Flags of the window. Currently the only supported flag is
* CV_WINDOW_AUTOSIZE .If this is set, the window size is automatically
* adjusted to fit the displayed image (see imshow() ), and you cannot
* change the window size manually.
* 目前只有也是默认的窗口状态是CV_WINDOW_AUTOSIZE,窗口会自动适应载入图片
* 不能人为改变大小。
*/
cvShowImage("EXample1", img);
/*
* C语言版本: void cvShowImage(const char* winname, const CvArr* image)
* Function: Displays an image in the specified window.(imShow())
* Parameters:
* winname – Name of the window.
* image – Image to be shown.
* Note: 1)The function imshow displays an image in the specified window.
* If the window was created with the CV_WINDOW_AUTOSIZE flag,
* the image is shown with its original size.
* 如果窗口的状态是CV_WINDOW_AUTOSIZE,图片显示其原来的大小;
* 2) Otherwise, the image is scaled to fit the window.The function
* may scale the image, depending on its depth:
* 否则,图像将被按比例适合窗口大小。
*
*/
cvWaitKey(0);
/* C语言版本:int cvWaitKey(int delay=0 )
* Function: Waits for a pressed key.
* Parameters:
* delay – Delay in milliseconds. 0 is the special value that means “forever”.
* 等待按键的毫秒时间,0(默认值)表示一直等待。
* return: It returns the code of the pressed key or -1 if no key was pressed before
* the specified time had elapsed.
* 返回按键值或者-1,-1表示在等待的过程中没有按键
* Note: 1)The function waitKey waits for a key event infinitely delay<=0 or
* for delay milliseconds, when it is positive. Since the OS has a minimum
* time betweenswitching threads, the function will not wait exactly delay ms,
* it will wait at least delay ms,depending on what else is running on your
* computer at that time.
* 如果delay<=0,将无限等待;delay>0 等待对应的毫秒数,不过因为OS是多线程,所以等待的时
* 间一定是大于给点值的。
* 2)The function only works if there is at least one HighGUI window created
* and the windowis active. If there are several HighGUI windows, any of them
* can be active.
* 此函数在至少有一个HighGUI窗口的情况下被激活(工作)。
*/
cvReleaseImage(&img);
/*
* C语言版本: void cvReleaseImage(IplImage** image)
* Fuction: Deallocates the image header and the image data.
* 释放保存图片数据的内存,并把图片的指针置为空。
* Parameters:
* image – Double pointer to the image header.
* Note:This call is a shortened form of
* if(*image)
{
cvReleaseData(*image);//回收内存
cvReleaseImageHeader(image);//把指针置为空
}
*/
cvDestroyWindow("Example1");
/* C语言版本:void cvDestroyWindow(const char* name)
* Fction: The function destroyWindow destroys the window with the given name.
* Parameters:
* winname – Name of the window to be destroyed.
*
*/
}