I am using Python v2.7 for this work. As an input i have a relatively white image with a clear black line on it. The line is always linear, no polynomial of second or above order. The line can be anyway on the image
I am trying to define the equation of this line in the form of y = ax +b
Currently my approach would be to find which pixel belongs to the line then do a linear regression to get the equation. But i am trying to find out which function in python i need to use to achieve this and this is where I would need some help
Or maybe you have an even simpler way of doing it.
adding one image as example
Okay so i found the way i wanted to do quite simply in the end
def estimate_coef(x, y):
# number of observations/points
n = np.size(x)
# mean of x and y vector
m_x, m_y = np.mean(x), np.mean(y)
# calculating cross-deviation and deviation about x
SS_xy = np.sum(y*x) - n*m_y*m_x
SS_xx = np.sum(x*x) - n*m_x*m_x
# calculating regression coefficients
a = SS_xy / SS_xx
b = m_y - a*m_x
return(a, b)
# MAIN CODE
# 1. Read image
# 2. find where the pixel belonging to the line are located
# 3. perform linear regression to get coeff
image = [] # contain the image read
# for all images to analyze
for x in range(len(dut.images)):
print "\n\nimage ",x, dut.images[x]
# read image (convert to greyscale)
image = imread(dut.images[x], mode="L")
height = image.shape[0] - 1
threshold = (np.min(image) + np.max(image)) / 2
line = np.where(image < threshold) #get coordinate of the pixel belonging to the line
x = line[1] # store the x position
y = height - line[0] # store the y position. Need to invert because of image origine being on top left corner instead of bottom left
#position = np.array([x,y])
a, b = estimate_coef(x, y)
print("Estimated coefficients:\n \
a = %.6f \n \
b = %.6f" % (a, b))