午夜视频在线网站,日韩视频精品在线,中文字幕精品一区二区三区在线,在线播放精品,1024你懂我懂的旧版人,欧美日韩一级黄色片,一区二区三区在线观看视频

分享

OpenCV-繪制圓角矩形

 翟天保的圖書館 2022-01-13

作者:翟天保Steven
版權(quán)聲明:著作權(quán)歸作者所有,商業(yè)轉(zhuǎn)載請聯(lián)系作者獲得授權(quán),非商業(yè)轉(zhuǎn)載請注明出處

功能函數(shù)

// 繪制圓角矩形
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::vector<std::vector<cv::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::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);
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::vector<std::vector<cv::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::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);
drawContours(mask, tmpContours, 0, color, thickness, lineType);  // 填充mask
}

測試效果?

圖1?原圖
圖2?繪制圓角矩形

? ? ? ?繪制圓角矩形其實(shí)是繪制了兩個(gè)旋轉(zhuǎn)矩形,交叉組合,然后以四交點(diǎn)為圓心繪制圓,就組成了圓角矩形,如圖3所示。

圖3?繪制邏輯

? ? ? ?同理,這個(gè)最好不要繪制到圖像外面,否則它在分析外圍線的時(shí)候會出錯(cuò),可以多加些判斷來修正,我就不寫了,交給兄弟們研究一下~

? ? ? ?如果文章幫助到你了,可以點(diǎn)個(gè)贊讓我知道,我會很快樂~加油!

    轉(zhuǎn)藏 分享 獻(xiàn)花(0

    0條評論

    發(fā)表

    請遵守用戶 評論公約