如何在Java中将2个矩阵相乘,然后将结果用作Imgproc.warpPerspective函数的转换矩阵 [英] How to multiply 2 matrices in java and then use the result as transformation matrix for Imgproc.warpPerspective function

查看:86
本文介绍了如何在Java中将2个矩阵相乘,然后将结果用作Imgproc.warpPerspective函数的转换矩阵的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建3个矩阵: A为3x4,B为4x4,C为4x3.然后我想将它们乘以D = A *(B * C)以接收3x3矩阵 然后用D调用warpPerspective. 上面所有这些都是使用Java完成的.在c ++中,与Java相对极端"容易,我找不到Java实现的任何真实示例.

I want to create 3 matrices: A of 3x4 and B of 4x4 and C of 4x3 . Then I want to multiply them D = A * (B * C) to receive a 3x3 matrix And then call warpPerspective with D. All of the above is needed to be done with Java. In c++ it is 'extremely' easy opposed to Java, I could not find any real example for Java implementation.

推荐答案

package test;

import org.opencv.core.Core;
import org.opencv.core.CvType;
import org.opencv.core.Mat;
import org.opencv.core.Size;
import org.opencv.imgproc.Imgproc;

public class MatrixMul {

    static {
        System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
    }

    public static void main(String[] args) {
        Mat A = new Mat(3, 4, CvType.CV_32FC1);
        Mat B = new Mat(4, 4, CvType.CV_32FC1);
        Mat C = new Mat(4, 3, CvType.CV_32FC1);
        Mat tmp = new Mat();
        Mat D = new Mat();

        A.put(0, 0, new float[] { 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3 });
        B.put(0, 0, new float[] { 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4 });
        C.put(0, 0, new float[] { 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3 });

        Core.gemm(B, C, 1, new Mat(), 0, tmp);
        Core.gemm(A, tmp, 1, new Mat(), 0, D);

//      Mat src = ...
//      Mat dst = new Mat();
//      Imgproc.warpPerspective(src, dst, D, new Size(3,3));

        System.out.println("A =\n" + A.dump());
        System.out.println("B =\n" + B.dump());
        System.out.println("C =\n" + C.dump());
        System.out.println("B * C =\n" + tmp.dump());
        System.out.println("D = A * (B * C) =\n" + D.dump());
    }

}

这篇关于如何在Java中将2个矩阵相乘,然后将结果用作Imgproc.warpPerspective函数的转换矩阵的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆