cvRectangle与cv::rectangle的用法

本文对比分析了C++中cvRectangle与cv::rectangle函数的原型与参数,提供了在Mat与IplImage*类型图像上绘制矩形的代码示例,并解释了两者之间的区别。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

cvRentangle和cv::rectangle函数原型对比:

C:

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


C++:

	void rectangle(Mat& img, Point pt1,Point pt2,const Scalar& color, int thickness=1, int lineType=8, int shift=0)
 	void rectangle(Mat& img, Rect rec, const Scalar& color, int thickness=1, int lineType=8, int shift=0 )


参数介绍:
img:图像.
pt1:矩形的一个顶点。
pt2:矩形对角线上的另一个顶点
color:线条颜色 (RGB) 或亮度(灰度图像 )(grayscale image)。
thickness:组成矩形的线条的粗细程度。取负值时(如 CV_FILLED)函数绘制填充了色彩的矩形。
line_type:线条的类型。见cvLine的描述
shift:坐标点的小数点位数。

代码:

#include <iostream>
#include <opencv2\highgui\highgui.hpp>

using namespace std;
using namespace cv;
int main()
{
	char *imageSrc = "I:\\OpenCV Learning\\picture\\sumpalace.jpg";
	Mat matImage = imread(imageSrc,-1);
        IplImage *iplImage = cvLoadImage(imageSrc,-1);
	
	if(matImage.data==0||iplImage->imageData ==0)
	{
		cout<<"图片加载失败"<<endl;
		return -1;
	}	
	
	cv::rectangle(matImage,cvPoint(20,200),cvPoint(200,300),Scalar(255,0,0),1,1,0);
	//Rect(int a,int b,int c,int d)a,b为矩形的左上角坐标,c,d为矩形的长和宽
	cv::rectangle(matImage,Rect(100,300,20,200),Scalar(0,0,255),1,1,0);
	cvRectangle(iplImage,cvPoint(20,200),cvPoint(200,300),Scalar(0,255,255),1,1,0);
	
	imshow("matImage",matImage);
	cvShowImage("IplImage",iplImage);
	waitKey();
	return 0;
}


结果:

 

如果需要在Mat类型的图上绘制矩形,选择cv::trctangle()
在IplImage*类型的图上绘制矩形,选择cvRectangle()

在后续文章中再做介绍。。。。。。

在C#中使用OpenCV实现用鼠标对图片进行选择区域操作,首先需要确保你已经安装了OpenCV的C#版本,即Emgu CV。然后可以创建一个窗体应用程序,并在其中添加鼠标事件处理函数来实现该功能。以下是一个简单的示例代码,展示了如何使用鼠标来选择图片上的一个区域,并在选择完毕后显示该区域: ```csharp using System; using System.Drawing; using System.Windows.Forms; using Emgu.CV; using Emgu.CV.Structure; using Emgu.CV.CvEnum; public partial class RegionSelectForm : Form { private bool isSelecting = false; private Rectangle selection = Rectangle.Empty; private Mat image = new Mat(); private Mat dst = new Mat(); public RegionSelectForm() { InitializeComponent(); this.MouseDown += new MouseEventHandler(RegionSelectForm_MouseDown); this.MouseMove += new MouseEventHandler(RegionSelectForm_MouseMove); this.MouseUp += new MouseEventHandler(RegionSelectForm_MouseUp); } private void RegionSelectForm_MouseDown(object sender, MouseEventArgs e) { isSelecting = true; selection.X = e.X; selection.Y = e.Y; selection.Width = 0; selection.Height = 0; } private void RegionSelectForm_MouseMove(object sender, MouseEventArgs e) { if (isSelecting) { int x = Math.Min(selection.X, e.X); int y = Math.Min(selection.Y, e.Y); int width = Math.Abs(selection.X - e.X); int height = Math.Abs(selection.Y - e.Y); selection = new Rectangle(x, y, width, height); this.Invalidate(); } } private void RegionSelectForm_MouseUp(object sender, MouseEventArgs e) { isSelecting = false; // 这里可以添加代码来处理选中的区域,例如复制到dst等操作 CvInvoke.cvCopy(image, dst, selection); } protected override void OnPaint(PaintEventArgs e) { base.OnPaint(e); if (!selection.IsEmpty) { // 绘制选中的区域 CvInvoke.cvRectangle(image, selection, new MCvScalar(255, 0, 0), 2); e.Graphics.DrawImage(image.ToBitmap(), Point.Empty); } } public void LoadImage(string imagePath) { image = new Mat(imagePath, ImreadModes.Color); dst = new Mat(image.Size, DepthType.Cv8U, 3); this.Size = image.Size; this.Invalidate(); } } // 使用示例 public class Program { public static void Main() { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); RegionSelectForm form = new RegionSelectForm(); form.LoadImage("path_to_your_image.jpg"); // 替换为你的图片路径 Application.Run(form); } } ``` 以上代码中,`RegionSelectForm` 是一个窗体类,其中包含了鼠标事件处理逻辑以及绘图逻辑。`LoadImage` 方法用于加载图片并初始化窗体的大小。在实际应用中,你可以根据需要来处理选中的区域,例如进行进一步的图像处理或者保存选中区域的图片等。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值