-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
36 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,26 +1,42 @@ | ||
# Can you do two linear regressions at once to do pixel-to-pixel mappings? | ||
import pandas as pd | ||
from river import linear_model, preprocessing | ||
############ From Ahmed Hafdi | ||
import numpy as np | ||
|
||
xcoord = preprocessing.StandardScaler() | preprocessing.TargetStandardScaler(linear_model.LinearRegression()) | ||
ycoord = preprocessing.StandardScaler() | preprocessing.TargetStandardScaler(linear_model.LinearRegression()) | ||
def find_affine_transformation(src_points, dst_points): | ||
""" | ||
Find the affine transformation matrix and translation vector. | ||
src_points: np.array of shape (n, 2) with source points (x, y). | ||
dst_points: np.array of shape (n, 2) with destination points (x', y'). | ||
Returns: affine_matrix of shape (2, 2) and translation vector of shape (2,). | ||
""" | ||
n = src_points.shape[0] | ||
A = np.zeros((2 * n, 4)) | ||
B = np.zeros((2 * n)) | ||
|
||
for i in range(n): | ||
A[2 * i] = [src_points[i, 0], src_points[i, 1], 1, 0] | ||
A[2 * i + 1] = [src_points[i, 1], -src_points[i, 0], 0, 1] | ||
B[2 * i] = dst_points[i, 0] | ||
B[2 * i + 1] = dst_points[i, 1] | ||
|
||
# Solve for the affine transformation parameters | ||
affine_params, _, _, _ = np.linalg.lstsq(A, B, rcond=None) | ||
print(affine_params) | ||
|
||
source_points = [ | ||
{"x": float(x), "y": float(y)} for y in range(1, 6) for x in range(1, 6) | ||
] | ||
affine_matrix = affine_params[:2].reshape((2, 2)) | ||
translation_vector = affine_params[2:] | ||
|
||
return affine_matrix, translation_vector | ||
|
||
def xfrm(p): | ||
return {"x": 2*p["x"] + 5, "y": 2*p["y"] }#+ p["x"]/10} | ||
source_points = [[x, y] for y in range(1, 6) for x in range(1, 6)] | ||
|
||
def scale(p): | ||
return {"x": p["x"] / 100.0, "y": p["y"] / 100.0} | ||
def xfrm(x, y): | ||
return [2*x + 5, 3*y + x/10] | ||
src_points = np.array(source_points) | ||
dst_points = np.array([xfrm(x, y) for x, y in source_points]) | ||
|
||
for p in source_points: | ||
out = xfrm(p) | ||
print(p, out) | ||
xcoord.learn_one(scale(p), scale(out)["x"]) | ||
ycoord.learn_one(scale(p), scale(out)["y"]) | ||
|
||
p = {"x": 2.5, "y": 2.5} | ||
print(xcoord.predict_one(scale(p)) * 100.0, ycoord.predict_one(scale(p)) * 100.0) | ||
print(p, xfrm(p)) | ||
affine_matrix, translation_vector = find_affine_transformation(src_points, dst_points) | ||
print("Affine Matrix:\n", affine_matrix) | ||
print("Translation Vector:\n", translation_vector) |