opencv3尋找最小包圍矩形-minAreaRect函數

來源:互聯網
上載者:User
#include<iostream>#include<vector>#include<opencv2/opencv.hpp>using namespace cv;using namespace std;int main(){Mat srcImage(Size(600, 600), CV_8UC3, Scalar(0));RNG &rng = theRNG();char key;while (1){//隨機產生一些點//首先就是隨機產生點的總數量int g_nPointCount = rng.uniform(3, 200);//接下來就是隨機產生一些點的座標vector<Point> points;for (int i = 0; i < g_nPointCount; i++){Point midPoint;midPoint.x = rng.uniform(srcImage.cols / 4, srcImage.cols * 3 / 4);midPoint.y = rng.uniform(srcImage.rows / 4, srcImage.rows * 3 / 4);points.push_back(midPoint);}//顯示剛剛隨機產生的那些點for (int i = 0; i < g_nPointCount; i++){circle(srcImage, points[i], 0, Scalar(rng.uniform(0, 255), rng.uniform(0, 255), rng.uniform(0, 255)), 3);}//在產生的那些隨機點中尋找最小包圍矩形//rectPoint變數中得到了矩形的四個頂點座標RotatedRect rectPoint = minAreaRect(points);//定義一個儲存以上四個點的座標的變數Point2f fourPoint2f[4];//將rectPoint變數中儲存的座標值放到 fourPoint的數組中rectPoint.points(fourPoint2f);//根據得到的四個點的座標  繪製矩形for (int i = 0; i < 3; i++){line(srcImage, fourPoint2f[i], fourPoint2f[i + 1], Scalar(rng.uniform(0, 255), rng.uniform(0, 255), rng.uniform(0, 255)), 3);}line(srcImage, fourPoint2f[0], fourPoint2f[3], Scalar(rng.uniform(0, 255), rng.uniform(0, 255), rng.uniform(0, 255)), 3);imshow("【繪製結束後的映像】", srcImage);key = waitKey();if (key == 27)break;elsesrcImage = Scalar::all(0);}return 0;}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.