'Python distance between two (not linear) lines

this is my first question here sorry if I miss some things.

So I have a Image with two lines, the right line is supposed to be the Zero line and I want to get the distance to the left line (very precise).

Picture

But somehow I´m not able to find a way to get this information. I want to get the distance between those two lines as an Height Information. The right line is the ground line and the left one is the one to measure the distance from.

Edit: So the Image is the result of an OpenCV addWeighted operation. Due to the Green color I tried to run it with the Numpy where() function to get at least the green lines but that was a complete fail.

    while x<966:
        y=0
        while y<1296:
            print(numpy.where(img_contours == 0., 255., 0.))
            y=y+1
        x=x+1 

Let just say the output was very useless and I simply have no plan what to do.

My Plan is that I create a base picture with a Laser-Line and take another Picture when there is something under the laser, with the shift of the laserline I want to calculate the height of the object.



Solution 1:[1]

Slowest possible way:

import numpy as np 
from imageio import imread
 
img = imread("gfCgG.png") 
h, w, d = img.shape
green_col = 1  # index into the RGBI tuple
line = {}

for i in range(h):
    cols = []
    for j in range(w):
        if img[i][j][green_col] != 0:
            cols.append(j)
        line[i] = cols line

Output will look like:

print(line)
{0: [],
 1: [],
 2: [],
 3: [],
 4: [],
 5: [],
 6: [],
 7: [],
 8: [],
 9: [],
 10: [211, 212, 213, 242, 243, 244],
 11: [211, 212, 213, 242, 243, 244, 245],
 12: [211, 212, 213, 242, 243, 244, 245],
 13: [211, 212, 213, 242, 243, 244, 245],
 14: [211, 212, 213, 242, 243, 244, 245],
 15: [211, 212, 213, 242, 243, 244, 245],
 16: [211, 212, 213, 242, 243, 244, 245],
 17: [211, 212, 213, 242, 243, 244, 245],
....

The first 9 lines show that there is no green, line 10 for example show that the lines are three pixels wide (from 211 to 213), a gap, and then from 242 to 244.

Its up to you to decide which is the correct center of the line.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Leonardo