主頁(yè) > 知識(shí)庫(kù) > OpenCV繪制圓角矩形的方法實(shí)例

OpenCV繪制圓角矩形的方法實(shí)例

熱門標(biāo)簽:地圖標(biāo)注多個(gè) 鶴壁手機(jī)自動(dòng)外呼系統(tǒng)違法嗎 B52系統(tǒng)電梯外呼顯示E7 怎么辦理400客服電話 萊蕪電信外呼系統(tǒng) 銀川電話機(jī)器人電話 企業(yè)微信地圖標(biāo)注 高德地圖標(biāo)注收入咋樣 沈陽(yáng)防封電銷電話卡

功能函數(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::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
}

測(cè)試代碼

#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
}

測(cè)試效果

圖1 原圖

圖2 繪制圓角矩形

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


圖3 繪制邏輯

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

總結(jié)

到此這篇關(guān)于OpenCV繪制圓角矩形的文章就介紹到這了,更多相關(guān)OpenCV繪制圓角矩形內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

您可能感興趣的文章:
  • Opencv繪制最小外接矩形、最小外接圓
  • OpenCV鼠標(biāo)繪制矩形和截取矩形區(qū)域圖像
  • python opencv常用圖形繪制方法(線段、矩形、圓形、橢圓、文本)

標(biāo)簽:葫蘆島 三亞 湘西 銀川 呼倫貝爾 烏魯木齊 安慶 呼倫貝爾

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《OpenCV繪制圓角矩形的方法實(shí)例》,本文關(guān)鍵詞  OpenCV,繪制,圓角,矩,形的,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無(wú)關(guān)。
  • 相關(guān)文章
  • 下面列出與本文章《OpenCV繪制圓角矩形的方法實(shí)例》相關(guān)的同類信息!
  • 本頁(yè)收集關(guān)于OpenCV繪制圓角矩形的方法實(shí)例的相關(guān)信息資訊供網(wǎng)民參考!
  • 推薦文章