2
從this問題與下面的代碼。有沒有一種方法可以指定按照最大面積順序返回標籤的功能?如果不是,如果stats
已被轉換爲numpy數組,那麼獲得元素索引的最佳方法是什麼(從最大區域到最小區域)?如何訂購opencv connectedComponentewithStat按地區?
# Import the cv2 library
import cv2
# Read the image you want connected components of
src = cv2.imread('/directorypath/image.bmp')
# Threshold it so it becomes binary
ret, thresh = cv2.threshold(src,0,255,cv2.THRESH_BINARY+cv2.THRESH_OTSU)
# You need to choose 4 or 8 for connectivity type
connectivity = 4
# Perform the operation
output = cv2.connectedComponentsWithStats(thresh, connectivity, cv2.CV_32S)
# Get the results
# The first cell is the number of labels
num_labels = output[0]
# The second cell is the label matrix
labels = output[1]
# The third cell is the stat matrix
stats = output[2]
# The fourth cell is the centroid matrix
centroids = output[3]
我不好。我將刪除評論並進行編輯以避免混淆。我已經將問題標記爲已接受。謝謝:) – matchifang
你知道第一個索引是否總是0,這意味着連接的組件是整個圖像? – matchifang
@matchifang我不確定'stats'輸出的模式。它似乎總是一個大數字。你想省略第一行並獲得有序數組嗎? – Divakar