作者:jink2005
转自:http://www.aiseminar.cn/bbs/forum.php?mod=viewthread&tid=327&extra=page%3D1
依照我发的OpenCV编程入门的前三篇文章,我编写了从摄像头采集图像的程序,运行环境为:VC6.0、Windows XP、MD-901摄像头。
程序全面源码如下:
- /* jink2005编写于www.aiseminar.cn */
- #include "stdio.h"
- #include "cv.h"
- #include "highgui.h"
- #pragma comment(lib, "cv.lib")
- #pragma comment(lib, "cxcore.lib")
- #pragma comment(lib, "highgui.lib")
- int main(int argc, char* argv[]) {
- /* 通过摄像头采集一帧图像 */
- IplImage* img = 0;
- CvCapture* capture = cvCaptureFromCAM(0);
- if (! cvGrabFrame(capture)) { // 采集一帧
- printf("Could not grab a frame!\n\7");
- exit(0);
- }
- img = cvRetrieveFrame(capture); // 提取采集到的帧
- /* 获得此帧的相关信息 */
- cvQueryFrame(capture); // 此调用是获得正确的采集属性所必需的
- int frameH = (int)cvGetCaptureProperty(capture, CV_CAP_PROP_FRAME_HEIGHT);
- int frameW = (int)cvGetCaptureProperty(capture, CV_CAP_PROP_FRAME_WIDTH);
- /* 显示图像到窗口 */
- cvNamedWindow("mainWin", CV_WINDOW_AUTOSIZE); // 创建窗体
- cvShowImage("mainWin", img);
- /* 释放采集源对像 */
- cvReleaseCapture(&capture);
- // 等待一个任意键
- cvWaitKey(0);
- cvReleaseImage(&img);
- return(0);
- }

相关知识及问题参考本版块其他文章。