OpenCV中Rect()函数常用操作和使用详解(含绘图示例)+Rectangle()函数用法

基本概念:

Rect(int x, int y, int width, int height);

参数含义:

Rect(左上角x坐标  ,  左上角y坐标,矩形的宽,矩形的高)

例如我们画一个图 Rect(20,50,30,40),  我用matlab画了一下,比较直观

那对于Rect(20,50,30,40)有哪些常用的操作?

rect.area(); //返回面积,1200

rect.size();//返回尺寸,30x40

rect.tl();// 返回左上角坐标(20,50)

rect.br();//返回右下角坐标(50,10)

rect.width();//返回宽度30

rect.height();//返回高度40

rect.contains(Point(x,y)) ; //返回布尔true/false, 判断x,y是否在这个矩形中

交集、并集, 矩阵对比,很像C语言

rect = rect1 & rect2;

rect = rect1 | rect2;

rect1 == rect2; //返回布尔值

rect1 != rect2 ; //返回布尔值

Rectangle用法
void cvRectangle( CvArr* img, CvPoint pt1, CvPoint pt2, CvScalar color,
                 int thickness=1, int line_type=8, int shift=0 );

img: 图像.
pt1 :矩形的一个顶点。
pt2:矩形对角线上的另一个顶点
color:线条颜色 (RGB) 或亮度(灰度图像 )(grayscale image)。

//后面这三个都是可有可没有的
thickness:组成矩形的线条的粗细程度。取负值时(如 CV_FILLED)函数绘制填充了色彩的矩形。
line_type:线条的类型。见cvLine的描述
shift:坐标点的小数点位数。

举例子:

rectangle(img, box.tl(), box.br(), Scalar(g_rng.uniform(0, 255), g_rng.uniform(0, 255), g_rng.uniform(0, 255)));//随机颜色 

  • 25
    点赞
  • 165
    收藏
    觉得还不错? 一键收藏
  • 7
    评论
using System; using System.Collections.Generic; using System.ComponentModel; using System.Drawing; using System.Data; using System.Linq; using System.Text; using System.Windows.Forms; using System.Diagnostics; using System.Drawing.Drawing2D; namespace KRT.Component.Common { /// /// 停靠面板控件 /// public class DockPanel : Panel { /// /// 关闭按钮 /// DockPanelCloseButton closeButton = new DockPanelCloseButton(); /// /// 浮动窗口 /// Form flowForm = new Form(); /// /// 分割栏 /// Splitter splitter = new Splitter(); /// /// 标题栏 /// Label titleBar = new Label(); /// /// 标题栏背景色 /// public Color TitleBackColor //using System.Drawing; { get { return titleBar.BackColor; } set { titleBar.BackColor = value; closeButton.BackColor = value; } } /// /// 标题栏前景色 /// public Color TitleForeColor { get { return titleBar.ForeColor; } set { titleBar.ForeColor = value; } } /// /// 标题栏文本 /// [Localizable(true)] public string TitleText { get { return titleBar.Text; } set { titleBar.Text = value; } } /// /// 标题栏可见 /// public bool TitleVisible { get { return titleBar.Visible; } set { titleBar.Visible = value; closeButton.Visible = value; } } /// /// 浮动窗口最大尺寸 /// Size flowFormMaxSize = new Size(640, 480); public Size FlowFormMaxSize { get { return flowFormMaxSize; } set { flowFormMaxSize = value; if (this.VisibleAll) { flowForm.Size = flowFormMaxSize; } } } /// /// 整个控件的隐藏、显示,包括浮动窗口状态下 /// bool visibleAll = true; public bool VisibleAll { get { return visibleAll; } set { visibleAll = value; if (flowForm.Controls.Count > 0) { flowForm.Visible = visibleAll; } else { this.Visible = visibleAll; } } } /// /// 控件初始化 /// public DockPanel() { //InitializeComponent(); flowForm.ShowInTaskbar = false; // 关闭按钮 closeButton.Location = new System.Drawing.Point(0, 0); closeButton.Name = "Close"; closeButton.Size = new System.Drawing.Size(16, 16); closeButton.TabIndex = 0; closeButton.BackColor = SystemColors.ControlDark; closeButton.Click += new EventHandler(closeButton_Click); // 浮动窗口 flowForm.FormBorderStyle = FormBorderStyle.SizableToolWindow; //VS2008自带的窗体设计器进行设置窗体的边框。 flowForm.ShowInTaskbar = false; //任务栏 flowForm.MaximizeBox = false; //最大化按钮 flowForm.Move += new EventHandler(flowForm_Move); //EventHandler是asp.net内置的委托,事件是特殊的委托。 flowForm.MouseCaptureChanged += new EventHandler(flowForm_MouseCaptureChanged); // flowForm.FormClosing += new FormClosingEventHandler(flowForm_FormClosing); // 标题栏 titleBar.AutoSize = false; //自动调整大小 titleBar.Dock = DockStyle.Top;//控件顶端;DockStyle是个枚举,有none, top, bottom... titleBar.Height = 18; titleBar.Padding = new Padding(3); titleBar.MouseDown += new MouseEventHandler(titleBar_MouseDown); titleBar.MouseMove += new MouseEventHandler(titleBar_MouseMove); titleBar.MouseUp += new MouseEventHandler(titleBar_MouseUp); //this.Padding = new Padding(1); this.Controls.Add(closeButton); this.Controls.Add(titleBar); this.ParentChanged += new EventHandler(DockPanel_ParentChanged); this.VisibleChanged += new EventHandler(DockPanel_VisibleChanged); this.MouseDown += new MouseEventHandler(titleBar_MouseDown); this.MouseMove += new MouseEventHandler(titleBar_MouseMove); this.MouseUp += new MouseEventHandler(titleBar_MouseUp); this.ControlAdded += new ControlEventHandler(DockPanel_ControlAdded); } /// /// 如果是用户单击浮动窗口的关闭按钮,隐藏浮动窗口 /// /// /// void flowForm_FormClosing(object sender, FormClosingEventArgs e) { if (e.CloseReason == CloseReason.UserClosing) { e.Cancel = true; flowForm.Visible = false; visibleAll = false; closeButton_Click(sender, e); } } /// /// 添加子控件时调整顺序 /// /// /// void DockPanel_ControlAdded(object sender, ControlEventArgs e) { if (e.Control != titleBar && e.Control != closeButton) { e.Control.BringToFront(); } } /// /// 同时修改Splitter的可视效果 /// /// /// void DockPanel_VisibleChanged(object sender, EventArgs e) { splitter.Visible = Visible; if (this.Parent != null && Visible) { if (!this.DesignMode) { if (this.Parent.Controls.IndexOf(splitter) < 0) { //splitter.Dock = this.Dock; //this.Parent.Controls.AddRange(new Control[] { splitter, this }); this.Parent.Controls.Add(splitter); int index = this.Parent.Controls.IndexOf(this); this.Parent.Controls.SetChildIndex(splitter, index); //this.Parent.Controls.SetChildIndex(this, index + 1); //foreach (Control item in this.Parent.Controls) //{ // Debug.WriteLine( // this.Parent.Controls.GetChildIndex(item).ToString() + " : " + // item.ToString()); //} } } } } /// /// 初始化Splitter /// /// /// void DockPanel_ParentChanged(object sender, EventArgs e) { //if (this.Parent != null) //{ // if (!this.DesignMode) // { // //Parent.Controls.Add(splitter); // splitter.Dock = this.Dock; // this.Parent.Controls.AddRange(new Control[] { splitter, this }); // } //} } /// /// 关闭事件 /// public event EventHandler CloseButtonClick; /// /// 单击关闭按钮,触发关闭事件 /// /// /// void closeButton_Click(object sender, EventArgs e) { VisibleAll = false; if (null != CloseButtonClick) { CloseButtonClick(sender, e); } } /// /// 浮动窗口开始拖动时触发 /// /// /// void flowForm_MouseCaptureChanged(object sender, EventArgs e) { if (!flowFormCapture) { flowFormCapture = true; } else { flowFormCapture = false; flowFormDock = true; } } bool flowFormDock = false; // 浮动窗口可以被停靠 bool flowFormCapture = false; // 浮动窗口开始拖动 /// /// 浮动窗口移动过程 /// /// /// void flowForm_Move(object sender, EventArgs e) { if (flowFormDock) { flowFormDock = false; switch (this.Dock) { case DockStyle.Left: if (flowForm.Location.X < Parent.Location.X) { ShowDockPanel(); } break; case DockStyle.Top: if (flowForm.Location.Y Parent.Location.X + Parent.Width) { ShowDockPanel(); } break; case DockStyle.Bottom: if (flowForm.Location.Y + flowForm.Height > Parent.Location.Y + Parent.Height) { ShowDockPanel(); } break; } } } /// /// 控件尺寸改变 /// /// protected override void OnSizeChanged(EventArgs e) { base.OnSizeChanged(e); closeButton.Location = new Point( ClientRectangle.Right - closeButton.Width - 1, 1); closeButton.Refresh(); } Point oldMouseLocation; // 鼠标位置 Rectangle rect; // 面板区域 bool mouseDown = false; // 鼠标按下 /// /// 鼠标按下事件 /// /// /// void titleBar_MouseDown(object sender, MouseEventArgs e) { if (this.Dock != DockStyle.None && this.Dock != DockStyle.Fill) { oldMouseLocation = e.Location; mouseDown = true; rect = this.RectangleToScreen(ClientRectangle); ControlPaint.DrawReversibleFrame(rect, Color.Black, FrameStyle.Thick); } } /// /// 鼠标移动事件 /// /// /// void titleBar_MouseMove(object sender, MouseEventArgs e) { if (mouseDown) { ControlPaint.DrawReversibleFrame(rect, Color.Black, FrameStyle.Thick); rect.Offset(e.X - oldMouseLocation.X, e.Y - oldMouseLocation.Y); oldMouseLocation = e.Location; ControlPaint.DrawReversibleFrame(rect, Color.Black, FrameStyle.Thick); } } /// /// 鼠标弹起事件 /// /// /// void titleBar_MouseUp(object sender, MouseEventArgs e) { if (mouseDown) { mouseDown = false; ControlPaint.DrawReversibleFrame(rect, Color.Black, FrameStyle.Thick); Rectangle rc = this.RectangleToScreen(ClientRectangle); Point pt = this.PointToScreen(e.Location); if (!rc.Contains(pt)) { ShowFlowForm(); } } } //protected override void OnPaint(PaintEventArgs e) //{ // base.OnPaint(e); // Pen borderPen = new Pen(borderColor); // Rectangle rc = new Rectangle( // e.ClipRectangle.Left, e.ClipRectangle.Top, // e.ClipRectangle.Right - 1, e.ClipRectangle.Bottom - 1); // e.Graphics.DrawRectangle(borderPen, rc); //} /// /// 显示浮动窗口 /// void ShowFlowForm() { flowForm.Show(Parent); flowForm.Location = new Point(rect.Left, rect.Top); Size newSize = new Size(rect.Width, rect.Height); if (newSize.Width > FlowFormMaxSize.Width) { newSize.Width = FlowFormMaxSize.Width; } newSize.Height = newSize.Height + (this.TitleVisible ? SystemInformation.CaptionHeight - this.titleBar.Height : SystemInformation.CaptionHeight); if (newSize.Height > FlowFormMaxSize.Height) { newSize.Height = FlowFormMaxSize.Height; } flowForm.Size = newSize; flowForm.Text = TitleText; while (this.Controls.Count > 2) { for (int i = 0; i < this.Controls.Count; i++) { if (this.Controls[i] != closeButton && this.Controls[i] != titleBar) { flowForm.Controls.Add(this.Controls[i]); break; } } } this.Visible = false; } /// /// 显示停靠面板 /// void ShowDockPanel() { this.Visible = true; while (flowForm.Controls.Count > 0) { this.Controls.Add(flowForm.Controls[0]); } flowForm.Hide(); } } }
### 回答1: OpenCVrectangle函数是用来绘制矩形的函数。它可以在图像上绘制一个矩形,可以设置矩形的位置、大小、颜色等参数。该函数的语法如下: cv2.rectangle(img, pt1, pt2, color[, thickness[, lineType[, shift]]]) 其,img表示要绘制矩形的图像,pt1和pt2分别表示矩形的左上角和右下角的坐标,color表示矩形的颜色,thickness表示矩形边框的宽度,lineType表示边框线的类型,shift表示坐标点的小数点位数。 ### 回答2: OpenCVrectangle函数用于在图像上绘制矩形。 该函数的语法如下: ``` cpp cv::rectangle(cv::inputOutputArray img, cv::Point pt1, cv::Point pt2, cv::Scalar color, int thickness = 1, int lineType = 8, int shift = 0); ``` 参数说明: - img:输入图像,在该图像上进行绘制。 - pt1, pt2:矩形的对角顶点坐标,即左上角和右下角的点坐标。 - color:矩形的颜色,可以是RGB或灰度值。 - thickness:矩形的线宽,默认为1。 - lineType:线的类型,默认为8。 - shift:坐标点的小数点位数,默认为0。 注意事项: - 矩形的线宽为正值时,绘制的是实心矩形;线宽为负值时,绘制的是空心矩形。 - 矩形的颜色可以通过BGR值来指定,例如红色可以表示为Scalars(0, 0, 255)。 - 矩形的线型可以是8连接线(默认)或4连接线。 该函数的作用是在图像上绘制一个矩形。通过指定对角顶点的坐标、线宽、颜色等参数,可以实现对矩形的自定义绘制。可以在图像上标出特定区域、框选目标等。矩形绘制完成后,可以通过imshow函数显示图像,或者通过imwrite函数保存图像。 ### 回答3: opencvrectangle函数用于在图像上绘制矩形框。该函数接受5个参数:图像、矩形左上角的点、矩形右下角的点、矩形边框的颜色和线条的粗细。 使用函数可以在图像上绘制一个矩形框,将一部分图像的特定区域标出来。矩形框的颜色可以通过指定BGR通道的数值来定义,例如(255, 0, 0)表示蓝色框,(0, 255, 0)表示绿色框,(0, 0, 255)表示红色框。线条的粗细可以通过整数值来设定,例如2表示粗细为2像素。 矩形框的左上角和右下角的点可以通过指定像素坐标来确定,例如(100, 100)表示左上角的点坐标为(100, 100),(200, 200)表示右下角的点坐标为(200, 200)。通过这两个点的坐标可以确定矩形框的位置和大小。 使用函数可以将图像的目标对象框出来,方便后续的分析和处理。例如在目标检测,可以使用函数将检测到的目标框出来,以便观察和验证检测结果。此外,该函数也可以用于标注图像的感兴趣区域,例如在图像处理,可以使用函数图像的ROI(感兴趣区域)框出来,对该区域进行特定的处理操作。 总之,opencvrectangle函数是一个非常有用的函数,它可以帮助我们在图像上绘制矩形框,方便后续的分析和处理。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值