Skip to content

Issues with Float Mats and warpAffine #8

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
FromDarkHell opened this issue Mar 19, 2024 · 1 comment
Closed

Issues with Float Mats and warpAffine #8

FromDarkHell opened this issue Mar 19, 2024 · 1 comment

Comments

@FromDarkHell
Copy link

FromDarkHell commented Mar 19, 2024

When doing something like:

import 'package:opencv_dart/opencv_dart.dart' as cv;

void cvTest() {
  cv.Mat mask = cv.Mat.ones(512, 512, cv.MatType.CV_32FC1);

  List<cv.Point2f> faceTemplate = [
    cv.Point2f(192.98138, 239.94708),
    cv.Point2f(318.90277, 240.1936),
    cv.Point2f(256.63416, 314.01935),
    cv.Point2f(201.26117, 371.41043),
    cv.Point2f(314.08905, 371.15118)
  ];

  List<cv.Point2f> landmarks = [
    cv.Point2f(916.1744018554688, 436.8168579101563),
    cv.Point2f(1179.1181030273438, 448.0384765625),
    cv.Point2f(1039.171106147766, 604.8748825073242),
    cv.Point2f(908.7911743164062, 683.4760314941407),
    cv.Point2f(1167.2201416015625, 693.495068359375),
  ];

  var (affineMatrix, _) =
      cv.estimateAffinePartial2D(landmarks, faceTemplate, method: cv.LMEDS);

  var invMask = cv.Mat.empty();
  cv.warpAffine(mask, invMask, affineMatrix, (2048, 2048));

  for (int i = 0; i < 2047; i++) {
    for (int j = 0; j < 2047; j++) {
      var val = invMask.at<double>(i, j);
      assert(val.isFinite);
    }
  }
}

This immediately fails because 0,0 is Infinity instead of 0.

Here is equivalent C++ code which does return a Mat with finite values (in this case, just 1 iirc)

#include "opencv2/opencv.hpp"
#include "opencv2/highgui/highgui.hpp"
#include <opencv2/core/types_c.h>
#include <iostream>
#include "opencv2/imgproc.hpp"
#include <vector>

using namespace cv;

int main(int argc, char **argv)
{
    Mat mask = Mat::ones(512, 512, CV_32FC1);

    std::vector<Point2f> faceTemplate = {
        Point2f(192.98138, 239.94708),
        Point2f(318.90277, 240.1936),
        Point2f(256.63416, 314.01935),
        Point2f(201.26117, 371.41043),
        Point2f(314.08905, 371.15118)};

    std::vector<Point2f> landmarks = {
        Point2f(916.1744018554688, 436.8168579101563),
        Point2f(1179.1181030273438, 448.0384765625),
        Point2f(1039.171106147766, 604.8748825073242),
        Point2f(908.7911743164062, 683.4760314941407),
        Point2f(1167.2201416015625, 693.495068359375),
    };

    Mat affineMatrix = cv::estimateAffinePartial2D(landmarks, faceTemplate, cv::noArray(), cv::LMEDS);

    Mat invMask = cv::Mat();
    cv::warpAffine(mask, invMask, affineMatrix, cv::Size(2048, 2048));

    for (int i = 0; i < 2047; i++)
    {
        for (int j = 0; j < 2047; j++)
        {
            float val = invMask.at<float>(i, j);
            assert(val == 0 || val == 1);
        }
    }

    return 0;
}

Edit: This also similarly happens when you use CV_64FC1; Except instead of +INF, it returns 1.7976931348623157e+308 or some similarly large number

@rainyl
Copy link
Owner

rainyl commented Mar 20, 2024

Got it, this is because the default value of Scalar.default_() is double.maxFinite, I forgot why I set maxfinite as the default value of scalar rather than zero...

For now, you can explicitly pass the borderValue: Scalar.black to fix,

Scalar? borderValue,
I will change it to zero in the next minor version.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants