If I have a Mat
image object (OpenCV) whose size is 960*720, on which I have calculated the coordinates of a Point
object, and then I scale this Mat image, and its new size is 640*480, how can I find the new coordinates of the Point
?
Asked
Active
Viewed 2,497 times
5

Sneftel
- 40,271
- 12
- 71
- 104

user140888
- 609
- 1
- 11
- 31
2 Answers
1
A point (x,y)
in the original matrix will be mapped to (x',y')
in the new matrix by
(x',y') = 2*(x,y)/3.
Reducing that to an OpenCV function we have:
cv::Point scale_point(cv::Point p) // p is location in 960*720 Mat
{
return 2 * p / 3; // return location in 640*480 Mat
}

Bull
- 11,771
- 9
- 42
- 53
-
Although this answers the question conceptually, it does not address the problem in an OpenCV context. – Dale Jun 28 '17 at 14:45
0
What I ended-up doing was to create a ScaledPoint
object that extended Point
. That way, it was less disruptive to the code I'd already been using with just the plain Point
object.
public class ScaledPoint extends Point {
public ScaledPoint (double[] points, double scale) {
super(points[0] * scale, points[1] * scale);
}
}
Then, I calculated a scaling factor and used it in the class I extended:
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);

Dale
- 5,520
- 4
- 43
- 79