Rect函数

本文详细介绍了OpenCV中Rect类的使用方法,包括构造函数、成员函数及操作符重载等,并提供了典型应用场景示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

了解opencv里面的函数,第一步必须是看官网上给出的文档。下面给出Rect类的c++使用。


class Rect_

template<typename _Tp> class CV_EXPORTS Rect_
{
public:
    typedef _Tp value_type;

    //! various constructors
    Rect_();
    Rect_(_Tp _x, _Tp _y, _Tp _width, _Tp _height);
    Rect_(const Rect_& r);
    Rect_(const CvRect& r);
    Rect_(const Point_<_Tp>& org, const Size_<_Tp>& sz);
    Rect_(const Point_<_Tp>& pt1, const Point_<_Tp>& pt2);

    Rect_& operator = ( const Rect_& r );
    //! the top-left corner
    Point_<_Tp> tl() const;
    //! the bottom-right corner
    Point_<_Tp> br() const;

    //! size (width, height) of the rectangle
    Size_<_Tp> size() const;
    //! area (width*height) of the rectangle
    _Tp area() const;

    //! conversion to another data type
    template<typename _Tp2> operator Rect_<_Tp2>() const;
    //! conversion to the old-style CvRect
    operator CvRect() const;

    //! checks whether the rectangle contains the point
    bool contains(const Point_<_Tp>& pt) const;

    _Tp x, y, width, height; //< the top-left corner, as well as width and height of the rectangle
};

Template class for 2D rectangles, described by the following parameters:

  • Coordinates of the top-left corner. This is a default interpretation of Rect_::x and Rect_::y in OpenCV. Though, in your algorithms you may count x and y from the bottom-left corner.
  • Rectangle width and height.

OpenCV typically assumes that the top and left boundary of the rectangle are inclusive, while the right and bottom boundaries are not. For example, the method Rect_::contains returns true if

x  \leq pt.x < x+width,      y  \leq pt.y < y+height

Virtually every loop over an image ROI in OpenCV (where ROI is specified by Rect_<int> ) is implemented as:

for(int y = roi.y; y < roi.y + rect.height; y++)
    for(int x = roi.x; x < roi.x + rect.width; x++)
    {
        // ...
    }

In addition to the class members, the following operations on rectangles are implemented:

  • \texttt{rect} = \texttt{rect} \pm \texttt{point} (shifting a rectangle by a certain offset)
  • \texttt{rect} = \texttt{rect} \pm \texttt{size} (expanding or shrinking a rectangle by a certain amount)
  • rect += point, rect -= point, rect += size, rect -= size (augmenting operations)
  • rect = rect1 & rect2 (rectangle intersection)
  • rect = rect1 | rect2 (minimum area rectangle containing rect2 and rect3 )
  • rect &= rect1, rect |= rect1 (and the corresponding augmenting operations)
  • rect == rect1, rect != rect1 (rectangle comparison)

This is an example how the partial ordering on rectangles can be established (rect1 \subseteq rect2):

template<typename _Tp> inline bool
operator <= (const Rect_<_Tp>& r1, const Rect_<_Tp>& r2)
{
    return (r1 & r2) == r1;
}

For your convenience, the Rect_<> alias is available:

typedef Rect_<int> Rect;

在模板类中公有中,给出了许多的构造函数,可以任选一种作为你使用的函数。我常用的就是Rect(a,b,c,d)。(a,b)表示矩形区域的左上角坐标,(c,d)则表示矩形区域的右下角坐标。

#include <iostream> #include <string> using namespace std; struct CPoint { int x ; int y ; }; class CRectangle { private: const int id;//常量数据成员 static int total;//静态数据成员 const static string sclass; const static int f=1.0f; CPoint lefttop ; CPoint rightdown ; public: CRectangle( ); CRectangle( CPoint& lt, CPoint& rd ); CPoint GetLefttop() const { return lefttop; } CPoint GetRightdown() const { return rightdown; } void SetLefttop(CPoint & pt) { lefttop=pt; } void SetRightdown(CPoint & pt) { rightdown=pt; } int Getid() const { return id; } static int Gettotal() { return total; } int Area( ) const; int Perimeter( ) const; }; int CRectangle::total=0;//静态数据成员必须在类的外部定义(正好一次)。 const string CRectangle::sclass="CRectangle"; CRectangle::CRectangle( ):id(++total) { lefttop.x=0; lefttop.y=0; rightdown.x=1; rightdown.y=1; } CRectangle::CRectangle( CPoint& lt, CPoint& rd ):id(++total) { lefttop = lt ; rightdown = rd ; } int CRectangle::Area( ) const { int wd= rightdown.x - lefttop.x ; int ht= rightdown.y - lefttop.y ; return wd * ht ; } int CRectangle::Perimeter( ) const { int wd= rightdown.x - lefttop.x ; int ht= rightdown.y - lefttop.y ; return 2 * ( wd + ht ) ; } int main() { CPoint lt, rd; cin >> lt.x >> lt.y; cin >> rd.x >> rd.y; CRectangle crt(lt,rd);//调用有参构造函数 CRectangle crt2;//调用默认构造函数 //创建常量对象 const CRectangle crt3(lt,rd); cout<<"当前创建的矩形个数为:"; cout<<CRectangle::Gettotal()<<endl; //返回矩形的左上和右下点 CPoint lt1=crt.GetLefttop(); CPoint lt2=crt.GetRightdown(); //显示矩形的坐标 cout<<crt.Getid()<<"号矩形的坐标是:"<<"("<<lt1.x<<","<<lt1.y<<"), "; cout<<"("<<lt2.x<<","<<lt2.y<<")"<<endl; //显示矩形的面积和周长 cout << "Area:"<<crt.Area( )<<endl; cout <<"Perimeter:"<<crt.Perimeter( )<<endl; //修改矩形的左上角点 cout<<"请输入矩形新的左上点坐标:"; cin>> lt.x>>lt.y; crt.SetLefttop(lt); lt1=crt.GetLefttop(); //显示修改后矩形的坐标 cout<<"矩形的坐标是:"<<"("<<lt1.x<<","<<lt1.y<<"), "; cout<<"("<<lt2.x<<","<<lt2.y<<")"<<endl; //显示修改后矩形的面积和周长 cout << "Area:"<<crt.Area( )<<endl; cout <<"Perimeter:"<<crt.Perimeter( )<<endl; }
### 矩形函数Rect Function)的定义 矩形函数是一种常见的数学函数,在信号处理领域有广泛应用。其基本定义如下: 对于任意变量 \( t \),如果满足条件 \( |t| < 0.5 \),则 \( \text{rect}(t) = 1.0 \)[^1]; 当 \( |t| = 0.5 \),\( \text{rect}(t) = 0.5 \); 而当 \( |t| > 0.5 \),\( \text{rect}(t) = 0 \)。 此外,还可以通过单位阶跃函数 \( u(t) \) 表达该函数的形式: \[ \text{rect}\left(\frac{t}{T}\right) = u(t + T/2) - u(t - T/2) \] 这意味着它可以通过两个不同位置上的单位阶跃函数相减来实现。 在 MATLAB 中实现了类似的逻辑用于计算 `rect` 函数值。具体来说,MATLAB 的实现方式是先初始化一个零矩阵作为输出向量,接着依据输入参数绝对值是否小于等于 0.5 来决定对应位置处的结果应设为 1 或保持原样为 0[^2]。 而在 Python 编程环境下也有针对此功能的设计思路。尽管未直接提供内置库支持像 MATLAB 那样的现成函数调用接口,但可通过自定义类或者简单函数完成相同目的[^3]。 值得注意的是,在实际应用过程中绘制此类图形时常需考虑数据范围选取合理性以及可能涉及的数据预处理操作比如归一化等问题[^4]。另外,“RECT”也可能被应用于其他上下文中指代完全不同的概念,例如 Delphi/Lazarus 平台下的图像拉伸显示方法中的参数名等情形下所代表的意义完全不同[^5]。 ```python def rect(x): import numpy as np result = np.zeros_like(x, dtype=float) mask = np.abs(x) <= 0.5 result[mask] = 1.0 return result # 测试代码片段 test_values = [-1, -0.6, -0.5, 0, 0.3, 0.7, 1] output = rect(np.array(test_values)) print(output) ``` 上述代码展示了如何基于 NumPy 库构建自己的版本的 `rect()` 方法,并给出了一组测试数值验证效果。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值