OpenCV繪制圓端矩形的示例代碼
本文主要介紹了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ì)算常長短軸 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); // 繪制外圍輪廓,如果不這樣操作,會得到一個(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 }
測試代碼
#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ì)算常長短軸 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); // 繪制外圍輪廓,如果不這樣操作,會得到一個(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 }
測試效果
圖1 原圖
圖2 繪制圓端矩形
繪制圓端矩形其實(shí)就是繪制了一個(gè)旋轉(zhuǎn)矩形,然后分析哪個(gè)軸更長,就在哪個(gè)軸上的兩端畫圓,再取外圍輪廓,大功告成,通俗來講就畫了一個(gè)矩形兩個(gè)圓,如圖3所示。
圖3 繪制邏輯
不過注意,這個(gè)圖形最好不要超過圖像邊界,因?yàn)槌^后再分析外圍輪廓,它認(rèn)為的外圍就到了內(nèi)部,如圖4所示。
圖4 外圍線
然后,你就會得到一個(gè)奇葩圖形,如圖5所示。
圖5 示意圖
到此這篇關(guān)于OpenCV繪制圓端矩形的示例代碼的文章就介紹到這了,更多相關(guān)OpenCV 圓端矩形內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
詳解Python實(shí)現(xiàn)按任意鍵繼續(xù)/退出的功能
在學(xué)Python時(shí)在總想實(shí)現(xiàn)一個(gè)按任意鍵繼續(xù)/退出的程序(受.bat毒害), 奈何一直沒有寫,今天抽時(shí)間寫出來了,下面分享給大家,有需要的可以參考借鑒。2016-08-08Python在信息學(xué)競賽中的運(yùn)用及Python的基本用法(詳解)
下面小編就為大家?guī)硪黄狿ython在信息學(xué)競賽中的運(yùn)用及Python的基本用法(詳解)。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-08-08Django中URLconf和include()的協(xié)同工作方法
這篇文章主要介紹了Django中URLconf和include()的協(xié)同工作方法,Django是Python眾人氣框架中最著名的一個(gè),需要的朋友可以參考下2015-07-07python3+PyQt5+Qt Designer實(shí)現(xiàn)界面可視化
本文主要介紹了python3+PyQt5+Qt Designer實(shí)現(xiàn)界面可視化,Qt Designer,用鼠標(biāo)拖拖就能完成窗體設(shè)計(jì),感興趣的可以了解一下2021-06-06Python wxauto 庫解鎖微信自動化的無限可能(示例代碼)
wxauto庫是基于Python的一個(gè)自動化工具,它主要用于操作和自動化WxPython應(yīng)用程序,這篇文章主要介紹了Python wxauto 庫解鎖微信自動化的無限可能,需要的朋友可以參考下2024-07-07