zoukankan      html  css  js  c++  java
  • opencv再学习之路(八)---设定感兴趣区域(RIO)

    RIO: region of interest ,在图像处理领域,我们常常需要设置感兴趣区域(ROI,region of interest),来专注或者简化我们的工作过程 。也就是从图像中选择的一个图像区域,这个区域是我们图像分析所关注的重点。我们圈定这个区域,以便进行进一步处理。而且,使用ROI指定我们想读入的目标,可以减少处理时间,增加精度,给图像处理来带不小的便利。

    首先讲解Rect 对象:

    Rect对象的定义:

    1 typedef Rect_<int> Rect;

    Rect_的定义:

     1 /*!
     2   The 2D up-right rectangle class
     3 
     4   The class represents a 2D rectangle with coordinates of the specified data type.
     5   Normally, cv::Rect ~ cv::Rect_<int> is used.
     6 */
     7 template<typename _Tp> class Rect_
     8 {
     9 public:
    10     typedef _Tp value_type;
    11 
    12     //! various constructors
    13     Rect_();
    14     Rect_(_Tp _x, _Tp _y, _Tp _width, _Tp _height);
    15     Rect_(const Rect_& r);
    16     Rect_(const CvRect& r);
    17     Rect_(const Point_<_Tp>& org, const Size_<_Tp>& sz);
    18     Rect_(const Point_<_Tp>& pt1, const Point_<_Tp>& pt2);
    19 
    20     Rect_& operator = ( const Rect_& r );
    21     //! the top-left corner
    22     Point_<_Tp> tl() const;
    23     //! the bottom-right corner
    24     Point_<_Tp> br() const;
    25 
    26     //! size (width, height) of the rectangle
    27     Size_<_Tp> size() const;
    28     //! area (width*height) of the rectangle
    29     _Tp area() const;
    30 
    31     //! conversion to another data type
    32     template<typename _Tp2> operator Rect_<_Tp2>() const;
    33     //! conversion to the old-style CvRect
    34     operator CvRect() const;
    35 
    36     //! checks whether the rectangle contains the point
    37     bool contains(const Point_<_Tp>& pt) const;
    38 
    39     _Tp x, y, width, height; //< the top-left corner, as well as width and height of the rectangle
    40 };

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

    1 Rect_(),形参列表为空,即定义一个空窗口(默认值为:x=y=width=height=0);
    2 Rect_(_Tp _x, _Tp _y, _Tp _width, _Tp _height),定义一个左上角点坐标为(_x, _y)的_width*_height矩形窗口;
    3 Rect_(const Rect_& r),使用其他的Rect_对象初始化;
    4 Rect_(const CvRect& r),使用CvRect对象初始化;
    5 Rect_(const Point_<_Tp>& org, const Size_<_Tp>& sz),分别将位置坐标(_x, _y)和窗口大小(_width, _height)用Point_和Size_对象初始化;
    6 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的常见用法涵盖:

     1 Mat image = imread("C:\Users\Leo\Desktop\lena.jpg");
     2 Rect rect1(256, 256, 128, 128);
     3 Rect rect2(224, 224, 128, 128);
     4 
     5 Mat roi1;
     6 image(rect1).copyTo(roi1); // copy the region rect1 from the image to roi1
     7 imshow("1", roi1);
     8 waitKey(0);
     9 
    10 Mat roi2;
    11 image(rect2).copyTo(roi2); // copy the region rect2 from the image to roi2
    12 imshow("2", roi2);
    13 waitKey(0);
    14 
    15 cv::Rect rect3 = rect1&rect2; // intersection of the two sets
    16 Mat roi3;
    17 image(rect3).copyTo(roi3);
    18 imshow("3", roi3);
    19 waitKey(0);
    20 
    21 Rect rect4 = rect1|rect2; // union of the two sets (the minimum bounding rectangle)
    22 Mat roi4;
    23 image(rect4).copyTo(roi4);
    24 imshow("4", roi4);
    25 waitKey(0);
    26 
    27 Rect rect5(10, 10, 128, 128);
    28 roi1.copyTo(image(rect5)); // copy the region rect1 to the designated region in the image
    29 imshow("5", image);
    30 waitKey(0);

     

    RIO区域定义的两种方法:

      1. 使用cv::Rect,创建一个Rect区域后就可以将其直接传入到图像中,从而将图形中矩形所在区域的位置表示出来。

    1 //定义一个Mat类型并给其设定ROI区域  
    2 Mat imageROI;  
    3 //方法一  
    4 imageROI=image(Rect(500,250,logo.cols,logo.rows)); 

          2. 直接指定感兴趣行或列的范围(Range).Range是指从起始索引到终止索引(不包括终止索引)的一段连续序列。cv::Range可以用来定义Range.如果使用cv::Range来定义ROI,那么前例中定义ROI的代码可以为:

    1 //方法二  
    2 imageROI=srcImage3(Range(250,250+logoImage.rows),Range(200,200+logoImage.cols));  

    实例如何将一幅图像的某一部分提取出来

    1     Mat srcImage = imread("04.jpg");
    2     Mat RIO=srcImage(Rect(cvRound(srcImage.cols*2/3),0,402,srcImage.rows));
  • 相关阅读:
    java网络编程基础——TCP网络编程二
    java网络编程基础——TCP网络编程一
    java网络编程基础——基本网络支持
    《红色警戒3》增援代码大全
    怎么重复使用inputStream?
    Java对象和XML相互转换
    Freemarker数字格式化总结
    Java Web之过滤器(Filter)
    mysql报错后,tomcat假死,多半是数据库连接池设置有问题
    关于tomcat启动时的警告 :Property maxActive is not used in DBCP2, use maxTotal instead. 和 Property maxWait i
  • 原文地址:https://www.cnblogs.com/zhp218/p/8532331.html
Copyright © 2011-2022 走看看