I have a rectangle within a 2d space and a set of points within the rectangle. The rectangle is moving in the following manner: the center moves a value of u
on the x-axis, v
on the y-axis and everything is scaled in a factor of sx
and sy
respectively. I only get the location of the points after the motion. My goal is to estimate the (u, v, sx, sy)
vector.
I have the following data at runtime:
px – ndarray with the x values of the points before the movement
py – ndarray with the y values of the points before the movement
cx – ndarray with the x values of the points after the movement
cy – ndarray with the y values of the points after the movement
x0 – the x value of the location of the center of the rectangle in the previous frame.
y0 – the y value of the location of the center of the rectangle in the previous frame.
The equation to calculate the position of the point in the current frame given the (u, v, sx, sy)
vector is given by:
x_currentFrameCalaulated = sx * px + u + x0 * (1 – sx)
y_currentFrameCalaulated = sy * py + v + y0 * (1 – sy)
Note that the axes are non-dependent. I have defined the following function to be minimized:
def estimateCurr(vec, previous, current, center):
return np.abs(current - (vec[1]* previous + vec[0]+ center * (1 - vec[1])))
Here, vec[0] represents the motion concerning the axis, and vec[1] represents the scale.
I am setting the bound according to my problem in the following manner:
h = 270
w = 480
boundsX = Bounds([-w, -1], [w, w - 1])
boundsY = Bounds([-h, -1], [h, h - 1])
Initializing a guess to [0, 0]
:
init = np.zeros((2 , ))
I am resuming to try and find the optimal solution by:
res = minimize(estimateCurr, x0 = init, args=(px, cx, centerX), method='trust-constr', bounds=boundsX, options={'verbose': 1})
print(res)
res = minimize(estimateCurr, x0 = init, args=(py, cy, centerY), method='trust-constr', bounds=boundsY, options={'verbose': 1})
print(res)
I am getting:
ValueError: all the input arrays must have same number of dimensions,
but the array at index 0 has 2 dimension(s) and the array at index 1
has 1 dimension(s)
This is obvious since I have 17 points, and the sizes of printing:
print(px.shape)
print(cx.shape)
print(centerX.shape)
print(init.shape)
(17,) (17,) (17,) (2,)
However, I am not sure how to set the correct sizes for the problem, or even if I am using the correct solver. I tried multiplying the (u, v, sx, sy)
to fit the size of the data to no evil. How do I approach this problem?