如何連接影像中不相交的垂直線而不將它們擴展到外邊緣區域?
輸入影像(簡化示例):

使用Morph Close操作:
inputImg = cv2.imread(imgPath)
grayInput = cv2.cvtColor(inputImg, cv2.COLOR_BGR2GRAY)
thre = cv2.inRange(grayInput, 0, 155)
closed = cv2.morphologyEx(thre, cv2.MORPH_CLOSE, np.ones((500,1), np.uint8))
電流輸出:

期望的輸出:

PS我可以只添加大于關閉內核的邊距,但由于生產影像的尺寸很大,這將導致記憶體效率低下。生產影像也有大量帶有隨機間隙的線條。
uj5u.com熱心網友回復:
對影像進行二值化,找到包含白色像素的列,然后連接每列中最低和最高白色像素之間的線。
下面的代碼將完成此操作。我用評論解釋了它。
import cv2
import numpy as np
img = cv2.imread("xoxql.png") # Load the image
img = (img < 255).all(axis=2) # Binarize the image
rows, cols = img.nonzero() # Get all nonzero indices
unique_cols = np.unique(cols) # Select columns that contain nonzeros
for col_index in unique_cols: # Loop through the columns
start = rows[col_index == cols].min() # Select the lowest nonzero row index
end = rows[col_index == cols].max() # Select the highest nonzero row index
img[start:end, col_index] = True # Set all "pixels" to True between the lowest and highest row indices
img = img * np.uint8(255) # Multiply by 255 to convert the matrix back to an image
cv2.imwrite("result.png", img) # Save the image

影像右側的線條沒有完全對齊,這會在邊緣留下一些間隙。
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/487148.html
標籤:Python opencv 图像处理 计算机视觉 边缘检测
上一篇:終端視窗和Vs代碼變數
