I have a grayscale image consisting of MxN 1 byte pixels. The image shows K vertical lines. Each vertical line has the same thickness.

Due to a systematic error each line is imaged in a different horizontal position than the actual position. The horizontal shift differs between lines.

Consider a scenario where there are K=2 lines. This figure shows the position of the imaged lines as red and the correct position as blue:

enter image description here

We see that the left line is shifted 1 to the right of its actual position and the right line is shifted 2 to the right of its actual position.

In this case I may use $$RMSE=\sqrt{1^2+2^2}$$ as the error of the imaging.

I am looking for an algorithm that automatically calculates such an error/penalty function. Using this algorithm I will then tune two parameters (using gradient descent or similar) until the error is minimized.

Consider a part of a row in such an image. This figure shows the pixel values and one vertical line as a red rectangle:

enter image description here

One first step may be to find the center of the vertical line as a fractional horizontal pixel coordinate.

How could I do this?

Having found this I could for each row find a set of K fractional horizontal coordinates representing the K imaged lines: $$\{x_{imaged_i}\}$$.

I could then perhaps for each imaged line find the distance to the closest actual line: $$d_i=|x_{imaged_i}-x_{actual_i}|$$ and sum up the square of these distances and take the square root?

$$RMSE=\sqrt{\Sigma d_i^2}$$


1 Answer 1


The location could be found using a simple weighting of the pixel values:

Pixel location 0 1 2 3 4 5
Pixel value 0 50 255 200 0 0
Product 0 1 $\times$ 50 = 50 2 $\times$ 255 = 510 3 $\times$ 200 = 600 4 $\times$ 0 = 0 5 $\times$ 0 = 0

And then sum the products up and divide by the summed pixel values.

$$ \bar{x} = \frac{0 + 50 + 510 + 600 + 0 + 0}{0 + 50 + 255 + 200 + 0 + 0} = \frac{1160}{505} = 2.297 $$

The trick will be to decide when one line stops and the other starts.

  • $\begingroup$ Is this a weighted version of the centroid formulae in 1 dimension? $\endgroup$
    – Andy
    Sep 5, 2023 at 20:03
  • 1
    $\begingroup$ @Andy Yes, it's just finding the weighted centroid. $\endgroup$
    – Peter K.
    Sep 5, 2023 at 20:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.