Python Forum
A coding beginner needs help
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
A coding beginner needs help
#1
Could someone please help me understand what the problem is with the following code? Also, how to fix the problems please?

Thank you very very very much! Heart

----------------------------------------------------------------------------------------------------------------------------------------------------------
import numpy as np

# Replace these with your actual point coordinates
# Format: [x*, y*, x, y] for each point
points = [
    [x1*, y1*, x1, y1],  # Point 1
    [x2*, y2*, x2, y2],  # Point 2  
    [x3*, y3*, x3, y3],  # Point 3
    [x4*, y4*, x4, y4]   # Point 4
]

def compute_projective_parameters(points):
    """Compute 8-parameter projective transformation"""
    
    # Build matrices
    A = []
    L = []
    
    for x_star, y_star, x, y in points:
        # First equation: x = a1x* + a2y* + a0 - c1xx* - c2xy*
        A.append([x_star, y_star, 1, 0, 0, 0, -x*x_star, -x*y_star])
        L.append(x)
        
        # Second equation: y = b1x* + b2y* + b0 - c1yx* - c2yy*
        A.append([0, 0, 0, x_star, y_star, 1, -y*x_star, -y*y_star])
        L.append(y)
    
    A = np.array(A)
    L = np.array(L)
    
    # Solve using least squares
    parameters = np.linalg.lstsq(A, L, rcond=None)[0]
    
    return parameters

# Compute parameters
params = compute_projective_parameters(points)

# Print results
param_names = ['a1', 'a2', 'a0', 'b1', 'b2', 'b0', 'c1', 'c2']
print("2D Projective Transformation Parameters:")
for name, value in zip(param_names, params):
    print(f"{name} = {value:.6f}")

# Verify transformation
print("\nVerification (original vs transformed):")
for i, (x_star, y_star, x_true, y_true) in enumerate(points):
    denominator = params[6]*x_star + params[7]*y_star + 1
    x_trans = (params[0]*x_star + params[1]*y_star + params[2]) / denominator
    y_trans = (params[3]*x_star + params[4]*y_star + params[5]) / denominator
    
    print(f"Point {i+1}:")
    print(f"  Original: ({x_true:.3f}, {y_true:.3f})")
    print(f"  Transformed: ({x_trans:.3f}, {y_trans:.3f})")
    print(f"  Error: ({abs(x_true-x_trans):.6f}, {abs(y_true-y_trans):.6f})") 
----------------------------------------------------------------------------------------------------------------------------------------------------------
buran write Sep-22-2025, 04:21 AM:
Please post all code, output and errors (it it's entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button.
Reply
#2
The difficulty is in making effort to understand how to use the code, isn't it?
# Replace these with your actual point coordinates
# Format: [x*, y*, x, y] for each point
Source: here at 16:47

points = [
    [565, 244, 786, 144],  # Point 1
    [1355, 244, 1190, 302],  # Point 2  
    [1355, 685, 1190, 761],  # Point 3
    [565, 685, 786, 908]   # Point 4
]
Output:
2D Projective Transformation Parameters: a1 = 2.881919 a2 = 0.000000 a0 = -130.470768 b1 = 0.865186 b2 = 3.301338 b0 = -1019.948209 c1 = 0.001603 c2 = 0.000000 Verification (original vs transformed): Point 1: Original: (786.000, 144.000) Transformed: (786.000, 144.000) Error: (0.000000, 0.000000) Point 2: Original: (1190.000, 302.000) Transformed: (1190.000, 302.000) Error: (0.000000, 0.000000) Point 3: Original: (1190.000, 761.000) Transformed: (1190.000, 761.000) Error: (0.000000, 0.000000) Point 4: Original: (786.000, 908.000) Transformed: (786.000, 908.000) Error: (0.000000, 0.000000)
Littlefish likes this post
Reply
#3
(Sep-22-2025, 08:55 AM)paul18fr Wrote: The difficulty is in making effort to understand how to use the code, isn't it?
# Replace these with your actual point coordinates
# Format: [x*, y*, x, y] for each point
Source: here at 16:47

points = [
    [565, 244, 786, 144],  # Point 1
    [1355, 244, 1190, 302],  # Point 2  
    [1355, 685, 1190, 761],  # Point 3
    [565, 685, 786, 908]   # Point 4
]
Output:
2D Projective Transformation Parameters: a1 = 2.881919 a2 = 0.000000 a0 = -130.470768 b1 = 0.865186 b2 = 3.301338 b0 = -1019.948209 c1 = 0.001603 c2 = 0.000000 Verification (original vs transformed): Point 1: Original: (786.000, 144.000) Transformed: (786.000, 144.000) Error: (0.000000, 0.000000) Point 2: Original: (1190.000, 302.000) Transformed: (1190.000, 302.000) Error: (0.000000, 0.000000) Point 3: Original: (1190.000, 761.000) Transformed: (1190.000, 761.000) Error: (0.000000, 0.000000) Point 4: Original: (786.000, 908.000) Transformed: (786.000, 908.000) Error: (0.000000, 0.000000)

-------------------------------------------------------------------------------------------------------------
Great information, it helped me resolved the problem, thank you very much!!!

Please see the revised code below:


-------------------------------------------------------------------------------------------------------------

import numpy as np

# Measured coordinates (x*, y*)
x_star = [-111739, 108368, -113698, 106450]
y_star = [113676.2, 111712.5, -106448, -108411]

# Calibrated coordinates (x, y)
x_cal = [-110000, 110000, -110000, 110000]
y_cal = [110000, 110000, -110000, -110000]

# Construct A matrix and b vector
A = []
b = []

for i in range(4):
# Equation for x
row_x = [x_star[i], y_star[i], 1, 0, 0, 0, -x_cal[i]*x_star[i], -x_cal[i]*y_star[i]]
A.append(row_x)
b.append(x_cal[i])

# Equation for y
row_y = [0, 0, 0, x_star[i], y_star[i], 1, -y_cal[i]*x_star[i], -y_cal[i]*y_star[i]]
A.append(row_y)
b.append(y_cal[i])

A = np.array(A)
b = np.array(b)

# Solve the linear system
p = np.linalg.solve(A, b)

# Extract parameters
a1, a2, a0, b1, b2, b0, c1, c2 = p

print("2D Projective Transformation Parameters:")
print(f"a1 = {a1:.10f}")
print(f"a2 = {a2:.10f}")
print(f"a0 = {a0:.10f}")
print(f"b1 = {b1:.10f}")
print(f"b2 = {b2:.10f}")
print(f"b0 = {b0:.10f}")
print(f"c1 = {c1:.10f}")
print(f"c2 = {c2:.10f}")

print("\nVerification:")
for i in range(4):
denom = c1 * x_star[i] + c2 * y_star[i] + 1
x_transformed = (a1 * x_star[i] + a2 * y_star[i] + a0) / denom
y_transformed = (b1 * x_star[i] + b2 * y_star[i] + b0) / denom
print(f"Point {i+1}: calibrated ({x_cal[i]}, {y_cal[i]}), transformed ({x_transformed:.2f}, {y_transformed:.2f})")
-------------------------------------------------------------------------------------------------------------
buran write Sep-23-2025, 04:47 AM:
Please post all code, output and errors (it it's entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Tic-Tac game (Beginner's coding) Shahmadhur13 6 6,290 Dec-21-2024, 08:22 AM
Last Post: FreyaFuentes
  Beginner Coding Help calpro 3 2,747 Oct-30-2021, 02:01 PM
Last Post: Yoriz

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020