本系列文章由 @yhl_leo 出品,转载请注明出处。

文章链接: http://blog.csdn.net/yhl_leo/article/details/50593825


首先看一下Rect对象的定义:

typedef Rect_<int> Rect;

再看Rect_的定义:

/*!
The 2D up-right rectangle class The class represents a 2D rectangle with coordinates of the specified data type.
Normally, cv::Rect ~ cv::Rect_<int> is used.
*/
template<typename _Tp> class 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
};

从上面的定义至少可以发现两点:一,类Rect_的类模板中的数据类型_TpRect_<int>中被指定为整型;二,从Rect_的构造函数可以看出,其形参列表一共有6种形式:

  • Rect_(),形参列表为空,即定义一个空窗口(默认值为:x=y=width=height=0);
  • Rect_(_Tp _x, _Tp _y, _Tp _width, _Tp _height),定义一个左上角点坐标为(_x, _y)_width*_height矩形窗口;
  • Rect_(const Rect_& r),使用其他的Rect_对象初始化;
  • Rect_(const CvRect& r),使用CvRect对象初始化;
  • Rect_(const Point_<_Tp>& org, const Size_<_Tp>& sz),分别将位置坐标(_x, _y)和窗口大小(_width, _height)Point_Size_对象初始化;
  • Rect_(const Point_<_Tp>& pt1, const Point_<_Tp>& pt2),分别将坐标位置(_x, _y)和窗口大小(_width, _height)Point_Point_对象初始化。

在OpenCV库中,图像像素坐标与所在行列数的对应关系为:x -> col, y -> row, width -> cols, height -> rows

下面给出一段代码,基本可以把Rect的常见用法涵盖:

Mat image = imread("C:\\Users\\Leo\\Desktop\\lena.jpg");
Rect rect1(256, 256, 128, 128);
Rect rect2(224, 224, 128, 128); Mat roi1;
image(rect1).copyTo(roi1); // copy the region rect1 from the image to roi1
imshow("1", roi1);
waitKey(0); Mat roi2;
image(rect2).copyTo(roi2); // copy the region rect2 from the image to roi2
imshow("2", roi2);
waitKey(0); cv::Rect rect3 = rect1&rect2; // intersection of the two sets
Mat roi3;
image(rect3).copyTo(roi3);
imshow("3", roi3);
waitKey(0); Rect rect4 = rect1|rect2; // union of the two sets (the minimum bounding rectangle)
Mat roi4;
image(rect4).copyTo(roi4);
imshow("4", roi4);
waitKey(0); Rect rect5(10, 10, 128, 128);
roi1.copyTo(image(rect5)); // copy the region rect1 to the designated region in the image
imshow("5", image);
waitKey(0);

结果为:

相应代码,可以在Github账户中下载:yhlleo

Opencv 使用Rect选取与设置窗口ROI的更多相关文章

  1. 【opencv学习笔记六】图像的ROI区域选择与复制

    图像的数据量还是比较大的,对整张图片进行处理会影响我们的处理效率,因此常常只对图像中我们需要的部分进行处理,也就是感兴趣区域ROI.今天我们来看一下如何设置图像的感兴趣区域ROI.以及对ROI区域图像 ...

  2. swt shell设置窗口位于屏幕中间

    /**     * 设置窗口位于屏幕中间     * @param shell 要调整位置的窗口对象     */    public static void center(Shell shell)  ...

  3. Qt 之 设置窗口边框的圆角(使用QSS和PaintEvent两种方法)

    Qt在设置窗口边框圆角时有两种方式,一种是设置样式,另一种是在paintEvent事件中绘制窗口.下面分别叙述用这两种方式来实现窗口边框圆角的效果. 一.使用setStyleSheet方法 this- ...

  4. QT中设置窗口背景颜色

    QWidget是所有用户界面对象的基类,这意味着可以用同样的方法为其它子类控件改变背景颜色. Qt中窗口背景的设置,下面介绍三种方法. 1.使用QPalette 2.使用Style Sheet 3.绘 ...

  5. Windows 7个性化配置,关闭Win7动画效果,设置窗口背景为“ 豆绿色”

    减少眼睛疲劳配色(豆绿色): RGB:, , ,颜色名称:#C7EDCC 1.任务栏设置 2.关闭Win7动画效果 控制面板 -> 轻松访问 -> 优化视频显示 3.去掉窗口阴影 右键单击 ...

  6. plsql设置窗口默认格式

    一:plsql设置窗口默认格式 窗口视图设置完毕后,选择“窗口”菜单——点击“保存”版面. 等到下次重启后,就会呈现保存的版面. OK,设置完毕!

  7. Qt 技巧:去除对话框边框 + 设置窗口可移动和透明

    1.去除对话框标题栏和边框 在构造函数里设置:    this->setWindowFlags(Qt::FramelessWindowHint); Qt::Dialog     (按照对话框的形 ...

  8. C# 跨进程 设置窗口owner

    窗口间跨进程通信 1. 发送方 public const int WM_InsertChart_Completed = 0x00AA; //查找窗口 [DllImport("User32.d ...

  9. pyqt pyside 设置窗口关闭时删除自身

    pyqt pyside 设置窗口关闭时删除自身 self.setAttribute(QtCore.Qt.WA_DeleteOnClose)

随机推荐

  1. Swift入门(四)——可选类型(Optionals)与断言(Assert)

    可选类型是什么? 首先看一个问题,Swift中String类型的变量有一个叫做toInt的方法,能够把String类型变量转换为Int类型变量. var stringValue = "5&q ...

  2. DesignPattern_Java:Factory Method Pattern

    工厂方法模式 Factory Method :(虚拟构造函数模式 Virtual Constructor,多态性工厂模式 Ploymorphic Facoty) Define an interface ...

  3. (十一)Unity5新特性----实战2D游戏

    孙广东  2015.7.11 在本教程中,将了解到U5新功能.你通过本教程.您将了解下面内容: Changes in Component Access Physics Effectors Adding ...

  4. Shiro 学习应用(续)

    在前面的文章中为大家介绍了 Shrio 的基础概念.可能比較笼统.没有深入到开发过程的一些问题.如今集中在本帖中归纳一下有关问题. FormAuthenticationFilter 表单过滤器 表单过 ...

  5. Java学习之道:Java 导出EXCEL

    1.Apache POI简单介绍  Apache POI是Apache软件基金会的开放源代码函式库.POI提供API给Java程式对Microsoft Office格式档案读和写的功能. .NET的开 ...

  6. MYSQL Training: MySQL I

    让以admin身份登录.源代码: 非常easy的注入 在username输入 admin' OR '1'='1 OK.

  7. DNS反射放大攻击分析——DNS反射放大攻击主要是利用DNS回复包比请求包大的特点,放大流量,伪造请求包的源IP地址为受害者IP,将应答包的流量引入受害的服务器

    DNS反射放大攻击分析 摘自:http://www.shaojike.com/2016/08/19/DNS%E6%94%BE%E5%A4%A7%E6%94%BB%E5%87%BB%E7%AE%80%E ...

  8. c:forTokens标签delims截取字符

    转自:https://blog.csdn.net/love398146779/article/details/83853958 两个name要相同,在里边内容为空的时候才会全显示. <logic ...

  9. 剑指offer——04重建二叉树(Python3)

    思路:在数据结构中,有一个条件反射,谈及二叉树,就递归.所以在实现重建二叉树时,也应该用到递归的思想. 在前序遍历中,根节点处于第一个:在中序遍历中,根节点的左边为左子树节点,根节点右边为右子树节点. ...

  10. golang sftp传输文件

    之前有一篇介绍如何使用 golang 通过SSH协议来执行远程命令:golang执行远程命令 同样,通过SSH协议也可以使用 golang 来远程传输文件. 除了 SSH 的库,为了传输文件,还需要用 ...