【发布时间】:2013-06-26 11:15:15
【问题描述】:
如果我有一个大小为 960*720 的 Mat 图像对象 (OpenCV),我在其上计算了一个 Point 对象的坐标,然后我缩放这个 Mat 图像,它的新大小是 640 *480,如何找到Point的新坐标?
【问题讨论】:
如果我有一个大小为 960*720 的 Mat 图像对象 (OpenCV),我在其上计算了一个 Point 对象的坐标,然后我缩放这个 Mat 图像,它的新大小是 640 *480,如何找到Point的新坐标?
【问题讨论】:
原矩阵中的一个点(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
}
【讨论】:
我最终做的是创建一个扩展Point 的ScaledPoint 对象。这样一来,它对我已经使用纯 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);
【讨论】: