呃,网上的学习笔记好乱@@
还是自己边学边写吧。不然几个月后又忘记怎么弄了。
OpenCV如何在VS2010下配置的方法及OpenCV的介绍就不写了。
如有需要,再写如何配置的笔记。
第一个练习——【打开图片】
键入代码:
#include "stdafx.h"
#include "cv.h"
#include
#include
int _tmain(int argc, _TCHAR* argv[])
{
IplImage *img = cvLoadImage("funny-pictures.jpg");
cvNamedWindow("Image:",1);
cvShowImage("Image:",img);
cvWaitKey();
cvDestroyWindow("Image:");
cvReleaseImage(&img);
return 0;
}
程序运行后显示如下:
代码解析:
#include "stdafx.h"
#include "cv.h"
#include
#include
引用不再解释,不太理解可详见c++ premier。稍后转录过来。
int _tmain(int argc, _TCHAR* argv[])
在main函数中定义一个argc用于读取输入参数个数,argv[]数组用于存放输入的参数。
如输入test E:jay.jpg,argc读取参数个数为2,argv[0]为test,而argv[1]中的E:jay.jpg读出为E:\jay.jpg。这个具体原因待整理。更多详见http://www.opencv.org.cn/index.p ... v%E8%AF%B4%E6%98%8E
IplImage *img = cvLoadImage("E:lena.jpg");
此行代码是将图像加载到内存,cvLoadImage()函数通过文件名确定被加载文件的格式并自动分配图像所需内存。cvLoadImage()函数可以打开大部分常用图像格式,如BMP,JPEG,JPG,PNG等。该函数执行完后会返回一个指针,该指针指向描述图像文件数据结构IplImage分配的内存。E:lena.jpg为指定图像所在地址,也可直接为lena.jpg,此处为程序默认储存地址([VS2010默认项目文件夹]项目文件夹项目文件夹 下)内的图片。lena.jpg原为大家图像都很熟悉的草帽女,但是我测试的时候没有去找就直接在e盘下重命名了张图片。
cvNamedWindow("Image:",1);
cvNamedWindow()函数将在屏幕上创建一个窗口用于显示图像。函数中第一个参数为窗口命名为"Image:",第二个参数为定义窗口属性,默认值为0,表示窗口大小不会因图像的大小而改变,图像将根据窗口大小进行变化充满窗口。为1或CV_WINDOW_AUTOSIZE时,窗口将根据图像的实际大小自动变化适应图像。
cvShowImage("Image:",img);
cvShowImage()函数通过第一个参数确定在已创建的哪个窗口中显示图像,且该函数被调用时窗口将被重绘并将图像显示到窗口中。
cvWaitKey();
函数功能为使程序暂停,等待触发按键。函数中参数为正值时,程序将暂停该整数值个毫秒后继续执行程序,没有按下按键也会如此。设置为0或者负数将一直等待用户触发按键。
vc++_2008和vc++_2010安装方法可参考:http://www.opencv.org.cn/index.p ... E8%A3%85OpenCV2.3.1
vs2010配置VC++目录可参考:http://blog.csdn.net/zhangyafengcpp/article/details/6847821
视频文件处理:
第一段测试代码
#include "stdafx.h"
#include
#include
//#include
#include
#include
#include
using namespace std;
int main()
{
IplImage *frame = NULL;
CvCapture *capture = NULL;
capture = cvCaptureFromAVI("D:\123\1.AVI");
frame = cvQueryFrame(capture);
cvNamedWindow("frame");
while(frame){
cvShowImage("frame", frame);
cvWaitKey(20);
cout << "Frame Grabbed." << endl;
frame = cvQueryFrame(capture);
}
return 0;
}
运行后界面显示如下
前面的不再做过多解析@@
其实我也想不起来了,四天没碰。。浪费时间啥也没记住。。
cvCaptureFromAVI()通过参数读入AVI文件及其所有信息并返回一个CvCapture指针,对人AVI视频时参数为该指针的cvQueryFrame()将为图像分配内存,与图像显示不同,不需要cvLoadImage为图像分配内存。CvCapture被释放后,每一帧图像的内存空间也将被释放。
cout函数让图像播放时将不断输出 Frame Grabbed.
第二段测试代码
#include "stdafx.h"
#include
#include
#include
#include
#include
#include
// 使用标准命名空间
using namespace std;
CvCapture* g_capture1 = NULL;
int main(int argc, char** argv )
{
// 建立播放窗口
cvNamedWindow( "Video Test 1", CV_WINDOW_AUTOSIZE );
// 捕捉视频文件
argv[1]="D:\123\1.avi";
g_capture1 = cvCreateFileCapture( argv[1] );
// 开始播放并保存视频
IplImage* frame1;
while(1)
{
// 获取、显示源文件的帧画面
frame1 = cvQueryFrame( g_capture1 );
if( !frame1 ) break;
cvShowImage( "Video Test 1", frame1 );
// 若按下 ESC 键,则退出程序
char c = cvWaitKey(33);
if( c==27 ) break;
}
// 释放内存,关闭窗口
cvReleaseCapture( &g_capture1 );
cvDestroyWindow( "Video Test 1" );
return 0;
}
下面段函数我电脑上运行不出结果。。待解决
// test2_video.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include
#include
//#include
#include
#include
// 使用标准命名空间
using namespace std;
// 初始化进度条的位置
int g_slider_position1 = 0;
int g_slider_position2 = 0;
CvCapture* g_capture1 = NULL;
CvCapture* g_capture2 = NULL;
// 定义回调函数用于播放进度的控制
void onTrackbarSlide1( int pos1 )
{
cvSetCaptureProperty( g_capture1, CV_CAP_PROP_POS_FRAMES, pos1 );
}
void onTrackbarSlide2( int pos2 )
{
cvSetCaptureProperty( g_capture2, CV_CAP_PROP_POS_FRAMES, pos2 );
}
int main(int argc, char** argv )
{
// 建立播放窗口
cvNamedWindow( "Video Test 1", CV_WINDOW_AUTOSIZE );
cvNamedWindow( "Video Test 2", CV_WINDOW_AUTOSIZE );
// 捕捉视频文件
g_capture1 = cvCreateFileCapture( argv[1] );
g_capture2 = cvCreateFileCapture( argv[2] );
// 读取、显示视频文件的帧数
int frames1 = (int) cvGetCaptureProperty( g_capture1, CV_CAP_PROP_FRAME_COUNT );
cout << "frames1 = " << frames1 << endl;
// 建立进度条
if( frames1 != 0 )
cvCreateTrackbar(
"Position",
"Video Test 1",
&g_slider_position1,
frames1,
onTrackbarSlide1
);
int frames2 = (int) cvGetCaptureProperty( g_capture2, CV_CAP_PROP_FRAME_COUNT );
cout << "frames2 = " << frames2 << endl;
if( frames2 != 0 )
cvCreateTrackbar(
"Position",
"Video Test 2",
&g_slider_position2,
frames2,
onTrackbarSlide2
);
// 读取视频文件信息
double fps1 = (int) cvGetCaptureProperty( g_capture1, CV_CAP_PROP_FPS );
double fps2 = (int) cvGetCaptureProperty( g_capture2, CV_CAP_PROP_FPS );
CvSize size1 = cvSize(
(int)cvGetCaptureProperty(g_capture1, CV_CAP_PROP_FRAME_WIDTH),
(int)cvGetCaptureProperty(g_capture1, CV_CAP_PROP_FRAME_HEIGHT));
CvSize size2 = cvSize(
(int)cvGetCaptureProperty(g_capture2, CV_CAP_PROP_FRAME_WIDTH),
(int)cvGetCaptureProperty(g_capture2, CV_CAP_PROP_FRAME_HEIGHT));
// 创建 VideoWriter
CvVideoWriter* wrVideo1 = cvCreateVideoWriter(argv[3], CV_FOURCC('M','J','P','G'), fps1, size1);
CvVideoWriter* wrVideo2 = cvCreateVideoWriter(argv[4], CV_FOURCC('M','J','P','G'), fps2, size2);
int frs = 0;
// 开始播放并保存视频
IplImage* frame1;
IplImage* frame2;
while( frs < frames1 && frs < frames2 )
{
// 获取、显示源文件的帧画面
frame1 = cvQueryFrame( g_capture1 );
if( !frame1 ) break;
cvShowImage( "Video Test 1", frame1 );
frame2 = cvQueryFrame( g_capture2 );
if( !frame2 ) break;
cvShowImage( "Video Test 2", frame2 );
// 保存:将当前帧写入到目标视频文件
cvWriteFrame( wrVideo1, frame1 );
cvWriteFrame( wrVideo2, frame2 );
// 若按下 ESC 键,则退出程序
char c = cvWaitKey(33);
if( c==27 ) break;
}
// 释放内存,关闭窗口
cvReleaseCapture( &g_capture1 );
cvReleaseCapture( &g_capture2 );
cvReleaseVideoWriter( &wrVideo1 );
cvReleaseVideoWriter( &wrVideo2 );
cvDestroyWindow( "Video Test 1" );
cvDestroyWindow( "Video Test 2" );
return 0;
}
呃,先贴过来,因为太多要测试的,边测试边写
----------------------------------------
#include "stdafx.h"
#include "cv.h"
#include "highgui.h"
#include
#include
int main( int argc, char** argv )
{
IplImage* img = 0;
int nFrames = 50;
CvCapture* capture = 0;
CvVideoWriter *writer = 0;
int isColor = 1;
int fps = 25; // or 30
int frameW = 640; //
int frameH = 480; //
if( argc == 1 || (argc == 2 && strlen(argv[1]) == 1 && isdigit(argv[1][0])))
capture = cvCaptureFromCAM( argc == 2 ? argv[1][0] - '0' : 0 );
if( !capture )
{
fprintf(stderr,"Could not initialize capturing...");
return -1;
}
writer=cvCreateVideoWriter("D:\out.avi",CV_FOURCC('X','V','I','D'),
fps,cvSize(frameW,frameH),isColor);
//存储视频文件 CvCapture* capture = cvCaptureFromCAM(0); // capture from video device #0
for(int i=0;i<nframes;i++)
{
cvGrabFrame(capture); // 抓取帧
img=cvRetrieveFrame(capture); // 恢复图像
cvWriteFrame(writer,img); // 将帧添加入视频文件
//显示所抓视频
cvNamedWindow("Live", CV_WINDOW_AUTOSIZE);//创建窗口
cvShowImage("Live", img);//显示所抓视频
cvWaitKey(50); // wait 20 ms
}
cvReleaseVideoWriter(&writer);
return 0;
}
录制avi视频
#include "cv.h"
#include "highgui.h"
#include
#include
int main( int argc, char** argv )
{
IplImage* img = 0;
int nFrames = 500;
CvCapture* capture = 0;
CvVideoWriter *writer = 0;
int isColor = 1;
int fps = 25; // or 30
int frameW = 640; //
int frameH = 480; //
if( argc == 1 || (argc == 2 && strlen(argv[1]) == 1 && isdigit(argv[1][0])))
capture = cvCaptureFromCAM( argc == 2 ? argv[1][0] - '0' : 0 );
if( !capture )
{
fprintf(stderr,"Could not initialize capturing...");
return -1;
}
writer=cvCreateVideoWriter("D:\out.mp4",CV_FOURCC('D','I','V','X'),
fps,cvSize(frameW,frameH),isColor);
//存储视频文件CvCapture* capture = cvCaptureFromCAM(0); // capture from video device #0
for(int i=0;i<nframes;i++)
{
cvGrabFrame(capture); // 抓取帧
img=cvRetrieveFrame(capture); // 恢复图像
cvWriteFrame(writer,img); // 将帧添加入视频文件
//显示所抓视频
cvNamedWindow("Live", CV_WINDOW_AUTOSIZE);//创建窗口
cvShowImage("Live", img);//显示所抓视频
cvWaitKey(20); // wait 20 ms
}
cvReleaseVideoWriter(&writer);
return 0;
}
录制mpga视频
------------------------------------------------
#include "stdafx.h"
#include "highgui.h"
#include "cv.h"
#pragma comment( linker, "/subsystem:\"windows\" /entry:\"mainCRTStartup\"" ) //消除console窗口
//此处稍后写学习笔记讲解
//初始化进度条的位置
int g_slider_position=0;
CvCapture* g_capture=NULL;
//进度条的回调函数,播放进度控制
void onTrackbarSlide(int pos)
{
cvSetCaptureProperty(g_capture,CV_CAP_PROP_POS_FRAMES,pos);//设置视频
}
int main(int argc, char* argv[])
{
//创建播放窗口
cvNamedWindow("Window Name", CV_WINDOW_AUTOSIZE);
//捕获视频
g_capture=cvCreateFileCapture("D:\out1.avi");
//获取视频的帧数
int frames = (int) cvGetCaptureProperty(g_capture,CV_CAP_PROP_FRAME_COUNT);
//建立进度条
if(frames!=0)
{
cvCreateTrackbar("Trackbar Name","Window Name",&g_slider_position,frames,onTrackbarSlide);
}
//捕获、播放视频
IplImage* frame;
while(1)
{
frame=cvQueryFrame(g_capture);
if( !frame ) break;
//获取视频播放位置
int trapos=(int)cvGetCaptureProperty(g_capture,CV_CAP_PROP_POS_FRAMES);
//设置进度条位置,使其和视频播放同步
cvSetTrackbarPos("Trackbar Name","Window Name", trapos);
//播放视频
cvShowImage("Window Name",frame);
//等待按键
char c=cvWaitKey(33);
if(c==27) break;
}
//释放资源
cvReleaseCapture(&g_capture);
cvDestroyWindow( "Window Name");
return 0;
}
添加进度条成功
呃。由于是上一条录制视频的时候录制的自己
所以播放也就播放自己了。。截图截个搓搓的头发吧
int g_slider_position=0;
CvCapture* g_capture=NULL;
为进度条添加全局变量。而回调函数需要使用CvCapture对象,故将它定义为全局变量。为增加程序可读性,前加g_方便修改者寻找。
void onTrackbarSlide(int pos)
{
cvSetCaptureProperty(g_capture,CV_CAP_PROP_POS_FRAMES,pos);//设置视频
}
定义一个在进度条被拖动时调用的回调函数。拖动条位置会被作为一个32位的整数以参数形式传入。
CV_CAP_PROP_POS_FRAMES表示我们以帧数来设置读入位置,FRAMES若用AVI_RATIO代替表示通过视频长度比例来设置读入位置。最后,新的进度条位置作为参数传入。
int frames = (int) cvGetCaptureProperty(g_capture,CV_CAP_PROP_FRAME_COUNT);
需要从CvCapture结构查询数据时,可使用cvGetCaptureProperty函数,我们希望获得视频文件的总帧数以对进度条进行设置。
if(frames!=0)
{
cvCreateTrackbar("Trackbar Name","Window Name",&g_slider_position,frames,onTrackbarSlide);
}
借助cvCreateTrackbar()创建进度条,可设置进度条名称和所属窗口。将一个变量绑定到进度条来表示其进度条最大值及一个回调函数(不需要回调函数的时候为空,进度条被拖动时触发)。cvCreateTrackbar()返回帧数为0时,进度条不会被创建。因为有些编码方式无法获得帧数,这时候只能播放而无法获得进度条。
------------------------------------------------------
图像变换:
#include "stdafx.h"
#include "cv.h"
#include
#include
void example2_4( IplImage* image )
{
// Create some windows to show the inpu
// and output images in.
//
cvNamedWindow( "Example2_4-in", CV_WINDOW_AUTOSIZE );
cvNamedWindow( "Example2_4-out", CV_WINDOW_AUTOSIZE );
// Create a window to show our input image
//
cvShowImage( "Example2_4-in", image );
// Create an image to hold the smoothed output
IplImage* out = cvCreateImage(
cvGetSize(image),
IPL_DEPTH_8U,
3
);
// Do the smoothing
//
cvSmooth( image, out, CV_GAUSSIAN, 5,5 );
cvSmooth( out, out, CV_GAUSSIAN, 5, 5);
// Show the smoothed image in the output window
//
cvShowImage( "Example2_4-out", out );
// Be tidy
//
cvReleaseImage( &out );
// Wait for the user to hit a key, then clean up the windows
//
cvWaitKey( 0 );
cvDestroyWindow("Example2_4-in" );
cvDestroyWindow("Example2_4-out" );
}
int main( int argc, char** argv )
{
argv[1]="E:lena.jpg";
IplImage* img = cvLoadImage( argv[1] );
cvNamedWindow("Example1", CV_WINDOW_AUTOSIZE );
cvShowImage("Example1", img );
example2_4( img );
cvWaitKey(0);
cvReleaseImage( &img );
cvDestroyWindow("Example1");
}
cvShowImage()和学习笔记之初试中无异,cvCreateImage()来为新的帧分配空间且只分配一帧图像的空间,再次调用时覆盖前一次的数据(这样每次调用返回的指针是一样的)。
cvGetsize(image)获得CvSize结构,第一个参数说明里当前图像的结构大学结构,第二个参数告诉了我们各通道每个像素点的数据类型,最后一个参数说明了通道的总数。程序中图像通道是3个(每个通道为8位)。图像大小同image。
该例程为平滑处理函数,通过使用每个像素周围3*3区域进行高斯平滑处理。
#include "stdafx.h"
#include "cv.h"
#include
#include
IplImage* doPyrDown(
IplImage* in,
int filter = IPL_GAUSSIAN_5x5)
{
// Best to make sure input image is divisible by two.
assert( in->width%2 == 0 && in->height%2 == 0 );
IplImage* out = cvCreateImage(
cvSize( in->width/2, in->height/2 ),
in->depth,
in->nChannels
);
cvPyrDown( in, out );
return( out );
};
int main( int argc, char** argv )
{
argv[1]="E:lena.jpg";
IplImage* img = cvLoadImage( argv[1] );
IplImage* img2 = cvCreateImage(
cvSize( img->width/2,img->height/2 ),
img->depth, img->nChannels);
cvNamedWindow("Example1", CV_WINDOW_AUTOSIZE );
cvNamedWindow("Example2", CV_WINDOW_AUTOSIZE );
cvShowImage("Example1", img );
img2 = doPyrDown( img );
cvShowImage("Example2", img2 );
cvWaitKey(0);
cvReleaseImage( &img );
cvReleaseImage( &img2 );
cvDestroyWindow("Example1");
cvDestroyWindow("Example2");
}
#include "stdafx.h"
#include "cv.h"
#include
#include
IplImage* doCanny(
IplImage* in,
double lowThresh,
double highThresh,
double aperture)
{
if (in->nChannels != 1)
return(0);
// Canny only handles gray scale images
IplImage* out = cvCreateImage(
cvGetSize( in ),
in->depth,
// IPL_DEPTH_8U,
1);
cvCanny( in, out, lowThresh, highThresh, aperture );
return( out );
};
int main( int argc, char** argv )
{
argv[1]="E:lena.jpg";
IplImage* img_rgb = cvLoadImage( argv[1] );
IplImage* img_gry = cvCreateImage(
cvSize( img_rgb->width,img_rgb->height ),
img_rgb->depth,
1);
cvCvtColor(img_rgb, img_gry ,CV_BGR2GRAY);
cvNamedWindow("Example Gray", CV_WINDOW_AUTOSIZE );
cvNamedWindow("Example Canny", CV_WINDOW_AUTOSIZE );
cvShowImage("Example Gray", img_gry );
IplImage* img_cny = doCanny( img_gry, 10, 100, 3 );
cvShowImage("Example Canny", img_cny );
cvWaitKey(0);
cvReleaseImage( &img_rgb);
cvReleaseImage( &img_gry);
cvReleaseImage( &img_cny);
cvDestroyWindow("Example Gray");
cvDestroyWindow("Example Canny");
}
#include "stdafx.h"
#include "cv.h"
#include
#include
IplImage* doCanny(
IplImage* in,
double lowThresh,
double highThresh,
double aperture)
{
IplImage* out = cvCreateImage(
cvGetSize( in ),
in->depth,
//IPL_DEPTH_8U,
1);
cvCanny( in, out, lowThresh, highThresh, aperture );
return( out );
};
IplImage* doPyrDown(
IplImage* in,
int filter = IPL_GAUSSIAN_5x5)
{
// Best to make sure input image is divisible by two.
// assert( in->width%2 == 0 && in->height%2 == 0 );
IplImage* out = cvCreateImage(
cvSize( in->width/2, in->height/2 ),
in->depth,
in->nChannels
);
cvPyrDown( in, out );
return( out );
};
int main( int argc, char** argv )
{
IplImage* img_rgb = cvLoadImage("E:\lena.jpg");
IplImage* img_gry = cvCreateImage( cvSize( img_rgb->width,img_rgb->height ), img_rgb->depth, 1);
cvCvtColor(img_rgb, img_gry ,CV_BGR2GRAY);
IplImage* img_pyr = doPyrDown( img_gry, IPL_GAUSSIAN_5x5 );
IplImage* img_pyr2 = doPyrDown( img_pyr, IPL_GAUSSIAN_5x5 );
IplImage* img_cny = doCanny( img_pyr2, 10, 100, 3 );
cvNamedWindow("Example Gray", CV_WINDOW_AUTOSIZE );
cvNamedWindow("Example Pyr", CV_WINDOW_AUTOSIZE );
cvNamedWindow("Example Canny", CV_WINDOW_AUTOSIZE );
cvShowImage("Example Gray", img_gry );
cvShowImage("Example Pyr", img_pyr2 );
cvShowImage("Example Canny", img_cny );
cvWaitKey(0);
cvReleaseImage( &img_rgb);
cvReleaseImage( &img_gry);
cvReleaseImage( &img_pyr);
cvReleaseImage( &img_pyr2);
cvReleaseImage( &img_cny);
cvDestroyWindow("Example Gray");
cvDestroyWindow("Example Pyr");
cvDestroyWindow("Example Canny");
}
|