如何在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
Pointby adding thexandyvalues 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
相关产品推荐
相关产品推荐

