opencv矩形cv::Rect数据结构详解

更新时间:2023-06-19 21:06:34 阅读: 评论:0

opencv矩形cv::Rect数据结构详解
⽂章⽬录
⼀、源码
template<typename _Tp> class Rect_
{
public:
typedef _Tp value_type;
//! default constructor
Rect_();
Rect_(_Tp _x, _Tp _y, _Tp _width, _Tp _height);
Rect_(const Rect_& r);化妆正确步骤
Rect_(Rect_&& r) CV_NOEXCEPT;
Rect_(const Point_<_Tp>& org,const Size_<_Tp>& sz);
Rect_(const Point_<_Tp>& pt1,const Point_<_Tp>& pt2);
Rect_& operator =(const Rect_& r );
Rect_& operator =( Rect_&& r ) CV_NOEXCEPT;
//! the top-left corner
Point_<_Tp>tl()const;regsvr
//! 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;
//! true if empty
bool empty()const;
//! conversion to another data type
template<typename _Tp2> operator Rect_<_Tp2>()const;婚礼证婚人
//! checks whether the rectangle contains the point
bool contains(const Point_<_Tp>& pt)const;
_Tp x;//!< x coordinate of the top-left corner
_Tp y;//!< y coordinate of the top-left corner
_Tp width;//!< width of the rectangle
_Tp height;//!< height of the rectangle
};
typedef Rect_<int> Rect2i;
typedef Rect_<float> Rect2f;
typedef Rect_<double> Rect2d;
typedef Rect2i Rect;
Rect_是⼀个模板类,矩形由点、长和宽基本属性组成,opencv内部通过
typedef关键字分别以int 、float 和double三种基础类型命名了模板类Rect_的类型别名Rect2i,Rect2f,Rect2d,⽽Rect⼜是Rect2i 的类型别名。
⼆、cv::Rect的成员和属性
1.成员
rect.tl()//矩形左上⾓点的坐标
rect.br()//矩形右下⾓点的坐标
rect.size()//矩形的⼤⼩
rect.area()//矩形的⾯积
rect.x;//表⽰左上⾓点x的坐标
rect.y;//表⽰左上⾓点y的坐标
rect.width;//表⽰矩形的宽度
rect.height;//表⽰矩形的⾼度
三、代码⽰例
void test_Rect()
胡子像什么
{
cv::Mat src;
src = cv::imread("D:\\QtProject\\Opencv_Example\\rect\\rect.jpg", cv::IMREAD_COLOR);
pty()){
cout <<"Cannot load image"<< endl;
return;
}
//show(src);
// 绘制矩形框
cv::Rect rect1(60,60,200,400);
cv::rectangle(src, rect1, cv::Scalar(0,0,0),1,8,0);
/
/ tl(): 矩形左上⾓点的坐标    br():矩形右下⾓点的坐标
cv::circle(src, rect1.tl(),3, cv::Scalar(0,255,255),3,8,0);
文明公约内容cv::circle(src, rect1.br(),3, cv::Scalar(0,255,255),3,8,0);
// 判定⼀个点是否在矩形内
cv::Point pt1(100,200);
cv::Point pt2(459,450);
cv::circle(src, pt1,3, cv::Scalar(0,0,255),3,8,0);
cv::circle(src, pt2,3, cv::Scalar(0,0,255),3,8,0);
qDebug()<<"pt1 in r1: "<< ains(pt1)<<" pt2 in r1: "<< ains(pt2)<<'\t';
儒家圣人
qDebug()<<"rect1 area: "<< rect1.area()<<" rect1 size: "<< rect1.size().height<<"X"<<rect1.size().width<<'\t'; // 平移rect1 到 movePt
cv::Point movePt(0,500);//向Y轴移动500
cv::Rect rect2 = rect1 + movePt;
// tl(): 矩形左上⾓点的坐标    br():矩形右下⾓点的坐标
cv::circle(src, rect2.tl(),3, cv::Scalar(0,0,0),3,8,0);
cv::circle(src, rect2.br(),3, cv::Scalar(0,0,0),3,8,0);
cv::rectangle(src, rect2, cv::Scalar(0,0,255),2,8,0);
//⽐较两个矩形
std::cout <<"rect1 == rect2: "<< std::to_string(rect1 == rect2);
//求两个矩形的并
cv::Rect rect3 = rect1 | rect2;
cv::Point movePt2(300,0);//向Y轴移动500
cv::Rect rect4 = rect3 + movePt2;
cv::rectangle(src, rect4, cv::Scalar(0,255,0),2,8,0);
imshow("src",src);
//排序矩形
//std::vector<cv::Rect> Rect;
无缘无份
//Rect.push_back(rect1);
//Rect.push_back(rect2);
//Rect.push_back(rect3);
//std::sort(Rect.begin(), d(), compareRect_y);
//for (cv::Rect rect : Rect) {
//std::cout << "sorted rect: "<<rect.tl() << " rect.area: " << rect.area()<<std::endl;
//}
}
四、运⾏效果
高三地理

本文发布于:2023-06-19 21:06:34,感谢您对本站的认可!

本文链接:https://www.wtabcd.cn/fanwen/fan/82/993599.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:矩形   是否   属性   模板   详解
相关文章
留言与评论(共有 0 条评论)
   
验证码:
推荐文章
排行榜
Copyright ©2019-2022 Comsenz Inc.Powered by © 专利检索| 网站地图