您好,登錄后才能下訂單哦!
這篇文章主要介紹“怎么用OpenCV繪制圓角矩形”,在日常操作中,相信很多人在怎么用OpenCV繪制圓角矩形問題上存在疑惑,小編查閱了各式資料,整理出簡單好用的操作方法,希望對大家解答”怎么用OpenCV繪制圓角矩形”的疑惑有所幫助!接下來,請跟著小編一起來學(xué)習(xí)吧!
// 繪制圓角矩形 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); //畫四個角的圓 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)建一個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); //畫四個角的圓 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)建一個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í)是繪制了兩個旋轉(zhuǎn)矩形,交叉組合,然后以四交點(diǎn)為圓心繪制圓,就組成了圓角矩形,如圖3所示。
圖3 繪制邏輯
同理,這個最好不要繪制到圖像外面,否則它在分析外圍線的時候會出錯,可以多加些判斷來修正,我就不寫了,交給兄弟們研究一下~
到此,關(guān)于“怎么用OpenCV繪制圓角矩形”的學(xué)習(xí)就結(jié)束了,希望能夠解決大家的疑惑。理論與實(shí)踐的搭配能更好的幫助大家學(xué)習(xí),快去試試吧!若想繼續(xù)學(xué)習(xí)更多相關(guān)知識,請繼續(xù)關(guān)注億速云網(wǎng)站,小編會繼續(xù)努力為大家?guī)砀鄬?shí)用的文章!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。