截取图像
cv:: Mat pic2;
pic ( cv:: Rect ( 50 , 50 , 200 , 200 ) ) . copyTo ( pic2) ;
cv:: imshow ( "Image Display2" , pic2) ;
将图像区域复制到图像指定的区域
pic2. copyTo ( pic ( cv:: Rect ( 200 , 200 , 200 , 200 ) ) ) ;
cv:: imshow ( "Image Display3" , pic) ;
通过迭代器获取图像像素
void ModifyColorData ( cv:: Mat& img , cv:: Mat& out , int pixel) { out = img. clone ( ) ; cv:: Mat_< cv:: Vec3b> :: iterator itstart = out. begin< cv:: Vec3b> ( ) ; cv:: Mat_< cv:: Vec3b> :: iterator itend = out. end< cv:: Vec3b> ( ) ; for ( ; itstart != itend; itstart++ ) { ( * itstart) [ 0 ] = ( * itstart) [ 0 ] / pixel * pixel + pixel / 3 ; ( * itstart) [ 1 ] = ( * itstart) [ 1 ] / pixel * pixel + pixel / 3 ; ( * itstart) [ 2 ] = ( * itstart) [ 2 ] / pixel * pixel + pixel / 3 ; } cv:: imshow ( "目标图像" , out) ;
}
绘制直线
void drawLine ( cv:: Mat& img) { cv:: Point p1 = cv:: Point ( 20 , 30 ) ; cv:: Point p2; p2. x = 250 ; p2. y = 30 ; cv:: Scalar myColor = cv:: Scalar ( 0 , 0 , 255 ) ; cv:: line ( img, p1, p2, myColor , 5 , cv:: LINE_8) ;
}
绘制矩形
void drawrectangle ( cv:: Mat& img) { cv:: Rect rect = cv:: Rect ( 50 , 50 , 250 , 300 ) ; cv:: Scalar color = cv:: Scalar ( 255 , 0 , 0 ) ; cv:: rectangle ( img, rect, color, 3 , cv:: LINE_8) ;
}
绘制圆
void drawcircle ( cv:: Mat& img) { cv:: Point point = cv:: Point ( img. cols / 2 , img. rows / 2 ) ; cv:: Scalar color = cv:: Scalar ( 0 , 255 , 0 ) ; cv:: circle ( img, point, 100 , color, 2 , cv:: LINE_8) ;
}
绘制椭圆
void drawellipse ( cv:: Mat& img) { cv:: Point point = cv:: Point ( img. cols / 2 , img. rows / 2 ) ; cv:: Scalar color = cv:: Scalar ( 0 , 255 , 255 ) ; cv:: ellipse ( img , point , cv:: Size ( 20 , 50 ) , 0 , 0 , 360 , color) ;
}
绘制多边形
void drawpolyline ( cv:: Mat& img) { std:: vector< cv:: Point> pts; pts. push_back ( cv:: Point ( 50 , 50 ) ) ; pts. push_back ( cv:: Point ( 200 , 50 ) ) ; pts. push_back ( cv:: Point ( 200 , 200 ) ) ; pts. push_back ( cv:: Point ( 50 , 200 ) ) ; cv:: polylines ( img , pts , true , cv:: Scalar ( 0 , 255 , 0 ) , 3 , cv:: LINE_8) ;
}