



















#include <opencv2/opencv.hpp> #include <vector> using namespace cv; using namespace std; // 计算向量叉积 int crossProduct(Point a, Point b, Point c) { Point v1 = {b.x - a.x, b.y - a.y}; Point v2 = {c.x - b.x, c.y - b.y}; return v1.x * v2.y - v1.y * v2.x; } // 判断角点类型 void detectCornerType(const vector<Point>& points) { int n = points.size(); for (int i = 0; i < n; i++) { int prev = (i > 0) ? i - 1 : n - 1; int next = (i < n - 1) ? i + 1 : 0; int cp = crossProduct(points[prev], points[i], points[next]); if (cp > 0) { cout << "内角点 at: " << points[i] << endl; } else if (cp < 0) { cout << "外角点 at: " << points[i] << endl; } else { cout << "直线点或平角点 at: " << points[i] << endl; } } } int main() { // 加载图像 Mat img = imread("path_to_image"); // 转换为灰度图 Mat gray; cvtColor(img, gray, COLOR_BGR2GRAY); // 二值化 Mat binary; threshold(gray, binary, 100, 255, THRESH_BINARY); // 查找轮廓 vector<vector<Point>> contours; findContours(binary, contours, RETR_EXTERNAL, CHAIN_APPROX_SIMPLE); // 近似多边形 vector<Point> approx; approxPolyDP(contours[0], approx, arcLength(contours[0], true) * 0.02, true); // 判断角点类型 detectCornerType(approx); return 0; }
此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。