使用c++實(shí)現(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)矩形的四個(gè)角點(diǎn)
cv::Point2f ps[4];
rotatedrect.points(ps);
// 構(gòu)建輪廓線
std::vector<std::vector<cv::Point>> tmpContours; // 創(chuàng)建一個(gè)InputArrayOfArrays 類型的點(diǎn)集
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)矩形的四個(gè)角點(diǎn)
cv::Point2f ps[4];
rotatedrect.points(ps);
// 構(gòu)建輪廓線
std::vector<std::vector<cv::Point>> tmpContours;
// 創(chuàng)建一個(gè)InputArrayOfArrays 類型的點(diǎn)集
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)矩形的位置坐標(biāo),我經(jīng)常配合cv::minAreaRect函數(shù)使用;
得到坐標(biāo)信息后,結(jié)合繪制輪廓線的drawContours函數(shù),即可完成。
以上就是使用c++實(shí)現(xiàn)OpenCV繪制圖形旋轉(zhuǎn)矩形的詳細(xì)內(nèi)容,更多關(guān)于c++實(shí)現(xiàn)OpenCV繪制的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
VC++基于Dx實(shí)現(xiàn)的截圖程序示例代碼
這篇文章主要介紹了VC++基于Dx實(shí)現(xiàn)的截圖程序示例代碼,比較實(shí)用的功能,需要的朋友可以參考下2014-07-07
C++中求旋轉(zhuǎn)數(shù)組中的最小數(shù)字(經(jīng)典面試題)
這篇文章主要介紹了C++中求旋轉(zhuǎn)數(shù)組中的最小數(shù)字(經(jīng)典面試題)的相關(guān)資料,需要的朋友可以參考下2017-03-03
詳解C語言中動態(tài)內(nèi)存管理及柔性數(shù)組的使用
這篇文章主要為大家詳細(xì)介紹一下C語言中動態(tài)內(nèi)存管理以及柔性數(shù)組的使用方法,文中的示例代碼講解詳細(xì),對我們學(xué)習(xí)C語言有一定的幫助,需要的可以參考一下2022-07-07
VisualStudio2022編寫C語言的實(shí)現(xiàn)步驟
VisualStudio2022是一款強(qiáng)大的集成開發(fā)環(huán)境,可以用來編寫C語言程序,本文主要介紹了VisualStudio2022編寫C語言的實(shí)現(xiàn)步驟,具有一定的參考價(jià)值,感興趣的可以了解一下2024-06-06
C語言代碼實(shí)現(xiàn)學(xué)生成績管理系統(tǒng)
這篇文章主要為大家詳細(xì)介紹了C語言代碼實(shí)現(xiàn)學(xué)生成績管理系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-06-06

