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 * 完成时间:1:34 3/2 星期六 2013 23 */ 24 25 #define ROWS 400 // 矩阵行数 26 #define COLS 400 // 矩阵列数 27 28 // 定义绿色长方形的两个顶点 29 CvPoint point1 = cvPoint( 80 , 20 ); 30 CvPoint point2 = cvPoint( 160 , 80 ); 31 32 // Help function 33 // 检查当前点是否在要画的长方形区域内 34 bool isThePointInRectangle( int x, int y) 35 { 36 if ( (x <= point2.x) && (y <= point2.y) 37 & (x >= point1.x) && (y >= point1.y)) 38 return true ; 39 else 40 return false ; 41 } 42 43 int main() 44 { 45 // 三通道的二维字节类型矩阵 46 CvMat* mat = cvCreateMat(ROWS, COLS, CV_8UC3); 47 48 // 将所有数值初始化为零 49 cvZero(mat); 50 // 获得矩阵元素(0,0)的指针 51 uchar *p = (uchar*)cvPtr2D(mat, 0 , 0 ); 52 // 为矩阵赋值,画长方形 53 for ( int i = 0 ; i < mat->cols; i++ ) 54 { 55 for ( int j = 0 ; j < mat->rows; j++ ) 56 { 57 p = (uchar* )cvPtr2D(mat, i, j); 58 // 使其指向中间的通道 59 p++ ; 60 if (isThePointInRectangle(i, j) == true ) 61 { 62 *p = 255 ; 63 } 64 p++ ; 65 } 66 } 67 68 cvShowImage( " img " , mat); 69 cvWaitKey( 0 ); 70 71 cvReleaseMat(& mat); 72 cvDestroyAllWindows(); 73 74 return 0 ; 75 }
运行结果: