rainyl / opencv_dart

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

X and Y axis inversion when drawing a circle using the circle function #284

Closed pablo-souza closed 2 weeks ago

pablo-souza commented 2 weeks ago

Describe the bug X and Y axis inversion when drawing a circle using the circle function

To reproduce

alternatives = //image
gray = await cv.cvtColorAsync(alternatives, cv.COLOR_BGR2GRAY);
blur = await cv.gaussianBlurAsync(gray, (3, 3), 0,);
thresh = (await cv.thresholdAsync(blur, 150, 255, cv.THRESH_BINARY)).$2;
final contornos = (await cv.findContoursAsync(
    thresh, cv.RETR_LIST, cv.CHAIN_APPROX_NONE))
    .$1;

for (var contour in contornos) {
  var area = cv.contourArea(contour);
  var perimeter = cv.arcLength(contour, true);
  var circularidade = 4 * 3.14159 * (area / (perimeter * perimeter));
  if (0.7 < circularidade && circularidade <= 1.2){
    var (center,radius) = cv.minEnclosingCircle(contour);
    int ratio = radius.toInt();
    int x = center.x.toInt();
    int y = center.y.toInt();
    cv.circle(
      alternatives,
      cv.Point(x,y),
      ratio, cv.Scalar.red,
    );
    cont++;
  }
}

return cv.imencode(".png", alternatives).$2;

Expected behavior Draw a circle around each alternative photo_2024-11-02_12-14-44

Found behavior A single circle was drawn photo_2024-11-02_12-14-40

Solution

alternatives = //image
gray = await cv.cvtColorAsync(alternatives, cv.COLOR_BGR2GRAY);
blur = await cv.gaussianBlurAsync(gray, (3, 3), 0,);
thresh = (await cv.thresholdAsync(blur, 150, 255, cv.THRESH_BINARY)).$2;
final contornos = (await cv.findContoursAsync(
    thresh, cv.RETR_LIST, cv.CHAIN_APPROX_NONE))
    .$1;

for (var contour in contornos) {
  var area = cv.contourArea(contour);
  var perimeter = cv.arcLength(contour, true);
  var circularidade = 4 * 3.14159 * (area / (perimeter * perimeter));
  if (0.7 < circularidade && circularidade <= 1.2){
    var (center,radius) = cv.minEnclosingCircle(contour);
    int ratio = radius.toInt();
    int x = center.x.toInt();
    int y = center.y.toInt();
    cv.circle(
      alternatives,
      cv.Point(y,x), //inversion
      ratio, cv.Scalar.red,
    );
    cont++;
  }
}

return cv.imencode(".png", alternatives).$2;
rainyl commented 2 weeks ago

verified, will be fixed soon.

rainyl commented 2 weeks ago

@pablo-souza This bug has been resolved in v1.3.2, you can upgrade now~