版本:opencv-4.7.0-windows
Opencv中,大多数时候,逆时针旋转是正方向,但在RotatedRect和ellipse中,顺时针旋转是正方向。
//RotatedRect的角度参数是顺时针为正方向
RotatedRect(const Point2f& center, const Size2f& size, float angle);
//ellipse的angle、startAngle、endAngle都是顺时针为正方向
void ellipse(InputOutputArray img, Point center, Size axes,
double angle, double startAngle, double endAngle,
const Scalar& color, int thickness = 1,
int lineType = LINE_8, int shift = 0);
源码说明:
代码测试:
#include <opencv2/opencv.hpp>
int main()
{
#pragma region RotatedRect顺时针为正
cv::Mat image = cv::Mat(500, 500, CV_8UC3, cv::Scalar(255,255,255));
cv::Scalar color(0, 0, 255);
cv::Point2f center1 = cv::Point2f(100, 100);
cv::Size2f axes1 = cv::Size2f(100, 50);
cv::RotatedRect rotatedRect(center1, axes1, 0);
cv::Point2f vertices[4];
rotatedRect.points(vertices);
for (int i = 0; i < 4; i++) {
cv::line(image, vertices[i], vertices[(i + 1) % 4], color, 1, cv::LINE_AA);
}
cv::Point2f center2 = cv::Point2f(300, 100);
cv::RotatedRect rotatedRect1(center2, axes1, 5);
rotatedRect1.points(vertices);
for (int i = 0; i < 4; i++) {
cv::line(image, vertices[i], vertices[(i + 1) % 4], color, 1, cv::LINE_AA);
}
#pragma endregion
#pragma region ellipse顺时针为正
cv::Point center3 = cv::Point(50,300);
int radius1 = 50;
int startAngle = 0;
int endAngle = 90;
cv::ellipse(image, center3, cv::Size(radius1, radius1), 0, startAngle, endAngle, color, 5, cv::LINE_AA);
cv::Point center4 = cv::Point(250, 300);
endAngle = 180;
cv::ellipse(image, center4, cv::Size(radius1, radius1), 0, startAngle, endAngle, color, 5, cv::LINE_AA);
cv::Point center5 = cv::Point(400, 300);
cv::ellipse(image, center5, cv::Size(radius1, radius1), 30, startAngle, endAngle, color, 5, cv::LINE_AA);
cv::imshow("Image", image);
cv::waitKey(0);
cv::destroyAllWindows();
#pragma region ellipse顺时针为正
}
测试结果: