簡體   English   中英

opencv:如何在單個深度圖像中找到輪廓?

[英]opencv: how to find contours in a single depth image?

嘗試在單個深度圖像中查找輪廓時出現以下錯誤。 我已經有一個深度 img,我相信我不需要使用cv.cvtcolor

img = np.random.rand(224,224)
img.astype('uint8')
threshold_value = int(np.max(img) * 0.2)
print(threshold_value)
_, img = cv.threshold(img, threshold_value, 255, cv.THRESH_BINARY)
plt.imshow(img)
plt.show()
_, contours, _ = cv.findContours(img,
                                    cv.RETR_TREE, cv.CHAIN_APPROX_SIMPLE)
plt.imshow(img)
plt.show()

錯誤:

FindContours 僅在模式 != CV_RETR_FLOODFILL 時支持 CV_8UC1 圖像,否則僅在函數 'cvStartFindContours_Impl' 中支持 CV_32SC1 圖像

img.astype('uint8')

什么也沒做。 您應該將結果分配給某些內容,例如:

img = img.astype('uint8')

那么你不會得到你描述的錯誤。

但結果仍然不會像您預期的那樣,因為np.random.rand()分配范圍 (0..1) 內的值,並且通過將它們轉換為uint8一切都變為零。 可能也想修復那個部分。

8UC1 表示 8 位像素,因此嘗試使用將圖像轉換為灰度。

您應該首先轉換值,使值最多為 255。然后,您應該轉換類型。 像這樣的東西:

img = np.random.rand(224,224)
# Take values from 0-1 to 0-255
img *= 255
# Convert type
img = img.astype('uint8')
(...)

暫無
暫無

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

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