最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

OpenCV繪制圓端矩形的示例代碼

 更新時間:2021年08月30日 09:17:07   作者:翟天保Steven  
本文主要介紹了OpenCV繪制圓端矩形的示例代碼,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下

本文主要介紹了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
 
	// 計算常長短軸
	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 繪制圓端矩形

繪制圓端矩形其實就是繪制了一個旋轉(zhuǎn)矩形,然后分析哪個軸更長,就在哪個軸上的兩端畫圓,再取外圍輪廓,大功告成,通俗來講就畫了一個矩形兩個圓,如圖3所示。

 

圖3 繪制邏輯

不過注意,這個圖形最好不要超過圖像邊界,因為超過后再分析外圍輪廓,它認(rèn)為的外圍就到了內(nèi)部,如圖4所示。

 

圖4 外圍線

然后,你就會得到一個奇葩圖形,如圖5所示。

圖5 示意圖

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

相關(guān)文章

  • python 使用fileinput讀取文件

    python 使用fileinput讀取文件

    這篇文章主要介紹了python 使用fileinput讀取文件,幫助大家更好的理解和學(xué)習(xí)使用python,感興趣的朋友可以了解下
    2021-03-03
  • 回調(diào)函數(shù)的意義以及python實現(xiàn)實例

    回調(diào)函數(shù)的意義以及python實現(xiàn)實例

    本篇文章主要介紹了回調(diào)函數(shù)的意義以及python實現(xiàn)實例,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-06-06
  • Django的models模型的具體使用

    Django的models模型的具體使用

    這篇文章主要介紹了Django的models模型的具體使用,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-07-07
  • python單例模式實例解析

    python單例模式實例解析

    這篇文章主要為大家詳細(xì)介紹了python單例模式實例的相關(guān)資料,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2018-08-08
  • Python快速進修指南之向量數(shù)據(jù)庫文本搜索

    Python快速進修指南之向量數(shù)據(jù)庫文本搜索

    這篇文章主要為大家介紹了Java開發(fā)快速進修Python指南之向量數(shù)據(jù)庫文本搜索,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2024-01-01
  • Python中刪除文件的程序代碼

    Python中刪除文件的程序代碼

    很多軟件在運行時會自動創(chuàng)建一些備份文件,在程序退出后又不自動刪除備份文件,隨著文件數(shù)量的增加,每隔一段時間就要清理一下。
    2011-03-03
  • Python使用pandas對數(shù)據(jù)進行差分運算的方法

    Python使用pandas對數(shù)據(jù)進行差分運算的方法

    今天小編就為大家分享一篇Python使用pandas對數(shù)據(jù)進行差分運算的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-12-12
  • Python爬取附近餐館信息代碼示例

    Python爬取附近餐館信息代碼示例

    這篇文章主要介紹了Python爬取附近餐館信息代碼示例,具有一定借鑒價值,需要的朋友可以參考下。
    2017-12-12
  • python使用requests設(shè)置讀取超時時間

    python使用requests設(shè)置讀取超時時間

    在Python中,使用requests庫進行網(wǎng)絡(luò)請求時,可以通過設(shè)置?timeout參數(shù)來指定讀取超時時間,本文就來介紹一下,具有一定的參考價值,感興趣的可以了解一下
    2023-11-11
  • python?playwright?庫上傳和下載操作(自動化測試?playwright)

    python?playwright?庫上傳和下載操作(自動化測試?playwright)

    這篇文章主要介紹了python?playwright?庫上傳和下載操作(自動化測試?playwright?),playwright中的上傳和下載比selenium的上傳和下載要簡便些,本文結(jié)合實例代碼給大家介紹的非常詳細(xì),需要的朋友可以參考下
    2023-05-05

最新評論

丹凤县| 安吉县| 洛扎县| 武陟县| 达孜县| 博白县| 会同县| 肥西县| 大田县| 海林市| 疏附县| 邵阳县| 珠海市| 宜丰县| 平罗县| 琼海市| 彭水| 锦屏县| 富阳市| 道真| 仙桃市| 大名县| 彰武县| 乌拉特中旗| 临猗县| 永新县| 蓝田县| 常山县| 理塘县| 仲巴县| 长丰县| 保靖县| 宜良县| 滦南县| 泊头市| 临猗县| 于田县| 沙雅县| 合阳县| 文昌市| 老河口市|