Python 知道什么是像素值​;使用Bresenham直线算法,一条直线通过灰度图像

Python 知道什么是像素值​;使用Bresenham直线算法,一条直线通过灰度图像,python,python-3.x,numpy,opencv,matplotlib,Python,Python 3.x,Numpy,Opencv,Matplotlib,如何使用Bresenham直线算法获得灰度图像的像素值,给定一条不同角度的直线,如本代码部分所示 我希望你能帮助我 import cv2 import numpy as np img= cv2.imread("0.L3Luzr_Esq.tif") def bresenham(origin, dest): # debug code print origin print dest # end debug code x0 = origin[0

如何使用Bresenham直线算法获得灰度图像的像素值,给定一条不同角度的直线,如本代码部分所示 我希望你能帮助我

    import cv2
import numpy as np

img= cv2.imread("0.L3Luzr_Esq.tif")

    def bresenham(origin, dest):
    # debug code
    print origin
    print dest
    # end debug code
    x0 = origin[0]; y0 = origin[1]
    x1 = dest[0]; y1 = dest[1]
    steep = abs(y1 - y0) > abs(x1 - x0)
    backward = x0 > x1



    if steep:
        x0, y0 = y0, x0
        x1, y1 = y1, x1
    if backward:
        x0, x1 = x1, x0
        y0, y1 = y1, y0

    dx = x1 - x0
    dy = abs(y1 - y0)
    error = dx / 2
    y = y0

    if y0 < y1: ystep = 1 
    else: ystep = -1
    result = []
    for x in range(x0, x1):
    if steep: result.append((y, x))
    else: result.append((x, y))
    error -= dy
    if error < 0:
        y += ystep
        error += dx

检查谢谢我会检查的
return result
if backward: return result.reverse()

    else: return result
   print result
   return result