日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 >

OpenCV学习笔记(十八):凸包,最小包围区域算子:convexHull(),minAreaRect(),minEnclosingTriangle(),minEnclosingCircle()

發布時間:2024/7/23 38 豆豆
生活随笔 收集整理的這篇文章主要介紹了 OpenCV学习笔记(十八):凸包,最小包围区域算子:convexHull(),minAreaRect(),minEnclosingTriangle(),minEnclosingCircle() 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

OpenCV學習筆記(十八):凸包,最小包圍區域算子:convexHull(),minAreaRect(),minEnclosingTriangle(),minEnclosingCircle()

1、convexHull()函數

計算出圖像點集的凸包,根據圖像的輪廓點,通過函數convexhull轉化成凸包的點坐標,從而畫出圖像的凸包。

void convexHull( InputArray points, // 輸入的點集,一般是用圖像輪廓函數求得的輪廓點 OutputArray hull, // 輸出的是凸包的二維xy點的坐標值,針對每一個輪廓形成的。 bool clockwise = false, // 表示凸包的方向,true順時針 flase 逆時針 bool returnPoints = true // true表示返回點,false返回點地址的索引值 ==returnPoints 標識符== 當輸出數組"hull" 是std::vector時,忽略該標志,輸出取決于向量的類型: std::vector<int>表示 returnPoints=false, 返回索引值 std::vector<Point>表示 returnPoints=true。返回點 )

2、boundingRect()函數

該函數計算并返回指定點集的最小右上界矩形。

Rect cv::boundingRect(InputArray points)

3、minAreaRect()函數

對給定的2D 點集,尋找可旋轉的最小面積包圍矩形

RotatedRect cv::minAreaRect(InputArray points)

4、minEnclosingCircle()函數

找到包圍2D點集的最小面積的圓。

void cv::minEnclosingCircle( InputArray points, Point2f & center, float & radius )

5、minEnclosingTriangle()函數

找到一個包含2D點集的最小面積三角形,并返回其面積。

double cv::minEnclosingTriangle( InputArray points, OutputArray triangle )

6、minEnclosingTriangle()函數

在一組二維點周圍擬合一個橢圓。

RotatedRect cv::fitEllipse( InputArray points )

7、approxPolyDP()函數

以指定的精度近似生成多邊形曲線。
函數逼近一條曲線或另一條曲線/頂點較少的多邊形,使它們之間的距離小于或等于指定的精度。它使用Douglas-Peucker算法

void approxPolyDP( InputArray curve, // 輸入的點集(存儲在std::vector或Mat中的二維點的輸入向量) OutputArray approxCurve, // 輸出的點集,當前點集是能最小包容指定點集的。draw出來即是一個多邊形; double epsilon, // 指定的精度,也即是原始曲線與近似曲線之間的最大距離。 bool closed // 若為true,則說明近似曲線是閉合的,它的首位都是相連,反之,若為false,則斷開。 );

8、示例一: (凸包)

#include <opencv2/opencv.hpp>using namespace cv; using namespace std;int main() {// 1、初始化變量和隨機值Mat image(600, 600, CV_8UC3);RNG& rng = theRNG(); // 生成單個均勻分布的隨機數或隨機數數組。// 2、循環,按下ESC,Q,q鍵程序退出,否則有鍵按下便一直更新while(1){// 2.1 參數初始化char key;//鍵值int count = (unsigned)rng%100 + 3;//隨機生成點的數量(最少3個點)vector<Point> points; //點值// 2.2 隨機生成點坐標for(int i = 0; i < count; i++ ){Point point;point.x = rng.uniform(image.cols/4, image.cols*3/4); // 一定范圍內點坐標point.y = rng.uniform(image.rows/4, image.rows*3/4);points.push_back(point);}// 2.3 檢測凸包// 操作方向為 順市時針vector<Point> hull; // 輸出凸包點集 坐標索引值convexHull(Mat(points), hull, true);// 2.4 繪制出隨機顏色的點image = Scalar::all(0); // 背景清零(設為黑色)for(int i = 0; i < count; i++ )// 繪制點circle(image, points[i], 3, Scalar(rng.uniform(0, 255), rng.uniform(0, 255), rng.uniform(0, 255)), CV_FILLED, CV_AA);// // 根據凸包點集的索引 繪制凸包線 // int hullcount = (int)hull.size(); //凸包的邊數(點數) // for(int i = 0; i < hullcount; i++ ) // { // Point point = points[hull[i]]; // 第一個點坐標 // 方式一:從最后一個點為起點繪制 Point point_last = points[hull[hullcount-1]]; //連接凸包邊的坐標點(hull[hullcount-1]:凸包最后一個點 索引值) line(image, point_last, point, Scalar(255, 255, 255), 1, CV_AA); point_last = point;// // 方式二:第一個點為起點繪制 // Point point1= points[hull[(i+1)%hullcount]]; // 第二個點 // line(image, point, point1, Scalar(255, 255, 255), 1, CV_AA); // }// 2.5 根據凸包點集 繪制凸包線int hullcount = (int)hull.size();for(int i = 0; i < hullcount; i++ ){Point point = hull[i]; // 第一個點坐標Point point1= hull[(i+1)%hullcount]; // 第二個點line(image, point, point1, Scalar(255, 255, 255), 1, CV_AA);}// 2.6 顯示效果圖imshow("凸包檢測示例", image);//按下ESC,Q,或者q,程序退出key = (char)waitKey();if( key == 27 || key == 'q' || key == 'Q' )break;}waitKey(0);return 0; }


9、示例二: (最小包圍區域)

這個程序演示了如何使用函數minAreaRect() minEnclosingTriangle() minEnclosingCircle()找到一組點的最小包圍矩形、三角形、圓形。隨機點生成,然后封閉。按ESC,'Q’或’q’退出和任何其他鍵重新生成點集。

#include <opencv2/opencv.hpp> #include<vector>using namespace cv; using namespace std;//using std::vector;int main() {// 1、創建背景圖片Mat img(500, 500, CV_8UC3,Scalar::all(0));// 2、返回默認的隨機數生成器RNG& rng = theRNG(); for(;;){// 隨機生成一些點// 首先就是隨機生成點的總數量int i, count = rng.uniform(1, 101);vector<Point> points;// 3、生成一組隨機的點for( i = 0; i < count; i++ ){Point pt;pt.x = rng.uniform(img.cols/4, img.cols*3/4);pt.y = rng.uniform(img.rows/4, img.rows*3/4);points.push_back(pt);}// 4、找出隨機點所在區域的最小包圍矩形RotatedRect box = minAreaRect(points); // 獲得旋轉矩形 box// 將box變量中存儲的坐標值放到 fourPoint的數組中Point2f fourPoint[4];box.points(fourPoint);// 5、找出最小包圍三角形vector<Point2f> triangle;minEnclosingTriangle(points, triangle);// 6、找出最小包圍圓的圓心center和半徑radiusPoint2f center;float radius = 0;minEnclosingCircle(points, center, radius);///7、繪制所生產的隨機點for( i = 0; i < count; i++ )circle( img, points[i], 3, Scalar(0, 0, 255), FILLED, LINE_AA );// 8、繪制最小包圍矩形for( i = 0; i < 4; i++ )line(img, fourPoint[i], fourPoint[(i+1)%4], Scalar(0, 255, 0), 1, LINE_AA);///9、繪制最小包圍三角形for( i = 0; i < 3; i++ )line(img, triangle[i], triangle[(i+1)%3], Scalar(255, 255, 0), 1, LINE_AA);// 10、繪制最小包圍圓circle(img, center, cvRound(radius), Scalar(0, 255, 255), 1, LINE_AA);// 11、顯示imshow( "Rectangle, triangle & circle", img );// 鍵盤命令輸入char key = (char)waitKey();if( key == 27 || key == 'q' || key == 'Q' ) // 'ESC'break;elseimg = Scalar::all(0); // 背景清空}return 0; }

結果:



總結

以上是生活随笔為你收集整理的OpenCV学习笔记(十八):凸包,最小包围区域算子:convexHull(),minAreaRect(),minEnclosingTriangle(),minEnclosingCircle()的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。