How to convert cv::Mat to torch::Tensor?

The value of matrix in my code changed after converting from cv::Mat to torch::Tensor:

torch::Tensor get_rotate_matrix(double cx, double cy, double angle, double scale)
{
    cv::Point2f center = cv::Point2f((float)cx, (float)cy);
    cv::Mat matrix = cv::getRotationMatrix2D(center, angle, scale);

    std::cout << "matrix \n"
              << matrix << std::endl;

    torch::Tensor result = torch::from_blob(matrix.ptr<float>(), {2, 3});
    std::cout << "result" << std::endl;
    for (int i = 0; i < 2; i++)
    {
        for (int j = 0; j < 3; j++)
        {
            std::cout << result[i][j].item().toFloat() << "\t";
        }
        std::cout << std::endl;
    }
    return result.clone();
}

I got the following output:


matrix
[0.898794046299167, 0.4383711467890774, -7.755363398773218;
  -0.4383711467890774, 0.898794046299167, 6.407830541907433]
result
-0.00326877     1.8497  -2.67433e+16
1.71919 -1.59202e+36    -2.48471

How can I convert cv::Mat to torch::Tensor correctly?

Looking forward to your replay.

you try this

torch::tensor(at::ArrayRef<float>(img.data, h* w)).view({ h, w });