-4

I have a List variable 'l' which has values like this:

List<Point> l = new ArrayList<Point>();
Imgproc.convexHull(contours,hull);
for (int j = 0; j < intlist.length; j++) {
     l.add(contours.toList().get(hull.toList().get(j)));
}

After this I am getting following values of l:

l = [{1,2}, {3,4}, {5,6}]

and so on. How to find the average of 1st and 2nd values in l.get(i) for all i, as an array. For example, for the above I want the following:

x = 3
y = 4
Community
  • 1
  • 1
Ank
  • 1,864
  • 4
  • 31
  • 51
  • 3
    Iteration + average of each value. What is the problem on what you should have tried ? – AxelH Jun 19 '17 at 06:37
  • Possible duplicate of [Calculating average of an array list?](https://stackoverflow.com/questions/10791568/calculating-average-of-an-array-list) – Drahoš Maďar Jun 19 '17 at 06:37
  • 3
    There is something wrong in the way you fill the list. In a list of `Point` you are only supposed to get one pair of numbers in every item. Instead, it seems like your list is actually a `List>`. Please show the "some processing" in your question. – RealSkeptic Jun 19 '17 at 06:37
  • 1
    if doing this: ***l.get(0)*** returns this: ***[{1,2}, {3,4}, {5,6}]*** then you have a list of POINTS not point.... – ΦXocę 웃 Пepeúpa ツ Jun 19 '17 at 06:38
  • @AxelH How to do that? Android Studio tells me that even '+' operator cannot be applied to 'org.opencv.core.Point', 'org.opencv.core.Point' – Ank Jun 19 '17 at 06:52
  • @dram This question is not a duplicate, I have already tried all the answers given on that link – Ank Jun 19 '17 at 06:54
  • If it was possible to downvote an edit, I would do it. How is it supposed to helped to get how you build your list (that still look like a simple List of one point per index, To be sure, print the context of your list). But I still don't see what you have tried to get the average value. And you can't use `+` operator on Object, it is restricted to primitive numeric type. You need to get `p.get(x)` and `p.getY()` to get the numeric value of each point. NOTE: It seems you are playing with a project that is above your knowledge, you should take some course first about basic java. – AxelH Jun 19 '17 at 06:54
  • 1
    @AxelH If I do l.get(0), then I get {1,2} – Ank Jun 19 '17 at 06:59
  • @RealSkeptic I have edited my post, please have a look – Ank Jun 19 '17 at 07:01
  • @ΦXocę웃Пepeúpaツ I have edited my post, please have a look – Ank Jun 19 '17 at 07:01
  • @Ank, then do you get `[{1,2}, {3,4}, {5,6}]` or `{1,2}`? – RealSkeptic Jun 19 '17 at 07:01
  • First problem then, you have a `List`, not a `List` or `List>` so you only have ONE `Point` per index. So you already have the average value in this case ;) During the population, you need to iterate also on the `contours` length to get every value and add them in a `Collection`, that `Collection` will be added in your `List` – AxelH Jun 19 '17 at 07:02
  • @RealSkeptic If I do l.get(0), then I get {1,2} – Ank Jun 19 '17 at 07:02
  • OK, good. So you want to find the average of the whole list? Well, you need first to define what "average" means for points. For numbers, it's the sum of the numbers divided by their count. But What is the average of points? As soon as you have a proper definition, then you can devise a program that will iterate the list and do the appropriate calculation based on the definition. – RealSkeptic Jun 19 '17 at 07:04
  • @AxelH I want the average of all the indices. How to do that? – Ank Jun 19 '17 at 07:07
  • @RealSkeptic Yeah, I want the average of points like numbers! – Ank Jun 19 '17 at 07:08
  • @Ank what is the **definition** of the average of points? Points are *not* numbers, they are pairs of numbers, locations in a plane, you have to give a **definition** for an average of locations that makes sense in your context. – RealSkeptic Jun 19 '17 at 07:09
  • @RealSkeptic I want the average of all the x and all the y coordinates separately – Ank Jun 19 '17 at 07:11
  • @RealSkeptic How to do that? How to get just the x or y coordinate, to find their average? – Ank Jun 19 '17 at 07:22
  • 1
    Did you check the documentation for `Point`? – RealSkeptic Jun 19 '17 at 07:23
  • @RealSkeptic So I tried this: https://docs.oracle.com/javase/7/docs/api/java/awt/Point.html#getX() but now I am getting the error that Cannot resolve method 'getX(int)' – Ank Jun 19 '17 at 07:44
  • @RealSkeptic seems like there is no such method. Am I looking at the wrong documentation? Can you please send me the link – Ank Jun 19 '17 at 08:03

1 Answers1

1

Finally I got the answer! Below code will do the job:

double sum_x = 0;
double sum_y = 0;
int j;
for (j = 0; j < hull.size().height; j++) {
     l.add(contours.toList().get(hull.toList().get(j)));
     sum_x += l.get(j).x;
     sum_y += l.get(j).y;
}
x = sum_x / j;
y = sum_y / j;
Ank
  • 1,864
  • 4
  • 31
  • 51