Point *convex_hull(Point *points, int n, int *m)参数分别是什么
时间: 2024-02-10 15:01:37 浏览: 34
参数说明如下:
- `points`:点的数组,每个点是一个二维坐标点。类型为 `Point*`。
- `n`:点的个数,即数组 `points` 的长度。类型为 `int`。
- `m`:返回凸包上点的个数,即凸包的顶点数。类型为 `int*`,需要传入地址,函数内部会修改该值。
函数的返回值是一个 `Point*` 类型的数组,表示凸包上的点。
相关问题
import cv2 import math def cal_ang(start, center, end): point_1 = start point_2 = center point_3 = end a = math.sqrt( (point_2[0] - point_3[0]) * (point_2[0] - point_3[0]) + (point_2[1] - point_3[1]) * (point_2[1] - point_3[1])) b = math.sqrt( (point_1[0] - point_3[0]) * (point_1[0] - point_3[0]) + (point_1[1] - point_3[1]) * (point_1[1] - point_3[1])) c = math.sqrt( (point_1[0] - point_2[0]) * (point_1[0] - point_2[0]) + (point_1[1] - point_2[1]) * (point_1[1] - point_2[1])) A = math.degrees(math.acos((a * a - b * b - c * c) / (-2 * b * c))) B = math.degrees(math.acos((b * b - a * a - c * c) / (-2 * a * c))) C = math.degrees(math.acos((c * c - a * a - b * b) / (-2 * a * b))) return B img = cv2.imread('46.png') gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY) ret,thresh = cv2.threshold(gray, 70, 255, cv2.THRESH_BINARY) contours,hierarchy=cv2.findContours(thresh,cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_NONE) hull = cv2.convexHull(contours[0],returnPoints=False) defects = cv2.convexityDefects(contours[0],hull) start = end = (0,0) for i in range(0,defects.shape[0]): s,e,f,d = defects[i,0] start = tuple(contours[0][s][0]) end = tuple(contours[0][e][0]) far = tuple(contours[0][f][0]) if d > 5000: cv2.line(img,start,end,[0,255,0],2) cv2.circle(img,end,5,[0,0,255],-1) cv2.circle(img,start,5,[0,0,255],-1) break cv2.imshow('find', img) center,radius = cv2.minEnclosingCircle(contours[0]) cv2.circle(img,(int(center[0]),int(center[1])),8,(255,0,255),-1) cv2.circle(img,end,8,[255,0,0],-1) cv2.circle(img,start,8,[255,0,0],-1) cv2.line(img,start,(int(center[0]),int(center[1])),[0,0,255],2) cv2.line(img,end,(int(center[0]),int(center[1])),[0,0,255],2) angle = cal_ang(start,center,end) print('angle = %0.2f' % angle) length = (1 - angle / 360.0) * math.pi * radius * 2 print((angle / 360.0)) print('radius = %0.2f' % radius) strL = 'length=%0.2f' % length cv2.putText(img,strL,(int(center[0]-40),int(center[1]+40)),0,0.8,(0,255,0),2) cv2.imshow('result', img) angle_1 = cal_ang(start, center, ((center[0]+100),(center[1]))) angle_2 = cal_ang(end, center, ((center[0]+100),(center[1]))) cv2.ellipse(img,(int(center[0]),int(center[1])),(int(radius),int(radius)),0,-angle_1,0,(255,0,255),2, cv2.LINE_AA) cv2.ellipse(img,(int(center[0]),int(center[1])),(int(radius),int(radius)),0,0,angle_2,(255,0,255),2,cv2.LINE_AA) cv2.imshow('result', img) cv2.imwrite('result.png',img) cv2.waitKey(0) cv2.destroyAllWindows(),将这段代码转换为c++
以下是将Python代码转换为C++的结果。注意,由于数据类型和库函数的区别,代码可能需要进行微调才能正常运行。
```c++
#include <iostream>
#include <opencv2/opencv.hpp>
#include <math.h>
using namespace std;
using namespace cv;
double cal_ang(Point start, Point center, Point end) {
Point2f point_1 = start;
Point2f point_2 = center;
Point2f point_3 = end;
double a = sqrt(pow(point_2.x - point_3.x, 2) + pow(point_2.y - point_3.y, 2));
double b = sqrt(pow(point_1.x - point_3.x, 2) + pow(point_1.y - point_3.y, 2));
double c = sqrt(pow(point_1.x - point_2.x, 2) + pow(point_1.y - point_2.y, 2));
double A = acos((a * a - b * b - c * c) / (-2 * b * c)) * 180 / CV_PI;
double B = acos((b * b - a * a - c * c) / (-2 * a * c)) * 180 / CV_PI;
double C = acos((c * c - a * a - b * b) / (-2 * a * b)) * 180 / CV_PI;
return B;
}
int main() {
Mat img = imread("46.png");
Mat gray;
cvtColor(img, gray, COLOR_BGR2GRAY);
threshold(gray, gray, 70, 255, THRESH_BINARY);
vector<vector<Point>> contours;
vector<Vec4i> hierarchy;
findContours(gray, contours, hierarchy, RETR_EXTERNAL, CHAIN_APPROX_NONE);
vector<vector<Point>> hull(contours.size());
vector<vector<int>> hullsI(contours.size());
vector<vector<Vec4i>> defects(contours.size());
convexHull(contours[0], hull[0], false);
convexHull(contours[0], hullsI[0], false);
if (hullsI[0].size() > 0) {
Point2f* pts = new Point2f[hullsI[0].size()];
for (size_t i = 0; i < hullsI[0].size(); i++) {
pts[i] = contours[0][hullsI[0][i]];
}
int n = hullsI[0].size();
convexityDefects(pts, n, hullsI[0], defects[0]);
delete[] pts;
}
Point start, end;
for (int i = 0; i < defects[0].size(); i++) {
Vec4i& v = defects[0][i];
int startidx = v[0];
Point ptStart(contours[0][startidx]);
int endidx = v[1];
Point ptEnd(contours[0][endidx]);
int faridx = v[2];
Point ptFar(contours[0][faridx]);
if (v[3] > 5000) {
line(img, ptStart, ptEnd, Scalar(0, 255, 0), 2);
circle(img, ptEnd, 5, Scalar(0, 0, 255), -1);
circle(img, ptStart, 5, Scalar(0, 0, 255), -1);
start = ptStart;
end = ptEnd;
break;
}
}
Point2f center;
float radius = 0;
minEnclosingCircle(contours[0], center, radius);
circle(img, center, 8, Scalar(255, 0, 255), -1);
circle(img, end, 8, Scalar(255, 0, 0), -1);
circle(img, start, 8, Scalar(255, 0, 0), -1);
line(img, start, Point(center), Scalar(0, 0, 255), 2);
line(img, end, Point(center), Scalar(0, 0, 255), 2);
double angle = cal_ang(start, center, end);
cout << "angle = " << angle << endl;
double length = (1 - angle / 360.0) * CV_PI * radius * 2;
cout << angle / 360.0 << endl;
cout << "radius = " << radius << endl;
stringstream ss;
ss << "length=" << fixed << setprecision(2) << length;
String strL = ss.str();
putText(img, strL, Point(center.x - 40, center.y + 40), 0, 0.8, Scalar(0, 255, 0), 2);
double angle_1 = cal_ang(start, center, Point(center.x + 100, center.y));
double angle_2 = cal_ang(end, center, Point(center.x + 100, center.y));
ellipse(img, center, Size(radius, radius), 0, -angle_1, 0, Scalar(255, 0, 255), 2, LINE_AA);
ellipse(img, center, Size(radius, radius), 0, 0, angle_2, Scalar(255, 0, 255), 2, LINE_AA);
imshow("result", img);
imwrite("result.png", img);
waitKey(0);
destroyAllWindows();
return 0;
}
```
C语言用蛮力法解决凸包问题
凸包问题可以使用蛮力法来解决,其基本思想是考虑将所有点都进行组合,找到一个能够包含所有点的凸多边形。
具体步骤如下:
1. 枚举所有点对,找到其中的一条直线分割点集为两个部分,每个部分内部都不包含其他点。
2. 对于两个部分,分别递归地执行第1步,直到只剩下一个点或者没有点。
3. 最后将所有的凸多边形合并成一个凸包。
下面是一个使用蛮力法求解凸包的C语言实现代码示例:
```c
#include <stdio.h>
struct Point {
int x;
int y;
};
int cross_product(struct Point a, struct Point b, struct Point c) {
return (b.x - a.x) * (c.y - a.y) - (b.y - a.y) * (c.x - a.x);
}
int is_left_turn(struct Point a, struct Point b, struct Point c) {
return cross_product(a, b, c) > 0;
}
int is_right_turn(struct Point a, struct Point b, struct Point c) {
return cross_product(a, b, c) < 0;
}
void find_upper_hull(struct Point *points, int n, struct Point *hull, int *hull_size) {
hull[0] = points[0];
hull[1] = points[1];
*hull_size = 2;
for (int i = 2; i < n; i++) {
while (*hull_size > 1 && is_right_turn(hull[*hull_size - 2], hull[*hull_size - 1], points[i])) {
*hull_size -= 1;
}
hull[*hull_size] = points[i];
*hull_size += 1;
}
}
void find_lower_hull(struct Point *points, int n, struct Point *hull, int *hull_size) {
hull[0] = points[n - 1];
hull[1] = points[n - 2];
*hull_size = 2;
for (int i = n - 3; i >= 0; i--) {
while (*hull_size > 1 && is_right_turn(hull[*hull_size - 2], hull[*hull_size - 1], points[i])) {
*hull_size -= 1;
}
hull[*hull_size] = points[i];
*hull_size += 1;
}
}
void find_convex_hull(struct Point *points, int n, struct Point *hull, int *hull_size) {
struct Point upper_hull[n];
struct Point lower_hull[n];
find_upper_hull(points, n, upper_hull, hull_size);
find_lower_hull(points, n, lower_hull, hull_size);
for (int i = 0; i < *hull_size; i++) {
hull[i] = upper_hull[i];
}
for (int i = 1; i < *hull_size - 1; i++) {
hull[*hull_size + i - 1] = lower_hull[i];
}
*hull_size = 2 * (*hull_size - 1);
}
int main() {
struct Point points[] = {{0, 3}, {1, 1}, {2, 2}, {4, 4}, {0, 0}, {1, 2}, {3, 1}, {3, 3}};
int n = sizeof(points) / sizeof(points[0]);
struct Point hull[n];
int hull_size;
find_convex_hull(points, n, hull, &hull_size);
printf("Convex Hull:\n");
for (int i = 0; i < hull_size; i++) {
printf("(%d, %d)\n", hull[i].x, hull[i].y);
}
return 0;
}
```
该代码实现了一个简单的凸包求解算法,可以通过枚举所有点对,找到凸包的上下半部分,然后将它们合并成一个凸包。由于蛮力法的时间复杂度为O(n^3),因此对于大规模的数据集,该算法可能不太适合使用。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)