這是實際影像

我想在二進制影像中找到 2 個不規則邊緣之間的距離。那就是我在下圖中用紅色標記了。我的想法是在兩邊畫一條彩色線(比如紅色),然后以 10 個相等的間隔計算它們之間的距離(黃色標記)

我將影像裁剪成兩部分。說上半部分是

下半部分是

我想畫兩條紅線,或者只是以某種方式找到這兩條線之間的距離。我一直在使用 OpenCV 和 PILLOW 進行很多步驟。
還有一些影像實體,其中幾列頂部影像中沒有黑色像素。如何計算僅從頂部到黑色像素的距離?只是上圖。
uj5u.com熱心網友回復:
這是一種可能的方法。我會把細節留給你。但想法是使用 Numpy argmax(或適當的 argmin)來獲取每列中全黑之后的第一個白色的索引。首先,我垂直翻轉影像,使黑色位于頂部。Numpy argmax,似乎找到了沿列的第一個白色值。
輸入(下圖):

import numpy as np
import cv2
# read image
img = cv2.imread("img.png")
# Note that there is a 2 pixel tall white line at the bottom
# Note that there is columns of black at the left and right sides
# convert to grayscale
img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# threshold
img = cv2.threshold(img,0,255,cv2.THRESH_BINARY)[1]
# flip vertically
img = cv2.flip(img, 0)
# remove 2 pixels at the top
img = img[2:, :]
# add one pixel row of white at bottom
img = cv2.copyMakeBorder(img, 0, 2, 0, 0, cv2.BORDER_CONSTANT, value=255)
# find max along each column
max = np.argmax(img, axis=0)
print(max.shape)
print(max)
cv2.imshow('flipped',img)
cv2.waitKey(0)
cv2.destroyAllWindows()
這是輸出:
(709,)
[112 112 112 112 32 32 32 32 32 32 32 32 32 32 32 32 32 32
31 31 31 31 31 31 31 31 31 31 30 30 30 30 30 30 30 30
30 30 30 30 30 30 30 30 30 30 30 31 31 31 31 31 31 31
31 31 31 31 31 31 31 31 31 30 30 30 30 30 30 30 30 30
30 30 30 31 31 32 32 33 33 34 35 35 36 36 36 36 36 36
36 36 36 36 36 36 36 36 37 37 38 38 39 39 39 40 40 40
41 41 43 43 44 45 45 46 46 46 46 46 46 46 46 46 46 46
46 46 46 46 46 46 46 46 46 46 46 46 46 45 45 45 45 44
44 44 44 44 44 44 44 44 44 44 44 44 43 43 43 43 43 43
43 43 43 43 43 43 43 44 44 44 44 44 44 44 44 43 43 43
42 42 42 42 42 42 42 42 42 42 41 41 40 40 40 40 40 40
40 40 40 40 40 40 40 40 40 40 40 40 39 39 39 39 39 39
39 39 39 39 39 39 39 39 39 39 39 39 38 38 37 37 37 37
37 37 37 36 36 36 36 36 35 35 35 34 34 34 34 34 34 34
34 34 33 33 32 32 32 32 32 32 32 32 32 32 32 32 32 32
33 33 34 34 35 35 36 37 37 37 37 38 38 38 38 38 38 38
38 38 38 38 38 38 38 39 39 40 40 40 40 40 39 39 39 39
39 39 38 38 38 37 37 37 37 37 37 37 37 37 37 37 37 37
37 38 38 38 39 40 40 41 41 41 39 39 38 37 37 37 37 37
37 37 37 37 37 37 37 37 37 37 37 37 37 37 37 37 38 38
38 39 39 39 39 39 39 39 39 41 41 41 41 41 41 41 41 41
41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41
40 40 40 40 40 40 40 40 40 40 39 39 38 38 37 37 37 37
36 36 36 36 36 36 35 35 34 34 34 33 33 33 32 31 31 31
31 31 31 31 31 31 31 31 31 31 31 31 31 31 31 31 31 31
31 31 31 31 31 31 31 31 30 30 29 29 29 28 28 28 28 27
27 26 26 26 26 26 26 26 26 26 26 26 26 26 26 27 27 27
27 27 27 27 27 27 28 28 28 28 28 28 28 28 28 28 28 28
28 28 28 28 28 28 28 28 28 28 28 28 28 28 29 29 29 29
29 29 29 29 29 29 29 29 29 29 29 29 29 29 29 29 29 29
29 28 28 27 27 27 27 27 27 27 27 27 27 27 27 27 28 28
28 28 29 29 30 30 30 31 31 32 32 33 33 34 34 35 35 36
36 36 37 37 37 38 38 39 39 39 39 39 39 39 39 39 39 39
39 38 38 37 37 37 37 37 37 36 36 35 35 35 35 35 35 35
35 35 35 35 35 35 35 35 35 35 35 36 36 36 36 36 36 36
36 36 36 36 36 36 37 37 37 37 37 37 37 37 37 37 37 37
37 37 37 37 37 37 37 37 37 37 37 37 37 37 37 37 37 37
37 37 37 37 37 37 37 37 37 37 37 37 37 37 37 37 37 37
37 37 37 37 37 37 37 37 37 37 37 37 37 38 38 38 39 39
39 39 39 40 112 112 112]
對于頂部,使用 argmin 或使用反轉極性img=255-img,然后使用 argmax。
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/446923.html
