discorpy icon indicating copy to clipboard operation
discorpy copied to clipboard

generate_4_source_target_perspective_points function question

Open Young-JZ-Wang opened this issue 4 years ago • 6 comments

Hi there,

I'm working on a camera calibration-related project and I found this library very useful. However, I'm currently stuck on understanding def generate_4_source_target_perspective_points(points, input_order="yx", equal_dist=False, scale="mean"): specifically, this section where you obtained target points from source points. I have trouble figuring out the **y1 = (a0 * b3 + b1) / (1.0 + a0 ** 2)**. I wonder if you could provide me with a reference on this part. Many thanks!

dist_h, dist_v = dist_h * 0.5, dist_v * 0.5 b1 = bh - np.abs(dist_v / np.cos(np.arctan(a0))) b2 = bh + np.abs(dist_v / np.cos(np.arctan(a0))) b3 = bv - np.abs(dist_h / np.cos(np.arctan(a0))) b4 = bv + np.abs(dist_h / np.cos(np.arctan(a0))) y1 = (a0 * b3 + b1) / (1.0 + a0 ** 2) x1 = -a0 * y1 + b3 y2 = (a0 * b4 + b1) / (1.0 + a0 ** 2) x2 = -a0 * y2 + b4 y3 = (a0 * b3 + b2) / (1.0 + a0 ** 2) x3 = -a0 * y3 + b3 y4 = (a0 * b4 + b2) / (1.0 + a0 ** 2) x4 = -a0 * y4 + b4 target_points = np.asarray([[y1, x1], [y2, x2], [y3, x3], [y4, x4]]) return source_points, target_points

Young-JZ-Wang avatar Aug 17 '22 00:08 Young-JZ-Wang

Hi young-oct, There's no reference for that. I worked out the formula by hand and it should be somewhere in one of my notebooks :). I hope the following notes can help:

  • The idea of the function is to find 4 points staying at 4 corner of a rectangle or square given 4 perspective points.
  • From 4 perspective points we calculate slopes and intercepts of 4 lines going through each two points.
  • For horizontal line, I used this formula y = ax +b. For vertical line, using this: x = ay +b. This is to avoid numerical error of poly_fit.
  • Using these 4 calculated values of slopes and intercepts we can estimate the slopes and intercepts of two horizontal lines and two vertical lines of the rectangle/square. From these estimated slopes and intercepts, we find the cross points between. These points are the target points.

nghia-vo avatar Aug 17 '22 01:08 nghia-vo

@nghia-vo Thank you so much for the quick reply. I get the general idea on what to do but this specific step is where I was most confused y = (a0 * b3 + b1) / (1.0 + a0 ** 2). If you don't mind sharing your notes at [email protected], it would be much appreciated.

Young-JZ-Wang avatar Aug 17 '22 02:08 Young-JZ-Wang

@nghia-vo I'm trying to correct this image following your example, but for some reason, I can't seem to have a square checkerboard as the final out. I also find when I set the num_coef = 3 to 5. The final image looks very bad. I wonder if you could provide a few pointers on this. Any help is greatly appreciated.

num_coef = 3 myplot

num_coef = 5 myplot1

Young-JZ-Wang avatar Aug 17 '22 02:08 Young-JZ-Wang

That is the formula to calculate the cross point between line number 1 (horizontal): y = a0 * x + b1 and line number 3 (vertical): x = - a0 * y + b3 Solving these two equations to get y and x: y = a0 (-a0 * y + b3) + b1 <=> y (1+a0 ** 2) = a0 * b3 + b1 <=> y = (a0 *b3 + b1)/(1+a0 ** 2)

Will contact you by e-mail to get the original image of the second question.

nghia-vo avatar Aug 17 '22 14:08 nghia-vo

@young-oct could you explain the distortion you applied to your (Input) image? thanks

rcatwood avatar Aug 17 '22 15:08 rcatwood

@rcatwood Thanks for getting back to me. I emailed your colleague @nghia-vo. I'm working with optical coherence tomography images.

Young-JZ-Wang avatar Aug 17 '22 20:08 Young-JZ-Wang