【问题标题】:Calculation of coordinates of point on a scaled Mat image in OpenCv在 OpenCv 中计算缩放 Mat 图像上的点坐标
【发布时间】:2013-06-26 11:15:15
【问题描述】:

如果我有一个大小为 960*720 的 Mat 图像对象 (OpenCV),我在其上计算了一个 Point 对象的坐标,然后我缩放这个 Mat 图像,它的新大小是 640 *480,如何找到Point的新坐标?

【问题讨论】:

    标签: opencv scaling mat


    【解决方案1】:

    原矩阵中的一个点(x,y)会被映射到新矩阵中的(x',y')

    (x',y') = 2*(x,y)/3.
    

    将其简化为我们拥有的 OpenCV 函数:

    cv::Point scale_point(cv::Point p) // p is location in 960*720 Mat
    {
        return 2 * p / 3;   // return location in 640*480 Mat
    }
    

    【讨论】:

    • 虽然这从概念上回答了这个问题,但它并没有解决 OpenCV 上下文中的问题。
    【解决方案2】:

    我最终做的是创建一个扩展PointScaledPoint 对象。这样一来,它对我已经使用纯 Point 对象的代码的破坏性较小。

    public class ScaledPoint extends Point {
        public ScaledPoint (double[] points, double scale) {
            super(points[0] * scale, points[1] * scale);
        }
    }
    

    然后,我计算了一个比例因子并在我扩展的类中使用它:

    Mat originalObject; 
    // TODO: populate the original object
    Mat scaledObject; 
    // TODO: populate the scaled object
    double scaleFactor = scaledObject.getHeight()/(double)originalObject.getHeight();
    matOfSomePoints = new Mat(4,1, CvType.CV_23FC2);
    // TODO: populate the above matrix with your points
    Point aPointForTheUnscaledImage = new Point(matOfSomePoints.get(0,0));
    Point aPointForTheScaledImage = new ScaledPoint(matOfSomePoints.get(0,0), scaleFactor);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-11-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-15
      • 1970-01-01
      相关资源
      最近更新 更多