开发工具|openCV 学习笔记1, (tbb_debug错误, 学习opencv 例2-1, 2-2, 2-3, 2-4, 2-5, 2-6, 2-7, 2-8,22-9,2-0)

openCV心得(1) 资料《学习openCV》第二章主要介绍了一些比较常见以及有意思的函数和数据类型,让初步入门的学生对图像处理更有兴趣,虽然对函数的内在实验不明白,一些定义的常量的含义也不是很清楚,但是学习完第二章还是很开心的,至少知道了图像处理的一些基本,如轮廓处理等;
知识点:
a)图像的通道:
1通道指灰度图片,
3通道指RGB组合的图片
4通道是在3通道及基础上加了微软提出的透明度。
b)图像平滑处理:使线条更流畅。
c)图像的缩放:可惜现在只能进行2倍的缩放
d)图像轮廓处理:显示图像轮廓。
e)CV头文件中包含了很多图像处理的软件
f)highgui.h头文件中包含的是图像获得输出等的功能。
g)提示tbb_debug错误时,若机子是64为,且opencv中文论坛里的方法不行,那么可以试试这个链接里的方法:http://download.csdn.net/detail/threedonkey/4228886


下面是书上的例子

/* 显示摄像头里的内容 */#include #include int main(int argc, TCHAR* argv[]) { int c; // allocate memory for an image IplImage *img; // capture from video device #1 CvCapture* capture = cvCaptureFromCAM(0); // create a window to display the images cvNamedWindow("mainWin", CV_WINDOW_AUTOSIZE); // position the window cvMoveWindow("mainWin", 5, 5); while(1) { // retrieve the captured frame img=cvQueryFrame(capture); // show the image in the window cvShowImage("mainWin", img ); // wait 10 ms for a key to be pressed c=cvWaitKey(10); // escape key terminates program if(c == 27) break; } return 0; }



【开发工具|openCV 学习笔记1, (tbb_debug错误, 学习opencv 例2-1, 2-2, 2-3, 2-4, 2-5, 2-6, 2-7, 2-8,22-9,2-0)】//

/*获取一张图片并显示的窗口*/ int main(int argc, char**argv){ // 定义一个图像类,获取图片 IplImage *img = cvLoadImage( "_U__2IU1~S]UA~YQ{P5LA0I.jpg" ); // ? // 创建一个窗口,大小自动与图片对齐 cvNamedWindow( "donkeyAiMe", CV_WINDOW_AUTOSIZE ); // 在窗口显示图片 cvShowImage( "donkeyAiMe", img ); // 等待用户输入任意键结束 cvWaitKey(0); // 释放空间 cvReleaseImage( &img ); // 关闭窗口 cvDestroyWindow( "donkeyAiMe" ); return 0; }


//




/* 获取一个视频文件, 显示在窗口 */#include #include int main(){ // 创建一个窗口 cvNamedWindow( "donkeyaime", CV_WINDOW_AUTOSIZE ); // 获得avi文件,capture指向文件头 CvCapture* capture = cvCreateFileCapture( "car.avi" ); IplImage* frame; while(1) { // frame接收capture结构体中的当前帧信息,并且更新结构体信息,frame不需要开辟内存,cvCapture结构体已经分配好内存。 frame = cvQueryFrame( capture ); if( !frame ) break; // 将图像显示到刚才的窗口中 cvShowImage( "donkeyaime", frame); // 等待33ms用户输入, 输入为Esc,c == 27, 无输入时, c是-1 char c = cvWaitKey(33); if(c == 27) break; } // 释放结构体申请的内存,同时关闭打开的文件句柄 cvReleaseCapture( &capture ); // 关闭窗口 cvDestroyWindow( "donkeyaime" ); return 0; }


//



/* 读取一个视频文件, 并显示在窗口内,同时加入滚动条功能 */#include #include // 定义滚动条位置和CvCapture对象,由于是全局, 所以加个g_,好区分 int g_slider_position = 0; CvCapture* g_capture= NULL; // 拖动滚动条时自动调用此函数 void onTrackbarSlide(int pos){ // cvSetCaptureProperty 设置CvCapture对象的属性 // CV_CAP_PROP_POS_FRAMES 表示用视频帧数设置读入位置 cvSetCaptureProperty(g_capture, CV_CAP_PROP_POS_FRAMES, pos ); }int main(){ cvNamedWindow( "donkeyaime", CV_WINDOW_AUTOSIZE ); g_capture = cvCreateFileCapture( "car.avi" ); // cvGetCaptureProperty 获得CvCapture对象的属性 // frames获得视频总帧数 int frames = (int) cvGetCaptureProperty(g_capture, CV_CAP_PROP_FRAME_COUNT); if( frames ){ // cvCreateTrackbar 设置滚动条信息,名称,所属窗口, 位置, 位置上界,回调函数 cvCreateTrackbar("Position", "donkeyaime", &g_slider_position, frames, onTrackbarSlide); } IplImage* frame; while(1) { // frame接收capture结构体中的当前帧信息,并且更新结构体信息,frame不需要开辟内存,cvCapture结构体已经分配好内存。 frame = cvQueryFrame( g_capture ); if( !frame ) break; // 将图像显示到刚才的窗口中 cvShowImage( "donkeyaime", frame); // 滚动条前进 cvSetTrackbarPos 设置滚动条位置 g_slider_position ++; cvSetTrackbarPos( "Position", "donkeyaime", g_slider_position ); // 等待33ms用户输入, 输入为Esc,c == 27, 无输入时, c是-1 char c = cvWaitKey(33); if(c == 27) break; } // 释放结构体申请的内存,同时关闭打开的文件句柄 cvReleaseCapture( &g_capture ); // 关闭窗口 cvDestroyWindow( "donkeyaime" ); return 0; }


//

/* 对图像进行高斯平滑处理 */#include #include void exp2_4(IplImage * image){ // 新建两个窗口 cvNamedWindow( "donkeyaime_in" ); cvNamedWindow( "donkeyaime_out" ); cvShowImage( "donkeyaime_in", image ); // 创建一个拓片对象, 并设定其图像结构空间 IplImage* out = cvCreateImage( cvGetSize(image), IPL_DEPTH_8U, 3); // 对图像每个像素周围3*3区域进行高斯图像平滑处理 cvSmooth( image, out, CV_GAUSSIAN, 3, 3 ); cvShowImage( "donkeyaime_out", out ); cvReleaseImage( &out ); cvWaitKey( 0 ); cvDestroyWindow( "donkeyaime_in" ); cvDestroyWindow( "donkeyaime_out" ); }int main(){ IplImage* img; img = cvLoadImage("me.bmp"); exp2_4(img); return 0; }



//




/************************************************************************/ /*图像缩放,只能是按2倍关系缩放*/ /*文档链接:*/ /* http://www.opencv.org.cn/index.php/Cv%E5%9B%BE%E5%83%8F%E5%A4%84%E7%90%86#PyrDown*/ /************************************************************************/#include #include #include void doPyrDown( IplImage* in, int filter = IPL_GAUSSIAN_5x5 ){ // assert宏, 当括号内条件不满足时, 退出程序, 报告错误 assert(in->width % 2 == 0 && in->height % 2 == 0); // 创建一个IplImage对象,并初始化对象空间, 是原图像1/2 IplImage* out = cvCreateImage(cvSize(in->width / 2, in->height / 2), in->depth, in->nChannels); // 调用函数, 进行图片缩放, 函数属于 cv.h图像处理 cvPyrDown(in, out, filter); cvNamedWindow( "donkeyaime_out", CV_WINDOW_AUTOSIZE ); cvShowImage( "donkeyaime_out", out ); cvWaitKey(0); }void doPyrUp( IplImage* in, int filter = IPL_GAUSSIAN_5x5 ){ IplImage* out = cvCreateImage(cvSize(in->width * 2, in->height * 2), in->depth, in->nChannels); cvPyrUp( in, out, filter ); cvNamedWindow( "donkeyaime_out", CV_WINDOW_AUTOSIZE ); cvShowImage( "donkeyaime_out", out ); cvWaitKey(0); }int main(){ IplImage* img; cvNamedWindow( "donkeyaime_in", CV_WINDOW_AUTOSIZE ); img = cvLoadImage("me.bmp"); cvShowImage( "donkeyaime_in", img ); doPyrUp(img); cvWaitKey(0); cvReleaseImage(&img); cvDestroyWindow( "donkeyaime_in" ); return 0; }


//



/************************************************************************/ /*canny图像边缘处理,只能对灰度图片使用*/ /*canny 在 cv头文件中*/ /************************************************************************/#include #include #include #define low 500 #define high 501// 函数功能是按照输入阀门值将图片边缘检测出来 IplImage* doCanny( IplImage* in, double lowThresh, double highThresh, double aperture ){ if(in->nChannels != 1) return 0; IplImage* out = cvCreateImage( cvGetSize(in), IPL_DEPTH_8U, 1 ); cvCanny(in, out, lowThresh, highThresh, aperture); return out; }int main(){ IplImage * img, * out1, * out2; cvNamedWindow( "donkeyaime_in", CV_WINDOW_AUTOSIZE ); cvNamedWindow( "donkeyaime_out1", CV_WINDOW_AUTOSIZE ); cvNamedWindow( "donkeyaime_out2", CV_WINDOW_AUTOSIZE ); // 参数0表示将读入的图转化为单通道图片 img = cvLoadImage( "me.bmp", 0 ); cvShowImage( "donkeyaime_in", img ); out1 = doCanny( img, 0, 1, 3 ); out2 = doCanny(img, low, high, 3); cvShowImage( "donkeyaime_out1", out1 ); cvShowImage( "donkeyaime_out2", out2 ); cvWaitKey(0); cvReleaseImage( &img ); cvReleaseImage( &out1 ); cvReleaseImage( &out2 ); cvDestroyWindow( "donkeyaime_in" ); cvDestroyWindow( "doukeyaime_out1" ); cvDestroyWindow( "donkeyaime_out2" ); return 0; }

//
/************************************************************************/ /*图像缩放4倍后进行cvCanny边缘处理*/ /************************************************************************/#include #include #include IplImage* doPyrDown( IplImage* in, int filter = CV_GAUSSIAN_5x5 ){ IplImage * out; printf("%d %d\n", in->width, in->height ); assert( in->width % 2 == 0 && in->height % 2 == 0 ); out = cvCreateImage( cvSize( in->width / 2, in->height / 2 ), in->depth, in->nChannels ); cvPyrDown( in, out, filter ); return out; }IplImage* doCanny( IplImage* in, double lowThresh, double highThresh, double aperture ){ if( in->nChannels != 1 ) return 0; IplImage * out = cvCreateImage( cvGetSize( in ), in->depth, in->nChannels ); cvCanny( in, out, lowThresh, highThresh, aperture ); return out; }int main(){ IplImage * img, * out; cvNamedWindow( "donkeyaime_in", CV_WINDOW_AUTOSIZE ); cvNamedWindow( "donkeyaime_out", CV_WINDOW_AUTOSIZE ); img = cvLoadImage( "me1.jpg", 0 ); cvShowImage( "donkeyaime_in", img ); out = doPyrDown( img, CV_GAUSSIAN_5x5 ); out = doPyrDown( out, CV_GAUSSIAN_5x5 ); out = doCanny( out, 10, 100, 3 ); cvShowImage( "donkeyaime_out", out ); cvWaitKey( 0 ); cvReleaseImage( &img ); cvReleaseImage( &out ); cvDestroyWindow( "donkeyaime_in" ); cvDestroyWindow( "donkeyaime_out" ); return 0; }

//


/************************************************************************/ /*摄像头录入视频, 并进行边缘处理*/ /************************************************************************/#include #include IplImage* doCanny( IplImage* in, double lowThresh, double highThresh, double aperture ){ if(in->nChannels != 1) return 0; IplImage* out = cvCreateImage( cvGetSize(in), IPL_DEPTH_8U, 1 ); cvCanny(in, out, lowThresh, highThresh, aperture); return out; }int main(){ CvCapture* capture; capture = cvCreateCameraCapture(0); assert( capture != NULL ); IplImage * frame, *frameChange; cvNamedWindow( "donkeyaime_in" ); cvNamedWindow( "donkeyaime_out" ); while(1){ frame = cvQueryFrame( capture ); // 必须转化成灰度图片 frameChange = cvCreateImage( cvGetSize( frame ), frame->depth, 1 ); cvCvtColor( frame, frameChange, CV_BGR2GRAY ); frameChange = doCanny( frameChange, 10, 100, 3 ); cvShowImage( "donkeyaime_in", frame ); cvShowImage( "donkeyaime_out", frameChange ); char c = cvWaitKey(33); if(c == 27) break; } cvReleaseCapture( &capture ); cvDestroyWindow( "donkeyaime_in" ); cvDestroyWindow( "donkeyaime_out" ); return 0; }

//
/************************************************************************/ /*读取一个视频文件, 进行一些转换后重新写入视频文件中, */ /*可惜我估计是没有编码器的原因, 从写完后视频不能看了*/ /************************************************************************/#include #include int main(){ CvCapture* capture = 0; capture = cvCreateFileCapture( "car.avi" ); if( !capture ) return -1; IplImage * bgr_frame = cvQueryFrame( capture ); double fps = cvGetCaptureProperty( capture, CV_CAP_PROP_FPS ); CvSize size = cvSize( (int)cvGetCaptureProperty( capture, CV_CAP_PROP_FRAME_WIDTH ), (int)cvGetCaptureProperty( capture, CV_CAP_PROP_FRAME_HEIGHT ) ); CvVideoWriter *writer = cvCreateVideoWriter( "car.avi", CV_FOURCC( 'M', 'J', 'P', 'G' ), fps, size ); IplImage* logpolar_frame = cvCreateImage( size, IPL_DEPTH_8U, 3 ); while( (bgr_frame = cvQueryFrame( capture )) != NULL ){ cvLogPolar( bgr_frame, logpolar_frame, cvPoint2D32f(bgr_frame->width/2, bgr_frame->height/2), 40, CV_INTER_LINEAR+CV_WARP_FILL_OUTLIERS ); cvWriteFrame( writer, logpolar_frame ); } cvReleaseVideoWriter( &writer ); cvReleaseImage( &logpolar_frame ); cvReleaseCapture( &capture ); return 0; }







    推荐阅读