《学习OpenCV》练习题第三章第四题

 1 #include <highgui.h>

 2 #include <cv.h>

 3 #include <stdio.h>

 4 

 5 #pragma comment (lib,"opencv_calib3d231d.lib")

 6 #pragma comment (lib,"opencv_contrib231d.lib")

 7 #pragma comment (lib,"opencv_core231d.lib")

 8 #pragma comment (lib,"opencv_features2d231d.lib")

 9 #pragma comment (lib,"opencv_flann231d.lib")

10 #pragma comment (lib,"opencv_gpu231d.lib")

11 #pragma comment (lib,"opencv_haartraining_engined.lib")

12 #pragma comment (lib,"opencv_highgui231d.lib")

13 #pragma comment (lib,"opencv_imgproc231d.lib")

14 #pragma comment (lib,"opencv_legacy231d.lib")

15 #pragma comment (lib,"opencv_ml231d.lib")

16 #pragma comment (lib,"opencv_objdetect231d.lib")

17 #pragma comment (lib,"opencv_ts231d.lib")

18 #pragma comment (lib,"opencv_video231d.lib")

19 

20 /*

21  *《学习OpenCV》第三章第四题

22  * 完成时间:2:13 3/2 星期六 2013

23  */

24 

25 // 图像宽高宏定义

26 #define IMG_WIDTH  400

27 #define IMG_HEIGHT 400

28 

29 // 两个顶点 

30 CvPoint point1 = cvPoint(80, 20);

31 CvPoint point2 = cvPoint(160, 80);

32 

33 // Help function

34 // 检查当前点是否在要画的长方形区域内

35 bool isThePointInRectangle(int x, int y)

36 {

37     if( (x <= point2.x) && (y <= point2.y) 

38         & (x >= point1.x) && (y >= point1.y))

39         return true;

40     else 

41         return false;

42 }

43 

44 

45 int main()

46 {

47     // 创建一个三通道RGB图像

48     IplImage* image = cvCreateImage(cvSize(IMG_WIDTH, IMG_HEIGHT), 8, 3 );

49     

50     // 将矩阵元素全部置零

51     cvZero(image);

52     uchar* p;

53     for(int i = 0; i < image->height; i++)

54     {

55         for(int j = 0; j < image->width; j++)

56         {

57             p = cvPtr2D(image, i, j, 0);

58             if(isThePointInRectangle(i, j) == true)

59             {

60                 *(++p) = 255;

61             }

62         }

63     }

64     cvNamedWindow("img", 1);

65     cvShowImage("img", image);

66     cvWaitKey(0);

67 

68     cvReleaseImage(&image);

69     cvDestroyWindow("img");

70 

71     return 0; 

72 }

运行结果:

《学习OpenCV》练习题第三章第四题

你可能感兴趣的:(opencv)