Generating pixel values of line connecting 2 points

风流意气都作罢 提交于 2019-12-21 22:28:10

问题


I am having some troubles trying to connect separate points in my picture (see below).

Right now, the code I have is as follows:

first_time = resulting_coords[0:40]
for i in range(len(first_time)):
    if(i < (len(first_time))-1):
        cv2.line(copy_test_image[0], (resulting_coords[i,0],resulting_coords[i,1]), 
             (resulting_coords[i+1,0],resulting_coords[i+1,1]), (0,0,225), 2)
    elif(i>=(len(first_time))-1):
#         print(i)
        cv2.line(copy_test_image[0], (resulting_coords[i,0],resulting_coords[i,1]), 
             (resulting_coords[0,0],resulting_coords[0,1]), (0,0,225), 2)

Which actually gives the result I want:

Results in

But I cannot extract the coordinates of the pixels of these lines.

I tried using a rather complicated function which uses the P1(x1,y1) and P2(x2,y2) points and connects them with a line with pixel coords:

def pixels_in_shape(x1, y1, x2, y2, img):
    P1=[x1,y1]
    P2=[x2,y2]
    #define local variables for readability
    imageH = img.shape[0]
    imageW = img.shape[1]
    P1X = int(P1[0])
    P1Y = int(P1[1])
    P2X = int(P2[0])
    P2Y = int(P2[1])
    #difference and absolute difference between points
    #used to calculate slope and relative location between points
    dX = P2X - P1X
    dY = P2Y - P1Y
    dXa = np.abs(dX)
    dYa = np.abs(dY)
    #predefine numpy array for output based on distance between points
    itbuffer = np.zeros(shape=(int(np.maximum(dYa,dXa)),3),dtype=np.float32)
    itbuffer.fill(np.nan)
    #Obtain coordinates along the line using a form of Bresenham's algorithm
    negY = P1Y > P2Y
    negX = P1X > P2X
    if P1X == P2X: #vertical line segment
        itbuffer[:,0] = P1X
        if negY:
            itbuffer[:,1] = np.arange(P1Y,P1Y - dYa,-1)
        else:
            itbuffer[:,1] = np.arange(P1Y,P1Y + dYa)              
    elif P1Y == P2Y: #horizontal line segment
        itbuffer[:,1] = P1Y
        if negX:
            itbuffer[:,0] = np.arange(P1X,P1X-dXa,-1)
        else:
            itbuffer[:,0] = np.arange(P1X,P1X+dXa)
    else: #diagonal line segment
        steepSlope = dYa > dXa
        if steepSlope:
            slope=dX/dY
            if negY:
                itbuffer[:,1] = np.arange(P1Y,P1Y-dYa,-1)
            else:
                itbuffer[:,1] = np.arange(P1Y,P1Y+dYa)
            itbuffer[:,0] = (slope*(itbuffer[:,1]-P1Y)).astype(np.int) + P1X
        else:
            slope=dY/dX
            if negX:
                itbuffer[:,0] = np.arange(P1X,P1X-dXa,-1)
            else:
                itbuffer[:,0] = np.arange(P1X,P1X+dXa)
            itbuffer[:,1] = (slope*(itbuffer[:,0]-P1X)).astype(np.int) + P1Y
    #Remove points outside of image
    colX = itbuffer[:,0]
    colY = itbuffer[:,1]
    itbuffer = itbuffer[(colX >= 0) & (colY >=0) & (colX<imageW) & (colY<imageH)]
    #Get intensities from img ndarray
    itbuffer[:,2] = img[itbuffer[:,1].astype(np.uint),itbuffer[:,0].astype(np.uint)]

    return itbuffer

However, this results in following output (which is obviously wrong):

Could you give me any idea on how to solve this? I need to connect these dots, since I am trying to extract the region inside the border indicated by the dots.

**

EDIT: SOLVED. Used function from

** (Modifying Bresenham's line algorithm)

def bresenham_line(x0, y0, x1, y1):
    steep = abs(y1 - y0) > abs(x1 - x0)
    if steep:
        x0, y0 = y0, x0  
        x1, y1 = y1, x1

    switched = False
    if x0 > x1:
        switched = True
        x0, x1 = x1, x0
        y0, y1 = y1, y0

    if y0 < y1: 
        ystep = 1
    else:
        ystep = -1

    deltax = x1 - x0
    deltay = abs(y1 - y0)
    error = -deltax / 2
    y = y0

    line = []    
    for x in range(x0, x1 + 1):
        if steep:
            line.append((y,x))
        else:
            line.append((x,y))

        error = error + deltay
        if error > 0:
            y = y + ystep
            error = error - deltax
    if switched:
        line.reverse()
    return line

来源:https://stackoverflow.com/questions/50995499/generating-pixel-values-of-line-connecting-2-points

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!