rainyl / opencv_dart

OpenCV bindings for Dart language and Flutter. Support Asynchronous Now!
https://pub.dev/packages/opencv_dart
Apache License 2.0
138 stars 18 forks source link

VecVecPoint manipulation #153

Closed gustavovisentini closed 4 months ago

gustavovisentini commented 4 months ago

Question

its possible order by size of contourArea in this vector VecVecPoint?

The code reads a series of contours, calculates and orders their areas, and then uses these areas to calculate metrics such as total volume and a central metric (dmv). The logic behind this appears to be to analyze the distribution of contour areas and find a central point of balance based on volume.

My code in c++

std::vector<int> pointsVector;
    for(int i=0;i<contours.size();i++){
        pointsVector.push_back(contourArea(contours[i]));
    }
    //std::cout<<"\n Vetor Ordenado:\n\n";
    std::sort(pointsVector.begin(), pointsVector.end());
    for(int i=0;i<pointsVector.size();i++){
        //std::cout<<"\n"<<pointsVector[i];
    }

    int areaTotalGotas=0;
    for(int i=0;i<pointsVector.size();i++){
        areaTotalGotas=areaTotalGotas+pointsVector[i];
    }

    //std::cout<<"\nVolume: "<<areaTotalGotas<<"\n\n";
    int dmvPre = areaTotalGotas/2;
    int dmvAux=0;
    int dmv=0;
    int pos=0;
    for(int i=0;i<=pointsVector.size();i++){
        if (dmvAux<dmvPre){
            dmvAux=dmvAux+pointsVector[i];
            pos = i;
        }
    }
    dmv = pointsVector[pos];
    //std::cout<<"\n\nDMV "<<dmv<<"\n\n Volume medio calculado: "<<dmvPre<<"\n\n";
rainyl commented 4 months ago

VecVecPoint is Iterable, so you can just use normal Iterable methods. e.g., something like

final points = [
  [cv.Point(1, 1), cv.Point(2, 2), cv.Point(3, 3), cv.Point(4, 4)],
  [cv.Point(9, 9), cv.Point(0, 0), cv.Point(1, 6), cv.Point(2, 8)],
  [cv.Point(5, 5), cv.Point(6, 6), cv.Point(7, 7), cv.Point(8, 8)],
];
final vec = points.cvd;
final areas = vec.map(cv.contourArea).toList(growable: false);
final vecSorted = vec.indexed.toList(growable: false);
vecSorted.sort((a, b)=>areas[a.$1].compareTo(areas[b.$1]));
print(areas); // [0.0, 29.0, 0.0]
print(vecSorted.map((e) => e.$2)); // ((Point(1, 1), Point(2, 2), Point(3, 3), Point(4, 4)), (Point(5, 5), Point(6, 6), Point(7, 7), Point(8, 8)), (Point(9, 9), Point(0, 0), Point(1, 6), Point(2, 8)))
rainyl commented 4 months ago

vector wrappers like VecU8, VecPoint etc. will be refactored in the next release for better performance and returning reference, I have tried my best to keep compatibility but this do is a breaking change.

If you have any further questions, please open a new issue, I am going to close this one, have fun with opencv_dart!