// 繪制圓角矩形
void DrawRotatedRectChamfer(cv::Mat mask,const cv::RotatedRect rotatedrect, float radius,const cv::Scalar color, int thickness, int lineType)
{
// 創(chuàng)建畫布
cv::Mat canvas = cv::Mat::zeros(mask.size(), CV_8UC1);
cv::RotatedRect newrotatedrect = rotatedrect;
//畫寬縮減后的矩形
cv::RotatedRect r1 = newrotatedrect;
r1.size.width = r1.size.width - 2 * radius;
DrawRotatedRect(canvas, r1, cv::Scalar(255), 5, lineType);
//畫高縮減后的矩形
cv::RotatedRect r2 = newrotatedrect;
r2.size.height = r2.size.height - 2 * radius;
DrawRotatedRect(canvas, r2, cv::Scalar(255), 5,lineType);
//畫四個(gè)角的圓
cv::RotatedRect r3 = r2;
r3.size.width = r1.size.width;
cv::Point2f ps[4];
r3.points(ps);
for (int i = 0; i != 4; ++i) {
cv::circle(canvas, ps[i], (int)radius, cv::Scalar(255), 5);
}
std::vectorstd::vectorcv::Point>> EXcontours;
cv::findContours(canvas, EXcontours, cv::RETR_EXTERNAL, CHAIN_APPROX_SIMPLE);
drawContours(mask, EXcontours, 0, color, thickness, lineType); // 填充mask
}
// 繪制旋轉(zhuǎn)矩形
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::vectorstd::vectorcv::Point>> tmpContours; // 創(chuàng)建一個(gè)InputArrayOfArrays 類型的點(diǎn)集
std::vectorcv::Point> 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 iostream>
#include opencv2/opencv.hpp>
#include stdio.h>
using namespace std;
using namespace cv;
void DrawRotatedRectChamfer(cv::Mat mask, const cv::RotatedRect rotatedrect, float radius, const cv::Scalar color, int thickness, int lineType);
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(800, 600), 50);
DrawRotatedRectChamfer(result, rorect, 100, cv::Scalar(0, 0, 255), 5, 16);
imshow("original", src);
imshow("result", result);
waitKey(0);
return 0;
}
// 繪制圓角矩形
void DrawRotatedRectChamfer(cv::Mat mask,const cv::RotatedRect rotatedrect, float radius,const cv::Scalar color, int thickness, int lineType)
{
// 創(chuàng)建畫布
cv::Mat canvas = cv::Mat::zeros(mask.size(), CV_8UC1);
cv::RotatedRect newrotatedrect = rotatedrect;
//畫寬縮減后的矩形
cv::RotatedRect r1 = newrotatedrect;
r1.size.width = r1.size.width - 2 * radius;
DrawRotatedRect(canvas, r1, cv::Scalar(255), 5, lineType);
//畫高縮減后的矩形
cv::RotatedRect r2 = newrotatedrect;
r2.size.height = r2.size.height - 2 * radius;
DrawRotatedRect(canvas, r2, cv::Scalar(255), 5,lineType);
//畫四個(gè)角的圓
cv::RotatedRect r3 = r2;
r3.size.width = r1.size.width;
cv::Point2f ps[4];
r3.points(ps);
for (int i = 0; i != 4; ++i) {
cv::circle(canvas, ps[i], (int)radius, cv::Scalar(255), 5);
}
std::vectorstd::vectorcv::Point>> EXcontours;
cv::findContours(canvas, EXcontours, cv::RETR_EXTERNAL, CHAIN_APPROX_SIMPLE);
drawContours(mask, EXcontours, 0, color, thickness, lineType); // 填充mask
}
// 繪制旋轉(zhuǎn)矩形
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::vectorstd::vectorcv::Point>> tmpContours; // 創(chuàng)建一個(gè)InputArrayOfArrays 類型的點(diǎn)集
std::vectorcv::Point> 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
}
同理,這個(gè)最好不要繪制到圖像外面,否則它在分析外圍線的時(shí)候會(huì)出錯(cuò),可以多加些判斷來(lái)修正,我就不寫了,交給兄弟們研究一下~
到此這篇關(guān)于OpenCV繪制圓角矩形的文章就介紹到這了,更多相關(guān)OpenCV繪制圓角矩形內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!