opencv中Point操作

为了描述图像中的点,opencv中提供了点的模板类,分为2维点模板类Point_和3维点模板类Point3_。Point_通过2维图像平面中的x和y坐标确定点的位置,Point3_通过3维立体图像中的x、y、z坐标确定点的位置。对于点的坐标的类型可以是int、double、float类型,下面是源代码中的定义:

typedef Point_ Point2i;
typedef Point2i Point;
typedef Point_ Point2f;
typedef Point_ Point2d;
typedef Point3_ Point3i;
typedef Point3_ Point3f;
typedef Point3_ Point3d;

由上面的定义我们可以发现Point_、Point2i、Point互相等价,所以为了方便我们在定义整型点的时候会直接使用Point。同样,Point_与 Point2f等价,Point_ 与Point2d等价,Point3_与 Point3i等价,Point3_ 与Point3f等价,Point3_与 Point3d等价,这样的定义会方便用户的使用。另外可以转换点的坐标到指定的类型,对于浮点型的点向整型的点进行转换采用的是四舍五入的方法。
实例化一个类也是非常方便的,用法如下(以2D整型坐标点为例):

Point point;//创建一个2D点对象
point.x = 10;//初始化x坐标值
point.y = 8;//初始化y坐标值
或者
Point point = Point(10, 8);

这些点的类可以实现运算操作(三维点还支持向量运算和比较运算):

pt1 = pt2 + pt3;
pt1 = pt2 - pt3;
pt1 = pt2 * a;
pt1 = a * pt2;
pt1 += pt2;
pt1 -= pt2;
pt1 *= a;
double value = norm(pt); // L2
normpt1 == pt2;
pt1 != pt2;

下面是示例代码:

 Point2f a(0.3f, 0.f), b(0.f, 0.4f);
  Point pt = (a + b)*10.f;//浮点型坐标可以直接与整型坐标进行赋值操作
  cout << pt.x << ", " << pt.y << endl;
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要在Java OpenCV绘制两个点之间的线,可以按照以下步骤进行操作: 1. 首先,使用OpenCV的 `line()` 函数绘制两个点之间的直线。这个函数需要传入三个参数:图像、起点坐标和终点坐标。 2. 然后,计算出这两个点的心坐标。可以使用以下代码计算心坐标: ``` int center_x = (start_x + end_x) / 2; int center_y = (start_y + end_y) / 2; ``` 3. 最后,使用 `line()` 函数再次将图像心与终点坐标之间的直线绘制出来。这个函数需要传入三个参数:图像、心坐标和终点坐标。 完整的代码示例: ``` import org.opencv.core.Core; import org.opencv.core.Point; import org.opencv.highgui.HighGui; import org.opencv.imgcodecs.Imgcodecs; import org.opencv.imgproc.Imgproc; public class DrawLine { public static void main(String[] args) { // Load the OpenCV library System.loadLibrary(Core.NATIVE_LIBRARY_NAME); // Load the image String filename = "image.jpg"; Mat img = Imgcodecs.imread(filename); // Define the start and end points Point start = new Point(100, 100); Point end = new Point(200, 200); // Draw the line between the two points Imgproc.line(img, start, end, new Scalar(0, 255, 0), 2); // Calculate the center point int center_x = (int)((start.x + end.x) / 2); int center_y = (int)((start.y + end.y) / 2); Point center = new Point(center_x, center_y); // Draw the line between the center point and the end point Imgproc.line(img, center, end, new Scalar(0, 0, 255), 2); // Display the image HighGui.imshow("Image", img); HighGui.waitKey(); } } ```

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值