您如何重新分配观点? [英] How do you redistort a point?

查看:103
本文介绍了您如何重新分配观点?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用 Cv2.Undistort()的图像上变形了,这使图像变直了.之后,我在图像中找到了一些要点.我需要获取变形图像中这些点的位置.

我尝试了 Cv2.ProjectPoints(),但未能获取适当的坐标.它们超出了图像的范围.

这就是我要做的事情:

  List< float>arr = new List< float>();foreach(以点为单位的var i){arr.Add(i.X);arr.Add(i.Y);arr.Add(0);}Mat pointArr =新的Mat(3,points.Count,MatType.CV_32FC1,arr.ToArray());float [] rotArr = {1,0,0,0、1、0,0,0,1};float [] transArr = {0,0,0};Mat rot =新的Mat(3,1,MatType.CV_32F,rotArr);Mat trans =新的Mat(3,1,MatType.CV_32F,transArr);Cv2.ProjectPoints(pointArr,rot,trans,camMatrix,dist,outputPoints);列表< Point2f>point2Fs = new List< Point2f>();var lngt = outputPoints.Rows * outputPoints.Cols * outputPoints.Channels();var matarr = new List< float>();为(int i = 0; i< outputPoints.Rows; i ++){point2Fs.Add(新Point2f(outputPoints.ExtractChannel(0).At< float>(0,i),outputPoints.ExtractChannel(1).At< float>(0,i))));} 

points-我要在原始图像中找到的未失真图像中的点

有什么建议吗?

谢谢!

解决方案

我最终使用了

这是源代码:

  Point2f DistortPoint(Point2f点){var tempPt = new Point2f((point.X-intr.cx)/intr.fx,(point.Y-intr.cy)/intr.fy);var r2 = Math.Pow(tempPt.X,2)+ Math.Pow(tempPt.Y,2);var xtmp = tempPt.X *(((1 + intr.k1 * r2 + intr.k2 * Math.Pow(r2,2)+ intr.k3 * Math.Pow(r2,3)))/(1 + intr.k4 * r2 + intr.k5 * Math.Pow(r2,2)+ intr.k6 * Math.Pow(r2,3)))+ 2 * intr.p1 * tempPt.X * tempPt.Y+ intr.p2 *(r2 + 2 * Math.Pow(tempPt.X,2));var ytmp = tempPt.Y *(((1 + intr.k1 * r2 + intr.k2 * Math.Pow(r2,2)+ intr.k3 * Math.Pow(r2,3))/(1 + intr.k4 * r2 + intr.k5 * Math.Pow(r2,2)+ intr.k6 * Math.Pow(r2,3)))+ 2 * intr.p2 * tempPt.X * tempPt.Y+ intr.p1 *(r2 + 2 * Math.Pow(tempPt.Y,2));返回新的Point2f((float)(intr.fx * xtmp + intr.cx),(float)(intr.fy * ytmp + intr.cy));} 

I have a distorted image on which I use Cv2.Undistort() which straightens it out. After this I find some points in the image. I need to get the location of those points in the distorted image.

I have tried Cv2.ProjectPoints() but failed to get appropriate coordinates. They were outside the bounds of the image.

This is how I went about doing this:

List<float> arr = new List<float>();
foreach (var i in points)
{
    arr.Add(i.X);
    arr.Add(i.Y);
    arr.Add(0);
}
Mat pointArr = new Mat(3, points.Count, MatType.CV_32FC1, arr.ToArray());
float[] rotArr = {  1, 0, 0,
                    0, 1, 0,
                    0, 0, 1};

float[] transArr = { 0, 0, 0 };

Mat rot = new Mat(3, 1, MatType.CV_32F, rotArr);
Mat trans = new Mat(3, 1, MatType.CV_32F, transArr);


Cv2.ProjectPoints(pointArr, rot, trans, camMatrix, dist, outputPoints);
List<Point2f> point2Fs = new List<Point2f>();
var lngt = outputPoints.Rows * outputPoints.Cols * outputPoints.Channels();
var matarr = new List<float>();
for (int i = 0; i < outputPoints.Rows; i++)
{
    point2Fs.Add(new Point2f(outputPoints.ExtractChannel(0).At<float>(0, i), outputPoints.ExtractChannel(1).At<float>(0, i)));
}

points - the points in the undistorted image I want to find in the original

Any suggestions?

Thanks!

解决方案

I ended up using 2nd option proposed by Milo. I have rewritten the project point method without the rotation and translation part and starting the equation path from x' and y', where:

x' = (x_u - c_x) / f_x
y' = (y_u - c_y) / f_y

Then I applied the rest of the provided equations.

Here is the source code:

Point2f DistortPoint(Point2f point)
{
   var tempPt = new Point2f((point.X-intr.cx)/intr.fx, (point.Y-intr.cy)/intr.fy);
   var r2 = Math.Pow(tempPt.X, 2) + Math.Pow(tempPt.Y, 2);
   var xtmp = tempPt.X * ((1 + intr.k1 * r2 + intr.k2 * Math.Pow(r2, 2) + intr.k3 * Math.Pow(r2, 3)) /
                (1 + intr.k4 * r2 + intr.k5 * Math.Pow(r2, 2) + intr.k6 * Math.Pow(r2, 3))) + 2 * intr.p1 * tempPt.X * tempPt.Y + intr.p2 * (r2 + 2 * Math.Pow(tempPt.X, 2));
   var ytmp = tempPt.Y * ((1 + intr.k1 * r2 + intr.k2 * Math.Pow(r2, 2) + intr.k3 * Math.Pow(r2, 3)) /
                (1 + intr.k4 * r2 + intr.k5 * Math.Pow(r2, 2) + intr.k6 * Math.Pow(r2, 3))) + 2 * intr.p2 * tempPt.X * tempPt.Y + intr.p1 * (r2 + 2 * Math.Pow(tempPt.Y, 2));
   return new Point2f((float)(intr.fx * xtmp + intr.cx), (float)(intr.fy * ytmp + intr.cy));
}

这篇关于您如何重新分配观点?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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