簡體   English   中英

在兩端位於同一 x 坐標上的線之間畫一條線

[英]Draw a line between lines which ends lie on the same x coordinate

我有帶有線條的 png 文件作為 grascale png 上的掩碼。 我只需要連接兩端位於同一坐標上的線。 所以連接線是垂直的。 我也想在這里有一些公差,比方說 +/- 5 像素。

考慮下面的簡單示例。 我只想將較細的線與一條垂直的附加線連接起來。 我不想連接較粗的線。

像這里這樣的行

繪制線條的代碼:

def lines():
    img = np.zeros((256,256,1), dtype=np.uint8)
    img = cv2.line(img, (50,50), (128 ,128), color=(255), thickness=1)
    img = cv2.line(img, (128 ,200), (240 ,250), color=(255), thickness=1)
    img = cv2.line(img, (96 ,54), (150,164), color=(255), thickness=2)

到目前為止,我有這樣的事情。 僅用於連接點。

def dots():
    img = np.zeros((256,256,1), dtype=np.uint8)
    img = cv2.circle(img, (50,50), color=(255), radius=0, thickness=-1)
    img = cv2.circle(img, (128 ,200), color=(255), radius=0, thickness=-1)
    img = cv2.circle(img, (128 ,54), color=(255), radius=0, thickness=-1)

def connect_dots():
    img = dots()
    xy = np.argwhere(img == 255)
    x = xy == xy[:,1]
    x = np.argwhere(x[:,1] == True)
    x = xy[x]
    x = np.swapaxes(x, 1, 2)
    x = x[:,:2,:2]

    return x

def draw_line():
    img  = dots()
    start = np.flip(np.squeeze(connect_dots()[0,:,], axis=-1))
    end = np.flip(np.squeeze(connect_dots()[1,:,], axis=-1))
    print(start.shape, end)
    img = cv2.line(img, start, end, color=(177), thickness=1)
    cv2.imshow("connected lines", img)
    cv2.waitKey(0) 

但:

  1. 我覺得完全是意大利面
  2. 它只適用於點而不適用於線
  3. 沒有公差,該點必須完全位於相同的 x 坐標上,並且繪制的線必須完全垂直。

只有點 連接點

  • 檢測線的端點(它們在一側有鄰居但在另一側沒有)。

  • 按橫坐標對端點進行排序。

  • 掃描端點列表以檢測相距小於 5 像素的端點(除非它們是同一段的端點)。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM