You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Java OpenCV中实现两个点相加?(C++代码转Java)

Solution for Point Addition in Java OpenCV

Got it! The problem here is that C++ OpenCV's Point class has an overloaded + operator that lets you easily add two points together, but Java doesn't support operator overloading. So we have to manually calculate the combined x and y coordinates instead.

Step-by-Step Explanation:

  • In your C++ code, faces[0].tl() + eye.tl() takes the top-left corner of the first face rectangle and adds the top-left corner of the eye rectangle to it. This gives the absolute position of the eye within the frame.
  • In Java, we need to create a new Point by adding the x and y values of the two points separately.

Updated Java Code:

Rect[] faces = myFaces.toArray(new Rect[myFaces.size()]); // Avoids unchecked cast warning
Rect[] eyes = myEyes.toArray(new Rect[myEyes.size()]);

for (Rect eye : eyes) {
    // Calculate the absolute top-left point of the eye
    Point topLeft = new Point(
        faces[0].tl().x + eye.tl().x,
        faces[0].tl().y + eye.tl().y
    );
    // Calculate the absolute bottom-right point of the eye
    Point bottomRight = new Point(
        faces[0].tl().x + eye.br().x,
        faces[0].tl().y + eye.br().y
    );
    // Draw the rectangle
    Imgproc.rectangle(frame, topLeft, bottomRight, new Scalar(255, 0, 0), 2);
}

Quick Note:

I added new Rect[myFaces.size()] to the toArray() calls to get rid of the unchecked cast warning you might have seen. It's a small improvement that makes the code cleaner and safer.

内容的提问来源于stack exchange,提问作者Blazing Forrest

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.27 07:26:05