欢迎来到代码驿站!

C代码

当前位置:首页 > 软件编程 > C代码

OpenCV画任意圆弧曲线

时间:2021-03-03 10:08:30|栏目:C代码|点击:

逆时针画圆弧,原理:将360度分割成36份,分别标出每10度角度时的坐标点,然后将每个点连接起来。 

#include <iostream> 
#include <opencv2\core\core.hpp>
#include <opencv2\opencv.hpp> 
#include <opencv2\highgui\highgui.hpp> 
#include <opencv2\contrib\contrib.hpp> 
 
#include <fstream> 
#include <windows.h>
 
using namespace cv;
using namespace std;
 
// 图像、圆心、开始点、结束点、线宽
void DrawArc(Mat *src, Point ArcCenter, Point StartPoint, Point EndPoint, int Fill)
{
 if (Fill <= 0) return;
 
 vector<Point> Dots;
 double Angle1 = atan2((StartPoint.y - ArcCenter.y), (StartPoint.x - ArcCenter.x));
 double Angle2 = atan2((EndPoint.y - ArcCenter.y), (EndPoint.x - ArcCenter.x));
 double Angle = Angle1 - Angle2;
 Angle = Angle * 180.0 / CV_PI;
 
 if (Angle < 0) Angle = 360 + Angle;
 if (Angle == 0) Angle = 360;
 int brim = floor(Angle / 10); // 向下取整
 
 Dots.push_back(StartPoint);
 for (int i = 0; i < brim; i++)
 {
 double dSinRot = sin(-(10 * (i + 1)) * CV_PI / 180);
 double dCosRot = cos(-(10 * (i + 1)) * CV_PI / 180);
 int x = ArcCenter.x + dCosRot * (StartPoint.x - ArcCenter.x) - dSinRot * (StartPoint.y - ArcCenter.y);
 int y = ArcCenter.y + dSinRot * (StartPoint.x - ArcCenter.x) + dCosRot * (StartPoint.y - ArcCenter.y);
 Dots.push_back(Point(x, y));
 }
 Dots.push_back(EndPoint);
 RNG &rng = theRNG();
 Scalar color = Scalar(rng.uniform(100, 255), rng.uniform(100, 255), rng.uniform(100, 255));
 for (int i = 0; i < Dots.size() - 1; i++) {
 line(*src, Dots[i], Dots[i + 1], color, Fill);
 }
 Dots.clear();
}
 
int main()
{
 Mat Img = Mat::zeros(800, 800, CV_8UC3);
 int64 tim = getTickCount();
 
 // 坐标零点 400,400
 Point ZeroPoint = Point(400, 400);
 // 起始坐标 150,-100
 Point StartPoint = Point(ZeroPoint.x +150, ZeroPoint.y - (-100));
 // 结束坐标 -150,-100
 Point EndPoint = Point(ZeroPoint.x - 150, ZeroPoint.y - (-100));
 
 // 圆心相对起始点的坐标 -150,200
 int I = StartPoint.x - 150;
 int J = StartPoint.y - (+200);
 
 Point Arc = Point(I, J);
 
 // 显示圆心坐标
 circle(Img, Arc, 5, Scalar(0, 0, 255), -1);
 // 显示起始点坐标
 circle(Img, StartPoint, 5, Scalar(255, 0, 0), -1);
 // 显示结束点坐标
 circle(Img, EndPoint, 5, Scalar(0, 255, 0), -1);
 
 // 图像、圆心、开始点、结束点、线宽
 DrawArc(&Img, Arc, StartPoint, EndPoint, 2);
 imshow("正多边形", Img);
 
 tim = getTickCount() - tim;
 printf("处理耗时: %fms\n\n", tim * 1000 / getTickFrequency());
 waitKey(0);
 return 0;
}

效果如下:

上一篇:C++中关于委派(Delegates)的实现示例

栏    目:C代码

下一篇:C++ 中的Lambda表达式写法

本文标题:OpenCV画任意圆弧曲线

本文地址:http://www.codeinn.net/misctech/73459.html

推荐教程

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:914707363 | 邮箱:codeinn#126.com(#换成@)

Copyright © 2020 代码驿站 版权所有