简体   繁体   English

将OpenCV方法从Java重写为C ++

[英]Rewrite OpenCV method from Java to C++

Could anyone tell me how does this piece of code looks like in C++ ? 谁能告诉我这段代码在C ++中是什么样子? I'm really confused about java and it's representation of OpenCV. 我对Java及其OpenCV的表示感到非常困惑。

public static MatOfPoint convexHull(MatOfPoint contour) {
    MatOfInt indexes = new MatOfInt();
    Imgproc.convexHull(contour, indexes);
    Point[] convexPoint = new Point[indexes.rows()];

    double x, y;
    int index;
    for (int i = 0; i < indexes.rows(); i++) {
       index = (int) indexes.get(i, 0)[0];
       x = contour.get(index, 0)[0];
       y = contour.get(index, 0)[1];
       convexPoint[i] = new Point(x, y);
    }

  return new MatOfPoint(convexPoint);
}

What I got so far is : 到目前为止,我得到的是:

vector< Point > convexHull(vector< Point> & contour) {
  vector<int> indexes;
  convexHull(contour, indexes);
  Point[] convexPoint = new Point[indexes.size()];

  double x, y;
  int index;
  for (size_t i = 0; i < indexes.size(); i++) {
     //I really don't understant what's going on here  
  }
}

Also could anyone explain me please the body of for-loop of that java code ? 也有人可以解释一下该Java代码的for循环吗?

The loop populates the convexPoint array with points from contour , according to the indices returned by Imgproc.convexHull . 循环根据Imgproc.convexHull返回的索引,用来自contour点填充convexPoint数组。

I'm not sure about Java, but in with the C++ API it's rather pointless, since cv::convexHull will happily do that for you when the output argument is std::vector<cv::Point> . 我不确定Java,但是使用C ++ API却毫无意义,因为当输出参数为std::vector<cv::Point>时, cv::convexHull会为您高兴地做到这std::vector<cv::Point>

Then function then becomes 然后功能变为

std::vector<cv::Point> convexHull(std::vector<cv::Point> const& contour)
{
    std::vector<cv::Point> result;
    cv::convexHull(contour, result);
    return result;
}

and it becomes somewhat questionable whether you even need it. 是否需要甚至变得有些疑问。

First of all, I don't get why you want to convert a java code to c++ when that already exist. 首先,我不明白为什么要将Java代码转换为c ++。 Looking at your posted code (if this is copied from somewhere, it will be very helpful to post link here) 查看您发布的代码(如果是从某处复制的,则在此处发布链接将非常有帮助)

public static MatOfPoint convexHull(MatOfPoint contour) {
    MatOfInt indexes = new MatOfInt();
    Imgproc.convexHull(contour, indexes);
    Point[] convexPoint = new Point[indexes.rows()];

    double x, y;
    int index;
    for (int i = 0; i < indexes.rows(); i++) {
       index = (int) indexes.get(i, 0)[0];
       x = contour.get(index, 0)[0];
       y = contour.get(index, 0)[1];
       convexPoint[i] = new Point(x, y);
    }

  return new MatOfPoint(convexPoint);
}

That code is calling opencv Imgproc.convexHull(contour,indexes) . 该代码正在调用opencv Imgproc.convexHull(contour,indexes) It passes MatOfInt which means you will get indexes of points. 它通过MatOfInt ,这意味着您将获得点的索引。 Now if you look carefully at return value of the function, you can it is of type MatOfPoint , and that inside of the for loop, we are building the return object from point indexes. 现在,如果您仔细看一下该函数的返回值,您可以将其MatOfPoint类型,并且在for循环内部,我们将从点索引构建返回对象。 So the loop is just to convert from indexes to actual points. 因此,循环只是将索引转换为实际点。 This is to describe what's happening in that code. 这是为了描述该代码中正在发生的事情。 But what makes all this strange is that opencv C++ (Not sure if opencv/java has this) has already the function to return actual points. 但是令所有这些奇怪的是,opencv C ++(不确定opencv / java是否具有此功能)已经具有返回实际点的功能。 You can check opencv official documentation on convexHull function here 你可以在这里查看关于凸函数的opencv官方文档

Opencv c++ convexHull function signature is: OpenCV C ++的凸凸函数签名是:

void convexHull(InputArray points, OutputArray hull, bool clockwise=false, bool returnPoints=true )

And the parameters description is: 参数说明为:

Parameters : 参数
points – Input 2D point set, stored in std::vector or Mat. points –输入2D点集,存储在std :: vector或Mat中。 hull – Output convex hull. 船体 -输出凸船体。 It is either an integer vector of indices or vector of points. 它可以是索引的整数向量,也可以是点的向量。 In the first case, the hull elements are 0-based indices of the convex hull points in the original array (since the set of convex hull points is a subset of the original point set). 在第一种情况下,外壳元素是原始数组中凸包点的从0开始的索引(因为凸包点集是原始点集的子集)。 In the second case, hull elements are the convex hull points themselves. 在第二种情况下,船体元素本身就是凸的船体点。
clockwise – Orientation flag. 顺时针 –方向标记。 If it is true, the output convex hull is oriented clockwise. 如果为true,则输出凸包为顺时针方向。 Otherwise, it is oriented counter-clockwise. 否则,其方向为逆时针方向。 The assumed coordinate system has its X axis pointing to the right, and its Y axis pointing upwards. 假定的坐标系的X轴指向右侧,Y轴指向上方。 orientation – Convex hull orientation parameter in the old API, CV_CLOCKWISE or CV_COUNTERCLOCKWISE. 方向 -旧API中的凸包方向参数CV_CLOCKWISE或CV_COUNTERCLOCKWISE。 returnPoints – Operation flag. returnPoints –操作标志。 In case of a matrix, when the flag is true, the function returns convex hull points. 对于矩阵,当标志为true时,该函数返回凸包点。 Otherwise, it returns indices of the convex hull points. 否则,它返回凸包点的索引。 When the output array is std::vector, the flag is ignored, and the output depends on the type of the vector: std::vector implies returnPoints=true, std::vector implies returnPoints=false. 当输出数组为std :: vector时,将忽略该标志,并且输出取决于向量的类型:std :: vector表示returnPoints = true,std :: vector表示returnPoints = false。

As you can see from description of the parameters, if you pass hull as a vector of integers, you get back indexes. 从参数说明中可以看到,如果将船体作为整数向量传递,则将返回索引。 But if you pass a vector of points, you get back actual points rather than their indices. 但是,如果传递点向量,则将获得实际点而不是其索引。 That's exactly what your posted function is doing. 这正是您发布的功能正在执行的操作。 So rather than converting to c++, just use opencv c++ convexHull function directly by passing a vector of points. 因此,与其直接转换为c ++,不如直接通过传递点向量直接使用opencv c ++凸面函数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM