【问题标题】:Python - Finding the linear equation of a line in a imagePython - 查找图像中直线的线性方程
【发布时间】:2018-12-27 06:10:36
【问题描述】:

我正在使用 Python v2.7 来完成这项工作。 作为输入,我有一个相对白色的图像,上面有一条清晰的黑线。该线始终是线性的,没有二阶或更高阶的多项式。这条线无论如何都可以在图像上

我试图以 y = ax +b 的形式定义这条线的方程

目前我的方法是找出哪个像素属于该线,然后进行线性回归以获得方程。但我正在尝试找出我需要使用 python 中的哪个函数来实现这一点,这就是我需要帮助的地方

或者也许你有更简单的方法。

添加一张图片作为示例

【问题讨论】:

  • 考虑信号处理社区 - dsp.stackexchange.com
  • 你的出身在哪里?左下角?
  • @ErnestSKirubakaran 当我转换为数组时,图像上的 0,0 位于左上角。但我希望我的原点位于左下角。这就是为什么我反转了我的 y 变量

标签: python image-processing equation linear-equation


【解决方案1】:

好的,所以我最终找到了我想做的非常简单的方式

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)) 

【讨论】:

    猜你喜欢
    • 2016-04-27
    • 2014-09-21
    • 2011-02-21
    • 2012-10-01
    • 1970-01-01
    • 1970-01-01
    • 2017-08-18
    • 1970-01-01
    • 2015-03-10
    相关资源
    最近更新 更多