本系列文章由 @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. V$INSTANCE 字段说明

    http://blog.csdn.net/wyzxg/article/details/4728622 http://blog.csdn.net/warden2010/article/details/6 ...

  2. Shell编程入门(第二版)(上)

    简单的示例Shell程序 示例1. #!/bin/bash #This is to show what a shell script looks like echo "Our first e ...

  3. POJ 2470 Ambiguous permutations(简单题 理解题意)

    [题目简述]:事实上就是依据题目描写叙述:A permutation of the integers 1 to n is an ordering of these integers. So the n ...

  4. Android之怎样给ListView加入过滤器

    给ListView加入文字过滤器: //this.getListView().setTextFilterEnabled(true);//可能报错用以下的 listView.setTextFilterE ...

  5. 让VMware ESXi虚拟交换机支持VLAN

    眼下虚拟化应用比較广泛,通常情况下.一台物理主机在安装VMware ESXi或Hyper-V虚拟机软件后.能够在一台物理主机上创建多个虚拟机,而且创建的每一个虚拟机能够像原来的物理一样对外提供服务,这 ...

  6. Web前端开发实战2:二级下拉式菜单之JS实现

    上一篇博文提到了二级下拉式菜单是用HTML和CSS实现的.我们这一篇来用JavaScript脚本实现下拉菜单的显 示和隐藏. 使用 JavaScript方法实现我们须要用的知识有: 1)JS事件:on ...

  7. 去哪网实习总结:如何配置数据库连接(JavaWeb)

    本来是以做数据挖掘的目的进去哪网的,结构却成了系统开发.. . 只是还是比較认真的做了三个月,老师非常认同我的工作态度和成果. .. 实习立即就要结束了.总结一下几点之前没有注意过的变成习惯和问题,分 ...

  8. POJ 3050 Hopscotch 水~

    http://poj.org/problem?id=3050 题目大意: 在一个5*5的格子中走,每一个格子有个数值,每次能够往上下左右走一格,问走了5次后得到的6个数的序列一共同拥有多少种?(一開始 ...

  9. Tween动画TranslateAnimation细节介绍

    Tween动画有下面这几种: Animation   动画 AlphaAnimation 渐变透明度 RotateAnimation 画面旋转 ScaleAnimation 渐变尺寸缩放 Transl ...

  10. POJ 1236--Network of Schools【scc缩点构图 &amp;&amp; 求scc入度为0的个数 &amp;&amp; 求最少加几条边使图变成强联通】

    Network of Schools Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 13325   Accepted: 53 ...