2024-09-01
OpenCV
00

版本不一样的时候,返回也不一样。

我使用opencv/4.5.5。

下图是使用minAreaRect判定的角度,可以看到,数值范围是[0,90],看起来很离谱。

在这里插入图片描述

画出这张图使用的程序如下:

C++

cpp
int main() { std::string prefix1 = "/mnt/c/Users/dong.xie/Desktop/workcode/cpp_project/photo_translate/03implement/photo_translate/" "test_photo_translate/"; // 写一段程序,显示opencv中的角度是怎么计算的 // 构建一个全白色的4000*4000的图片 cv::Mat img = cv::Mat::zeros(4000, 4000, CV_8UC3); img.setTo(cv::Scalar(114, 114, 114)); // 遍历0度到360度 for (int i = 0; i < 360; i = i + 10) { //(2000,2000)与(3200,2000)是一条直线,(2000,2000)是原点,不断旋转这条直线,看看旋转后的直线的角度是多少 int p0x = 2000; int p0y = 2000; int p1x = 3200; int p1y = 2000; p1x = 1200 * std::cos(i * 3.1415926 / 180) + p0x; p1y = -1200 * std::sin(i * 3.1415926 / 180) + p0y; cv::Mat pts = (cv::Mat_<float>(4, 2) << p0x, p0y, p1x, p1y, p0x + 2, p0y + 2, p1x + 2, p1y + 2); // 绘制(p0x,p0y)与(p1x,p1y)这条直线 cv::line(img, cv::Point2f(p0x, p0y), cv::Point2f(p1x, p1y), cv::Scalar(0, 0, 255), 2); cv::RotatedRect rect = cv::minAreaRect(pts); // 画出旋转后的直线 cv::Point2f vertices[4]; rect.points(vertices); for (int i = 0; i < 4; i++) cv::line(img, vertices[i], vertices[(i + 1) % 4], cv::Scalar(0, 0, 255), 2); // 显示旋转后的直线的角度 cv::putText(img, std::to_string(rect.angle).substr(0, 5), cv::Point(p1x, p1y), cv::FONT_HERSHEY_SIMPLEX, 2, cv::Scalar(0, 255, 0), 2); } cv::imwrite(prefix1 + "test.jpg", img); return 0; }

那么python中呢,如果版本是一样的,那么结果是一样的,如果版本不同,比如4.4,那么就不会是一样的结果。

python
import cv2 import numpy as np print(cv2.__version__) img = np.zeros((4000, 4000, 3), np.uint8) img[:] = 114 for i in range(0, 360, 10): p0x = 2000 p0y = 2000 p1x = 3200 p1y = 2000 p1x = 1200 * np.cos(i * np.pi / 180) + p0x p1y = -1200 * np.sin(i * np.pi / 180) + p0y pts = np.array([[p0x, p0y], [p1x, p1y], [p0x + 2, p0y + 2], [p1x + 2, p1y + 2]], np.float32) cv2.line(img, (int(p0x), int(p0y)), (int(p1x), int(p1y)), (0, 0, 255), 2) rect = cv2.minAreaRect(pts) box = cv2.boxPoints(rect) box = np.int0(box) cv2.drawContours(img, [box], 0, (0, 0, 255), 2) cv2.putText(img, str(rect[2])[0:5], (int(p1x), int(p1y)), cv2.FONT_HERSHEY_SIMPLEX, 2, (0, 255, 0), 2) cv2.imwrite('test.jpg', img)

在这里插入图片描述

如果对你有用的话,可以打赏哦
打赏
ali pay
wechat pay

本文作者:Dong

本文链接:

版权声明:本博客所有文章除特别声明外,均采用 CC BY-NC。本作品采用《知识共享署名-非商业性使用 4.0 国际许可协议》进行许可。您可以在非商业用途下自由转载和修改,但必须注明出处并提供原作者链接。 许可协议。转载请注明出处!