【OpenCV】c++ OpenCV - cv::Rect 矩形用法與相關功能函數 sample code (內含範例程式碼)

前言

此文為 C ++ 在 OpenCV 的矩型用法

sample code

1. 宣告矩形

int x = 0;
int y = 0;
int width = 100;
int height = 50;
cv::Rect rect(x, y, width, height);

宣告一個左上角位於 (x,y),
長寬等於 (width, height) 的矩形。

  • 注意不是左上角與右下角的座標

2. (自己常用) 取得 x, y, w, h

stringstream bbox;
bbox << "bbox: x = " << rect.x;
bbox << ", y = " << rect.y;
bbox << ", w = " << rect.width;
bbox << ", h = " << rect.height;

3. 一些參數與功能

rect.area();     // rect的面積
rect.size();     // rect的尺寸 100 × 50
rect.tl();       // 左上頂點座標
rect.br();       // 右下頂點座標 
rect.width();    // rect的寬度
rect.height();   // rect的高度
rect.contains(Point(x, y));  // 查看rect有沒有包含Point(x, y)  

Reference

https://www.itread01.com/content/1547177430.html
https://docs.opencv.org/3.4/d2/d44/classcv_1_1Rect__.html