OPENCV using RECT to select and set window ROI

Source: Internet
Author: User


This series of articles by the @yhl_leo produced, reproduced please indicate the source.
Article Link: http://blog.csdn.net/yhl_leo/article/details/50593825

First look at the definition of the Rect object:

typedef rect_<int> Rect;

Then look at the definition of 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};

From the above definition can be found at least two points: first, the class Rect_ class template data type _TP in rect_<int> is specified as integral type, two, from the Rect_ constructor can be seen, its formal parameter list has 6 forms: rect_ (), parameter list is empty, That is, define an empty window (the default is: x=y=width=height=0), rect_ (_tp _x, _tp _y, _tp _width, _tp _height), define a _x for the upper-left corner point coordinates (_y, _width*_) Height Rectangle window; rect_ (const rect_& R), initialized with other rect_ objects, rect_ (const cvrect& R), initialized with Cvrect object, rect_ (const POINT_ <_Tp>& org, const size_<_tp>& SZ), initializes the position coordinates (_x, _y) and the window size (_width, _height) with Point_ and Size_ objects, respectively; Rect_ ( Const point_<_tp>& PT1, const point_<_tp>& pt2), respectively, coordinates the location (_x, _y) and window size (_width, _height) with Point_ and Point_ object initialization.

In the OpenCV library, the corresponding relationship between the pixel coordinates of the image and the number of rows is: X, col, y, row, width, cols, height, rows

Here's a piece of code that basically covers the common uses of rect:

Mat image = Imread ("c:\\users\\leo\\desktop\\lena.jpg");
Rect rect1 (n. ();
Rect Rect2 (224, 224, N.);

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 sets
Mat roi3;
Image (RECT3). CopyTo (ROI3);
Imshow ("3", roi3);
Waitkey (0);

Rect rect4 = Rect1|rect2; Union of the sets (the minimum bounding rectangle)
Mat roi4;
Image (Rect4). CopyTo (ROI4);
Imshow ("4", roi4);
Waitkey (0);

Rect Rect5 (Ten, ten, +);
Roi1.copyto (Image (RECT5)); Copy the region Rect1
to the designated region in the image Imshow ("5", image);
Waitkey (0);

The result is:


The corresponding code can be downloaded from the GitHub account: Yhlleo.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.