minAreaRect函数
生活随笔
收集整理的這篇文章主要介紹了
minAreaRect函数
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
1、minAreaRect函數(shù)
函數(shù)作用:
主要求得包含點(diǎn)集最小面積的矩形,,這個(gè)矩形是可以有偏轉(zhuǎn)角度的,可以與圖像的邊界不平行
2、minAreaRect函數(shù)調(diào)用形式
C++:?RotatedRect?minAreaRect(InputArray?points)
InputArray?points:表示輸入的點(diǎn)集
輸出是矩形的四個(gè)點(diǎn)坐標(biāo)
RotatedRect
class?RotatedRectThe class represents rotated (i.e. not up-right) rectangles on a plane. Each rectangle is specified by the center point (mass center), length of each side (represented by cv::Size2f structure) and the rotation angle in degrees.
C++:?RotatedRect::RotatedRect()
|
|
The sample below demonstrates how to use?RotatedRect:
Mat image(200, 200, CV_8UC3, Scalar(0)); RotatedRect rRect = RotatedRect(Point2f(100,100), Size2f(100,50), 30);Point2f vertices[4]; rRect.points(vertices);//獲取矩形的四個(gè)點(diǎn) for (int i = 0; i < 4; i++)line(image, vertices[i], vertices[(i+1)%4], Scalar(0,255,0));Rect brect = rRect.boundingRect(); rectangle(image, brect, Scalar(255,0,0));imshow("rectangles", image); waitKey(0);3、opencv代碼
#include "opencv2/highgui/highgui.hpp" #include "opencv2/imgproc/imgproc.hpp" #include <iostream> #include <stdio.h> #include <stdlib.h>using namespace cv; using namespace std;Mat src; Mat src_gray; int thresh = 100; int max_thresh = 255; RNG rng(12345);/// Function header void thresh_callback(int, void* );/** @function main */ int main( int argc, char** argv ) {/// 加載源圖像src = imread( argv[1], 1 );/// 轉(zhuǎn)為灰度圖并模糊化cvtColor( src, src_gray, CV_BGR2GRAY );blur( src_gray, src_gray, Size(3,3) );/// 創(chuàng)建窗體char* source_window = "Source";namedWindow( source_window, CV_WINDOW_AUTOSIZE );imshow( source_window, src );createTrackbar( " Threshold:", "Source", &thresh, max_thresh, thresh_callback );thresh_callback( 0, 0 );waitKey(0);return(0); }/** @function thresh_callback */ void thresh_callback(int, void* ) {Mat threshold_output;vector<vector<Point> > contours;vector<Vec4i> hierarchy;/// 閾值化檢測(cè)邊界threshold( src_gray, threshold_output, thresh, 255, THRESH_BINARY );/// 尋找輪廓findContours( threshold_output, contours, hierarchy, CV_RETR_TREE, CV_CHAIN_APPROX_SIMPLE, Point(0, 0) );/// 對(duì)每個(gè)找到的輪廓?jiǎng)?chuàng)建可傾斜的邊界框和橢圓vector<RotatedRect> minRect( contours.size() );vector<RotatedRect> minEllipse( contours.size() );for( int i = 0; i < contours.size(); i++ ){ minRect[i] = minAreaRect( Mat(contours[i]) );if( contours[i].size() > 5 ){ minEllipse[i] = fitEllipse( Mat(contours[i]) ); }}/// 繪出輪廓及其可傾斜的邊界框和邊界橢圓Mat drawing = Mat::zeros( threshold_output.size(), CV_8UC3 );for( int i = 0; i< contours.size(); i++ ){Scalar color = Scalar( rng.uniform(0, 255), rng.uniform(0,255), rng.uniform(0,255) );// contourdrawContours( drawing, contours, i, color, 1, 8, vector<Vec4i>(), 0, Point() );// ellipseellipse( drawing, minEllipse[i], color, 2, 8 );// rotated rectanglePoint2f rect_points[4]; minRect[i].points( rect_points );for( int j = 0; j < 4; j++ )line( drawing, rect_points[j], rect_points[(j+1)%4], color, 1, 8 );}/// 結(jié)果在窗體中顯示namedWindow( "Contours", CV_WINDOW_AUTOSIZE );imshow( "Contours", drawing ); }
opencv源碼:
#include "stdafx.h" #include "opencv2/highgui/highgui.hpp" #include "opencv2/imgproc/imgproc.hpp"#include <iostream>using namespace cv; using namespace std;void help() {cout << "This program demonstrates finding the minimum enclosing box or circle of a set\n""of points using functions: minAreaRect() minEnclosingCircle().\n""Random points are generated and then enclosed.\n""Call:\n""./minarea\n""Using OpenCV version %s\n" << CV_VERSION << "\n" << endl; }int main( int /*argc*/, char** /*argv*/ ) {help();Mat img(500, 500, CV_8UC3);RNG& rng = theRNG(); for(;;){int i, count = rng.uniform(1, 101);vector<Point> points;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);}RotatedRect box = minAreaRect(Mat(points));Point2f center, vtx[4];float radius = 0;minEnclosingCircle(Mat(points), center, radius);box.points(vtx);img = Scalar::all(0);for( i = 0; i < count; i++ )circle( img, points[i], 3, Scalar(0, 0, 255), CV_FILLED, CV_AA );for( i = 0; i < 4; i++ )line(img, vtx[i], vtx[(i+1)%4], Scalar(0, 255, 0), 1, CV_AA);circle(img, center, cvRound(radius), Scalar(0, 255, 255), 1, CV_AA); imshow( "rect & circle", img );char key = (char)cvWaitKey();if( key == 27 || key == 'q' || key == 'Q' ) // 'ESC'break;}return 0; }總結(jié)
以上是生活随笔為你收集整理的minAreaRect函数的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: minEnclosingCircle函数
- 下一篇: fitEllipse函数