opencv常用图像处理函数详解(二)

文章目录

      • 1.在图像上绘制几何形状
      • 2.在图像上写文字

1.在图像上绘制几何形状

  1. 绘制线段

    void cv::line(	InputOutputArray img,
    				Point pt1,
    				Point pt2,
    				const Scalar & 	color,
    				int thickness = 1,
    				int lineType = LINE_8,
    				int shift = 0 
    )		
    
    cv.line(img, pt1, pt2, color[, thickness[, lineType[, shift]]]) ->img
    
    说明:在图像上绘制连接两点的线段
    参数:
    	img	图像.
    	pt1	线段的起点.
    	pt2	线段的终点.
    	color 线段的颜色.
    	thickness 线段的粗细
    	lineType 线段的线型.
    	shift 点坐标中的小数位数.
    
  2. 绘制椭圆

    void cv::ellipse(	InputOutputArray img,
    					Point center,
    					Size axes,
    					double angle,
    					double startAngle,
    					double endAngle,
    					const Scalar & color,
    					int thickness = 1,
    					int lineType = LINE_8,
    					int shift = 0 
    )	 
    
    cv.ellipse(	img, center, axes, angle, startAngle, endAngle, color[, thickness[, lineType[, shift]]]	) ->img
    cv.ellipse(	img, box, color[, thickness[, lineType]]) ->img
    
    说明:绘制简单或粗椭圆弧或填充椭圆扇区
    参数:
    	img	图像.
    	center 椭圆的中点.
    	axes 椭圆主轴尺寸的一半.
    	angle 椭圆旋转角度(度).
    	startAngle 椭圆弧的起始角度(度)
    	endAngle 椭圆弧的终止角度(度).
    	color 椭圆弧的颜色.
    	thickness 线段的粗细
    	lineType 线段的线型.
    	shift 点坐标中的小数位数.
    

opencv常用图像处理函数详解(二)_第1张图片

  1. 绘制矩形

    void cv::rectangle(	InputOutputArray img,
    					Point pt1,
    					Point pt2,
    					const Scalar & 	color,
    					int thickness = 1,
    					int lineType = LINE_8,
    					int shift = 0 
    )		
    
    cv.rectangle(img, pt1, pt2, color[, thickness[, lineType[, shift]]]) ->img
    
    说明:在图像上绘制矩形
    参数:
    	img	图像.
    	pt1	矩形的左上角坐标点.
    	pt2	矩形的右下角坐标点.
    	color 线段的颜色.
    	thickness 线段的粗细
    	lineType 线段的线型.
    	shift 点坐标中的小数位数.
    
  2. 绘制圆

    void cv::circle(	InputOutputArray img,
    					Point center,
    					int radius,
    					const Scalar & 	color,
    					int thickness = 1,
    					int lineType = LINE_8,
    					int shift = 0 
    )		
    
    cv.circle(img, center, radius, color[, thickness[, lineType[, shift]]]) ->img
    
    说明:在图像上绘制圆
    参数:
    	img	图像.
    	pt1	圆形的中心点.
    	radius	圆形的半径.
    	color 线段的颜色.
    	thickness 线段的粗细
    	lineType 线段的线型.
    	shift 点坐标中的小数位数.
    
  3. 绘制多边形

    void cv::fillPoly(	InputOutputArray img,
    					InputArrayOfArrays pts,
    					const Scalar & 	color,
    					int thickness = 1,
    					int lineType = LINE_8,
    					Point offset = Point() 
    )		
    
    cv.fillPoly(img, pts, color[, lineType[, shift[, offset]]]) ->img
    
    说明:在图像上绘制圆
    参数:
    	img	图像.
    	pts	多边形阵列,其中每个多边形表示为点阵列.
    	color 线段的颜色.
    	thickness 线段的粗细
    	lineType 线段的线型.
    	offset 偏移量.
    

例程:

#include 
#include 
#include 

#define w 400 //定义图像的大小
using namespace cv;

void MyEllipse( Mat img, double angle );
void MyFilledCircle( Mat img, Point center );
void MyPolygon( Mat img );
void MyLine( Mat img, Point start, Point end );

int main( void ){
  //定义两个窗口的名称,用来显示绘制好的图像
  char atom_window[] = "Drawing 1: Atom";
  char rook_window[] = "Drawing 2: Rook";

  //绘制图像的矩阵
  Mat atom_image = Mat::zeros( w, w, CV_8UC3 );
  Mat rook_image = Mat::zeros( w, w, CV_8UC3 );

  //绘制多个椭圆
  MyEllipse( atom_image, 90 );
  MyEllipse( atom_image, 0 );
  MyEllipse( atom_image, 45 );
  MyEllipse( atom_image, -45 );

  //绘制圆
  MyFilledCircle( atom_image, Point( w/2, w/2) );

  //绘制多边形
  MyPolygon( rook_image );
	
  //绘制矩形
  rectangle( rook_image, Point( 0, 7*w/8 ), Point( w, w), Scalar( 0, 255, 255 ), FILLED, LINE_8 );
  
  //绘制直线
  MyLine( rook_image, Point( 0, 15*w/16 ), Point( w, 15*w/16 ) );
  MyLine( rook_image, Point( w/4, 7*w/8 ), Point( w/4, w ) );
  MyLine( rook_image, Point( w/2, 7*w/8 ), Point( w/2, w ) );
  MyLine( rook_image, Point( 3*w/4, 7*w/8 ), Point( 3*w/4, w ) );

  //显示图像
  imshow( atom_window, atom_image );
  moveWindow( atom_window, 0, 200 );
  imshow( rook_window, rook_image );
  moveWindow( rook_window, w, 200 );
  waitKey( 0 );
  return(0);
}

void MyEllipse( Mat img, double angle )
{
  int thickness = 2;
  int lineType = 8;
  ellipse( img, Point( w/2, w/2 ), Size( w/4, w/16 ), angle, 0, 360, Scalar( 255, 0, 0 ), thickness, lineType );
}

void MyFilledCircle( Mat img, Point center )
{
  circle( img, center, w/32, Scalar( 0, 0, 255 ), FILLED, LINE_8 );
}

void MyPolygon( Mat img )
{
  int lineType = LINE_8;
  Point rook_points[1][20];
  rook_points[0][0]  = Point(    w/4,   7*w/8 );
  rook_points[0][1]  = Point(  3*w/4,   7*w/8 );
  rook_points[0][2]  = Point(  3*w/4,  13*w/16 );
  rook_points[0][3]  = Point( 11*w/16, 13*w/16 );
  rook_points[0][4]  = Point( 19*w/32,  3*w/8 );
  rook_points[0][5]  = Point(  3*w/4,   3*w/8 );
  rook_points[0][6]  = Point(  3*w/4,     w/8 );
  rook_points[0][7]  = Point( 26*w/40,    w/8 );
  rook_points[0][8]  = Point( 26*w/40,    w/4 );
  rook_points[0][9]  = Point( 22*w/40,    w/4 );
  rook_points[0][10] = Point( 22*w/40,    w/8 );
  rook_points[0][11] = Point( 18*w/40,    w/8 );
  rook_points[0][12] = Point( 18*w/40,    w/4 );
  rook_points[0][13] = Point( 14*w/40,    w/4 );
  rook_points[0][14] = Point( 14*w/40,    w/8 );
  rook_points[0][15] = Point(    w/4,     w/8 );
  rook_points[0][16] = Point(    w/4,   3*w/8 );
  rook_points[0][17] = Point( 13*w/32,  3*w/8 );
  rook_points[0][18] = Point(  5*w/16, 13*w/16 );
  rook_points[0][19] = Point(    w/4,  13*w/16 );
  const Point* ppt[1] = { rook_points[0] };
  int npt[] = { 20 };
  fillPoly( img, ppt, npt, 1, Scalar( 255, 255, 255 ), lineType );
}

void MyLine( Mat img, Point start, Point end )
{
  int thickness = 2;
  int lineType = LINE_8;
  line( img, start, end, Scalar( 0, 0, 0 ), thickness, lineType );
}

opencv常用图像处理函数详解(二)_第2张图片

2.在图像上写文字

例程:

#include 
#include 
#include 
#include 
#include 

using namespace cv;

//全局变量
const int NUMBER = 8; //每个图形绘制的次数
const int DELAY = 5; //每次绘制图形后等待的时间ms

const int window_width = 900;
const int window_height = 600;
int x_1 = -window_width/2;
int x_2 = window_width*3/2;
int y_1 = -window_width/2;
int y_2 = window_width*3/2;

//函数声明
static Scalar randomColor( RNG& rng );
int Drawing_Random_Lines( Mat image, char* window_name, RNG rng );
int Drawing_Random_Rectangles( Mat image, char* window_name, RNG rng );
int Drawing_Random_Ellipses( Mat image, char* window_name, RNG rng );
int Drawing_Random_Polylines( Mat image, char* window_name, RNG rng );
int Drawing_Random_Filled_Polygons( Mat image, char* window_name, RNG rng );
int Drawing_Random_Circles( Mat image, char* window_name, RNG rng );
int Displaying_Random_Text( Mat image, char* window_name, RNG rng );
int Displaying_Big_End( Mat image, char* window_name, RNG rng );


/**
 * @function main
 */
int main( void )
{
  int c; // 用于显示错误代码的int变量

  //窗口名称,用于命名后续显示图像用的窗口
  char window_name[] = "Drawing_2 Tutorial";

  //创建一个随机数生成对象
  RNG rng( 0xFFFFFFFF );

  //创建一个以0填充的图像(黑色图像)
  Mat image = Mat::zeros( window_height, window_width, CV_8UC3 );

  //将图像显示在窗口上
  imshow( window_name, image );
  waitKey( DELAY );

  //第一步,先随机画几条线
  c = Drawing_Random_Lines(image, window_name, rng);
  if( c != 0 ) return 0;

  //第二步,绘制矩形
  c = Drawing_Random_Rectangles(image, window_name, rng);
  if( c != 0 ) return 0;

  //第三步,绘制椭圆
  c = Drawing_Random_Ellipses( image, window_name, rng );
  if( c != 0 ) return 0;

  //第四步,绘制多边形
  c = Drawing_Random_Polylines( image, window_name, rng );
  if( c != 0 ) return 0;

  //第五步,绘制填充的多边形
  c = Drawing_Random_Filled_Polygons( image, window_name, rng );
  if( c != 0 ) return 0;

  //第六步,绘制圆
  c = Drawing_Random_Circles( image, window_name, rng );
  if( c != 0 ) return 0;

  //第七步,绘制文字
  c = Displaying_Random_Text( image, window_name, rng );
  if( c != 0 ) return 0;

  //第八步,显示最终的文字
  c = Displaying_Big_End( image, window_name, rng );
  if( c != 0 ) return 0;

  waitKey(0);
  return 0;
}

/// Function definitions

/**
 * @function randomColor
 * @brief 从随机数中生成随机颜色
 */
static Scalar randomColor( RNG& rng )
{
  int icolor = (unsigned) rng;
  return Scalar( icolor&255, (icolor>>8)&255, (icolor>>16)&255 );
}


/**
 * @function Drawing_Random_Lines
 * @brief 在图像上画出随机线条
 */
int Drawing_Random_Lines( Mat image, char* window_name, RNG rng )
{
  Point pt1, pt2;

  for( int i = 0; i < NUMBER; i++ )
  {
    pt1.x = rng.uniform( x_1, x_2 );
    pt1.y = rng.uniform( y_1, y_2 );
    pt2.x = rng.uniform( x_1, x_2 );
    pt2.y = rng.uniform( y_1, y_2 );

    line( image, pt1, pt2, randomColor(rng), rng.uniform(1, 10), 8 );
    imshow( window_name, image );
    if( waitKey( DELAY ) >= 0 )
      { return -1; }
  }

  return 0;
}

/**
 * @function Drawing_Rectangles
 * @brief 在图像上画出随机矩形
 */
int Drawing_Random_Rectangles( Mat image, char* window_name, RNG rng )
{
  Point pt1, pt2;
  int lineType = 8;
  int thickness = rng.uniform( -3, 10 );

  for( int i = 0; i < NUMBER; i++ )
  {
    pt1.x = rng.uniform( x_1, x_2 );
    pt1.y = rng.uniform( y_1, y_2 );
    pt2.x = rng.uniform( x_1, x_2 );
    pt2.y = rng.uniform( y_1, y_2 );

    rectangle( image, pt1, pt2, randomColor(rng), MAX( thickness, -1 ), lineType );

    imshow( window_name, image );
    if( waitKey( DELAY ) >= 0 )
      { return -1; }
  }

  return 0;
}

/**
 * @function Drawing_Random_Ellipses
 * @brief 在图像上画出随机椭圆
 */
int Drawing_Random_Ellipses( Mat image, char* window_name, RNG rng )
{
  int lineType = 8;

  for ( int i = 0; i < NUMBER; i++ )
  {
    Point center;
    center.x = rng.uniform(x_1, x_2);
    center.y = rng.uniform(y_1, y_2);

    Size axes;
    axes.width = rng.uniform(0, 200);
    axes.height = rng.uniform(0, 200);

    double angle = rng.uniform(0, 180);

    ellipse( image, center, axes, angle, angle - 100, angle + 200,
             randomColor(rng), rng.uniform(-1,9), lineType );

    imshow( window_name, image );
    if( waitKey(DELAY) >= 0 )
      { return -1; }
  }

  return 0;
}

/**
 * @function Drawing_Random_Polylines
 * @brief 在图像上画出随机多边形
 */
int Drawing_Random_Polylines( Mat image, char* window_name, RNG rng )
{
  int lineType = 8;

  for( int i = 0; i< NUMBER; i++ )
  {
    Point pt[2][3];
    pt[0][0].x = rng.uniform(x_1, x_2);
    pt[0][0].y = rng.uniform(y_1, y_2);
    pt[0][1].x = rng.uniform(x_1, x_2);
    pt[0][1].y = rng.uniform(y_1, y_2);
    pt[0][2].x = rng.uniform(x_1, x_2);
    pt[0][2].y = rng.uniform(y_1, y_2);
    pt[1][0].x = rng.uniform(x_1, x_2);
    pt[1][0].y = rng.uniform(y_1, y_2);
    pt[1][1].x = rng.uniform(x_1, x_2);
    pt[1][1].y = rng.uniform(y_1, y_2);
    pt[1][2].x = rng.uniform(x_1, x_2);
    pt[1][2].y = rng.uniform(y_1, y_2);

    const Point* ppt[2] = {pt[0], pt[1]};
    int npt[] = {3, 3};

    polylines(image, ppt, npt, 2, true, randomColor(rng), rng.uniform(1,10), lineType);

    imshow( window_name, image );
    if( waitKey(DELAY) >= 0 )
      { return -1; }
  }
  return 0;
}

/**
 * @function Drawing_Random_Filled_Polygons
 * @brief 在图像上画出随机填充多边形
 */
int Drawing_Random_Filled_Polygons( Mat image, char* window_name, RNG rng )
{
  int lineType = 8;

  for ( int i = 0; i < NUMBER; i++ )
  {
    Point pt[2][3];
    pt[0][0].x = rng.uniform(x_1, x_2);
    pt[0][0].y = rng.uniform(y_1, y_2);
    pt[0][1].x = rng.uniform(x_1, x_2);
    pt[0][1].y = rng.uniform(y_1, y_2);
    pt[0][2].x = rng.uniform(x_1, x_2);
    pt[0][2].y = rng.uniform(y_1, y_2);
    pt[1][0].x = rng.uniform(x_1, x_2);
    pt[1][0].y = rng.uniform(y_1, y_2);
    pt[1][1].x = rng.uniform(x_1, x_2);
    pt[1][1].y = rng.uniform(y_1, y_2);
    pt[1][2].x = rng.uniform(x_1, x_2);
    pt[1][2].y = rng.uniform(y_1, y_2);

    const Point* ppt[2] = {pt[0], pt[1]};
    int npt[] = {3, 3};

    fillPoly( image, ppt, npt, 2, randomColor(rng), lineType );

    imshow( window_name, image );
    if( waitKey(DELAY) >= 0 )
       { return -1; }
  }
  return 0;
}

/**
 * @function Drawing_Random_Circles
 * @brief 在图像上画出随机圆
 */
int Drawing_Random_Circles( Mat image, char* window_name, RNG rng )
{
  int lineType = 8;

  for (int i = 0; i < NUMBER; i++)
  {
    Point center;
    center.x = rng.uniform(x_1, x_2);
    center.y = rng.uniform(y_1, y_2);

    circle( image, center, rng.uniform(0, 300), randomColor(rng),
            rng.uniform(-1, 9), lineType );

    imshow( window_name, image );
    if( waitKey(DELAY) >= 0 )
      { return -1; }
  }

  return 0;
}

/**
 * @function Displaying_Random_Text
 * @brief 在图像上画出随机打印出字
 */
int Displaying_Random_Text( Mat image, char* window_name, RNG rng )
{
  int lineType = 8;

  for ( int i = 1; i < NUMBER; i++ )
  {
    Point org;
    org.x = rng.uniform(x_1, x_2);
    org.y = rng.uniform(y_1, y_2);

    putText( image, "Testing text rendering", org, rng.uniform(0,8),
             rng.uniform(0,100)*0.05+0.1, randomColor(rng), rng.uniform(1, 10), lineType);

    imshow( window_name, image );
    if( waitKey(DELAY) >= 0 )
      { return -1; }
  }

  return 0;
}

/**
 * @function Displaying_Big_End
 */
int Displaying_Big_End( Mat image, char* window_name, RNG )
{
  Size textsize = getTextSize("OpenCV forever!", FONT_HERSHEY_COMPLEX, 3, 5, 0);
  Point org((window_width - textsize.width)/2, (window_height - textsize.height)/2);
  int lineType = 8;

  Mat image2;

  for( int i = 0; i < 255; i += 10 )
  {
    image2 = image - Scalar::all(i);
    putText( image2, "OpenCV forever!", org, FONT_HERSHEY_COMPLEX, 3,
             Scalar(i, i, 255), 5, lineType );

    imshow( window_name, image2 );
    if( waitKey(DELAY) >= 0 )
       { return -1; }
  }

  return 0;
}

你可能感兴趣的:(opencv,opencv,图像处理,计算机视觉)