使用c++實現(xiàn)OpenCV繪制旋轉(zhuǎn)矩形圖形
更新時間:2021年08月30日 17:28:53 作者:翟天保Steven
這篇文章主要給大家介紹了使用c++實現(xiàn)OpenCV繪制圖形旋轉(zhuǎn)矩形的方法案例,通過圖文及代碼形式進行了詳細的描述,有需要的朋友可以參考下,希望可以有所幫助
功能函數(shù)
// 繪制旋轉(zhuǎn)矩形
void DrawRotatedRect(cv::Mat mask,const cv::RotatedRect &rotatedrect,const cv::Scalar &color,int thickness, int lineType)
{
// 提取旋轉(zhuǎn)矩形的四個角點
cv::Point2f ps[4];
rotatedrect.points(ps);
// 構建輪廓線
std::vector<std::vector<cv::Point>> tmpContours; // 創(chuàng)建一個InputArrayOfArrays 類型的點集
std::vector<cv::Point> contours;
for (int i = 0; i != 4; ++i) {
contours.emplace_back(cv::Point2i(ps[i]));
}
tmpContours.insert(tmpContours.end(), contours);
// 繪制輪廓,即旋轉(zhuǎn)矩形
drawContours(mask, tmpContours, 0, color,thickness, lineType); // 填充mask
}
測試代碼
#include <iostream>
#include <opencv2/opencv.hpp>
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;
}
// 繪制旋轉(zhuǎn)矩形
void DrawRotatedRect(cv::Mat mask,const cv::RotatedRect &rotatedrect,const cv::Scalar &color,int thickness, int lineType)
{
// 提取旋轉(zhuǎn)矩形的四個角點
cv::Point2f ps[4];
rotatedrect.points(ps);
// 構建輪廓線
std::vector<std::vector<cv::Point>> tmpContours;
// 創(chuàng)建一個InputArrayOfArrays 類型的點集
std::vector<cv::Point> contours;
for (int i = 0; i != 4; ++i) {
contours.emplace_back(cv::Point2i(ps[i]));
}
tmpContours.insert(tmpContours.end(), contours);
// 繪制輪廓,即旋轉(zhuǎn)矩形
drawContours(mask, tmpContours, 0, color,thickness, lineType); // 填充mask
}
測試效果
繪制旋轉(zhuǎn)矩形首先需要得到旋轉(zhuǎn)矩形的位置坐標,我經(jīng)常配合cv::minAreaRect函數(shù)使用;
得到坐標信息后,結合繪制輪廓線的drawContours函數(shù),即可完成。
以上就是使用c++實現(xiàn)OpenCV繪制圖形旋轉(zhuǎn)矩形的詳細內(nèi)容,更多關于c++實現(xiàn)OpenCV繪制的資料請關注腳本之家其它相關文章!
相關文章
C語言經(jīng)典例程100例(經(jīng)典c程序100例)
這篇文章主要介紹了C語言經(jīng)典例程100例,經(jīng)典c程序100例,學習c語言的朋友可以參考一下2018-03-03
C/C++實現(xiàn)數(shù)字與字符串互相轉(zhuǎn)換的多種方法
在C/C++程序中,會需要把數(shù)字與字符串做出互相轉(zhuǎn)換的操作,用于實現(xiàn)程序想要的效果,下面將介紹多種方法實現(xiàn)數(shù)字與字符串互相轉(zhuǎn)換,文中有詳細的代碼示例供大家參考,需要的朋友可以參考下2024-08-08

