OpenCV要点总结——第四章 常用数据结构和函数

1.Mat、基础图像容器

(1)Mat类包括:矩阵头和矩阵

Mat A;//仅创建信息头部分
A=imread("1.jpg",CV_LOAD_IMAGE_COLOR);//为矩阵开辟内存

(2)Mat创建

Mat M(2,2,CV_8UC3,Scalar(0,0,255));//2行 2*3列 矩阵

note:为已存在的IplImage指针创建信息头

IplImage* img=cvLoadImage("1.jpg",1);
Mat mtx(img);//转换为Mat

(3)格式化输出

默认

paython:Formatter::FMT_PYTHON

CSV;Numpy;C:类同

2.常用数据结构

(1)点 Point

用法:

point(x,y)

note:Point_<int>、Point2i、Point等价

        Point_<float>、Point2f等价

(2)颜色Scalar

Scalar()表示四个元素的数组 OpenCV中多表示为颜色

Scalar(蓝,绿,红)

(3)尺寸Size

size(5,5);//宽  高

(4)矩形Rect

Rect(x,y,width,height);//左上角点 长和宽

note:tl()返回左上角点坐标,br()返回右下角点坐标

(5)颜色空间转换cvtColor

Mat girl=imread("girl.jpg"),dstImage; //载入图像到Mat
namedWindow("【1】动漫图"); //创建一个名为 "【1】动漫图"的窗口  
imshow("【1】动漫图",girl);//显示名为 "【1】动漫图"的窗口
cvtColor(girl, dstImage, COLOR_BGR2HSV);//转换颜色空间
imshow("效果图", dstImage);

3.基本图形绘制

椭圆、圆、多边形、线、矩形代码分析

#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
using namespace cv;

//此程序对于OpenCV3版需要额外包含头文件:
#include <opencv2/imgproc/imgproc.hpp>

#define WINDOW_NAME1 "【绘制图1】"        //为窗口标题定义的宏 
#define WINDOW_NAME2 "【绘制图2】"        //为窗口标题定义的宏 
#define WINDOW_WIDTH 0//定义窗口大小的宏

void DrawEllipse( Mat img, double angle );//绘制椭圆
void DrawFilledCircle( Mat img, Point center );//绘制圆
void DrawPolygon( Mat img );//绘制多边形
void DrawLine( Mat img, Point start, Point end );//绘制线段





int main()
{

	// 创建空白的Mat图像
	Mat atomImage = Mat::zeros( WINDOW_WIDTH, WINDOW_WIDTH, CV_8UC3 );
	Mat rookImage = Mat::zeros( WINDOW_WIDTH, WINDOW_WIDTH, CV_8UC3 );

	ShowHelpText();
	// ---------------------<1>绘制化学中的原子示例图------------------------

	//【1.1】先绘制出椭圆
	DrawEllipse( atomImage, 90 );//所属图片 旋转角度
	DrawEllipse( atomImage, 0 );
	DrawEllipse( atomImage, 45 );
	DrawEllipse( atomImage, -45 );

	//【1.2】再绘制圆心
	DrawFilledCircle( atomImage, Point( WINDOW_WIDTH/2, WINDOW_WIDTH/2) );//所属图片 圆心

	// ----------------------------<2>绘制组合图-----------------------------
	//【2.1】先绘制出多边形
	DrawPolygon( rookImage );//所属图片

	// 【2.2】绘制矩形
	rectangle( rookImage,//所属图片
		Point( 0, 7*WINDOW_WIDTH/8 ),//左上角点
		Point( WINDOW_WIDTH, WINDOW_WIDTH),//右下角点
		Scalar( 0, 255, 255 ),//颜色
		-1,//线宽thickness=-1
		8 );//线型

	// 【2.3】绘制一些线段
	DrawLine( rookImage, Point( 0, 15*WINDOW_WIDTH/16 ), Point( WINDOW_WIDTH, 15*WINDOW_WIDTH/16 ) );//所属图片 起点终点
	DrawLine( rookImage, Point( WINDOW_WIDTH/4, 7*WINDOW_WIDTH/8 ), Point( WINDOW_WIDTH/4, WINDOW_WIDTH ) );
	DrawLine( rookImage, Point( WINDOW_WIDTH/2, 7*WINDOW_WIDTH/8 ), Point( WINDOW_WIDTH/2, WINDOW_WIDTH ) );
	DrawLine( rookImage, Point( 3*WINDOW_WIDTH/4, 7*WINDOW_WIDTH/8 ), Point( 3*WINDOW_WIDTH/4, WINDOW_WIDTH ) );

	// ---------------------------<3>显示绘制出的图像------------------------
	imshow( WINDOW_NAME1, atomImage );
	moveWindow( WINDOW_NAME1, 0, 200 );
	imshow( WINDOW_NAME2, rookImage );
	moveWindow( WINDOW_NAME2, WINDOW_WIDTH, 200 );

	waitKey( 0 );
	return(0);
}



//-------------------------------【DrawEllipse( )函数】--------------------------------
//		描述:自定义的绘制函数,实现了绘制不同角度、相同尺寸的椭圆
//-----------------------------------------------------------------------------------------
void DrawEllipse( Mat img, double angle )
{
	int thickness = 2;
	int lineType = 8;

	ellipse( img,
		Point( WINDOW_WIDTH/2, WINDOW_WIDTH/2 ),//中心点
		Size( WINDOW_WIDTH/4, WINDOW_WIDTH/16 ),//大小
		angle,
		0,
		360,
		Scalar( 255, 129, 0 ),//蓝色
		thickness,//线宽
		lineType );//线型
}


//-----------------------------------【DrawFilledCircle( )函数】---------------------------
//		描述:自定义的绘制函数,实现了实心圆的绘制
//-----------------------------------------------------------------------------------------
void DrawFilledCircle( Mat img, Point center )
{
	int thickness = -1;
	int lineType = 8;

	circle( img,
		center,//圆心
		WINDOW_WIDTH/32,//半径
		Scalar( 0, 0, 255 ),//红色
		thickness,
		lineType );
}


//-----------------------------------【DrawPolygon( )函数】--------------------------
//		描述:自定义的绘制函数,实现了凹多边形的绘制
//--------------------------------------------------------------------------------------
void DrawPolygon( Mat img )
{
	int lineType = 8;

	//创建一些点
	Point rookPoints[1][20];
	rookPoints[0][0]  = Point(    WINDOW_WIDTH/4,   7*WINDOW_WIDTH/8 );
	rookPoints[0][1]  = Point(  3*WINDOW_WIDTH/4,   7*WINDOW_WIDTH/8 );
	rookPoints[0][2]  = Point(  3*WINDOW_WIDTH/4,  13*WINDOW_WIDTH/16 );
	rookPoints[0][3]  = Point( 11*WINDOW_WIDTH/16, 13*WINDOW_WIDTH/16 );
	rookPoints[0][4]  = Point( 19*WINDOW_WIDTH/32,  3*WINDOW_WIDTH/8 );
	rookPoints[0][5]  = Point(  3*WINDOW_WIDTH/4,   3*WINDOW_WIDTH/8 );
	rookPoints[0][6]  = Point(  3*WINDOW_WIDTH/4,     WINDOW_WIDTH/8 );
	rookPoints[0][7]  = Point( 26*WINDOW_WIDTH/40,    WINDOW_WIDTH/8 );
	rookPoints[0][8]  = Point( 26*WINDOW_WIDTH/40,    WINDOW_WIDTH/4 );
	rookPoints[0][9]  = Point( 22*WINDOW_WIDTH/40,    WINDOW_WIDTH/4 );
	rookPoints[0][10] = Point( 22*WINDOW_WIDTH/40,    WINDOW_WIDTH/8 );
	rookPoints[0][11] = Point( 18*WINDOW_WIDTH/40,    WINDOW_WIDTH/8 );
	rookPoints[0][12] = Point( 18*WINDOW_WIDTH/40,    WINDOW_WIDTH/4 );
	rookPoints[0][13] = Point( 14*WINDOW_WIDTH/40,    WINDOW_WIDTH/4 );
	rookPoints[0][14] = Point( 14*WINDOW_WIDTH/40,    WINDOW_WIDTH/8 );
	rookPoints[0][15] = Point(    WINDOW_WIDTH/4,     WINDOW_WIDTH/8 );
	rookPoints[0][16] = Point(    WINDOW_WIDTH/4,   3*WINDOW_WIDTH/8 );
	rookPoints[0][17] = Point( 13*WINDOW_WIDTH/32,  3*WINDOW_WIDTH/8 );
	rookPoints[0][18] = Point(  5*WINDOW_WIDTH/16, 13*WINDOW_WIDTH/16 );
	rookPoints[0][19] = Point(    WINDOW_WIDTH/4,  13*WINDOW_WIDTH/16 );

	const Point* ppt[1] = { rookPoints[0] };//顶点集
	int npt[] = { 20 };//顶点数

	fillPoly( img,
		ppt,
		npt,
		1,//多边形数量
		Scalar( 255, 255, 255 ),//白色
		lineType );
}


//-----------------------------------【DrawLine( )函数】--------------------------
//		描述:自定义的绘制函数,实现了线的绘制
//---------------------------------------------------------------------------------
void DrawLine( Mat img, Point start, Point end )
{
	int thickness = 2;
	int lineType = 8;
	line( img,
		start,
		end,
		Scalar( 0, 0, 0 ),//黑色
		thickness,
		lineType );
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值