如何使用c++实现OpenCV绘制图形旋转矩形 |
您所在的位置:网站首页 › opencv旋转矩形 › 如何使用c++实现OpenCV绘制图形旋转矩形 |
如何使用c++实现OpenCV绘制图形旋转矩形
发布时间:2021-08-30 20:39:49
来源:亿速云
阅读:269
作者:chen
栏目:开发技术
本篇内容介绍了“如何使用c++实现OpenCV绘制图形旋转矩形”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成! 目录功能函数 // 绘制旋转矩形 void DrawRotatedRect(cv::Mat mask,const cv::RotatedRect &rotatedrect,const cv::Scalar &color,int thickness, int lineType) { // 提取旋转矩形的四个角点 cv::Point2f ps[4]; rotatedrect.points(ps); // 构建轮廓线 std::vector tmpContours; // 创建一个InputArrayOfArrays 类型的点集 std::vector contours; for (int i = 0; i != 4; ++i) { contours.emplace_back(cv::Point2i(ps[i])); } tmpContours.insert(tmpContours.end(), contours); // 绘制轮廓,即旋转矩形 drawContours(mask, tmpContours, 0, color,thickness, lineType); // 填充mask }测试代码 #include #include using namespace std; using namespace cv; void DrawRotatedRect(cv::Mat mask, const cv::RotatedRect &rotatedrect, const cv::Scalar &color,int thickness, int lineType); int main() { cv::Mat src = imread("test.jpg"); cv::Mat result = src.clone(); cv::RotatedRect rorect(cv::Point(src.cols / 2, src.rows / 2), cv::Size(1000, 800), 50); DrawRotatedRect(result, rorect, cv::Scalar(0, 255, 255), 5,16); imshow("original", src); imshow("result", result); waitKey(0); return 0; } // 绘制旋转矩形 void DrawRotatedRect(cv::Mat mask,const cv::RotatedRect &rotatedrect,const cv::Scalar &color,int thickness, int lineType) { // 提取旋转矩形的四个角点 cv::Point2f ps[4]; rotatedrect.points(ps); // 构建轮廓线 std::vector tmpContours; // 创建一个InputArrayOfArrays 类型的点集 std::vector contours; for (int i = 0; i != 4; ++i) { contours.emplace_back(cv::Point2i(ps[i])); } tmpContours.insert(tmpContours.end(), contours); // 绘制轮廓,即旋转矩形 drawContours(mask, tmpContours, 0, color,thickness, lineType); // 填充mask }测试效果
图1 原图
图2 绘制旋转矩形 绘制旋转矩形首先需要得到旋转矩形的位置坐标,我经常配合cv::minAreaRect函数使用; 得到坐标信息后,结合绘制轮廓线的drawContours函数,即可完成。 “如何使用c++实现OpenCV绘制图形旋转矩形”的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识可以关注亿速云网站,小编将为大家输出更多高质量的实用文章! 推荐阅读: 使用OpenCV实现仿射变换—旋转功能 Python 旋转打印各种矩形的方法免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:[email protected]进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。 c++ opencv 上一篇新闻:python怎么用和matplotlib画一个魔方 下一篇新闻:Vue的vue.$set()方法源码案例详解 猜你喜欢 Java中方法重载和方法重写的区别有哪些 Java如何将PPT幻灯片转为HTML文件 springboot中常用注解有哪些 Java常用工具有哪些 Java如何实现分布式事务 Spring-Integration执行过程的示例分析 在foreach循环中JAVA集合不能添加或删除元素的原因是什么 python中opencv如何实现图片文本倾斜校正 如何使用SpringBoot将文件打包成zip存放或导出 C++中如何实现链表的排序算法 |
今日新闻 |
推荐新闻 |
CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3 |