minAreaRect函數(shù) 最小外接矩形

原文鏈接地址:
http://blog.csdn.net/qq_18343569/article/details/48000179

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 RotatedRect

class CV_EXPORTS RotatedRect
{
public:
//! various constructors
RotatedRect();
RotatedRect(const Point2f& center, const Size2f& size, float angle);
RotatedRect(const CvBox2D& box);

//! returns 4 vertices of the rectangle
void points(Point2f pts[]) const;
//! returns the minimal up-right rectangle containing the rotated rectangle
Rect boundingRect() const;
//! conversion to the old-style CvBox2D structure
operator CvBox2D() const;

Point2f center; //< the rectangle mass center
Size2f size;    //< width and height of the rectangle
float angle;    //< the rotation angle. When the angle is 0, 90, 180, 270 etc., the rectangle becomes an up-right rectangle.

};

The 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()C++: RotatedRect::RotatedRect(const Point2f& center, const Size2f& size, float angle)C++: RotatedRect::RotatedRect(const CvBox2D& box)

Parameters:
?center – The rectangle mass center.
?size – Width and height of the rectangle.
?angle – The rotation angle in a clockwise direction. When the angle is 0, 90, 180, 270 etc., the rectangle becomes an up-right rectangle.
?box – The rotated rectangle parameters as the obsolete CvBox2D structure.

C++: void RotatedRect::points(Point2f pts[]) const //! returns 4 vertices of the rectangleC++: Rect RotatedRect::boundingRect() constC++: RotatedRect::operator CvBox2D() const

Parameters:
?pts – The points array for storing rectangle vertices.

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) );
       // contour
       drawContours( drawing, contours, i, color, 1, 8, vector<Vec4i>(), 0, Point() );
       // ellipse
       ellipse( drawing, minEllipse[i], color, 2, 8 );
       // rotated rectangle
       Point2f 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;
}
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市篮绿,隨后出現(xiàn)的幾起案子孵延,更是在濱河造成了極大的恐慌,老刑警劉巖亲配,帶你破解...
    沈念sama閱讀 219,539評(píng)論 6 508
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件尘应,死亡現(xiàn)場(chǎng)離奇詭異惶凝,居然都是意外死亡,警方通過查閱死者的電腦和手機(jī)犬钢,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,594評(píng)論 3 396
  • 文/潘曉璐 我一進(jìn)店門苍鲜,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人玷犹,你說我怎么就攤上這事混滔。” “怎么了箱舞?”我有些...
    開封第一講書人閱讀 165,871評(píng)論 0 356
  • 文/不壞的土叔 我叫張陵遍坟,是天一觀的道長(zhǎng)。 經(jīng)常有香客問我晴股,道長(zhǎng)愿伴,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,963評(píng)論 1 295
  • 正文 為了忘掉前任电湘,我火速辦了婚禮隔节,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘寂呛。我一直安慰自己怎诫,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,984評(píng)論 6 393
  • 文/花漫 我一把揭開白布贷痪。 她就那樣靜靜地躺著幻妓,像睡著了一般。 火紅的嫁衣襯著肌膚如雪劫拢。 梳的紋絲不亂的頭發(fā)上肉津,一...
    開封第一講書人閱讀 51,763評(píng)論 1 307
  • 那天,我揣著相機(jī)與錄音舱沧,去河邊找鬼妹沙。 笑死,一個(gè)胖子當(dāng)著我的面吹牛熟吏,可吹牛的內(nèi)容都是我干的距糖。 我是一名探鬼主播,決...
    沈念sama閱讀 40,468評(píng)論 3 420
  • 文/蒼蘭香墨 我猛地睜開眼牵寺,長(zhǎng)吁一口氣:“原來是場(chǎng)噩夢(mèng)啊……” “哼悍引!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起帽氓,我...
    開封第一講書人閱讀 39,357評(píng)論 0 276
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤趣斤,失蹤者是張志新(化名)和其女友劉穎,沒想到半個(gè)月后杏节,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體唬渗,經(jīng)...
    沈念sama閱讀 45,850評(píng)論 1 317
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡典阵,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 38,002評(píng)論 3 338
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了镊逝。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片壮啊。...
    茶點(diǎn)故事閱讀 40,144評(píng)論 1 351
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖撑蒜,靈堂內(nèi)的尸體忽然破棺而出歹啼,到底是詐尸還是另有隱情,我是刑警寧澤座菠,帶...
    沈念sama閱讀 35,823評(píng)論 5 346
  • 正文 年R本政府宣布狸眼,位于F島的核電站,受9級(jí)特大地震影響浴滴,放射性物質(zhì)發(fā)生泄漏拓萌。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,483評(píng)論 3 331
  • 文/蒙蒙 一升略、第九天 我趴在偏房一處隱蔽的房頂上張望微王。 院中可真熱鬧,春花似錦品嚣、人聲如沸炕倘。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,026評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)罩旋。三九已至,卻和暖如春眶诈,著一層夾襖步出監(jiān)牢的瞬間涨醋,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 33,150評(píng)論 1 272
  • 我被黑心中介騙來泰國(guó)打工册养, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留东帅,地道東北人压固。 一個(gè)月前我還...
    沈念sama閱讀 48,415評(píng)論 3 373
  • 正文 我出身青樓球拦,卻偏偏與公主長(zhǎng)得像,于是被迫代替她去往敵國(guó)和親帐我。 傳聞我的和親對(duì)象是個(gè)殘疾皇子坎炼,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,092評(píng)論 2 355

推薦閱讀更多精彩內(nèi)容

  • 18- UIBezierPath官方API中文翻譯(待校對(duì)) ----------------- 華麗的分割線 -...
    醉臥欄桿聽雨聲閱讀 1,069評(píng)論 1 1
  • 誠(chéng)信這個(gè)我們從小被教育的話題一直灌輸在我們的腦海中谣光。上學(xué)時(shí)候,老師會(huì)說作弊可恥芬为,交朋友的時(shí)候萄金,如果言出必行蟀悦,一定會(huì)...
    xiaowubro閱讀 465評(píng)論 0 0
  • 前段時(shí)間認(rèn)識(shí)了一個(gè)朋友,閑聊之余氧敢,聽她聊到她的一個(gè)朋友日戈。下面我以第三人稱的方式來描述這個(gè)事。我朋友稱A, 我朋友的...
    螞蟻說說閱讀 780評(píng)論 1 0
  • “老丁已經(jīng)去考試了吧?” “對(duì)呀唯袄,八頭牛都攔不淄淝!” “我這里有高中數(shù)學(xué)必修一的往年中考練習(xí)題恋拷,特別全资厉,我發(fā)給你。...
    何悠閱讀 138評(píng)論 0 1