Rect函数

了解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; }
### 回答1: rect函数是MATLAB中一个用于生成矩形窗口的函数。它可以在图像、信号处理以及其他相关应用中起到很大的作用。 rect函数的语法格式是rectangle('Position',[x y width height]),其中x和y是矩形左下角的坐标,width和height分别是矩形的宽度和高度。这些参数可以通过输入具体数值来定义矩形的位置和大小。 rect函数所生成的矩形可以显示在MATLAB的图形窗口中,也可以在图像上进行绘制。它可以被用来实现目标检测、区域选择、图像分割等图像处理任务。当然,矩形也可以用于文档和报告的制作,可以方便地进行标注和突出显示。 除了定义基本的矩形外,rect函数还可以根据需要进行参数调整。比如,可以通过设置'EdgeColor'和'FaceColor'来改变矩形的边框颜色和填充颜色。这样就可以根据需求,将矩形绘制出不同的样式。 总之,rect函数是一个非常实用的MATLAB函数,可以帮助我们方便地绘制和操作矩形,实现各种图像处理和文档制作的需求。 ### 回答2: rect函数是Matlab中用于生成矩形脉冲信号的函数之一。该函数可以通过调节矩形脉冲的宽度和位置,生成具有不同特性的矩形脉冲信号。 在Matlab中,可以使用rect函数进行矩形脉冲信号的生成。rect函数的使用形式为rect(t, T),其中t是时间变量,T是矩形脉冲的宽度。 该函数可以生成一个以0为中心的矩形脉冲信号。当t的绝对值小于T/2时,矩形脉冲的值为1,否则为0。由于矩形脉冲是一个离散信号,因此需要指定t的取值范围,通常可以设定一个时间段来表示。 例如,如果我们希望生成一个宽度为2的矩形脉冲信号,在Matlab中可以写作rect(t, 2)。这将生成一个在t=-1到t=1范围内的矩形脉冲信号,其中信号的值为1。在t的范围之外,信号的值为0。 rect函数在信号处理和通信系统设计等领域中有着广泛的应用。通过调节矩形脉冲的宽度和位置,可以实现信号的锐化、滤波等处理,以及脉冲调制、调制解调等通信系统的实现。 总之,rect函数是Matlab中用于生成矩形脉冲信号的函数,通过调节矩形脉冲的宽度和位置,可以实现对信号的处理和通信系统的设计。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值