gpt4 book ai didi

Java findHomography 将图像点变换为地平面点

转载 作者:行者123 更新时间:2023-11-30 03:35:37 24 4
gpt4 key购买 nike

目标:从相机透视图中获取一个点(或一组点)并将其转换到相应的地平面点。

方法:使用findHomography来获取单应性Mat。计划使用perspectiveTransform()

问题:无法解释单应性Mat,也不了解如何继续。看来 9 个元素中有 5 个为空,我认为这意味着单应性有问题,尽管无法解释结果,所以 a 做了一些打印语句。

import org.opencv.calib3d.*;
import org.opencv.core.Core;
import org.opencv.core.Mat;
import org.opencv.core.MatOfPoint2f;
import org.opencv.core.Point;
import org.opencv.highgui.Highgui;
import org.opencv.imgproc.*;

import java.util.List;
import java.util.LinkedList;
import java.util.ArrayList;

final public class HomographyTest {

public static void main(String[] args) {

System.loadLibrary(Core.NATIVE_LIBRARY_NAME); // needed by OpenCV

String rfileoutput = "/home/will/dev/Homog.jpg";
String ofileoutput = "/home/will/dev/HomogOutput.jpg";

Point SEShedCornerDst = new Point(49, 74);
Point CloseForsythiaDst = new Point(41, 41);
Point CornerHazelDst = new Point(111, 157);
Point FarForsythiaDst = new Point(175, 21);
Point FirstLiquidAmberDst = new Point(235, 164);
Point SecondLiquidAmberDst = new Point(282, 721);
Point ThirdLiquidAmberDst = new Point(317, 544);

Point SEShedCornerSrc = new Point(30, 231);
Point CloseForsythiaSrc = new Point(160, 290);
Point CornerHazelSrc = new Point(50, 125);
Point FarForsythiaSrc = new Point(628, 146);
Point FirstLiquidAmberSrc = new Point(299, 64);
Point SecondLiquidAmberSrc = new Point(146, 37);
Point ThirdLiquidAmberSrc = new Point(48,34);

Point [] srcArray = new Point[7];
srcArray[0] = SEShedCornerSrc;
srcArray[1] = CloseForsythiaSrc;
srcArray[2] = CornerHazelSrc;
srcArray[3] = FarForsythiaSrc;
srcArray[4] = FirstLiquidAmberSrc;
srcArray[5] = SecondLiquidAmberSrc;
srcArray[6] = ThirdLiquidAmberSrc;

Mat OutputMat = new Mat();
LinkedList<Point> dstArray = new LinkedList<Point>();

dstArray.add(SEShedCornerDst);
dstArray.add(CloseForsythiaDst);
dstArray.add(CornerHazelDst);
dstArray.add(FarForsythiaDst);
dstArray.add(FirstLiquidAmberDst);
dstArray.add(SecondLiquidAmberDst);
dstArray.add(ThirdLiquidAmberDst);

MatOfPoint2f dst = new MatOfPoint2f();
dst.fromList(dstArray);

MatOfPoint2f src = new MatOfPoint2f();
src.fromArray(srcArray);

Mat Homog;


Homog = Calib3d.findHomography(src, dst, Calib3d.RANSAC, 10, OutputMat);

System.out.println("Columns = " + Homog.cols());
System.out.println("Rows = " + Homog.rows());
System.out.println("Width = " + Homog.width());
System.out.println("Dims = " + Homog.dims());

for (int i=1; i<= Homog.cols();i++){
for (int j=1; j<=Homog.rows();j++){
System.out.println("Row, column " + i + "," + j + " = " + Homog.get(j, i));
}
System.out.println();
}
System.out.println(Homog.toString());
System.out.println(OutputMat.toString());
Highgui.imwrite(rfileoutput, Homog);
Highgui.imwrite(ofileoutput, OutputMat);
}
}

输出;

Columns = 3
Rows = 3
Width = 3
Dims = 2
Row, column 1,1 = [D@674f1c67
Row, column 1,2 = [D@7ad1e32d
Row, column 1,3 = null

Row, column 2,1 = [D@6999de59
Row, column 2,2 = [D@74d4db38
Row, column 2,3 = null

Row, column 3,1 = null
Row, column 3,2 = null
Row, column 3,3 = null

Mat [ 3*3*CV_64FC1, isCont=true, isSubmat=false, nativeObj=0x7f744016bb50, dataAddr=0x7f744016b940 ]
Mat [ 7*1*CV_8UC1, isCont=true, isSubmat=false, nativeObj=0x7f7440166fe0, dataAddr=0x7f744016b9b0 ]

我走在正确的道路上吗?如果没有,我该怎么办?

如果是这样,在 perspectiveTransform 的 src Mat 中输入什么类型,以及如何在它所说的两个 channel 中执行此操作?

http://docs.opencv.org/java/org/opencv/core/Core.html#perspectiveTransform(org.opencv.core.Mat,%20org.opencv.core.Mat,%20org.opencv.core.Mat)

最佳答案

我无法真正解释你的代码,尤其是你的观点,但我认为这并不重要,因为正如你所说,你的目标是从两组点创建一个单应性矩阵,并用所得的单应性矩阵透视地变换图像.

迭代单应性矩阵

首先,您通过单应性矩阵(这是一个 3x3 矩阵)进行迭代时得到 null 的原因是因为 Mat 索引就像数组一样从 0 开始并以“长度 - 1”结束。

它应该看起来像这样:

for (int i = 0; i < homography.cols(); i++){
for (int j = 0; j < homography.rows(); j++){
System.out.println("[" + i + "," + j + "] = " + Arrays.toString(homography.get(j, i)));
}
}

查找单应性并应用(到图像)

要查找 srcdst 平面之间的单应性矩阵,您可以使用 findHomography()。有关此函数的更多信息,其参数请参见 API

// obtain your homography mat (picked your parameters.. you have to play to get the right results)
Mat homography = Calib3d.findHomography(src, dst, Calib3d.RANSAC, 10);
// image you want to transform
Mat image = Highgui.imread("image/img.jpg");
// outputMat will contain the perspectively changed image
Imgproc.warpPerspective(image, outputMat, homography, new Size(image.cols(), image.rows()));

将单应性应用于单个点(不是图像)

Mat src = new Mat();
// push a mat object with your points to the src
src.push_back(new MatOfPoint2f(new Point(x,y)));
// dst will contain your transformed points
Core.perspectiveTransform(src, dst, homography)

顺便说一句..

在 Java 中,变量名通常以小写字母开头。我看到了 HomogOutputMat。当我读到类似的东西时,我希望它们是类。

关于Java findHomography 将图像点变换为地平面点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27972703/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com