c_cpp 研究opencv3 4.3

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了c_cpp 研究opencv3 4.3相关的知识,希望对你有一定的参考价值。

#include <opencv2/opencv.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>

using namespace std;
using namespace cv;


#define WINDOW_WIDTH 600 //定义窗口大小的宏
#define WINDOW_NAME1 "【绘制图1】" // 为窗口标题定义的宏
#define WINDOW_NAME2 "【绘制图2】" //为窗口标题定义的宏
#define WINDOW_NAME "[案例第4章]"
//DrawEllipse() 函数
//自定义的绘制函数,实现绘制不同角度、相同尺寸的椭圆
void DrawEllipse(Mat img, double angle)
{
	int thicknes = 2;
	int lineType = 8;

	ellipse(img,
		Point(WINDOW_WIDTH/2, WINDOW_WIDTH/2), //椭圆中心点
		Size( WINDOW_WIDTH/4, WINDOW_WIDTH/16 ), //大小位于矩形 (WINDOW_WIDTH/4, WINDOW_WIDTH/16) 内
		angle, //椭圆旋转角度
		0, //扩展弧度 开始度数 0
		360, //扩展弧度 结束度数 360
		Scalar(255,129,0), //图像颜色 代表蓝色
		thicknes, //线宽
		lineType //线型(8联通线型)
		);
}

//自定义的绘制函数,实现同心圆的绘制
void DrawFilledCircle(Mat img, Point center)
{
	int thicknes = -1;
	int lineType = 8;
	circle(img,
		center, //原点
		WINDOW_WIDTH/32, //半径 
		Scalar(120, 0, 255), //颜色 BGR的格式
		thicknes,//线粗
		lineType 
		);
}

//自定义的绘制函数,实现了凹变形的绘制
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,120),
		lineType
		);
}


//自定义绘制函数,实现线的绘制
void DrawLine(Mat img, Point start, Point end)
{
	int thickness = 2;
	int lineType = 8;
	line(img,
		start,
		end,
		Scalar(0, 0, 255),
		thickness,
		lineType
		);
}

int main()
{
	//4.2.1 点的表示: Point 类
	Point point;
	point.x = 10;
	point.y = 8;
	//或者 Point point = Point(10, 8);
	//4.2.2 颜色的表示: Scalar 类
	Scalar scalar = Scalar(0, 120, 255); //定义的RGB值: 红色分量为 255,绿色分量为 120,蓝色分量为 0;数字只是代表参数所在的位置
	//4.2.3 尺寸的标识: Size 类
	Size sizeDemo = Size(5, 5); // 构造出的Size宽度和高度都为5,即 sizeDemo.width 和sizeDemo.height 都为5
	//4.2.4 矩阵的表示: Rect 类
	//4.2.5 颜色空间转换: cvtColor 类

	Mat srcImage(600, 600, CV_8UC3), tempImage, circleImage;
	srcImage.copyTo(tempImage);
	srcImage.copyTo(circleImage);

	DrawEllipse(tempImage,180);
	imshow("drawEllipse",tempImage);

	DrawFilledCircle(circleImage,Point(600/2, 600 / 2));
	imshow("drawfilledcircle", circleImage);


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

	//绘制化学中的原子示例图
	//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.1 先绘制出椭圆
	DrawPolygon( rookImage );

	//2.2 绘制矩形
	rectangle( rookImage,
		Point(0, 7* WINDOW_WIDTH/8),
		Point(WINDOW_WIDTH, WINDOW_WIDTH),
		Scalar(0,255,255),
		-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));

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

	waitKey(0);
	return 0;
}

以上是关于c_cpp 研究opencv3 4.3的主要内容,如果未能解决你的问题,请参考以下文章

c_cpp 研究opencv3 3.3

c_cpp 研究opencv3 1.6

c_cpp 研究opencv3 3.2.1

c_cpp 研究opencv3 2.6.2

ubuntu16.04+opencv3.4.3

ubuntu18.04安装opencv3.4.3遇到问题解决方法