使用 Emgu (OpenCV) 进行变换 - 仿射/透视?

发布于 2024-10-16 10:02:47 字数 1302 浏览 3 评论 0原文

我目前正在尝试通过使用 EMGU 来实现转换,尽管我似乎无法理解它是如何工作的(并且网上似乎没有任何示例)。

我已经得到了我的图像,其中包含我希望转换的 4 个点,虽然我不知道还需要什么其他变量,但它要求 'mapMat' ?

这是我到目前为止所得到的:

        float[,] tmp = {
                            {bottomLeft.x, bottomLeft.y},
                            {topLeft.x, topLeft.y},
                            {topRight.x, topRight.y},
                            {bottomRight.x, bottomRight.y}
                       };
        Matrix<float> sourceMat = new Matrix<float>(tmp);
        float[,] target = {
                            {0, height},
                            {0, 0},
                            {width, 0},
                            {width, height}
                       };
        Matrix<float> targetMat = new Matrix<float>(target);
        //mapMat = just a placeholder matrix?
        Matrix<float> mapMat = new Matrix<float>(target);
        CvInvoke.cvGetAffineTransform(sourceMat.Ptr, targetMat.Ptr, mapMat.Ptr);

但这不起作用。我也不确定仿射变换是否是最理想的解决方案?我也读过一些有关 FindHomography 和透视变换的内容,但不确定它们是否适用于这里。

我希望实现的目标转换是这样的:

http://img832.imageshack.us/ img832/5157/targettransform.png

任何帮助将不胜感激,

谢谢

I'm currently trying to implement transformations through using EMGU, though I can't seem to get my head round how it works (and there doesn't seem to be any examples online).

I've got my image, with the 4 points I wish to transform from (and to), though I don't know what other variables are required, it asks for 'mapMat' ?

Here is what I have so far:

        float[,] tmp = {
                            {bottomLeft.x, bottomLeft.y},
                            {topLeft.x, topLeft.y},
                            {topRight.x, topRight.y},
                            {bottomRight.x, bottomRight.y}
                       };
        Matrix<float> sourceMat = new Matrix<float>(tmp);
        float[,] target = {
                            {0, height},
                            {0, 0},
                            {width, 0},
                            {width, height}
                       };
        Matrix<float> targetMat = new Matrix<float>(target);
        //mapMat = just a placeholder matrix?
        Matrix<float> mapMat = new Matrix<float>(target);
        CvInvoke.cvGetAffineTransform(sourceMat.Ptr, targetMat.Ptr, mapMat.Ptr);

This however doesn't work. I was also unsure whether or not an affine transformation was the most ideal solution? I read something about FindHomography too and also perspective transformations, but not sure if they would apply here.

The target transformation i wish to achieve is like this:

http://img832.imageshack.us/img832/5157/targettransform.png

Any help would be greatly appreciated,

Thanks

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

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

发布评论

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

评论(1

誰ツ都不明白 2024-10-23 10:02:47

首先有一点介绍:

  • 如果你想要一个仿射变换(从你的图片看来),你至少需要 3 个点对,因为你有 6 个参数要估计
  • 如果你想要一个更通用的变换,即单应性,你至少需要 4 个点对,因为您有 8 个参数需要估计

所以假设您有 4 个源角和目标角,并且您想要估计透视变换,此代码应该执行您想要的操作:

PointF[] pts1 = new PointF[4];
PointF[] pts2 = new PointF[4];
HomographyMatrix homography;
for (int i = 0; i < 4; i++)
{
  pts1[i] = new PointF (sourceCorner[i].X, sourceCorner[i].Y);
  pts2[i] = new PointF (destCorner[i].X, destCorner[i].Y);
}
homography = CameraCalibration.GetPerspectiveTransform(pts1, pts2);

查看 CameraCalibration 了解其他有用的方法

First a little intro:

  • If you want an affine transformation (as it seems from your picture) you need at least 3 point pairs because you have 6 parameters to estimate
  • If you want a more generic transformation, i.e. an homography you need at least 4 point pairs because you have 8 parameters to estimate

So suppose you have your 4 source and destination corners, and you want to estimate a Perspective Transformation this code should do what you want:

PointF[] pts1 = new PointF[4];
PointF[] pts2 = new PointF[4];
HomographyMatrix homography;
for (int i = 0; i < 4; i++)
{
  pts1[i] = new PointF (sourceCorner[i].X, sourceCorner[i].Y);
  pts2[i] = new PointF (destCorner[i].X, destCorner[i].Y);
}
homography = CameraCalibration.GetPerspectiveTransform(pts1, pts2);

Take a look at CameraCalibration for other useful methods

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