使用JavaCV计算单应性的CvPoint2D32f到cvMat的列表

发布于 2025-01-08 07:03:53 字数 900 浏览 0 评论 0原文

使用 JavaCV。我在 ArrayList 中有 2 组 CvPoint2D32f 点,一组来自移动电话的图像计算机,另一组来自恒定的已知图像源。

我想使用这些点应用 cvFindHomogrpahy() 方法来查找点之间的单应矩阵。我正在使用以下代码来尝试执行此操作,但我陷入了如何从我知道的点到 cvFindHomogrpahy() 方法采用的 2 cvMat 的问题上参数:

matsrc = cvCreateMat(points.size(), 2, CV_32FC1);
matdst = cvCreateMat(known.size(), 2, CV_32FC1);

for(int s=0; s < points.size(); s++){
     CvPoint2D32f p = (CvPoint2D32f)points.get(i).get("Point");
     //Add this point to matsrc                         
}

for(int s=0; s < known.size(); s++){
     CvPoint2D32f p = (CvPoint2D32f)known.get(i).get("Point");
     //Add this point to matdst                         
}


CvMat mat = cvCreateMat(3, 3, CV_32FC1);
cvFindHomography(matsrc, matdst, mat); //Here the matrices created are used to find the 3x3 Homography transform Matrix

我的处理方式是否完全错误?

Using JavaCV. I have a 2 sets of CvPoint2D32f points in an ArrayList one from an image caputer from a mobile phone and another from a known image source that is constant.

I want to apply the cvFindHomogrpahy() Method using these points to find a Homography Matrix between the points. I am using the following code to try and do this but I am stuck on how to get from the points I know to the 2 cvMat that the cvFindHomogrpahy() Method takes as parameters:

matsrc = cvCreateMat(points.size(), 2, CV_32FC1);
matdst = cvCreateMat(known.size(), 2, CV_32FC1);

for(int s=0; s < points.size(); s++){
     CvPoint2D32f p = (CvPoint2D32f)points.get(i).get("Point");
     //Add this point to matsrc                         
}

for(int s=0; s < known.size(); s++){
     CvPoint2D32f p = (CvPoint2D32f)known.get(i).get("Point");
     //Add this point to matdst                         
}


CvMat mat = cvCreateMat(3, 3, CV_32FC1);
cvFindHomography(matsrc, matdst, mat); //Here the matrices created are used to find the 3x3 Homography transform Matrix

Am I going about this the totally wrong way?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

风和你 2025-01-15 07:03:53

总之你可以使用CvMat的put方法。

这是对代码进行一些修改后的完整解决方案:

import java.io.*;
import java.util.*;

import static com.googlecode.javacv.cpp.opencv_core.*;
import static com.googlecode.javacv.cpp.opencv_imgproc.*;
import static com.googlecode.javacv.cpp.opencv_highgui.*;
import static com.googlecode.javacv.cpp.opencv_calib3d.*;

/**
 *
 * @author rics
 */
public class Main {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {      
    List<CvPoint2D32f> points = new ArrayList<CvPoint2D32f>();
    List<CvPoint2D32f> known = new ArrayList<CvPoint2D32f>();
    // points and known should be filled with valid values
    // here are just some ad-hoc numbers that do not result a singular (unsolvable) configuration 
    for(int i=0; i < 2; i++){
        points.add(cvPoint2D32f((double)i,10 - 2* (double)i));
        known.add(cvPoint2D32f((double)i,10 - 2*(double)i));
    }
    for(int i=2; i < 5; i++){
        points.add(cvPoint2D32f((double)i,(double)i));
        known.add(cvPoint2D32f((double)i,(double)i));
    }
    CvMat matsrc = cvCreateMat(points.size(), 2, CV_32FC1);
    CvMat matdst = cvCreateMat(known.size(), 2, CV_32FC1);

    // filling the matrices with the point coordinates
    for(int s=0; s < points.size(); s++){
        CvPoint2D32f p = points.get(s);//.get("Point");
        //Add this point to matsrc                         
        matsrc.put(s,0,p.x());
        matsrc.put(s,1,p.y());
    }

    for(int s=0; s < known.size(); s++){
        CvPoint2D32f p = known.get(s);//.get("Point");
        //Add this point to matdst                         
        matdst.put(s,0,p.x());
        matdst.put(s,1,p.y());
    }

    CvMat mat = cvCreateMat(3, 3, CV_32FC1);
    cvFindHomography(matsrc, matdst, mat); //Here the matrices created are used to find the 3x3 Homography transform Matrix
    // displaying the resulting matrix
    for( int i = 0; i < 3; ++i) {
        for( int j = 0; j < 3; ++j) {
        System.out.print(mat.get(i,j) + ",");
        }
        System.out.println();
    }
    }
}

对我来说结果是:

1.0,5.011022034363615E-16,-5.249974158069671E-15,
1.6479031282788333E-15,1.0,-5.495862740459911E-15,
4.4222287862990617E-16,1.2693993089577568E-16,1.0,

In short you can use the put method of CvMat.

Here is a complete solution after some modifications of your code:

import java.io.*;
import java.util.*;

import static com.googlecode.javacv.cpp.opencv_core.*;
import static com.googlecode.javacv.cpp.opencv_imgproc.*;
import static com.googlecode.javacv.cpp.opencv_highgui.*;
import static com.googlecode.javacv.cpp.opencv_calib3d.*;

/**
 *
 * @author rics
 */
public class Main {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {      
    List<CvPoint2D32f> points = new ArrayList<CvPoint2D32f>();
    List<CvPoint2D32f> known = new ArrayList<CvPoint2D32f>();
    // points and known should be filled with valid values
    // here are just some ad-hoc numbers that do not result a singular (unsolvable) configuration 
    for(int i=0; i < 2; i++){
        points.add(cvPoint2D32f((double)i,10 - 2* (double)i));
        known.add(cvPoint2D32f((double)i,10 - 2*(double)i));
    }
    for(int i=2; i < 5; i++){
        points.add(cvPoint2D32f((double)i,(double)i));
        known.add(cvPoint2D32f((double)i,(double)i));
    }
    CvMat matsrc = cvCreateMat(points.size(), 2, CV_32FC1);
    CvMat matdst = cvCreateMat(known.size(), 2, CV_32FC1);

    // filling the matrices with the point coordinates
    for(int s=0; s < points.size(); s++){
        CvPoint2D32f p = points.get(s);//.get("Point");
        //Add this point to matsrc                         
        matsrc.put(s,0,p.x());
        matsrc.put(s,1,p.y());
    }

    for(int s=0; s < known.size(); s++){
        CvPoint2D32f p = known.get(s);//.get("Point");
        //Add this point to matdst                         
        matdst.put(s,0,p.x());
        matdst.put(s,1,p.y());
    }

    CvMat mat = cvCreateMat(3, 3, CV_32FC1);
    cvFindHomography(matsrc, matdst, mat); //Here the matrices created are used to find the 3x3 Homography transform Matrix
    // displaying the resulting matrix
    for( int i = 0; i < 3; ++i) {
        for( int j = 0; j < 3; ++j) {
        System.out.print(mat.get(i,j) + ",");
        }
        System.out.println();
    }
    }
}

The result for me is:

1.0,5.011022034363615E-16,-5.249974158069671E-15,
1.6479031282788333E-15,1.0,-5.495862740459911E-15,
4.4222287862990617E-16,1.2693993089577568E-16,1.0,
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文