一、读入图片显示
[objc] view plain copy
1. #include "cv.h"
2. #include "cxcore.h"
3. #include "highgui.h"
4.
5. #if 1
6. using namespace cv;
7. int main() {
8. //声明IplImage指针
9. IplImage* pImg;
10. //载入图片
11. "../Lena.jpg",CV_LOAD_IMAGE_COLOR); //C:\2.jpg 要使用'/' 而不是windows的 '\'
12. if(NULL==pImg)
13. {
14. "image can't read!\n");
15. return -1;
16. }
17. // Mat image(pImg); //有这句话,为什么会出错
18. //创建窗口
19. //cvNamedWindow("Image",CV_WINDOW_AUTOSIZE); // 为什么注释掉这句话,程序尼玛也运行
20. //显示图像
21. "Image",pImg);
22. //等待按键
23. 0);
24. //销毁窗口
25. "Image");
26. //释放图像
27. // 为什么我把这条语句放在cvWaitKey前面时,窗口任然有图像显示,放在cvShowImage前面时才没有图片显示
28. return 0;
29. }
30. #else
31. #if 0
32. using namespace cv; //下面函数要cv前缀
33. int main()
34. {
35. Mat image;
36. "C:/2.jpg");
37. "li",image);
38. waitKey();
39. return 0;
40. }
41. #else
42. using namespace cv;//下面的所有cv相关类型不用加上前缀了,这里的前缀为cv::
43. int main(int argc, char* argv[])
44. {
45. "Lena.jpg"); //声明Mat变量并调入lena的照片,图片放在工程目录中直接使用"Lena.jpg"不行,要把图片放在工程目录的上一层
46. //然后使用../Lena.jpg,对于使用vs的这使用..\\Lena.jpg,第一个\为转义字符
47.
48. if(!img.data) //判断图片调入是否成功
49. return -1; //调入图片失败则退出
50. //创建窗口,并确定其为大小不可变类型窗口
51. "loveLena", CV_WINDOW_AUTOSIZE);
52.
53. //显示图片。如果你不介意窗口大小可变,可以直接注释掉上一句。因为imshow可以直接创建窗口
54. "loveLena", img);
55.
56. //等待按键
57.
58. return 0;
59. }
60.
61. #endif
62. #endif
二、图像创建、保存和复制
最终在工程目录上一级目录中生成copy.jpg
[objc] view plain copy
1. #include "cv.h"
2. #include "highgui.h"
3.
4. int main( int argc, char** argv )
5. {
6. //声明IplImage指针
7.
8. //载入图像,强制转化为Gray
9. "../Lena.jpg", 0);
10. if(pImg != NULL )
11. {
12. 2 = cvCreateImage(cvGetSize(pImg),
13. pImg->depth,
14. pImg->nChannels);
15. 2, NULL);
16.
17. "../copy.jpg", pImg2);//把图像写入文件
18.
19. "Image", 1 );//创建窗口
20. "Image", pImg );//显示图像
21.
22. 0); //等待按键
23.
24. "Image" );//销毁窗口
25. //释放图像
26. 2 ); //释放图像
27. return 0;
28. }
29.
30. return -1;
31. }
三、canny边缘检测
重点函数:
[c] view plain copy
1. cvCanny<span style="color:#009900">(</span>pImg<span style="color:#339933">,</span> pCannyImg<span style="color:#339933">,</span> <span style="color:#0000dd">50</span><span style="color:#339933">,</span> <span style="color:#0000dd">150</span><span style="color:#339933">,</span> <span style="color:#0000dd">3</span><span style="color:#009900">)</span><span style="color:#339933">;</span>
[objc] view plain copy
1. #include "cv.h"
2. #include "cxcore.h"
3. #include "highgui.h"
4.
5. int main( int argc, char** argv )
6. {
7.
8. //声明IplImage指针
9. NULL;
10. NULL;
11.
12. //载入图像,强制转化为Gray
13. "../Lena.jpg", 0);
14.
15. if(pImg != NULL )
16.
17. {
18. //为canny边缘图像申请空间
19. pCannyImg = cvCreateImage(cvGetSize(pImg),
20. 8U,
21. 1);
22.
23. //canny边缘检测
24. 50, 150, 3);
25.
26. //创建窗口
27. "src", 1);
28. "canny",1);
29.
30. //显示图像
31. "src", pImg );
32. "canny", pCannyImg );
33. 0); //等待按键
34.
35. //销毁窗口
36. "src" );
37. "canny" );
38.
39. //释放图像
40. cvReleaseImage( &pImg );
41. cvReleaseImage( &pCannyImg );
42. return 0;
43. }
44.
45. return -1;
46. }
四、轮廓(contour)检测
[objc] view plain copy
1. #ifdef _CH_
2. #pragma package <opencv>
3. #endif
4.
5. #ifndef _EiC
6. #include "cv.h"
7. #include "cxcore.h"
8. #include "highgui.h"
9. #include <math.h>
10.
11. #endif
12. #define w 500
13. int levels = 3;
14. CvSeq* contours = 0;
15.
16. void on_trackbar(int pos)
17.
18. {
19. 8, 3 );
20. CvSeq* _contours = contours;
21. int _levels = levels - 3;
22.
23. if( _levels <= 0 ) // get to the nearest face to make it look more funny
24.
25. _contours = _contours->h_next->h_next->h_next;
26. cvZero( cnt_img );
27. 255,0,0), CV_RGB(0,255,0), _levels, 3, CV_AA, cvPoint(0,0) );
28. "contours", cnt_img );
29. cvReleaseImage( &cnt_img );
30. }
31.
32. int main( int argc, char** argv )
33. {
34. int i, j;
35. 0);
36. 8, 1 );
37. cvZero( img );
38.
39. for( i=0; i < 6; i++ )
40. {
41. int dx = (i%2)*250 - 30;
42. int dy = (i/2)*150;
43. 255);
44. 0);
45.
46. if( i == 0 )
47. {
48. for( j = 0; j <= 10; j++ )
49. {
50. double angle = (j+5)*CV_PI/21;
51. 100+j*10-80*cos(angle)),
52. 100-90*sin(angle))),
53. 100+j*10-30*cos(angle)),
54. 100-30*sin(angle))), white, 1, 8, 0);
55. }
56. }
57.
58. 150, dy+100), cvSize(100,70), 0, 0, 360, white, -1, 8, 0 );
59. 115, dy+70), cvSize(30,20), 0, 0, 360, black, -1, 8, 0 );
60. 185, dy+70), cvSize(30,20), 0, 0, 360, black, -1, 8, 0 );
61. 115, dy+70), cvSize(15,15), 0, 0, 360, white, -1, 8, 0 );
62. 185, dy+70), cvSize(15,15), 0, 0, 360, white, -1, 8, 0 );
63. 115, dy+70), cvSize(5,5), 0, 0, 360, black, -1, 8, 0 );
64. 185, dy+70), cvSize(5,5), 0, 0, 360, black, -1, 8, 0 );
65. 150, dy+100), cvSize(10,5), 0, 0, 360, black, -1, 8, 0 );
66. 150, dy+150), cvSize(40,10), 0, 0, 360, black, -1, 8, 0 );
67. 27, dy+100), cvSize(20,35), 0, 0, 360, white, -1, 8, 0 );
68. 273, dy+100), cvSize(20,35), 0, 0, 360, white, -1, 8, 0 );
69. }
70.
71. "image", 1 );
72. "image", img );
73.
74. sizeof(CvContour),
75. 0,0) );
76.
77. // comment this out if you do not want approximation
78. sizeof(CvContour), storage, CV_POLY_APPROX_DP, 3, 1 );
79. "contours", 1 );
80. "levels+3", "contours", &levels, 7, on_trackbar );
81.
82. 0);
83. 0);
84. cvReleaseMemStorage( &storage );
85. cvReleaseImage( &img );
86. return 0;
87.
88. }
89. #ifdef _EiC
90. main(1,"");
91. #endif
五、图像旋转与缩放
[objc] view plain copy
1. //按Esc键退出
2. #include "cv.h"
3. #include "highgui.h"
4. #include "math.h"
5.
6. int main (int argc, charchar **argv)
7. {
8. IplImage *src = 0;
9. IplImage *dst = 0;
10.
11. /* the first command line parameter must be image file name */
12. "../Lena.jpg", -1);
13.
14. if (src != 0)
15. {
16. int delta = 1;
17. int angle = 0;
18. int opt = 0; // 1: 旋转加缩放
19. // 0: 仅仅旋转
20.
21. double factor;
22. dst = cvCloneImage (src);
23. "src", 1);
24. "src", src);
25.
26. for (;;)
27. {
28. float m[6];
29.
30. // Matrix m looks like:
31. //
32. // [ m0 m1 m2 ] ===> [ A11 A12 b1 ]
33. // [ m3 m4 m5 ] [ A21 A22 b2 ]
34. //
35. 2, 3, CV_32F, m);
36. int w = src->width;
37. int h = src->height;
38.
39. if (opt) // 旋转加缩放
40. angle * CV_PI / 180.) + 1.0) * 2;
41. else // 仅仅旋转
42. 1;
43. 0] = (float) (factor * cos (-angle * 22 * CV_PI / 180.));
44. 1] = (float) (factor * sin (-angle * 22 * CV_PI / 180.));
45. 3] = -m[1];
46. 4] = m[0];
47.
48. // 将旋转中心移至图像中间
49. 2] = w * 0.5f;
50. 5] = h * 0.5f;
51.
52. // dst(x,y) = A * src(x,y) + b
53. cvZero (dst);
54. cvGetQuadrangleSubPix (src, dst, &M);
55. "dst", 1);
56. "dst", dst);
57.
58. if (cvWaitKey (1) == 27) //ESC
59. break;
60. int) (angle + delta) % 360;
61. // for-loop
62. }
63. return 0;
64. }
六、读入视频文件及运动物体检测
[objc] view plain copy
1. #include <stdio.h>
2. #include <cv.h>
3. #include <cxcore.h>
4. #include <highgui.h>
5.
6. int main( int argc, char** argv )
7. {
8. //声明IplImage指针
9. NULL;
10. NULL;
11. NULL;
12. NULL;
13. NULL;
14. NULL;
15. NULL;
16. int nFrmNum = 0;
17.
18. //创建窗口
19. "video", 1);
20. "background",1);
21. "foreground",1);
22.
23. //使窗口有序排列
24. "video", 30, 0);
25. "background", 360, 0);
26. "foreground", 690, 0);
27.
28. /*
29. if( argc > 2 )
30. {
31. fprintf(stderr, "Usage: bkgrd [video_file_name]\n");
32. return -1;
33. }
34. */
35.
36. //打开摄像头
37. // if (argc ==1)
38. if( !(pCapture = cvCaptureFromCAM(-1)))
39. {
40. "Can not open camera.\n");
41. return -2;
42. }
43.
44. //打开视频文件
45. /*
46. if(argc == 2)
47. if( !(pCapture = cvCaptureFromFile(argv[1])))
48. {
49. fprintf(stderr, "Can not open video file %s\n", argv[1]);
50. return -2;
51. }
52. */
53.
54. //逐帧读取视频
55. while(pFrame = cvQueryFrame( pCapture ))
56. {
57. nFrmNum++;
58. //如果是第一帧,需要申请内存,并初始化
59. if(nFrmNum == 1)
60. {
61. 8U,1);
62. 8U,1);
63. 32FC1);
64. 32FC1);
65. 32FC1);
66.
67. //转化成单通道图像再处理
68. 2GRAY);
69. 2GRAY);
70. cvConvert(pFrImg, pFrameMat);
71. cvConvert(pFrImg, pFrMat);
72. cvConvert(pFrImg, pBkMat);
73. }
74. else
75. {
76. 2GRAY);
77. cvConvert(pFrImg, pFrameMat);
78. //高斯滤波先,以平滑图像
79. //cvSmooth(pFrameMat, pFrameMat, CV_GAUSSIAN, 3, 0, 0);
80. //当前帧跟背景图相减
81. cvAbsDiff(pFrameMat, pBkMat, pFrMat);
82.
83. //二值化前景图
84. 60, 255.0, CV_THRESH_BINARY);
85. //进行形态学滤波,去掉噪音
86. //cvErode(pFrImg, pFrImg, 0, 1);
87. //cvDilate(pFrImg, pFrImg, 0, 1);
88.
89. //更新背景
90. 0.003, 0);
91. //将背景转化为图像格式,用以显示
92. cvConvert(pBkMat, pBkImg);
93.
94. //显示图像
95. "video", pFrame);
96. "background", pBkImg);
97. "foreground", pFrImg);
98.
99. //如果有按键事件,则跳出循环
100. //此等待也为cvShowImage函数提供时间完成显示
101. //等待时间可以根据CPU速度调整
102. if( cvWaitKey(2) >= 0 )
103. break;
104. }
105. }
106.
107. //销毁窗口
108. "video");
109. "background");
110. "foreground");
111.
112. //释放图像和矩阵
113. cvReleaseImage(&pFrImg);
114. cvReleaseImage(&pBkImg);
115. cvReleaseMat(&pFrameMat);
116. cvReleaseMat(&pFrMat);
117. cvReleaseMat(&pBkMat);
118. cvReleaseCapture(&pCapture);
119. return 0;
120. }
七、Hougn线段检测
[objc] view plain copy
1. /* This is a standalone program. Pass an image name as a first parameter of the program.
2. Switch between standard and probabilistic Hough transform by changing "#if 1" to "#if 0" and back */
3. #include <cv.h>
4. #include <highgui.h>
5. #include <math.h>
6.
7. int main(int argc, char** argv)
8. {
9. const char* filename = "../hough.png";
10. 0 );
11. IplImage* dst;
12. IplImage* color_dst;
13. 0);
14. 0;
15. int i;
16.
17. if( !src )
18. return -1;
19.
20. 8, 1 );
21. 8, 3 );
22.
23. 50, 200, 3 );
24. 2BGR );
25. #if 0
26. 2( dst, storage, CV_HOUGH_STANDARD, 1, CV_PI/180, 100, 0, 0 ); // 标准型
27.
28. for( i = 0; i < MIN(lines->total,100); i++ )
29. {
30. float* line = (float*)cvGetSeqElem(lines,i);
31. float rho = line[0];
32. float theta = line[1];
33. 1, pt2;
34. double a = cos(theta), b = sin(theta);
35. double x0 = a*rho, y0 = b*rho;
36. 1.x = cvRound(x0 + 1000*(-b));
37. 1.y = cvRound(y0 + 1000*(a));
38. 2.x = cvRound(x0 - 1000*(-b));
39. 2.y = cvRound(y0 - 1000*(a));
40. 1, pt2, CV_RGB(255,0,0), 3, CV_AA, 0 );
41. }
42. #else
43. 2( dst, storage, CV_HOUGH_PROBABILISTIC, 1, CV_PI/180, 50, 50, 10 ); //概率性
44. for( i = 0; i < lines->total; i++ )
45. {
46. CvPoint* line = (CvPoint*)cvGetSeqElem(lines,i);
47. 0], line[1], CV_RGB(255,0,0), 3, CV_AA, 0 );
48. }
49. #endif
50. "Source", 1 );
51. "Source", src );
52.
53. "Hough", 1 );
54. "Hough", color_dst );
55.
56. 0);
57.
58. return 0;
59. }
八、鼠标绘图
[objc] view plain copy
1. #ifdef _CH_
2. #pragma package <opencv>
3. #endif
4.
5. #include "cv.h"
6. #include "highgui.h"
7. #include <stdio.h>
8. #include <stdlib.h>
9.
10. IplImage* inpaint_mask = 0;
11. IplImage* img0 = 0, *img = 0, *inpainted = 0;
12. CvPoint prev_pt = {-1,-1};
13.
14. void on_mouse( int event, int x, int y, int flags, void* zhang)
15. {
16. if( !img )
17. return;
18.
19. if( event == CV_EVENT_LBUTTONUP || !(flags & CV_EVENT_FLAG_LBUTTON) )
20. 1,-1);
21. else if( event == CV_EVENT_LBUTTONDOWN )
22. prev_pt = cvPoint(x,y);
23.
24. else if( event == CV_EVENT_MOUSEMOVE && (flags & CV_EVENT_FLAG_LBUTTON) )
25. {
26. CvPoint pt = cvPoint(x,y);
27.
28. if( prev_pt.x < 0 )
29. prev_pt = pt;
30.
31. 255), 5, 8, 0 );
32. 255), 5, 8, 0 );
33. prev_pt = pt;
34. "image", img );
35. }
36. }
37.
38. int main( int argc, char** argv )
39.
40. {
41. char* filename = argc >= 2 ? argv[1] : (char*)"../Fruits.jpg";
42.
43. if( (img0 = cvLoadImage(filename,-1)) == 0 )
44. return 0;
45.
46. "Hot keys: \n"
47. "\tESC - quit the program\n"
48. "\tr - restore the original image\n"
49. "\ti or ENTER - run inpainting algorithm\n"
50. "\t\t(before running it, paint something on the image)\n" );
51. "image", 1 );
52. 0 );
53. 0 );
54. 8, 1 );
55. cvZero( inpaint_mask );
56. cvZero( inpainted );
57. "image", img );
58. "watershed transform", inpainted );
59. "image", on_mouse, 0 );
60.
61. for(;;)
62. {
63. int c = cvWaitKey(0);
64. if( (char)c == 27 )
65. break;
66. if( (char)c == 'r' )
67. {
68. cvZero( inpaint_mask );
69. 0, img,0 );
70. "image", img );
71. }
72.
73. if( (char)c == 'i' || (char)c == '\n' )
74. {
75. "inpainted image", 1 );
76. 3, CV_INPAINT_TELEA );
77. "inpainted image", inpainted );
78. }
79. }
80. return 1;
81. }
九、Yml文件读取与写入
[objc] view plain copy
1. /***********************************************************************
2.
3. * OpenCV 2.1 example
4.
5. ***********************************************************************/
6. #include "cv.h"
7. #include "highgui.h"
8. #include <iostream>
9.
10. using namespace cv; //下面的所有cv相关类型不用加上前缀了
11. using namespace std;
12.
13. int main(int argc, char* argv[])
14.
15. {
16. "../test.yml", FileStorage::WRITE); //写的形式打开yml。当然也可以打开xml,主要看后缀
17.
18. "i" << 5 << "r" << 3.1 << "str" << "ABCDEFGH"; //存入整型、浮点型、字符串
19. "../lena.jpg" ); //载入Lena妞的图片载入
20. "Lena_from_jpg", writeInImg ); //看一看Lena妞是否健在
21.
22. "lena" << writeInImg; //将Lena妞的图片矩阵插入test.yml
23. .release();
24. "test.yml", FileStorage::READ); //读的形式打开yml。当然也可以打开xml,主要看后缀
25.
26. if(readfs.isOpened() )
27. {
28. int i1 = (int)readfs["i"];
29. double r1 = (double)readfs["r"];
30. 1 = (string)readfs["str"];
31. Mat readOutImg;
32.
33. "lena"] >> readOutImg; //把Lena从yml中取出
34. "Lena_from_yml", readOutImg ); //看看是不是跟之前放进去的是同一个人
35. std::cout<<"read out i:"<<i1<<std::endl<<"read out r:"<<r1<<std::endl<<"read out str:"<<str1<<std::endl;
36.
37. }
38. .release();
39. waitKey();
40.
41. return 0;
42. }