使用c++實(shí)現(xiàn)OpenCV繪制圓端矩形
功能函數(shù)
// 繪制圓端矩形(藥丸狀,pill)
void DrawPill(cv::Mat mask, const cv::RotatedRect &rotatedrect, const cv::Scalar &color, int thickness, int lineType)
{
cv::Mat canvas = cv::Mat::zeros(mask.size(), CV_8UC1);
// 確定短邊,短邊繪制圓形
cv::RotatedRect rect = rotatedrect;
float r = rect.size.height / 2.0f;
if (rect.size.width > rect.size.height) {
rect.size.width -= rect.size.height;
}
else {
rect.size.height -= rect.size.width;
r = rect.size.width / 2.0f;
}
cv::Point2f ps[4];
rect.points(ps);
// 繪制邊緣
std::vector<std::vector<cv::Point>> tmpContours;
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(canvas, tmpContours, 0, cv::Scalar(255),5, lineType); // 填充mask
// 計(jì)算常長(zhǎng)短軸
float a = rotatedrect.size.width;
float b = rotatedrect.size.height;
int point01_x = (int)((ps[0].x + ps[1].x) / 2.0f);
int point01_y = (int)((ps[0].y + ps[1].y) / 2.0f);
int point03_x = (int)((ps[0].x + ps[3].x) / 2.0f);
int point03_y = (int)((ps[0].y + ps[3].y) / 2.0f);
int point12_x = (int)((ps[1].x + ps[2].x) / 2.0f);
int point12_y = (int)((ps[1].y + ps[2].y) / 2.0f);
int point23_x = (int)((ps[2].x + ps[3].x) / 2.0f);
int point23_y = (int)((ps[2].y + ps[3].y) / 2.0f);
cv::Point c0 = a < b ? cv::Point(point12_x, point12_y) : cv::Point(point23_x, point23_y);
cv::Point c1 = a < b ? cv::Point(point03_x, point03_y) : cv::Point(point01_x, point01_y);
// 長(zhǎng)軸兩端以填充的方式畫(huà)圓,直徑等于短軸
cv::circle(canvas, c0, (int)r, cv::Scalar(255), 5, lineType);
cv::circle(canvas, c1, (int)r, cv::Scalar(255), 5, lineType);
// 繪制外圍輪廓,如果不這樣操作,會(huì)得到一個(gè)矩形加兩個(gè)圓形,丑。。。
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
}
測(cè)試代碼
#include <iostream>
#include <opencv2/opencv.hpp>
using namespace std;
using namespace cv;
void DrawPill(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);
DrawPill(result, rorect, cv::Scalar(0, 255, 255),8,16);
imshow("original", src);
imshow("result", result);
waitKey(0);
return 0;
}
// 繪制圓端矩形(藥丸狀,pill)
void DrawPill(cv::Mat mask, const cv::RotatedRect &rotatedrect, const cv::Scalar &color, int thickness, int lineType)
{
cv::Mat canvas = cv::Mat::zeros(mask.size(), CV_8UC1);
// 確定短邊,短邊繪制圓形
cv::RotatedRect rect = rotatedrect;
float r = rect.size.height / 2.0f;
if (rect.size.width > rect.size.height) {
rect.size.width -= rect.size.height;
}
else {
rect.size.height -= rect.size.width;
r = rect.size.width / 2.0f;
}
cv::Point2f ps[4];
rect.points(ps);
// 繪制邊緣
std::vector<std::vector<cv::Point>> tmpContours;
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(canvas, tmpContours, 0, cv::Scalar(255),5, lineType); // 填充mask
// 計(jì)算常長(zhǎng)短軸
float a = rotatedrect.size.width;
float b = rotatedrect.size.height;
int point01_x = (int)((ps[0].x + ps[1].x) / 2.0f);
int point01_y = (int)((ps[0].y + ps[1].y) / 2.0f);
int point03_x = (int)((ps[0].x + ps[3].x) / 2.0f);
int point03_y = (int)((ps[0].y + ps[3].y) / 2.0f);
int point12_x = (int)((ps[1].x + ps[2].x) / 2.0f);
int point12_y = (int)((ps[1].y + ps[2].y) / 2.0f);
int point23_x = (int)((ps[2].x + ps[3].x) / 2.0f);
int point23_y = (int)((ps[2].y + ps[3].y) / 2.0f);
cv::Point c0 = a < b ? cv::Point(point12_x, point12_y) : cv::Point(point23_x, point23_y);
cv::Point c1 = a < b ? cv::Point(point03_x, point03_y) : cv::Point(point01_x, point01_y);
// 長(zhǎng)軸兩端以填充的方式畫(huà)圓,直徑等于短軸
cv::circle(canvas, c0, (int)r, cv::Scalar(255), 5, lineType);
cv::circle(canvas, c1, (int)r, cv::Scalar(255), 5, lineType);
// 繪制外圍輪廓,如果不這樣操作,會(huì)得到一個(gè)矩形加兩個(gè)圓形,丑。。。
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
}
測(cè)試效果
圖1 原圖
圖2 繪制圓端矩形
繪制圓端矩形其實(shí)就是繪制了一個(gè)旋轉(zhuǎn)矩形,然后分析哪個(gè)軸更長(zhǎng),就在哪個(gè)軸上的兩端畫(huà)圓,再取外圍輪廓,大功告成,通俗來(lái)講就畫(huà)了一個(gè)矩形兩個(gè)圓,如圖3所示。
不過(guò)注意,這個(gè)圖形最好不要超過(guò)圖像邊界,因?yàn)槌^(guò)后再分析外圍輪廓,它認(rèn)為的外圍就到了內(nèi)部,如圖4所示。

圖4 外圍線
然后,你就會(huì)得到一個(gè)奇葩圖形,如圖5所示。
圖5 示意圖
以上就是使用c++實(shí)現(xiàn)OpenCV繪制圓端矩形的詳細(xì)內(nèi)容,更多關(guān)于c++實(shí)現(xiàn)OpenCV繪制圓端矩形的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
C語(yǔ)言位段(位域)機(jī)制結(jié)構(gòu)體的特殊實(shí)現(xiàn)及解析
這篇文章主要為大家介紹了C語(yǔ)言位段位域機(jī)制結(jié)構(gòu)體的特殊實(shí)現(xiàn)講解有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步早日升職加薪2022-02-02
vs code 配置c/c++環(huán)境的詳細(xì)教程(推薦)
這篇文章主要介紹了vs code 配置c/c++環(huán)境的詳細(xì)教程(推薦),本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-11-11
淺談C語(yǔ)言中include""與include<>的區(qū)別
C語(yǔ)言中包含文件有兩種包含符號(hào),一個(gè)是<>尖括號(hào),另一個(gè)是""雙引號(hào)。那么這兩個(gè)有什么區(qū)別呢?本文就詳細(xì)的介紹一下,感興趣的可以了解一下2021-06-06
Qt數(shù)據(jù)庫(kù)應(yīng)用之實(shí)現(xiàn)通用數(shù)據(jù)庫(kù)采集
這篇文章主要為大家介紹了Qt中是如何實(shí)現(xiàn)通用數(shù)據(jù)庫(kù)采集的,文中的示例代碼講解詳細(xì),對(duì)我們學(xué)習(xí)Qt有一定幫助,感興趣的小伙伴可以了解一下2022-03-03
C語(yǔ)言中網(wǎng)絡(luò)地址與二進(jìn)制數(shù)之間轉(zhuǎn)換的函數(shù)小結(jié)
這篇文章主要介紹了C語(yǔ)言中網(wǎng)絡(luò)地址與二進(jìn)制數(shù)之間轉(zhuǎn)換的函數(shù)小結(jié),是C語(yǔ)言入門(mén)學(xué)習(xí)中的基礎(chǔ)知識(shí),需要的朋友可以參考下2015-09-09
C語(yǔ)言中隊(duì)列的結(jié)構(gòu)和函數(shù)接口的使用示例
隊(duì)列只允許一端進(jìn)行插入數(shù)據(jù)操作,在另一端進(jìn)行刪除數(shù)據(jù)操作的特殊線性表,隊(duì)列具有先進(jìn)先出FIFO的性質(zhì);隊(duì)列可用數(shù)組和鏈表 的方法實(shí)現(xiàn),使用鏈表的結(jié)構(gòu)實(shí)現(xiàn)更優(yōu)一些,因?yàn)槿绻褂脭?shù)組節(jié),出隊(duì)列時(shí)刪去首元素需要將整個(gè)數(shù)組前移,效率比較低2023-02-02
C++的靜態(tài)成員變量和靜態(tài)成員函數(shù)詳解
這篇文章主要為大家介紹了C++的靜態(tài)成員變量和靜態(tài)成員函數(shù),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來(lái)幫助2021-12-12
C++語(yǔ)言實(shí)現(xiàn)線性表之鏈表實(shí)例
這篇文章主要介紹了C++語(yǔ)言實(shí)現(xiàn)線性表之鏈表,實(shí)例分析了C++實(shí)現(xiàn)線性表中鏈表的原理與相關(guān)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-04-04

