使用c++實現(xiàn)OpenCV繪制圓端矩形
更新時間:2021年08月30日 17:25:23 作者:翟天保Steven
這篇文章主要介紹了使用c++實現(xiàn)OpenCV繪制圓端矩形,其中著重的講解了OpenCV使用過程中需要注意的一些小細節(jié),避免浪費大家在開發(fā)過程中浪費多余的時間
功能函數(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
// 計算常長短軸
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);
// 長軸兩端以填充的方式畫圓,直徑等于短軸
cv::circle(canvas, c0, (int)r, cv::Scalar(255), 5, lineType);
cv::circle(canvas, c1, (int)r, cv::Scalar(255), 5, lineType);
// 繪制外圍輪廓,如果不這樣操作,會得到一個矩形加兩個圓形,丑。。。
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
}
測試代碼
#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
// 計算常長短軸
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);
// 長軸兩端以填充的方式畫圓,直徑等于短軸
cv::circle(canvas, c0, (int)r, cv::Scalar(255), 5, lineType);
cv::circle(canvas, c1, (int)r, cv::Scalar(255), 5, lineType);
// 繪制外圍輪廓,如果不這樣操作,會得到一個矩形加兩個圓形,丑。。。
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
}
測試效果
圖1 原圖
圖2 繪制圓端矩形
繪制圓端矩形其實就是繪制了一個旋轉矩形,然后分析哪個軸更長,就在哪個軸上的兩端畫圓,再取外圍輪廓,大功告成,通俗來講就畫了一個矩形兩個圓,如圖3所示。
不過注意,這個圖形最好不要超過圖像邊界,因為超過后再分析外圍輪廓,它認為的外圍就到了內部,如圖4所示。

圖4 外圍線
然后,你就會得到一個奇葩圖形,如圖5所示。
圖5 示意圖
以上就是使用c++實現(xiàn)OpenCV繪制圓端矩形的詳細內容,更多關于c++實現(xiàn)OpenCV繪制圓端矩形的資料請關注腳本之家其它相關文章!
相關文章
C/C++利用棧和隊列實現(xiàn)停車場管理系統(tǒng)
數(shù)據(jù)結構的課程設計一般都不是很好理解,今天小編為大家總結了一下c和c++版本的常見棧和隊列的的停車場管理程序,需要的小伙伴可以參考一下2022-06-06
C++ 容器適配器priority_queue的使用及實現(xiàn)代碼
這篇文章主要介紹了C++ 容器適配器priority_queue的使用及實現(xiàn),本文通過實例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-04-04
C語言的getc()函數(shù)和gets()函數(shù)的使用對比
這篇文章主要介紹了C語言的getc()函數(shù)和gets()函數(shù)的使用對比,從數(shù)據(jù)流中一個是讀取字符一個是讀取字符串,需要的朋友可以參考下2015-08-08

